XmlDsigXPathTransformTest.cs 10.0 KB

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