XmlDsigC14NTransformTest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // XmlDsigC14NTransformTest.cs - NUnit Test Cases for XmlDsigC14NTransform
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Security.Cryptography.Xml;
  12. using System.Text;
  13. using System.Xml;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Security.Cryptography.Xml {
  16. [TestFixture]
  17. public class XmlDsigC14NTransformTest : Assertion {
  18. protected XmlDsigC14NTransform transform;
  19. [SetUp]
  20. protected void SetUp ()
  21. {
  22. transform = new XmlDsigC14NTransform ();
  23. }
  24. [Test]
  25. public void Properties ()
  26. {
  27. AssertEquals ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", transform.Algorithm);
  28. Type[] input = transform.InputTypes;
  29. Assert ("Input #", (input.Length == 3));
  30. // check presence of every supported input types
  31. bool istream = false;
  32. bool ixmldoc = false;
  33. bool ixmlnl = false;
  34. foreach (Type t in input) {
  35. if (t.ToString () == "System.IO.Stream")
  36. istream = true;
  37. if (t.ToString () == "System.Xml.XmlDocument")
  38. ixmldoc = true;
  39. if (t.ToString () == "System.Xml.XmlNodeList")
  40. ixmlnl = true;
  41. }
  42. Assert ("Input Stream", istream);
  43. Assert ("Input XmlDocument", ixmldoc);
  44. Assert ("Input XmlNodeList", ixmlnl);
  45. Type[] output = transform.OutputTypes;
  46. Assert ("Output #", (output.Length == 1));
  47. // check presence of every supported output types
  48. bool ostream = false;
  49. foreach (Type t in input) {
  50. if (t.ToString () == "System.IO.Stream")
  51. ostream = true;
  52. }
  53. Assert ("Output Stream", ostream);
  54. }
  55. private string Stream2String (Stream s)
  56. {
  57. StringBuilder sb = new StringBuilder ();
  58. int b = s.ReadByte ();
  59. while (b != -1) {
  60. sb.Append (Convert.ToChar (b));
  61. b = s.ReadByte ();
  62. }
  63. return sb.ToString ();
  64. }
  65. static string xml = "<Test attrib='at ' xmlns=\"http://www.go-mono.com/\" > \r\n <Toto/> text &amp; </Test >";
  66. static string c14xml1 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \r\n <Toto></Toto> text &amp; </Test>";
  67. static string c14xml2 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \n <Toto></Toto> text &amp; </Test>";
  68. private XmlDocument GetDoc ()
  69. {
  70. XmlDocument doc = new XmlDocument ();
  71. doc.PreserveWhitespace = true;
  72. doc.LoadXml (xml);
  73. return doc;
  74. }
  75. [Test]
  76. public void LoadInputAsXmlDocument ()
  77. {
  78. XmlDocument doc = GetDoc ();
  79. transform.LoadInput (doc);
  80. Stream s = (Stream) transform.GetOutput ();
  81. string output = Stream2String (s);
  82. // ??? this keeps the \r\n (0x0D, 0x0A) ???
  83. AssertEquals("XmlDocument", c14xml1, output);
  84. }
  85. [Test]
  86. [Ignore("FIXME: why doesn't this works with MS ???")]
  87. public void LoadInputAsXmlNodeList ()
  88. {
  89. XmlDocument doc = GetDoc ();
  90. transform.LoadInput (doc.ChildNodes);
  91. Stream s = (Stream) transform.GetOutput ();
  92. string output = Stream2String (s);
  93. AssertEquals("XmlChildNodes", c14xml2, output);
  94. }
  95. [Test]
  96. public void LoadInputAsStream ()
  97. {
  98. MemoryStream ms = new MemoryStream ();
  99. byte[] x = Encoding.ASCII.GetBytes (xml);
  100. ms.Write (x, 0, x.Length);
  101. ms.Position = 0;
  102. transform.LoadInput (ms);
  103. Stream s = (Stream) transform.GetOutput ();
  104. string output = Stream2String (s);
  105. AssertEquals("MemoryStream", c14xml2, output);
  106. }
  107. [Test]
  108. public void LoadInputWithUnsupportedType ()
  109. {
  110. byte[] bad = { 0xBA, 0xD };
  111. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  112. transform.LoadInput (bad);
  113. }
  114. [Test]
  115. [ExpectedException (typeof (ArgumentException))]
  116. public void UnsupportedOutput ()
  117. {
  118. XmlDocument doc = new XmlDocument();
  119. object o = transform.GetOutput (doc.GetType ());
  120. }
  121. }
  122. }