XmlDsigXsltTransformTest.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // can't use ExpectedException as XsltCompileException doesn't have a constructor with 0 parameters
  83. // [ExpectedException (typeof (XsltCompileException))]
  84. public void InvalidXslt ()
  85. {
  86. string test = "<Test>XmlDsigXsltTransform</Test>";
  87. XmlDocument doc = new XmlDocument ();
  88. doc.LoadXml (test);
  89. transform.LoadInnerXml (doc.ChildNodes);
  90. try {
  91. Stream s = (Stream) transform.GetOutput ();
  92. Fail ("Expected XsltCompileException but got none");
  93. }
  94. catch (XsltCompileException) {
  95. // expected
  96. }
  97. catch (Exception e) {
  98. Fail ("Expected XsltCompileException but got :" + e.ToString ());
  99. }
  100. }
  101. [Test]
  102. [ExpectedException (typeof (NullReferenceException))]
  103. public void OnlyInner ()
  104. {
  105. string test = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
  106. test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
  107. test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
  108. test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
  109. test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
  110. test += "</table></body></html></xsl:template></xsl:stylesheet>";
  111. XmlDocument doc = new XmlDocument ();
  112. doc.LoadXml (test);
  113. transform.LoadInnerXml (doc.ChildNodes);
  114. Stream s = (Stream) transform.GetOutput ();
  115. string output = Stream2Array (s);
  116. }
  117. private XmlDocument GetXslDoc ()
  118. {
  119. string test = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\">";
  120. test += "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
  121. test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
  122. test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
  123. test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
  124. test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
  125. test += "</table></body></html></xsl:template></xsl:stylesheet></Transform>";
  126. XmlDocument doc = new XmlDocument ();
  127. doc.LoadXml (test);
  128. return doc;
  129. }
  130. [Test]
  131. [Ignore ("not working")]
  132. public void LoadInputAsXmlDocument ()
  133. {
  134. XmlDocument doc = GetXslDoc ();
  135. transform.LoadInput (doc);
  136. Stream s = (Stream) transform.GetOutput ();
  137. string output = Stream2Array (s);
  138. }
  139. [Test]
  140. [Ignore ("not working")]
  141. public void LoadInputAsXmlNodeList ()
  142. {
  143. XmlDocument doc = GetXslDoc ();
  144. transform.LoadInput (doc.ChildNodes);
  145. Stream s = (Stream) transform.GetOutput ();
  146. string output = Stream2Array (s);
  147. }
  148. [Test]
  149. [Ignore ("not working")]
  150. public void LoadInputAsStream ()
  151. {
  152. XmlDocument doc = GetXslDoc ();
  153. MemoryStream ms = new MemoryStream ();
  154. doc.Save (ms);
  155. ms.Position = 0;
  156. transform.LoadInput (ms);
  157. Stream s = (Stream) transform.GetOutput ();
  158. string output = Stream2Array (s);
  159. }
  160. protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual)
  161. {
  162. for (int i=0; i < expected.Count; i++) {
  163. if (expected[i].OuterXml != actual[i].OuterXml)
  164. Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
  165. }
  166. }
  167. [Test]
  168. public void LoadInnerXml ()
  169. {
  170. XmlDocument doc = GetXslDoc ();
  171. transform.LoadInnerXml (doc.ChildNodes);
  172. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  173. AssertEquals ("LoadInnerXml", doc.ChildNodes, xnl);
  174. }
  175. [Test]
  176. [Ignore ("not working")]
  177. public void Load2 ()
  178. {
  179. XmlDocument doc = GetXslDoc ();
  180. transform.LoadInnerXml (doc.ChildNodes);
  181. transform.LoadInput (doc);
  182. Stream s = (Stream) transform.GetOutput ();
  183. string output = Stream2Array (s);
  184. }
  185. [Test]
  186. public void UnsupportedInput ()
  187. {
  188. byte[] bad = { 0xBA, 0xD };
  189. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  190. transform.LoadInput (bad);
  191. }
  192. [Test]
  193. [ExpectedException (typeof (ArgumentException))]
  194. public void UnsupportedOutput ()
  195. {
  196. XmlDocument doc = new XmlDocument();
  197. object o = transform.GetOutput (doc.GetType ());
  198. }
  199. }
  200. }