XmlDsigXsltTransformTest.cs 8.4 KB

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