XmlDsigXsltTransformTest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // (C) 2004 Novell (http://www.novell.com)
  9. //
  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. using System.Xml.Xsl;
  17. using NUnit.Framework;
  18. namespace MonoTests.System.Security.Cryptography.Xml {
  19. // Note: GetInnerXml is protected in XmlDsigXsltTransform making it
  20. // difficult to test properly. This class "open it up" :-)
  21. public class UnprotectedXmlDsigXsltTransform : XmlDsigXsltTransform {
  22. public XmlNodeList UnprotectedGetInnerXml () {
  23. return base.GetInnerXml ();
  24. }
  25. }
  26. [TestFixture]
  27. public class XmlDsigXsltTransformTest : Assertion {
  28. protected UnprotectedXmlDsigXsltTransform transform;
  29. [SetUp]
  30. protected void SetUp ()
  31. {
  32. transform = new UnprotectedXmlDsigXsltTransform ();
  33. }
  34. [Test]
  35. public void Properties ()
  36. {
  37. AssertEquals ("Algorithm", "http://www.w3.org/TR/1999/REC-xslt-19991116", transform.Algorithm);
  38. Type[] input = transform.InputTypes;
  39. Assert ("Input #", (input.Length == 3));
  40. // check presence of every supported input types
  41. bool istream = false;
  42. bool ixmldoc = false;
  43. bool ixmlnl = false;
  44. foreach (Type t in input) {
  45. if (t.ToString () == "System.IO.Stream")
  46. istream = true;
  47. if (t.ToString () == "System.Xml.XmlDocument")
  48. ixmldoc = true;
  49. if (t.ToString () == "System.Xml.XmlNodeList")
  50. ixmlnl = true;
  51. }
  52. Assert ("Input Stream", istream);
  53. Assert ("Input XmlDocument", ixmldoc);
  54. Assert ("Input XmlNodeList", ixmlnl);
  55. Type[] output = transform.OutputTypes;
  56. Assert ("Output #", (output.Length == 1));
  57. // check presence of every supported output types
  58. bool ostream = false;
  59. foreach (Type t in output) {
  60. if (t.ToString () == "System.IO.Stream")
  61. ostream = true;
  62. }
  63. Assert ("Output Stream", ostream);
  64. }
  65. [Test]
  66. public void GetInnerXml ()
  67. {
  68. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  69. AssertNull ("Default InnerXml", xnl);
  70. }
  71. private string Stream2Array (Stream s)
  72. {
  73. StringBuilder sb = new StringBuilder ();
  74. int b = s.ReadByte ();
  75. while (b != -1) {
  76. sb.Append (b.ToString("X2"));
  77. b = s.ReadByte ();
  78. }
  79. return sb.ToString ();
  80. }
  81. [Test]
  82. [ExpectedException (typeof (NullReferenceException))]
  83. public void EmptyXslt ()
  84. {
  85. string test = "<Test>XmlDsigXsltTransform</Test>";
  86. XmlDocument doc = new XmlDocument ();
  87. doc.LoadXml (test);
  88. transform.LoadInput (doc.ChildNodes);
  89. Stream s = (Stream) transform.GetOutput ();
  90. }
  91. [Test]
  92. // Note that this is _valid_ as an "embedded stylesheet".
  93. // (see XSLT spec 2.7)
  94. public void EmbeddedStylesheet ()
  95. {
  96. string test = "<Test xsl:version='1.0'>XmlDsigXsltTransform</Test>";
  97. XmlDocument doc = new XmlDocument ();
  98. doc.LoadXml (test);
  99. transform.LoadInnerXml (doc.ChildNodes);
  100. transform.LoadInput (doc);
  101. Stream s = (Stream) transform.GetOutput ();
  102. }
  103. [Test]
  104. [ExpectedException (typeof (XsltCompileException))]
  105. public void InvalidXslt ()
  106. {
  107. string test = "<xsl:element name='foo' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>XmlDsigXsltTransform</xsl:element>";
  108. XmlDocument doc = new XmlDocument ();
  109. doc.LoadXml (test);
  110. transform.LoadInnerXml (doc.ChildNodes);
  111. Stream s = (Stream) transform.GetOutput ();
  112. }
  113. [Test]
  114. [ExpectedException (typeof (NullReferenceException))]
  115. public void OnlyInner ()
  116. {
  117. string test = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
  118. test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
  119. test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
  120. test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
  121. test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
  122. test += "</table></body></html></xsl:template></xsl:stylesheet>";
  123. XmlDocument doc = new XmlDocument ();
  124. doc.LoadXml (test);
  125. transform.LoadInnerXml (doc.ChildNodes);
  126. Stream s = (Stream) transform.GetOutput ();
  127. string output = Stream2Array (s);
  128. }
  129. private XmlDocument GetXslDoc ()
  130. {
  131. string test = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" xmlns='http://www.w3.org/2000/09/xmldsig#'>";
  132. test += "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
  133. test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
  134. test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
  135. test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
  136. test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
  137. test += "</table></body></html></xsl:template></xsl:stylesheet></Transform>";
  138. XmlDocument doc = new XmlDocument ();
  139. doc.LoadXml (test);
  140. return doc;
  141. }
  142. [Test]
  143. public void LoadInputAsXmlDocument ()
  144. {
  145. XmlDocument doc = GetXslDoc ();
  146. transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
  147. transform.LoadInput (doc);
  148. Stream s = (Stream) transform.GetOutput ();
  149. string output = Stream2Array (s);
  150. }
  151. [Test]
  152. public void LoadInputAsXmlNodeList ()
  153. {
  154. XmlDocument doc = GetXslDoc ();
  155. transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
  156. transform.LoadInput (doc.ChildNodes);
  157. Stream s = (Stream) transform.GetOutput ();
  158. string output = Stream2Array (s);
  159. }
  160. [Test]
  161. public void LoadInputAsStream ()
  162. {
  163. XmlDocument doc = GetXslDoc ();
  164. transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
  165. MemoryStream ms = new MemoryStream ();
  166. doc.Save (ms);
  167. ms.Position = 0;
  168. transform.LoadInput (ms);
  169. Stream s = (Stream) transform.GetOutput ();
  170. string output = Stream2Array (s);
  171. }
  172. protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual)
  173. {
  174. for (int i=0; i < expected.Count; i++) {
  175. if (expected[i].OuterXml != actual[i].OuterXml)
  176. Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
  177. }
  178. }
  179. [Test]
  180. public void LoadInnerXml ()
  181. {
  182. XmlDocument doc = GetXslDoc ();
  183. transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
  184. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  185. AssertEquals ("LoadInnerXml", doc.DocumentElement.ChildNodes, xnl);
  186. }
  187. [Test]
  188. public void Load2 ()
  189. {
  190. XmlDocument doc = GetXslDoc ();
  191. transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
  192. transform.LoadInput (doc);
  193. Stream s = (Stream) transform.GetOutput ();
  194. string output = Stream2Array (s);
  195. }
  196. [Test]
  197. public void UnsupportedInput ()
  198. {
  199. byte[] bad = { 0xBA, 0xD };
  200. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  201. transform.LoadInput (bad);
  202. }
  203. [Test]
  204. [ExpectedException (typeof (ArgumentException))]
  205. public void UnsupportedOutput ()
  206. {
  207. XmlDocument doc = new XmlDocument();
  208. object o = transform.GetOutput (doc.GetType ());
  209. }
  210. }
  211. }