XmlDsigXsltTransformTest.cs 7.6 KB

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