XmlDsigBase64TransformTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 : Assertion {
  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. AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm);
  38. Type[] input = transform.InputTypes;
  39. Assert ("Input #", (input.Length == 3));
  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 ("Input Stream", istream);
  53. Assert ("Input XmlDocument", ixmldoc);
  54. Assert ("Input XmlNodeList", ixmlnl);
  55. Type[] output = transform.OutputTypes;
  56. Assert ("Output #", (output.Length == 1));
  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 ("Output Stream", ostream);
  64. }
  65. [Test]
  66. public void GetInnerXml ()
  67. {
  68. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  69. AssertNull ("Default InnerXml", xnl);
  70. }
  71. private string Stream2String (Stream s)
  72. {
  73. StreamReader sr = new StreamReader (s);
  74. return sr.ReadToEnd ();
  75. }
  76. static private string base64 = "XmlDsigBase64Transform";
  77. 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 };
  78. private XmlDocument GetDoc ()
  79. {
  80. string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>";
  81. XmlDocument doc = new XmlDocument ();
  82. doc.LoadXml (xml);
  83. return doc;
  84. }
  85. [Test]
  86. public void LoadInputAsXmlDocument ()
  87. {
  88. XmlDocument doc = GetDoc ();
  89. transform.LoadInput (doc);
  90. Stream s = (Stream) transform.GetOutput ();
  91. string output = Stream2String (s);
  92. AssertEquals("XmlDocument", base64, output);
  93. }
  94. [Test]
  95. public void LoadInputAsXmlNodeListFromXPath ()
  96. {
  97. XmlDocument doc = GetDoc ();
  98. XmlNodeList xpath = doc.SelectNodes ("//.");
  99. AssertEquals("XPathNodeList.Count", 3, xpath.Count);
  100. transform.LoadInput (xpath);
  101. Stream s = (Stream) transform.GetOutput ();
  102. string output = Stream2String (s);
  103. AssertEquals ("XPathNodeList", base64, output);
  104. }
  105. [Test]
  106. public void LoadInputAsXmlNodeList ()
  107. {
  108. XmlDocument doc = GetDoc ();
  109. transform.LoadInput (doc.ChildNodes);
  110. Stream s = (Stream) transform.GetOutput ();
  111. string output = Stream2String (s);
  112. // Note that ChildNodes does not contain the text node.
  113. AssertEquals ("XmlChildNodes", String.Empty, output);
  114. }
  115. [Test]
  116. public void LoadInputAsStream ()
  117. {
  118. MemoryStream ms = new MemoryStream ();
  119. byte[] x = Encoding.UTF8.GetBytes (Convert.ToBase64String (base64array));
  120. ms.Write (x, 0, x.Length);
  121. ms.Position = 0;
  122. transform.LoadInput (ms);
  123. Stream s = (Stream) transform.GetOutput ();
  124. string output = Stream2String (s);
  125. AssertEquals ("MemoryStream", base64, output);
  126. }
  127. [Test]
  128. public void LoadInputWithUnsupportedType ()
  129. {
  130. byte[] bad = { 0xBA, 0xD };
  131. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  132. transform.LoadInput (bad);
  133. }
  134. [Test]
  135. [ExpectedException (typeof (ArgumentException))]
  136. public void UnsupportedOutput ()
  137. {
  138. XmlDocument doc = new XmlDocument();
  139. object o = transform.GetOutput (doc.GetType ());
  140. }
  141. }
  142. }