XmlDsigXsltTransformTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // XmlDsigXsltTransformTest.cs - NUnit Test Cases for XmlDsigXsltTransform
  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;
  12. using System.Security.Cryptography.Xml;
  13. using System.Text;
  14. using System.Xml;
  15. using System.Xml.Xsl;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.Security.Cryptography.Xml {
  18. [TestFixture]
  19. public class XmlDsigXsltTransformTest : Assertion {
  20. protected XmlDsigXsltTransform transform;
  21. [SetUp]
  22. protected void SetUp ()
  23. {
  24. transform = new XmlDsigXsltTransform ();
  25. }
  26. [Test]
  27. public void Properties ()
  28. {
  29. AssertEquals ("Algorithm", "http://www.w3.org/TR/1999/REC-xslt-19991116", 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 Stream2Array (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. [Test]
  68. // can't use ExpectedException as it doesn't have a constructor with 0 parameters
  69. // [ExpectedException (typeof (XsltCompileException))]
  70. public void InvalidXslt ()
  71. {
  72. string test = "<Test>XmlDsigXsltTransform</Test>";
  73. XmlDocument doc = new XmlDocument ();
  74. doc.LoadXml (test);
  75. transform.LoadInnerXml (doc.ChildNodes);
  76. try {
  77. Stream s = (Stream) transform.GetOutput ();
  78. Fail ("Expected XsltCompileException but got none");
  79. }
  80. catch (XsltCompileException) {
  81. // expected
  82. }
  83. catch (Exception e) {
  84. Fail ("Expected XsltCompileException but got :" + e.ToString ());
  85. }
  86. }
  87. [Test]
  88. [ExpectedException (typeof (NullReferenceException))]
  89. public void OnlyInner ()
  90. {
  91. string test = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
  92. test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
  93. test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
  94. test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
  95. test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
  96. test += "</table></body></html></xsl:template></xsl:stylesheet>";
  97. XmlDocument doc = new XmlDocument ();
  98. doc.LoadXml (test);
  99. transform.LoadInnerXml (doc.ChildNodes);
  100. Stream s = (Stream) transform.GetOutput ();
  101. string output = Stream2Array (s);
  102. }
  103. private XmlDocument GetDoc ()
  104. {
  105. string test = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\">";
  106. test += "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
  107. test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
  108. test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
  109. test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
  110. test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
  111. test += "</table></body></html></xsl:template></xsl:stylesheet></Transform>";
  112. XmlDocument doc = new XmlDocument ();
  113. doc.LoadXml (test);
  114. return doc;
  115. }
  116. [Test]
  117. [Ignore ("not working")]
  118. public void LoadInputAsXmlDocument ()
  119. {
  120. XmlDocument doc = GetDoc ();
  121. transform.LoadInput (doc);
  122. Stream s = (Stream) transform.GetOutput ();
  123. string output = Stream2Array (s);
  124. }
  125. [Test]
  126. [Ignore ("not working")]
  127. public void LoadInputAsXmlNodeList ()
  128. {
  129. XmlDocument doc = GetDoc ();
  130. transform.LoadInput (doc.ChildNodes);
  131. Stream s = (Stream) transform.GetOutput ();
  132. string output = Stream2Array (s);
  133. }
  134. [Test]
  135. [Ignore ("not working")]
  136. public void LoadInputAsStream ()
  137. {
  138. XmlDocument doc = GetDoc ();
  139. MemoryStream ms = new MemoryStream ();
  140. doc.Save (ms);
  141. ms.Position = 0;
  142. transform.LoadInput (ms);
  143. Stream s = (Stream) transform.GetOutput ();
  144. string output = Stream2Array (s);
  145. }
  146. protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual)
  147. {
  148. for (int i=0; i < expected.Count; i++) {
  149. if (expected[i].OuterXml != actual[i].OuterXml)
  150. Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
  151. }
  152. }
  153. [Test]
  154. public void LoadInnerXml ()
  155. {
  156. string value = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" />";
  157. XmlDocument doc = new XmlDocument ();
  158. doc.LoadXml (value);
  159. transform.LoadInnerXml (doc.ChildNodes);
  160. // note: GetInnerXml is protected so we can't AssertEquals :-(
  161. // unless we use reflection (making it a lot more complicated)
  162. }
  163. [Test]
  164. [Ignore ("not working")]
  165. public void Load2 ()
  166. {
  167. XmlDocument doc = GetDoc ();
  168. transform.LoadInnerXml (doc.ChildNodes);
  169. transform.LoadInput (doc);
  170. Stream s = (Stream) transform.GetOutput ();
  171. string output = Stream2Array (s);
  172. }
  173. [Test]
  174. public void UnsupportedInput ()
  175. {
  176. byte[] bad = { 0xBA, 0xD };
  177. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  178. transform.LoadInput (bad);
  179. }
  180. [Test]
  181. [ExpectedException (typeof (ArgumentException))]
  182. public void UnsupportedOutput ()
  183. {
  184. XmlDocument doc = new XmlDocument();
  185. object o = transform.GetOutput (doc.GetType ());
  186. }
  187. }
  188. }