XmlDsigXPathTransformTest.cs 9.4 KB

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