XmlDsigXPathTransformTest.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. // Copyright (C) 2004-2005 Novell, Inc (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 System.Xml.XPath;
  18. using NUnit.Framework;
  19. namespace MonoTests.System.Security.Cryptography.Xml {
  20. // Note: GetInnerXml is protected in XmlDsigXPathTransform making it
  21. // difficult to test properly. This class "open it up" :-)
  22. public class UnprotectedXmlDsigXPathTransform : XmlDsigXPathTransform {
  23. public XmlNodeList UnprotectedGetInnerXml ()
  24. {
  25. return base.GetInnerXml ();
  26. }
  27. }
  28. [TestFixture]
  29. public class XmlDsigXPathTransformTest {
  30. protected UnprotectedXmlDsigXPathTransform transform;
  31. [SetUp]
  32. protected void SetUp ()
  33. {
  34. transform = new UnprotectedXmlDsigXPathTransform ();
  35. }
  36. [Test]
  37. public void Properties ()
  38. {
  39. Assert.AreEqual ("http://www.w3.org/TR/1999/REC-xpath-19991116", transform.Algorithm, "Algorithm");
  40. Type[] input = transform.InputTypes;
  41. Assert.IsTrue ((input.Length == 3), "Input #");
  42. // check presence of every supported input types
  43. bool istream = false;
  44. bool ixmldoc = false;
  45. bool ixmlnl = false;
  46. foreach (Type t in input) {
  47. if (t.ToString () == "System.IO.Stream")
  48. istream = true;
  49. if (t.ToString () == "System.Xml.XmlDocument")
  50. ixmldoc = true;
  51. if (t.ToString () == "System.Xml.XmlNodeList")
  52. ixmlnl = true;
  53. }
  54. Assert.IsTrue (istream, "Input Stream");
  55. Assert.IsTrue (ixmldoc, "Input XmlDocument");
  56. Assert.IsTrue (ixmlnl, "Input XmlNodeList");
  57. Type[] output = transform.OutputTypes;
  58. Assert.IsTrue ((output.Length == 1), "Output #");
  59. // check presence of every supported output types
  60. bool oxmlnl = false;
  61. foreach (Type t in output) {
  62. if (t.ToString () == "System.Xml.XmlNodeList")
  63. oxmlnl = true;
  64. }
  65. Assert.IsTrue (oxmlnl, "Output XmlNodeList");
  66. }
  67. protected void AreEqual (string msg, XmlNodeList expected, XmlNodeList actual)
  68. {
  69. for (int i=0; i < expected.Count; i++) {
  70. if (expected [i].OuterXml != actual [i].OuterXml)
  71. Assert.Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
  72. }
  73. Assert.AreEqual (expected.Count, actual.Count);
  74. }
  75. [Test]
  76. [Ignore ("throws a NullReferenceException - but it's (kind of internal)")]
  77. public void GetInnerXml ()
  78. {
  79. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  80. Assert.AreEqual (1, xnl.Count, "Default InnerXml.Count");
  81. Assert.AreEqual ("<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\"></XPath>", xnl [0].OuterXml, "Default InnerXml.OuterXml");
  82. }
  83. [Test]
  84. public void OnlyInner ()
  85. {
  86. XmlNodeList inner = InnerXml (""); // empty
  87. transform.LoadInnerXml (inner);
  88. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  89. Assert.AreEqual (0, xnl.Count, "Count");
  90. }
  91. private XmlDocument GetDoc ()
  92. {
  93. string test = "<catalog><cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><price>10.90</price>";
  94. test += "<year>1985</year></cd><cd><title>Hide your heart</title><artist>Bonnie Tyler</artist><price>9.90</price>";
  95. test += "<year>1988</year></cd><cd><title>Greatest Hits</title><artist>Dolly Parton</artist><price>9.90</price>";
  96. test += "<year>1982</year></cd><cd><title>Still got the blues</title><artist>Gary Moore</artist><price>10.20</price>";
  97. test += "<year>1990</year></cd><cd><title>Eros</title><artist>Eros Ramazzotti</artist><price>9.90</price>";
  98. test += "<year>1997</year></cd></catalog>";
  99. XmlDocument doc = new XmlDocument ();
  100. doc.LoadXml (test);
  101. return doc;
  102. }
  103. private XmlNodeList InnerXml (string xpathExpr)
  104. {
  105. string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
  106. XmlDocument doc = new XmlDocument ();
  107. doc.LoadXml (xpath);
  108. return doc.ChildNodes;
  109. }
  110. [Test]
  111. [Category ("NotWorking")]
  112. public void LoadInputAsXmlDocument ()
  113. {
  114. XmlDocument doc = GetDoc ();
  115. transform.LoadInput (doc);
  116. XmlNodeList inner = InnerXml ("//*/title");
  117. transform.LoadInnerXml (inner);
  118. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  119. Assert.AreEqual (73, xnl.Count);
  120. }
  121. [Test]
  122. public void LoadInputAsXmlDocument_EmptyXPath ()
  123. {
  124. XmlDocument doc = GetDoc ();
  125. transform.LoadInput (doc);
  126. // empty means no LoadInnerXml
  127. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  128. Assert.AreEqual (0, xnl.Count, "Empy Result");
  129. }
  130. [Test]
  131. [Category ("NotWorking")]
  132. public void LoadInputAsXmlNodeList ()
  133. {
  134. XmlDocument doc = GetDoc ();
  135. transform.LoadInput (doc.ChildNodes);
  136. XmlNodeList inner = InnerXml ("//*/title");
  137. transform.LoadInnerXml (inner);
  138. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  139. Assert.AreEqual (1, xnl.Count);
  140. }
  141. [Test]
  142. public void LoadInputAsXmlNodeList_EmptyXPath ()
  143. {
  144. XmlDocument doc = GetDoc ();
  145. transform.LoadInput (doc.ChildNodes);
  146. // empty means no LoadInnerXml
  147. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  148. Assert.AreEqual (0, xnl.Count, "Empy Result");
  149. }
  150. [Test]
  151. [Category ("NotWorking")]
  152. public void LoadInputAsStream ()
  153. {
  154. XmlDocument doc = GetDoc ();
  155. doc.PreserveWhitespace = true;
  156. MemoryStream ms = new MemoryStream ();
  157. doc.Save (ms);
  158. ms.Position = 0;
  159. transform.LoadInput (ms);
  160. XmlNodeList inner = InnerXml ("//*/title");
  161. transform.LoadInnerXml (inner);
  162. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  163. Assert.AreEqual (73, xnl.Count);
  164. }
  165. [Test]
  166. public void LoadInputAsStream_EmptyXPath ()
  167. {
  168. XmlDocument doc = GetDoc ();
  169. MemoryStream ms = new MemoryStream ();
  170. doc.Save (ms);
  171. ms.Position = 0;
  172. transform.LoadInput (ms);
  173. // empty means no LoadInnerXml
  174. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  175. Assert.AreEqual (0, xnl.Count, "Empy Result");
  176. }
  177. [Test]
  178. public void LoadInnerXml ()
  179. {
  180. XmlNodeList inner = InnerXml ("//*");
  181. transform.LoadInnerXml (inner);
  182. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  183. Assert.AreEqual (inner, xnl, "LoadInnerXml");
  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. [Test]
  200. public void TransformSimple ()
  201. {
  202. XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
  203. XmlDocument xpdoc = new XmlDocument ();
  204. string ns = "http://www.w3.org/2000/09/xmldsig#";
  205. string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>*|@*|namespace::*</XPath>"; // not absolute path.. so @* and namespace::* does not make sense.
  206. xpdoc.LoadXml (xpath);
  207. t.LoadInnerXml (xpdoc.ChildNodes);
  208. XmlDocument doc = new XmlDocument ();
  209. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  210. t.LoadInput (doc);
  211. XmlNodeList nl = (XmlNodeList) t.GetOutput ();
  212. Assert.AreEqual (XmlNodeType.Document, nl [0].NodeType);
  213. Assert.AreEqual (XmlNodeType.Element, nl [1].NodeType);
  214. Assert.AreEqual ("element", nl [1].LocalName);
  215. Assert.AreEqual (XmlNodeType.Element, nl [2].NodeType);
  216. Assert.AreEqual ("foo", nl [2].LocalName);
  217. Assert.AreEqual (XmlNodeType.Element, nl [3].NodeType);
  218. Assert.AreEqual ("bar", nl [3].LocalName);
  219. // MS.NET bug - ms.net returns ns node even when the
  220. // current node is ns node (it is like returning
  221. // attribute from attribute nodes).
  222. // Assert.AreEqual (XmlNodeType.Attribute, nl [4].NodeType);
  223. // Assert.AreEqual ("xmlns", nl [4].LocalName);
  224. }
  225. [Test]
  226. [Category ("NotWorking")]
  227. // MS.NET looks incorrect, or something incorrect in this test code; It turned out nothing to do with function here()
  228. public void FunctionHereObsolete ()
  229. {
  230. XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
  231. XmlDocument xpdoc = new XmlDocument ();
  232. string ns = "http://www.w3.org/2000/09/xmldsig#";
  233. // string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>here()</XPath>";
  234. string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'></XPath>";
  235. xpdoc.LoadXml (xpath);
  236. t.LoadInnerXml (xpdoc.ChildNodes);
  237. XmlDocument doc = new XmlDocument ();
  238. doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
  239. t.LoadInput (doc);
  240. XmlNodeList nl = (XmlNodeList) t.GetOutput ();
  241. Assert.AreEqual (0, nl.Count, "0");
  242. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  243. t.LoadInput (doc);
  244. nl = (XmlNodeList) t.GetOutput ();
  245. Assert.AreEqual (0, nl.Count, "1");
  246. doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
  247. t.LoadInput (doc);
  248. nl = (XmlNodeList) t.GetOutput ();
  249. Assert.AreEqual (0, nl.Count, "2");
  250. doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
  251. t.LoadInput (doc);
  252. nl = (XmlNodeList) t.GetOutput ();
  253. Assert.AreEqual (0, nl.Count, "3");
  254. doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
  255. t.LoadInput (doc);
  256. nl = (XmlNodeList) t.GetOutput ();
  257. Assert.AreEqual (0, nl.Count, "4");
  258. }
  259. }
  260. }