XmlDsigXPathTransformTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 : Assertion {
  30. protected UnprotectedXmlDsigXPathTransform transform;
  31. [SetUp]
  32. protected void SetUp ()
  33. {
  34. transform = new UnprotectedXmlDsigXPathTransform ();
  35. }
  36. [Test]
  37. public void Properties ()
  38. {
  39. AssertEquals ("Algorithm", "http://www.w3.org/TR/1999/REC-xpath-19991116", transform.Algorithm);
  40. Type[] input = transform.InputTypes;
  41. Assert ("Input #", (input.Length == 3));
  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 ("Input Stream", istream);
  55. Assert ("Input XmlDocument", ixmldoc);
  56. Assert ("Input XmlNodeList", ixmlnl);
  57. Type[] output = transform.OutputTypes;
  58. Assert ("Output #", (output.Length == 1));
  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 ("Output XmlNodeList", oxmlnl);
  66. }
  67. protected void AssertEquals (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. Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
  72. }
  73. AssertEquals (expected.Count, actual.Count);
  74. }
  75. [Test]
  76. #if NET_2_0
  77. [Ignore ("throws a NullReferenceException - but it's (kind of internal)")]
  78. #endif
  79. public void GetInnerXml ()
  80. {
  81. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  82. AssertEquals ("Default InnerXml.Count", 1, xnl.Count);
  83. AssertEquals ("Default InnerXml.OuterXml", "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\"></XPath>", xnl [0].OuterXml);
  84. }
  85. [Test]
  86. [ExpectedException (typeof (NullReferenceException))]
  87. public void OnlyInner ()
  88. {
  89. XmlNodeList inner = InnerXml (""); // empty
  90. transform.LoadInnerXml (inner);
  91. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  92. }
  93. private XmlDocument GetDoc ()
  94. {
  95. string test = "<catalog><cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><price>10.90</price>";
  96. test += "<year>1985</year></cd><cd><title>Hide your heart</title><artist>Bonnie Tyler</artist><price>9.90</price>";
  97. test += "<year>1988</year></cd><cd><title>Greatest Hits</title><artist>Dolly Parton</artist><price>9.90</price>";
  98. test += "<year>1982</year></cd><cd><title>Still got the blues</title><artist>Gary Moore</artist><price>10.20</price>";
  99. test += "<year>1990</year></cd><cd><title>Eros</title><artist>Eros Ramazzotti</artist><price>9.90</price>";
  100. test += "<year>1997</year></cd></catalog>";
  101. XmlDocument doc = new XmlDocument ();
  102. doc.LoadXml (test);
  103. return doc;
  104. }
  105. private XmlNodeList InnerXml (string xpathExpr)
  106. {
  107. string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
  108. XmlDocument doc = new XmlDocument ();
  109. doc.LoadXml (xpath);
  110. return doc.ChildNodes;
  111. }
  112. [Test]
  113. #if NET_2_0
  114. [Category ("NotWorking")]
  115. #endif
  116. public void LoadInputAsXmlDocument ()
  117. {
  118. XmlDocument doc = GetDoc ();
  119. transform.LoadInput (doc);
  120. XmlNodeList inner = InnerXml ("//*/title");
  121. transform.LoadInnerXml (inner);
  122. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  123. #if NET_2_0
  124. AssertEquals (73, xnl.Count);
  125. #else
  126. AssertEquals (47, xnl.Count);
  127. #endif
  128. }
  129. [Test]
  130. #if NET_2_0
  131. [ExpectedException (typeof (XPathException))]
  132. #endif
  133. public void LoadInputAsXmlDocument_EmptyXPath ()
  134. {
  135. XmlDocument doc = GetDoc ();
  136. transform.LoadInput (doc);
  137. // empty means no LoadInnerXml
  138. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  139. AssertEquals ("Empy Result", 0, xnl.Count);
  140. }
  141. [Test]
  142. [Category ("NotWorking")]
  143. public void LoadInputAsXmlNodeList ()
  144. {
  145. XmlDocument doc = GetDoc ();
  146. transform.LoadInput (doc.ChildNodes);
  147. XmlNodeList inner = InnerXml ("//*/title");
  148. transform.LoadInnerXml (inner);
  149. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  150. AssertEquals (1, xnl.Count);
  151. }
  152. [Test]
  153. #if NET_2_0
  154. [ExpectedException (typeof (XPathException))]
  155. #endif
  156. public void LoadInputAsXmlNodeList_EmptyXPath ()
  157. {
  158. XmlDocument doc = GetDoc ();
  159. transform.LoadInput (doc.ChildNodes);
  160. // empty means no LoadInnerXml
  161. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  162. AssertEquals ("Empy Result", 0, xnl.Count);
  163. }
  164. [Test]
  165. #if NET_2_0
  166. [Category ("NotWorking")]
  167. #endif
  168. public void LoadInputAsStream ()
  169. {
  170. XmlDocument doc = GetDoc ();
  171. doc.PreserveWhitespace = true;
  172. MemoryStream ms = new MemoryStream ();
  173. doc.Save (ms);
  174. ms.Position = 0;
  175. transform.LoadInput (ms);
  176. XmlNodeList inner = InnerXml ("//*/title");
  177. transform.LoadInnerXml (inner);
  178. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  179. #if NET_2_0
  180. AssertEquals (73, xnl.Count);
  181. #else
  182. AssertEquals (47, xnl.Count);
  183. #endif
  184. }
  185. [Test]
  186. #if NET_2_0
  187. [ExpectedException (typeof (XPathException))]
  188. #endif
  189. public void LoadInputAsStream_EmptyXPath ()
  190. {
  191. XmlDocument doc = GetDoc ();
  192. MemoryStream ms = new MemoryStream ();
  193. doc.Save (ms);
  194. ms.Position = 0;
  195. transform.LoadInput (ms);
  196. // empty means no LoadInnerXml
  197. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  198. AssertEquals ("Empy Result", 0, xnl.Count);
  199. }
  200. [Test]
  201. public void LoadInnerXml ()
  202. {
  203. XmlNodeList inner = InnerXml ("//*");
  204. transform.LoadInnerXml (inner);
  205. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  206. AssertEquals ("LoadInnerXml", inner, xnl);
  207. }
  208. [Test]
  209. public void UnsupportedInput ()
  210. {
  211. byte[] bad = { 0xBA, 0xD };
  212. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  213. transform.LoadInput (bad);
  214. }
  215. [Test]
  216. [ExpectedException (typeof (ArgumentException))]
  217. public void UnsupportedOutput ()
  218. {
  219. XmlDocument doc = new XmlDocument ();
  220. object o = transform.GetOutput (doc.GetType ());
  221. }
  222. [Test]
  223. public void TransformSimple ()
  224. {
  225. XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
  226. XmlDocument xpdoc = new XmlDocument ();
  227. string ns = "http://www.w3.org/2000/09/xmldsig#";
  228. string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>*|@*|namespace::*</XPath>"; // not absolute path.. so @* and namespace::* does not make sense.
  229. xpdoc.LoadXml (xpath);
  230. t.LoadInnerXml (xpdoc.ChildNodes);
  231. XmlDocument doc = new XmlDocument ();
  232. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  233. t.LoadInput (doc);
  234. XmlNodeList nl = (XmlNodeList) t.GetOutput ();
  235. AssertEquals (XmlNodeType.Document, nl [0].NodeType);
  236. AssertEquals (XmlNodeType.Element, nl [1].NodeType);
  237. AssertEquals ("element", nl [1].LocalName);
  238. AssertEquals (XmlNodeType.Element, nl [2].NodeType);
  239. AssertEquals ("foo", nl [2].LocalName);
  240. AssertEquals (XmlNodeType.Element, nl [3].NodeType);
  241. AssertEquals ("bar", nl [3].LocalName);
  242. // MS.NET bug - ms.net returns ns node even when the
  243. // current node is ns node (it is like returning
  244. // attribute from attribute nodes).
  245. // AssertEquals (XmlNodeType.Attribute, nl [4].NodeType);
  246. // AssertEquals ("xmlns", nl [4].LocalName);
  247. }
  248. [Test]
  249. [Category ("NotWorking")]
  250. // MS.NET looks incorrect, or something incorrect in this test code; It turned out nothing to do with function here()
  251. #if NET_2_0
  252. [ExpectedException (typeof (XPathException))]
  253. #endif
  254. public void FunctionHereObsolete ()
  255. {
  256. XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
  257. XmlDocument xpdoc = new XmlDocument ();
  258. string ns = "http://www.w3.org/2000/09/xmldsig#";
  259. // string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>here()</XPath>";
  260. string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'></XPath>";
  261. xpdoc.LoadXml (xpath);
  262. t.LoadInnerXml (xpdoc.ChildNodes);
  263. XmlDocument doc = new XmlDocument ();
  264. doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
  265. t.LoadInput (doc);
  266. // FX 2.0 throws at next line!
  267. XmlNodeList nl = (XmlNodeList) t.GetOutput ();
  268. AssertEquals (0, nl.Count);
  269. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  270. t.LoadInput (doc);
  271. nl = (XmlNodeList) t.GetOutput ();
  272. AssertEquals (1, nl.Count);
  273. doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
  274. t.LoadInput (doc);
  275. nl = (XmlNodeList) t.GetOutput ();
  276. AssertEquals (2, nl.Count);
  277. doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
  278. t.LoadInput (doc);
  279. nl = (XmlNodeList) t.GetOutput ();
  280. AssertEquals (3, nl.Count);
  281. doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
  282. t.LoadInput (doc);
  283. nl = (XmlNodeList) t.GetOutput ();
  284. AssertEquals (1, nl.Count);
  285. AssertEquals (XmlNodeType.Attribute, nl [0].NodeType);
  286. }
  287. }
  288. }