XmlDsigXsltTransformTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // XmlDsigXsltTransformTest.cs - NUnit Test Cases for XmlDsigXsltTransform
  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 XmlDsigXsltTransformTest : TestCase {
  18. public XmlDsigXsltTransformTest () : base ("System.Security.Cryptography.Xml.XmlDsigXsltTransform testsuite") {}
  19. public XmlDsigXsltTransformTest (string name) : base (name) {}
  20. protected XmlDsigXsltTransform transform;
  21. protected override void SetUp ()
  22. {
  23. transform = new XmlDsigXsltTransform ();
  24. }
  25. protected override void TearDown () {}
  26. public static ITest Suite {
  27. get {
  28. return new TestSuite (typeof (XmlDsigXsltTransformTest));
  29. }
  30. }
  31. public void TestProperties ()
  32. {
  33. AssertEquals ("Algorithm", "http://www.w3.org/TR/1999/REC-xslt-19991116", 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 Stream2Array (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. public void Test ()
  72. {
  73. string test = "<Test>XmlDsigXsltTransform</Test>";
  74. XmlDocument doc = new XmlDocument ();
  75. doc.LoadXml (test);
  76. transform.LoadInnerXml (doc.ChildNodes);
  77. Stream s = (Stream) transform.GetOutput ();
  78. string output = Stream2Array (s);
  79. // load as XmlDocument
  80. transform.LoadInput (doc);
  81. s = (Stream) transform.GetOutput ();
  82. output = Stream2Array (s);
  83. // load as XmlNodeList
  84. transform.LoadInput (doc.ChildNodes);
  85. s = (Stream) transform.GetOutput ();
  86. output = Stream2Array (s);
  87. // load as Stream
  88. MemoryStream ms = new MemoryStream ();
  89. doc.Save (ms);
  90. ms.Position = 0;
  91. transform.LoadInput (ms);
  92. s = (Stream) transform.GetOutput ();
  93. output = Stream2Array (s);
  94. }
  95. protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual)
  96. {
  97. for (int i=0; i < expected.Count; i++) {
  98. if (expected[i].OuterXml != actual[i].OuterXml) {
  99. Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
  100. }
  101. }
  102. }
  103. public void TestLoadInnerXml ()
  104. {
  105. string value = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" />";
  106. XmlDocument doc = new XmlDocument ();
  107. doc.LoadXml (value);
  108. transform.LoadInnerXml (doc.ChildNodes);
  109. // note: GetInnerXml is protected so we can't AssertEquals :-(
  110. // unless we use reflection (making ti a lot more complicated)
  111. }
  112. public void TestUnsupportedInput ()
  113. {
  114. byte[] bad = { 0xBA, 0xD };
  115. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  116. transform.LoadInput (bad);
  117. }
  118. public void TestUnsupportedOutput ()
  119. {
  120. try {
  121. XmlDocument doc = new XmlDocument();
  122. object o = transform.GetOutput (doc.GetType ());
  123. Fail ("Expected ArgumentException but got none");
  124. }
  125. catch (ArgumentException) {
  126. // this is what we expected
  127. }
  128. catch (Exception e) {
  129. Fail ("Expected ArgumentException but got: " + e.ToString ());
  130. }
  131. }
  132. }
  133. }