XmlDsigXPathTransformTest.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. public void LoadInputAsXmlNodeList ()
  129. {
  130. XmlDocument doc = GetDoc ();
  131. transform.LoadInput (doc.ChildNodes);
  132. XmlNodeList inner = InnerXml ("//*/title");
  133. transform.LoadInnerXml (inner);
  134. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  135. AssertEquals (47, xnl.Count);
  136. }
  137. [Test]
  138. public void LoadInputAsXmlNodeList_EmptyXPath ()
  139. {
  140. XmlDocument doc = GetDoc ();
  141. transform.LoadInput (doc.ChildNodes);
  142. // empty means no LoadInnerXml
  143. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  144. AssertEquals ("Empy Result", 0, xnl.Count);
  145. }
  146. [Test]
  147. public void LoadInputAsStream ()
  148. {
  149. XmlDocument doc = GetDoc ();
  150. doc.PreserveWhitespace = true;
  151. MemoryStream ms = new MemoryStream ();
  152. doc.Save (ms);
  153. ms.Position = 0;
  154. transform.LoadInput (ms);
  155. XmlNodeList inner = InnerXml ("//*/title");
  156. transform.LoadInnerXml (inner);
  157. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  158. AssertEquals (47, xnl.Count);
  159. }
  160. [Test]
  161. public void LoadInputAsStream_EmptyXPath ()
  162. {
  163. XmlDocument doc = GetDoc ();
  164. MemoryStream ms = new MemoryStream ();
  165. doc.Save (ms);
  166. ms.Position = 0;
  167. transform.LoadInput (ms);
  168. // empty means no LoadInnerXml
  169. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  170. AssertEquals ("Empy Result", 0, xnl.Count);
  171. }
  172. [Test]
  173. public void LoadInnerXml ()
  174. {
  175. XmlNodeList inner = InnerXml ("//*");
  176. transform.LoadInnerXml (inner);
  177. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  178. AssertEquals ("LoadInnerXml", inner, xnl);
  179. }
  180. [Test]
  181. public void UnsupportedInput ()
  182. {
  183. byte[] bad = { 0xBA, 0xD };
  184. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  185. transform.LoadInput (bad);
  186. }
  187. [Test]
  188. [ExpectedException (typeof (ArgumentException))]
  189. public void UnsupportedOutput ()
  190. {
  191. XmlDocument doc = new XmlDocument ();
  192. object o = transform.GetOutput (doc.GetType ());
  193. }
  194. [Test]
  195. public void TransformSimple ()
  196. {
  197. XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
  198. XmlDocument xpdoc = new XmlDocument ();
  199. string ns = "http://www.w3.org/2000/09/xmldsig#";
  200. string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>*|@*|namespace::*</XPath>"; // not absolute path.. so @* and namespace::* does not make sense.
  201. xpdoc.LoadXml (xpath);
  202. t.LoadInnerXml (xpdoc.ChildNodes);
  203. XmlDocument doc = new XmlDocument ();
  204. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  205. t.LoadInput (doc);
  206. XmlNodeList nl = (XmlNodeList) t.GetOutput ();
  207. AssertEquals (XmlNodeType.Document, nl [0].NodeType);
  208. AssertEquals (XmlNodeType.Element, nl [1].NodeType);
  209. AssertEquals ("element", nl [1].LocalName);
  210. AssertEquals (XmlNodeType.Element, nl [2].NodeType);
  211. AssertEquals ("foo", nl [2].LocalName);
  212. AssertEquals (XmlNodeType.Element, nl [3].NodeType);
  213. AssertEquals ("bar", nl [3].LocalName);
  214. // MS.NET bug - ms.net returns ns node even when the
  215. // current node is ns node (it is like returning
  216. // attribute from attribute nodes).
  217. // AssertEquals (XmlNodeType.Attribute, nl [4].NodeType);
  218. // AssertEquals ("xmlns", nl [4].LocalName);
  219. }
  220. [Test]
  221. [Ignore ("MS.NET looks incorrect, or something incorrect in this test code; It turned out nothing to do with function here()")]
  222. public void FunctionHereObsolete ()
  223. {
  224. XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
  225. XmlDocument xpdoc = new XmlDocument ();
  226. string ns = "http://www.w3.org/2000/09/xmldsig#";
  227. // string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>here()</XPath>";
  228. string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'></XPath>";
  229. xpdoc.LoadXml (xpath);
  230. t.LoadInnerXml (xpdoc.ChildNodes);
  231. XmlDocument doc = new XmlDocument ();
  232. doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
  233. t.LoadInput (doc);
  234. XmlNodeList nl = (XmlNodeList) t.GetOutput ();
  235. AssertEquals (0, nl.Count);
  236. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  237. t.LoadInput (doc);
  238. nl = (XmlNodeList) t.GetOutput ();
  239. AssertEquals (1, nl.Count);
  240. doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
  241. t.LoadInput (doc);
  242. nl = (XmlNodeList) t.GetOutput ();
  243. AssertEquals (2, nl.Count);
  244. doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
  245. t.LoadInput (doc);
  246. nl = (XmlNodeList) t.GetOutput ();
  247. AssertEquals (3, nl.Count);
  248. doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
  249. t.LoadInput (doc);
  250. nl = (XmlNodeList) t.GetOutput ();
  251. AssertEquals (1, nl.Count);
  252. AssertEquals (XmlNodeType.Attribute, nl [0].NodeType);
  253. }
  254. }
  255. }