XmlDsigBase64TransformTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 : Assertion {
  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. AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm);
  30. Type[] input = transform.InputTypes;
  31. 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. Assert ("Input Stream", istream);
  45. Assert ("Input XmlDocument", ixmldoc);
  46. Assert ("Input XmlNodeList", ixmlnl);
  47. Type[] output = transform.OutputTypes;
  48. 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. 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. AssertEquals("XmlDocument", base64, Encoding.UTF8.GetString (output));
  94. }
  95. [Test]
  96. public void LoadInputAsXmlNodeListFromXPath ()
  97. {
  98. XmlDocument doc = GetDoc ();
  99. XmlNodeList xpath = doc.SelectNodes ("//.");
  100. AssertEquals("XPathNodeList.Count", 3, xpath.Count);
  101. transform.LoadInput (xpath);
  102. Stream s = (Stream) transform.GetOutput ();
  103. byte[] output = Stream2Array (s);
  104. AssertEquals("XPathNodeList", base64, Encoding.UTF8.GetString (output));
  105. }
  106. [Test]
  107. [Ignore ("LAMESPEC or BUG but this returns nothing with MS implementation ???")]
  108. public void LoadInputAsXmlNodeList ()
  109. {
  110. XmlDocument doc = GetDoc ();
  111. transform.LoadInput (doc.ChildNodes);
  112. Stream s = (Stream) transform.GetOutput ();
  113. byte[] output = Stream2Array (s);
  114. AssertEquals("XmlChildNodes", null, Encoding.UTF8.GetString (output));
  115. }
  116. [Test]
  117. public void LoadInputAsStream ()
  118. {
  119. string base64 = "XmlDsigBase64Transform";
  120. byte[] base64array = Encoding.UTF8.GetBytes (base64);
  121. MemoryStream ms = new MemoryStream ();
  122. byte[] x = Encoding.UTF8.GetBytes (Convert.ToBase64String (base64array));
  123. ms.Write (x, 0, x.Length);
  124. ms.Position = 0;
  125. transform.LoadInput (ms);
  126. Stream s = (Stream) transform.GetOutput ();
  127. byte[] output = Stream2Array (s);
  128. AssertEquals("MemoryStream", base64, Encoding.UTF8.GetString (output));
  129. }
  130. [Test]
  131. public void LoadInputWithUnsupportedType ()
  132. {
  133. byte[] bad = { 0xBA, 0xD };
  134. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  135. transform.LoadInput (bad);
  136. }
  137. [Test]
  138. [ExpectedException (typeof (ArgumentException))]
  139. public void UnsupportedOutput ()
  140. {
  141. XmlDocument doc = new XmlDocument();
  142. object o = transform.GetOutput (doc.GetType ());
  143. }
  144. }
  145. }