XmlDsigXPathTransformTest.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // XmlDsigXPathTransformTest.cs - NUnit Test Cases for XmlDsigXPathTransform
  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 XmlDsigXPathTransform making it
  20. // difficult to test properly. This class "open it up" :-)
  21. public class UnprotectedXmlDsigXPathTransform : XmlDsigXPathTransform {
  22. public XmlNodeList UnprotectedGetInnerXml ()
  23. {
  24. return base.GetInnerXml ();
  25. }
  26. }
  27. [TestFixture]
  28. public class XmlDsigXPathTransformTest : Assertion {
  29. protected UnprotectedXmlDsigXPathTransform transform;
  30. [SetUp]
  31. protected void SetUp ()
  32. {
  33. transform = new UnprotectedXmlDsigXPathTransform ();
  34. }
  35. [Test]
  36. public void Properties ()
  37. {
  38. AssertEquals ("Algorithm", "http://www.w3.org/TR/1999/REC-xpath-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 oxmlnl = false;
  60. foreach (Type t in output) {
  61. if (t.ToString () == "System.Xml.XmlNodeList")
  62. oxmlnl = true;
  63. }
  64. Assert ("Output XmlNodeList", oxmlnl);
  65. }
  66. protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual)
  67. {
  68. for (int i=0; i < expected.Count; i++) {
  69. if (expected [i].OuterXml != actual [i].OuterXml)
  70. Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
  71. }
  72. }
  73. [Test]
  74. public void GetInnerXml ()
  75. {
  76. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  77. AssertEquals ("Default InnerXml.Count", 1, xnl.Count);
  78. AssertEquals ("Default InnerXml.OuterXml", "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\"></XPath>", xnl [0].OuterXml);
  79. }
  80. [Test]
  81. [ExpectedException (typeof (NullReferenceException))]
  82. public void OnlyInner ()
  83. {
  84. XmlNodeList inner = InnerXml (""); // empty
  85. transform.LoadInnerXml (inner);
  86. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  87. }
  88. private XmlDocument GetDoc ()
  89. {
  90. string test = "<catalog><cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><price>10.90</price>";
  91. test += "<year>1985</year></cd><cd><title>Hide your heart</title><artist>Bonnie Tyler</artist><price>9.90</price>";
  92. test += "<year>1988</year></cd><cd><title>Greatest Hits</title><artist>Dolly Parton</artist><price>9.90</price>";
  93. test += "<year>1982</year></cd><cd><title>Still got the blues</title><artist>Gary Moore</artist><price>10.20</price>";
  94. test += "<year>1990</year></cd><cd><title>Eros</title><artist>Eros Ramazzotti</artist><price>9.90</price>";
  95. test += "<year>1997</year></cd></catalog>";
  96. XmlDocument doc = new XmlDocument ();
  97. doc.LoadXml (test);
  98. return doc;
  99. }
  100. private XmlNodeList InnerXml (string xpathExpr)
  101. {
  102. string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
  103. XmlDocument doc = new XmlDocument ();
  104. doc.LoadXml (xpath);
  105. return doc.ChildNodes;
  106. }
  107. [Test]
  108. public void LoadInputAsXmlDocument ()
  109. {
  110. XmlDocument doc = GetDoc ();
  111. transform.LoadInput (doc);
  112. XmlNodeList inner = InnerXml ("//*/title");
  113. transform.LoadInnerXml (inner);
  114. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  115. // FIXME: This cannot be the *right* result - I must be missing something :(
  116. AssertEquals ("XPath result", doc.ChildNodes, xnl);
  117. }
  118. [Test]
  119. public void LoadInputAsXmlDocument_EmptyXPath ()
  120. {
  121. XmlDocument doc = GetDoc ();
  122. transform.LoadInput (doc);
  123. // empty means no LoadInnerXml
  124. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  125. AssertEquals ("Empy Result", 0, xnl.Count);
  126. }
  127. [Test]
  128. public void LoadInputAsXmlNodeList ()
  129. {
  130. XmlDocument doc = GetDoc ();
  131. transform.LoadInput (doc.ChildNodes);
  132. XmlNodeList inner = InnerXml ("//*/title");
  133. transform.LoadInnerXml (inner);
  134. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  135. // FIXME: This cannot be the *right* result - I must be missing something :(
  136. AssertEquals ("XPath result", doc.ChildNodes, xnl);
  137. }
  138. [Test]
  139. public void LoadInputAsXmlNodeList_EmptyXPath ()
  140. {
  141. XmlDocument doc = GetDoc ();
  142. transform.LoadInput (doc.ChildNodes);
  143. // empty means no LoadInnerXml
  144. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  145. AssertEquals ("Empy Result", 0, xnl.Count);
  146. }
  147. [Test]
  148. public void LoadInputAsStream ()
  149. {
  150. XmlDocument doc = GetDoc ();
  151. doc.PreserveWhitespace = true;
  152. MemoryStream ms = new MemoryStream ();
  153. doc.Save (ms);
  154. ms.Position = 0;
  155. transform.LoadInput (ms);
  156. XmlNodeList inner = InnerXml ("//*/title");
  157. transform.LoadInnerXml (inner);
  158. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  159. // FIXME: This cannot be the *right* result - I must be missing something :(
  160. AssertEquals ("XPath result", doc.ChildNodes, xnl);
  161. }
  162. [Test]
  163. public void LoadInputAsStream_EmptyXPath ()
  164. {
  165. XmlDocument doc = GetDoc ();
  166. MemoryStream ms = new MemoryStream ();
  167. doc.Save (ms);
  168. ms.Position = 0;
  169. transform.LoadInput (ms);
  170. // empty means no LoadInnerXml
  171. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  172. AssertEquals ("Empy Result", 0, xnl.Count);
  173. }
  174. [Test]
  175. public void LoadInnerXml ()
  176. {
  177. XmlNodeList inner = InnerXml ("//*");
  178. transform.LoadInnerXml (inner);
  179. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  180. AssertEquals ("LoadInnerXml", inner, xnl);
  181. }
  182. [Test]
  183. public void UnsupportedInput ()
  184. {
  185. byte[] bad = { 0xBA, 0xD };
  186. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  187. transform.LoadInput (bad);
  188. }
  189. [Test]
  190. [ExpectedException (typeof (ArgumentException))]
  191. public void UnsupportedOutput ()
  192. {
  193. XmlDocument doc = new XmlDocument ();
  194. object o = transform.GetOutput (doc.GetType ());
  195. }
  196. [Test]
  197. public void TransformSimple ()
  198. {
  199. XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
  200. XmlDocument xpdoc = new XmlDocument ();
  201. string ns = "http://www.w3.org/2000/09/xmldsig#";
  202. string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>*|@*|namespace::*</XPath>"; // not absolute path.. so @* and namespace::* does not make sense.
  203. xpdoc.LoadXml (xpath);
  204. t.LoadInnerXml (xpdoc.ChildNodes);
  205. XmlDocument doc = new XmlDocument ();
  206. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  207. t.LoadInput (doc);
  208. XmlNodeList nl = (XmlNodeList) t.GetOutput ();
  209. AssertEquals (XmlNodeType.Document, nl [0].NodeType);
  210. AssertEquals (XmlNodeType.Element, nl [1].NodeType);
  211. AssertEquals ("element", nl [1].LocalName);
  212. AssertEquals (XmlNodeType.Element, nl [2].NodeType);
  213. AssertEquals ("foo", nl [2].LocalName);
  214. AssertEquals (XmlNodeType.Element, nl [3].NodeType);
  215. AssertEquals ("bar", nl [3].LocalName);
  216. // MS.NET bug - ms.net returns ns node even when the
  217. // current node is ns node (it is like returning
  218. // attribute from attribute nodes).
  219. // AssertEquals (XmlNodeType.Attribute, nl [4].NodeType);
  220. // AssertEquals ("xmlns", nl [4].LocalName);
  221. }
  222. [Test]
  223. [Ignore ("MS.NET looks incorrect, or something incorrect in this test code")]
  224. public void FunctionHere ()
  225. {
  226. XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
  227. XmlDocument xpdoc = new XmlDocument ();
  228. string ns = "http://www.w3.org/2000/09/xmldsig#";
  229. string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>here()</XPath>";
  230. xpdoc.LoadXml (xpath);
  231. t.LoadInnerXml (xpdoc.ChildNodes);
  232. XmlDocument doc = new XmlDocument ();
  233. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  234. t.LoadInput (doc);
  235. XmlNodeList nl = (XmlNodeList) t.GetOutput ();
  236. AssertEquals (0, nl.Count);
  237. }
  238. }
  239. }