XmlDsigXsltTransformTest.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // XmlDsigXsltTransformTest.cs - NUnit Test Cases for XmlDsigXsltTransform
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  9. // (C) 2004 Novell (http://www.novell.com)
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.IO;
  14. using System.Security.Cryptography;
  15. using System.Security.Cryptography.Xml;
  16. using System.Text;
  17. using System.Xml;
  18. using System.Xml.Xsl;
  19. using NUnit.Framework;
  20. namespace MonoTests.System.Security.Cryptography.Xml {
  21. // Note: GetInnerXml is protected in XmlDsigXsltTransform making it
  22. // difficult to test properly. This class "open it up" :-)
  23. public class UnprotectedXmlDsigXsltTransform : XmlDsigXsltTransform {
  24. public UnprotectedXmlDsigXsltTransform ()
  25. {
  26. }
  27. public UnprotectedXmlDsigXsltTransform (bool includeComments)
  28. : base (includeComments)
  29. {
  30. }
  31. public XmlNodeList UnprotectedGetInnerXml () {
  32. return base.GetInnerXml ();
  33. }
  34. }
  35. [TestFixture]
  36. public class XmlDsigXsltTransformTest {
  37. protected UnprotectedXmlDsigXsltTransform transform;
  38. [SetUp]
  39. protected void SetUp ()
  40. {
  41. transform = new UnprotectedXmlDsigXsltTransform ();
  42. }
  43. [Test] // ctor ()
  44. public void Constructor1 ()
  45. {
  46. CheckProperties (transform);
  47. }
  48. [Test] // ctor (Boolean)
  49. public void Constructor2 ()
  50. {
  51. transform = new UnprotectedXmlDsigXsltTransform (true);
  52. CheckProperties (transform);
  53. transform = new UnprotectedXmlDsigXsltTransform (false);
  54. CheckProperties (transform);
  55. }
  56. void CheckProperties (XmlDsigXsltTransform transform)
  57. {
  58. Assert.AreEqual ("http://www.w3.org/TR/1999/REC-xslt-19991116", transform.Algorithm, "Algorithm");
  59. Type[] input = transform.InputTypes;
  60. Assert.IsTrue ((input.Length == 3), "Input #");
  61. // check presence of every supported input types
  62. bool istream = false;
  63. bool ixmldoc = false;
  64. bool ixmlnl = false;
  65. foreach (Type t in input) {
  66. if (t.ToString () == "System.IO.Stream")
  67. istream = true;
  68. if (t.ToString () == "System.Xml.XmlDocument")
  69. ixmldoc = true;
  70. if (t.ToString () == "System.Xml.XmlNodeList")
  71. ixmlnl = true;
  72. }
  73. Assert.IsTrue (istream, "Input Stream");
  74. Assert.IsTrue (ixmldoc, "Input XmlDocument");
  75. Assert.IsTrue (ixmlnl, "Input XmlNodeList");
  76. Type[] output = transform.OutputTypes;
  77. Assert.IsTrue ((output.Length == 1), "Output #");
  78. // check presence of every supported output types
  79. bool ostream = false;
  80. foreach (Type t in output) {
  81. if (t.ToString () == "System.IO.Stream")
  82. ostream = true;
  83. }
  84. Assert.IsTrue (ostream, "Output Stream");
  85. }
  86. [Test]
  87. public void GetInnerXml ()
  88. {
  89. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  90. Assert.IsNull (xnl, "Default InnerXml");
  91. }
  92. private string Stream2Array (Stream s)
  93. {
  94. StringBuilder sb = new StringBuilder ();
  95. int b = s.ReadByte ();
  96. while (b != -1) {
  97. sb.Append (b.ToString("X2"));
  98. b = s.ReadByte ();
  99. }
  100. return sb.ToString ();
  101. }
  102. [Test]
  103. [ExpectedException (typeof (ArgumentNullException))]
  104. public void EmptyXslt ()
  105. {
  106. string test = "<Test>XmlDsigXsltTransform</Test>";
  107. XmlDocument doc = new XmlDocument ();
  108. doc.LoadXml (test);
  109. transform.LoadInput (doc.ChildNodes);
  110. Stream s = (Stream) transform.GetOutput ();
  111. }
  112. [Test]
  113. // Note that this is _valid_ as an "embedded stylesheet".
  114. // (see XSLT spec 2.7)
  115. public void EmbeddedStylesheet ()
  116. {
  117. string test = "<Test xsl:version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>XmlDsigXsltTransform</Test>";
  118. XmlDocument doc = new XmlDocument ();
  119. doc.LoadXml (test);
  120. transform.LoadInnerXml (doc.ChildNodes);
  121. transform.LoadInput (doc);
  122. Stream s = (Stream) transform.GetOutput ();
  123. }
  124. [Test]
  125. public void InvalidXslt ()
  126. {
  127. bool result = false;
  128. try {
  129. string test = "<xsl:element name='foo' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>XmlDsigXsltTransform</xsl:element>";
  130. XmlDocument doc = new XmlDocument ();
  131. doc.LoadXml (test);
  132. transform.LoadInnerXml (doc.ChildNodes);
  133. Stream s = (Stream) transform.GetOutput ();
  134. }
  135. catch (Exception e) {
  136. // we must deal with an internal exception
  137. result = (e.GetType ().ToString ().EndsWith ("XsltLoadException"));
  138. result = true;
  139. }
  140. finally {
  141. Assert.IsTrue (result, "Exception not thrown");
  142. }
  143. }
  144. [Test]
  145. [ExpectedException (typeof (ArgumentNullException))]
  146. public void OnlyInner ()
  147. {
  148. string test = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
  149. test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
  150. test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
  151. test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
  152. test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
  153. test += "</table></body></html></xsl:template></xsl:stylesheet>";
  154. XmlDocument doc = new XmlDocument ();
  155. doc.LoadXml (test);
  156. transform.LoadInnerXml (doc.ChildNodes);
  157. Stream s = (Stream) transform.GetOutput ();
  158. string output = Stream2Array (s);
  159. }
  160. private XmlDocument GetXslDoc ()
  161. {
  162. string test = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" xmlns='http://www.w3.org/2000/09/xmldsig#'>";
  163. test += "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
  164. test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
  165. test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
  166. test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
  167. test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
  168. test += "</table></body></html></xsl:template></xsl:stylesheet></Transform>";
  169. XmlDocument doc = new XmlDocument ();
  170. doc.LoadXml (test);
  171. return doc;
  172. }
  173. [Test]
  174. public void LoadInputAsXmlDocument ()
  175. {
  176. XmlDocument doc = GetXslDoc ();
  177. transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
  178. transform.LoadInput (doc);
  179. Stream s = (Stream) transform.GetOutput ();
  180. string output = Stream2Array (s);
  181. }
  182. [Test]
  183. public void LoadInputAsXmlNodeList ()
  184. {
  185. XmlDocument doc = GetXslDoc ();
  186. transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
  187. transform.LoadInput (doc.ChildNodes);
  188. Stream s = (Stream) transform.GetOutput ();
  189. string output = Stream2Array (s);
  190. }
  191. [Test]
  192. public void LoadInputAsStream ()
  193. {
  194. XmlDocument doc = GetXslDoc ();
  195. transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
  196. MemoryStream ms = new MemoryStream ();
  197. doc.Save (ms);
  198. ms.Position = 0;
  199. transform.LoadInput (ms);
  200. Stream s = (Stream) transform.GetOutput ();
  201. string output = Stream2Array (s);
  202. }
  203. protected void AreEqual (string msg, XmlNodeList expected, XmlNodeList actual)
  204. {
  205. for (int i=0; i < expected.Count; i++) {
  206. if (expected[i].OuterXml != actual[i].OuterXml)
  207. Assert.Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
  208. }
  209. }
  210. [Test]
  211. public void LoadInnerXml ()
  212. {
  213. XmlDocument doc = GetXslDoc ();
  214. transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
  215. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  216. AssertNodeListEqual (doc.DocumentElement.ChildNodes, xnl, "LoadInnerXml");
  217. }
  218. void AssertNodeListEqual (XmlNodeList nl1, XmlNodeList nl2, string label)
  219. {
  220. Assert.AreEqual (nl1.Count, nl2.Count, label + ": node count");
  221. IEnumerator e1, e2;
  222. int i;
  223. for (i = 0, e1 = nl1.GetEnumerator (), e2 = nl2.GetEnumerator (); e1.MoveNext (); i++) {
  224. Assert.IsTrue (e2.MoveNext (), label + " : nl2.MoveNext");
  225. Assert.AreEqual (e1.Current, e2.Current, label + " : node at " + i);
  226. }
  227. Assert.IsFalse (e2.MoveNext (), label + " : nl2 has extras");
  228. }
  229. [Test]
  230. public void Load2 ()
  231. {
  232. XmlDocument doc = GetXslDoc ();
  233. transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
  234. transform.LoadInput (doc);
  235. Stream s = (Stream) transform.GetOutput ();
  236. string output = Stream2Array (s);
  237. }
  238. [Test]
  239. public void UnsupportedInput ()
  240. {
  241. byte[] bad = { 0xBA, 0xD };
  242. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  243. transform.LoadInput (bad);
  244. }
  245. [Test]
  246. [ExpectedException (typeof (ArgumentException))]
  247. public void UnsupportedOutput ()
  248. {
  249. XmlDocument doc = new XmlDocument();
  250. object o = transform.GetOutput (doc.GetType ());
  251. }
  252. }
  253. }