XmlDsigBase64TransformTest.cs 4.3 KB

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