XmlDsigBase64TransformTest.cs 4.9 KB

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