XmlDsigXPathTransformTest.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. AssertEquals (expected.Count, actual.Count);
  73. }
  74. [Test]
  75. public void GetInnerXml ()
  76. {
  77. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  78. AssertEquals ("Default InnerXml.Count", 1, xnl.Count);
  79. AssertEquals ("Default InnerXml.OuterXml", "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\"></XPath>", xnl [0].OuterXml);
  80. }
  81. [Test]
  82. [ExpectedException (typeof (NullReferenceException))]
  83. public void OnlyInner ()
  84. {
  85. XmlNodeList inner = InnerXml (""); // empty
  86. transform.LoadInnerXml (inner);
  87. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  88. }
  89. private XmlDocument GetDoc ()
  90. {
  91. string test = "<catalog><cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><price>10.90</price>";
  92. test += "<year>1985</year></cd><cd><title>Hide your heart</title><artist>Bonnie Tyler</artist><price>9.90</price>";
  93. test += "<year>1988</year></cd><cd><title>Greatest Hits</title><artist>Dolly Parton</artist><price>9.90</price>";
  94. test += "<year>1982</year></cd><cd><title>Still got the blues</title><artist>Gary Moore</artist><price>10.20</price>";
  95. test += "<year>1990</year></cd><cd><title>Eros</title><artist>Eros Ramazzotti</artist><price>9.90</price>";
  96. test += "<year>1997</year></cd></catalog>";
  97. XmlDocument doc = new XmlDocument ();
  98. doc.LoadXml (test);
  99. return doc;
  100. }
  101. private XmlNodeList InnerXml (string xpathExpr)
  102. {
  103. string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
  104. XmlDocument doc = new XmlDocument ();
  105. doc.LoadXml (xpath);
  106. return doc.ChildNodes;
  107. }
  108. [Test]
  109. public void LoadInputAsXmlDocument ()
  110. {
  111. XmlDocument doc = GetDoc ();
  112. transform.LoadInput (doc);
  113. XmlNodeList inner = InnerXml ("//*/title");
  114. transform.LoadInnerXml (inner);
  115. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  116. AssertEquals (47, xnl.Count);
  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. [Category ("NotWorking")]
  129. public void LoadInputAsXmlNodeList ()
  130. {
  131. XmlDocument doc = GetDoc ();
  132. transform.LoadInput (doc.ChildNodes);
  133. XmlNodeList inner = InnerXml ("//*/title");
  134. transform.LoadInnerXml (inner);
  135. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  136. AssertEquals (1, xnl.Count);
  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. AssertEquals (47, xnl.Count);
  160. }
  161. [Test]
  162. public void LoadInputAsStream_EmptyXPath ()
  163. {
  164. XmlDocument doc = GetDoc ();
  165. MemoryStream ms = new MemoryStream ();
  166. doc.Save (ms);
  167. ms.Position = 0;
  168. transform.LoadInput (ms);
  169. // empty means no LoadInnerXml
  170. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  171. AssertEquals ("Empy Result", 0, xnl.Count);
  172. }
  173. [Test]
  174. public void LoadInnerXml ()
  175. {
  176. XmlNodeList inner = InnerXml ("//*");
  177. transform.LoadInnerXml (inner);
  178. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  179. AssertEquals ("LoadInnerXml", inner, xnl);
  180. }
  181. [Test]
  182. public void UnsupportedInput ()
  183. {
  184. byte[] bad = { 0xBA, 0xD };
  185. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  186. transform.LoadInput (bad);
  187. }
  188. [Test]
  189. [ExpectedException (typeof (ArgumentException))]
  190. public void UnsupportedOutput ()
  191. {
  192. XmlDocument doc = new XmlDocument ();
  193. object o = transform.GetOutput (doc.GetType ());
  194. }
  195. [Test]
  196. public void TransformSimple ()
  197. {
  198. XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
  199. XmlDocument xpdoc = new XmlDocument ();
  200. string ns = "http://www.w3.org/2000/09/xmldsig#";
  201. string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>*|@*|namespace::*</XPath>"; // not absolute path.. so @* and namespace::* does not make sense.
  202. xpdoc.LoadXml (xpath);
  203. t.LoadInnerXml (xpdoc.ChildNodes);
  204. XmlDocument doc = new XmlDocument ();
  205. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  206. t.LoadInput (doc);
  207. XmlNodeList nl = (XmlNodeList) t.GetOutput ();
  208. AssertEquals (XmlNodeType.Document, nl [0].NodeType);
  209. AssertEquals (XmlNodeType.Element, nl [1].NodeType);
  210. AssertEquals ("element", nl [1].LocalName);
  211. AssertEquals (XmlNodeType.Element, nl [2].NodeType);
  212. AssertEquals ("foo", nl [2].LocalName);
  213. AssertEquals (XmlNodeType.Element, nl [3].NodeType);
  214. AssertEquals ("bar", nl [3].LocalName);
  215. // MS.NET bug - ms.net returns ns node even when the
  216. // current node is ns node (it is like returning
  217. // attribute from attribute nodes).
  218. // AssertEquals (XmlNodeType.Attribute, nl [4].NodeType);
  219. // AssertEquals ("xmlns", nl [4].LocalName);
  220. }
  221. [Test]
  222. [Category ("NotWorking")]
  223. // MS.NET looks incorrect, or something incorrect in this test code; It turned out nothing to do with function here()
  224. public void FunctionHereObsolete ()
  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. string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'></XPath>";
  231. xpdoc.LoadXml (xpath);
  232. t.LoadInnerXml (xpdoc.ChildNodes);
  233. XmlDocument doc = new XmlDocument ();
  234. doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
  235. t.LoadInput (doc);
  236. XmlNodeList nl = (XmlNodeList) t.GetOutput ();
  237. AssertEquals (0, nl.Count);
  238. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  239. t.LoadInput (doc);
  240. nl = (XmlNodeList) t.GetOutput ();
  241. AssertEquals (1, nl.Count);
  242. doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
  243. t.LoadInput (doc);
  244. nl = (XmlNodeList) t.GetOutput ();
  245. AssertEquals (2, nl.Count);
  246. doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
  247. t.LoadInput (doc);
  248. nl = (XmlNodeList) t.GetOutput ();
  249. AssertEquals (3, nl.Count);
  250. doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
  251. t.LoadInput (doc);
  252. nl = (XmlNodeList) t.GetOutput ();
  253. AssertEquals (1, nl.Count);
  254. AssertEquals (XmlNodeType.Attribute, nl [0].NodeType);
  255. }
  256. }
  257. }