XmlDsigBase64TransformTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // XmlDsigBase64TransformTest.cs - NUnit Test Cases for XmlDsigBase64Transform
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. // (C) 2004 Novell (http://www.novell.com)
  9. //
  10. #if !MOBILE
  11. using System;
  12. using System.IO;
  13. using System.Security.Cryptography;
  14. using System.Security.Cryptography.Xml;
  15. using System.Text;
  16. using System.Xml;
  17. using NUnit.Framework;
  18. namespace MonoTests.System.Security.Cryptography.Xml {
  19. // Note: GetInnerXml is protected in XmlDsigBase64Transform making it
  20. // difficult to test properly. This class "open it up" :-)
  21. public class UnprotectedXmlDsigBase64Transform : XmlDsigBase64Transform {
  22. public XmlNodeList UnprotectedGetInnerXml () {
  23. return base.GetInnerXml ();
  24. }
  25. }
  26. [TestFixture]
  27. public class XmlDsigBase64TransformTest {
  28. protected UnprotectedXmlDsigBase64Transform transform;
  29. [SetUp]
  30. protected void SetUp ()
  31. {
  32. transform = new UnprotectedXmlDsigBase64Transform ();
  33. Type t = typeof (XmlDsigBase64Transform);
  34. }
  35. [Test]
  36. public void Properties ()
  37. {
  38. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm, "Algorithm");
  39. Type[] input = transform.InputTypes;
  40. Assert.IsTrue ((input.Length == 3), "Input #");
  41. // check presence of every supported input types
  42. bool istream = false;
  43. bool ixmldoc = false;
  44. bool ixmlnl = false;
  45. foreach (Type t in input) {
  46. if (t.ToString () == "System.IO.Stream")
  47. istream = true;
  48. if (t.ToString () == "System.Xml.XmlDocument")
  49. ixmldoc = true;
  50. if (t.ToString () == "System.Xml.XmlNodeList")
  51. ixmlnl = true;
  52. }
  53. Assert.IsTrue (istream, "Input Stream");
  54. Assert.IsTrue (ixmldoc, "Input XmlDocument");
  55. Assert.IsTrue (ixmlnl, "Input XmlNodeList");
  56. Type[] output = transform.OutputTypes;
  57. Assert.IsTrue ((output.Length == 1), "Output #");
  58. // check presence of every supported output types
  59. bool ostream = false;
  60. foreach (Type t in input) {
  61. if (t.ToString () == "System.IO.Stream")
  62. ostream = true;
  63. }
  64. Assert.IsTrue (ostream, "Output Stream");
  65. }
  66. [Test]
  67. public void Types ()
  68. {
  69. Type [] input = transform.InputTypes;
  70. input [0] = null;
  71. input [1] = null;
  72. input [2] = null;
  73. // property does not return a clone
  74. foreach (Type t in transform.InputTypes) {
  75. Assert.IsNull (t);
  76. }
  77. // it's not a static array
  78. XmlDsigBase64Transform t2 = new XmlDsigBase64Transform ();
  79. foreach (Type t in t2.InputTypes) {
  80. Assert.IsNotNull (t);
  81. }
  82. }
  83. [Test]
  84. public void GetInnerXml ()
  85. {
  86. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  87. Assert.IsNull (xnl, "Default InnerXml");
  88. }
  89. private string Stream2String (Stream s)
  90. {
  91. StreamReader sr = new StreamReader (s);
  92. return sr.ReadToEnd ();
  93. }
  94. static private string base64 = "XmlDsigBase64Transform";
  95. static private byte[] base64array = { 0x58, 0x6D, 0x6C, 0x44, 0x73, 0x69, 0x67, 0x42, 0x61, 0x73, 0x65, 0x36, 0x34, 0x54, 0x72, 0x61, 0x6E, 0x73, 0x66, 0x6F, 0x72, 0x6D };
  96. private XmlDocument GetDoc ()
  97. {
  98. string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>";
  99. XmlDocument doc = new XmlDocument ();
  100. doc.LoadXml (xml);
  101. return doc;
  102. }
  103. [Test]
  104. public void LoadInputAsXmlDocument ()
  105. {
  106. XmlDocument doc = GetDoc ();
  107. transform.LoadInput (doc);
  108. Stream s = (Stream) transform.GetOutput ();
  109. string output = Stream2String (s);
  110. Assert.AreEqual (base64, output, "XmlDocument");
  111. }
  112. [Test]
  113. public void LoadInputAsXmlNodeListFromXPath ()
  114. {
  115. XmlDocument doc = GetDoc ();
  116. XmlNodeList xpath = doc.SelectNodes ("//.");
  117. Assert.AreEqual (3, xpath.Count, "XPathNodeList.Count");
  118. transform.LoadInput (xpath);
  119. Stream s = (Stream) transform.GetOutput ();
  120. string output = Stream2String (s);
  121. Assert.AreEqual (base64, output, "XPathNodeList");
  122. }
  123. [Test]
  124. public void LoadInputAsXmlNodeList ()
  125. {
  126. XmlDocument doc = GetDoc ();
  127. transform.LoadInput (doc.ChildNodes);
  128. Stream s = (Stream) transform.GetOutput ();
  129. string output = Stream2String (s);
  130. // Note that ChildNodes does not contain the text node.
  131. Assert.AreEqual (String.Empty, output, "XmlChildNodes");
  132. }
  133. [Test]
  134. public void LoadInputAsStream ()
  135. {
  136. MemoryStream ms = new MemoryStream ();
  137. byte[] x = Encoding.UTF8.GetBytes (Convert.ToBase64String (base64array));
  138. ms.Write (x, 0, x.Length);
  139. ms.Position = 0;
  140. transform.LoadInput (ms);
  141. Stream s = (Stream) transform.GetOutput ();
  142. string output = Stream2String (s);
  143. Assert.AreEqual (base64, output, "MemoryStream");
  144. }
  145. [Test]
  146. public void LoadInputWithUnsupportedType ()
  147. {
  148. byte[] bad = { 0xBA, 0xD };
  149. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  150. transform.LoadInput (bad);
  151. }
  152. [Test]
  153. [ExpectedException (typeof (ArgumentException))]
  154. public void UnsupportedOutput ()
  155. {
  156. XmlDocument doc = new XmlDocument();
  157. object o = transform.GetOutput (doc.GetType ());
  158. }
  159. }
  160. }
  161. #endif