XmlDsigBase64TransformTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. //
  9. using System;
  10. using System.IO;
  11. using System.Security.Cryptography;
  12. using System.Security.Cryptography.Xml;
  13. using System.Text;
  14. using System.Xml;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Security.Cryptography.Xml {
  17. [TestFixture]
  18. public class XmlDsigBase64TransformTest {
  19. protected XmlDsigBase64Transform transform;
  20. [SetUp]
  21. protected void SetUp ()
  22. {
  23. transform = new XmlDsigBase64Transform ();
  24. Type t = typeof (XmlDsigBase64Transform);
  25. }
  26. [Test]
  27. public void Properties ()
  28. {
  29. Assertion.AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm);
  30. Type[] input = transform.InputTypes;
  31. Assertion.Assert ("Input #", (input.Length == 3));
  32. // check presence of every supported input types
  33. bool istream = false;
  34. bool ixmldoc = false;
  35. bool ixmlnl = false;
  36. foreach (Type t in input) {
  37. if (t.ToString () == "System.IO.Stream")
  38. istream = true;
  39. if (t.ToString () == "System.Xml.XmlDocument")
  40. ixmldoc = true;
  41. if (t.ToString () == "System.Xml.XmlNodeList")
  42. ixmlnl = true;
  43. }
  44. Assertion.Assert ("Input Stream", istream);
  45. Assertion.Assert ("Input XmlDocument", ixmldoc);
  46. Assertion.Assert ("Input XmlNodeList", ixmlnl);
  47. Type[] output = transform.OutputTypes;
  48. Assertion.Assert ("Output #", (output.Length == 1));
  49. // check presence of every supported output types
  50. bool ostream = false;
  51. foreach (Type t in input) {
  52. if (t.ToString () == "System.IO.Stream")
  53. ostream = true;
  54. }
  55. Assertion.Assert ("Output Stream", ostream);
  56. }
  57. private string Stream2String (Stream s)
  58. {
  59. StringBuilder sb = new StringBuilder ();
  60. int b = s.ReadByte ();
  61. while (b != -1) {
  62. sb.Append (b.ToString("X2"));
  63. b = s.ReadByte ();
  64. }
  65. return sb.ToString ();
  66. }
  67. private byte[] Stream2Array (Stream s)
  68. {
  69. string st = Stream2String (s);
  70. byte[] array = new byte [st.Length / 2];
  71. for (int i=0; i < array.Length; i++) {
  72. string hex = st.Substring (i*2, 2);
  73. array [i] = Convert.ToByte(hex, 16);
  74. }
  75. return array;
  76. }
  77. static private string base64 = "XmlDsigBase64Transform";
  78. 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 };
  79. private XmlDocument GetDoc ()
  80. {
  81. string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>";
  82. XmlDocument doc = new XmlDocument ();
  83. doc.LoadXml (xml);
  84. return doc;
  85. }
  86. [Test]
  87. public void LoadInputAsXmlDocument ()
  88. {
  89. XmlDocument doc = GetDoc ();
  90. transform.LoadInput (doc);
  91. Stream s = (Stream) transform.GetOutput ();
  92. byte[] output = Stream2Array (s);
  93. UTF8Encoding utf8 = new UTF8Encoding ();
  94. Assertion.AssertEquals("XmlDocument", base64, utf8.GetString (output));
  95. }
  96. [Test]
  97. public void LoadInputAsXmlNodeListFromXPath ()
  98. {
  99. XmlDocument doc = GetDoc ();
  100. XmlNodeList xpath = doc.SelectNodes ("//.");
  101. transform.LoadInput (xpath);
  102. Stream s = (Stream) transform.GetOutput ();
  103. byte[] output = Stream2Array (s);
  104. UTF8Encoding utf8 = new UTF8Encoding ();
  105. Assertion.AssertEquals("XPathNodeList", base64, utf8.GetString (output));
  106. }
  107. [Test]
  108. [Ignore ("FIXME: works with Mono ??? why doesn't this works with MS ???")]
  109. public void LoadInputAsXmlNodeList ()
  110. {
  111. XmlDocument doc = GetDoc ();
  112. transform.LoadInput (doc.ChildNodes);
  113. Stream s = (Stream) transform.GetOutput ();
  114. byte[] output = Stream2Array (s);
  115. UTF8Encoding utf8 = new UTF8Encoding ();
  116. Assertion.AssertEquals("XmlChildNodes", base64, utf8.GetString (output));
  117. }
  118. [Test]
  119. public void LoadInputAsStream ()
  120. {
  121. string base64 = "XmlDsigBase64Transform";
  122. UTF8Encoding utf8 = new UTF8Encoding ();
  123. byte[] base64array = utf8.GetBytes (base64);
  124. MemoryStream ms = new MemoryStream ();
  125. byte[] x = utf8.GetBytes (Convert.ToBase64String (base64array));
  126. ms.Write (x, 0, x.Length);
  127. ms.Position = 0;
  128. transform.LoadInput (ms);
  129. Stream s = (Stream) transform.GetOutput ();
  130. byte[] output = Stream2Array (s);
  131. Assertion.AssertEquals("MemoryStream", base64, utf8.GetString (output));
  132. }
  133. [Test]
  134. public void LoadInputWithUnsupportedType ()
  135. {
  136. byte[] bad = { 0xBA, 0xD };
  137. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  138. transform.LoadInput (bad);
  139. }
  140. [Test]
  141. [ExpectedException (typeof (ArgumentException))]
  142. public void UnsupportedOutput ()
  143. {
  144. XmlDocument doc = new XmlDocument();
  145. object o = transform.GetOutput (doc.GetType ());
  146. }
  147. }
  148. }