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