2 // XmlDsigXPathTransformTest.cs - NUnit Test Cases for XmlDsigXPathTransform
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
\r
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
\r
13 using System.Security.Cryptography;
14 using System.Security.Cryptography.Xml;
17 using System.Xml.Xsl;
\r
18 using System.Xml.XPath;
20 using NUnit.Framework;
22 namespace MonoTests.System.Security.Cryptography.Xml {
24 // Note: GetInnerXml is protected in XmlDsigXPathTransform making it
25 // difficult to test properly. This class "open it up" :-)
26 public class UnprotectedXmlDsigXPathTransform : XmlDsigXPathTransform {
28 public XmlNodeList UnprotectedGetInnerXml ()
30 return base.GetInnerXml ();
35 public class XmlDsigXPathTransformTest : Assertion {
37 protected UnprotectedXmlDsigXPathTransform transform;
40 protected void SetUp ()
42 transform = new UnprotectedXmlDsigXPathTransform ();
46 public void Properties ()
48 AssertEquals ("Algorithm", "http://www.w3.org/TR/1999/REC-xpath-19991116", transform.Algorithm);
50 Type[] input = transform.InputTypes;
51 Assert ("Input #", (input.Length == 3));
52 // check presence of every supported input types
56 foreach (Type t in input) {
57 if (t.ToString () == "System.IO.Stream")
59 if (t.ToString () == "System.Xml.XmlDocument")
61 if (t.ToString () == "System.Xml.XmlNodeList")
64 Assert ("Input Stream", istream);
65 Assert ("Input XmlDocument", ixmldoc);
66 Assert ("Input XmlNodeList", ixmlnl);
68 Type[] output = transform.OutputTypes;
69 Assert ("Output #", (output.Length == 1));
70 // check presence of every supported output types
72 foreach (Type t in output) {
73 if (t.ToString () == "System.Xml.XmlNodeList")
76 Assert ("Output XmlNodeList", oxmlnl);
79 protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual)
81 for (int i=0; i < expected.Count; i++) {
82 if (expected [i].OuterXml != actual [i].OuterXml)
83 Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
85 AssertEquals (expected.Count, actual.Count);
90 [Ignore ("throws a NullReferenceException - but it's (kind of internal)")]
92 public void GetInnerXml ()
94 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
95 AssertEquals ("Default InnerXml.Count", 1, xnl.Count);
96 AssertEquals ("Default InnerXml.OuterXml", "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\"></XPath>", xnl [0].OuterXml);
100 [ExpectedException (typeof (NullReferenceException))]
101 public void OnlyInner ()
103 XmlNodeList inner = InnerXml (""); // empty
104 transform.LoadInnerXml (inner);
105 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
108 private XmlDocument GetDoc ()
110 string test = "<catalog><cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><price>10.90</price>";
111 test += "<year>1985</year></cd><cd><title>Hide your heart</title><artist>Bonnie Tyler</artist><price>9.90</price>";
112 test += "<year>1988</year></cd><cd><title>Greatest Hits</title><artist>Dolly Parton</artist><price>9.90</price>";
113 test += "<year>1982</year></cd><cd><title>Still got the blues</title><artist>Gary Moore</artist><price>10.20</price>";
114 test += "<year>1990</year></cd><cd><title>Eros</title><artist>Eros Ramazzotti</artist><price>9.90</price>";
115 test += "<year>1997</year></cd></catalog>";
116 XmlDocument doc = new XmlDocument ();
121 private XmlNodeList InnerXml (string xpathExpr)
123 string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
124 XmlDocument doc = new XmlDocument ();
126 return doc.ChildNodes;
131 [Category ("NotWorking")]
133 public void LoadInputAsXmlDocument ()
135 XmlDocument doc = GetDoc ();
136 transform.LoadInput (doc);
137 XmlNodeList inner = InnerXml ("//*/title");
138 transform.LoadInnerXml (inner);
139 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
141 AssertEquals (73, xnl.Count);
143 AssertEquals (47, xnl.Count);
149 [ExpectedException (typeof (XPathException))]
\r
151 public void LoadInputAsXmlDocument_EmptyXPath ()
153 XmlDocument doc = GetDoc ();
154 transform.LoadInput (doc);
155 // empty means no LoadInnerXml
156 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
157 AssertEquals ("Empy Result", 0, xnl.Count);
161 [Category ("NotWorking")]
162 public void LoadInputAsXmlNodeList ()
164 XmlDocument doc = GetDoc ();
165 transform.LoadInput (doc.ChildNodes);
166 XmlNodeList inner = InnerXml ("//*/title");
167 transform.LoadInnerXml (inner);
168 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
169 AssertEquals (1, xnl.Count);
174 [ExpectedException (typeof (XPathException))]
\r
176 public void LoadInputAsXmlNodeList_EmptyXPath ()
178 XmlDocument doc = GetDoc ();
179 transform.LoadInput (doc.ChildNodes);
180 // empty means no LoadInnerXml
181 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
182 AssertEquals ("Empy Result", 0, xnl.Count);
187 [Category ("NotWorking")]
189 public void LoadInputAsStream ()
191 XmlDocument doc = GetDoc ();
192 doc.PreserveWhitespace = true;
193 MemoryStream ms = new MemoryStream ();
196 transform.LoadInput (ms);
197 XmlNodeList inner = InnerXml ("//*/title");
198 transform.LoadInnerXml (inner);
199 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
201 AssertEquals (73, xnl.Count);
\r
203 AssertEquals (47, xnl.Count);
209 [ExpectedException (typeof (XPathException))]
211 public void LoadInputAsStream_EmptyXPath ()
213 XmlDocument doc = GetDoc ();
214 MemoryStream ms = new MemoryStream ();
217 transform.LoadInput (ms);
218 // empty means no LoadInnerXml
219 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
220 AssertEquals ("Empy Result", 0, xnl.Count);
224 public void LoadInnerXml ()
226 XmlNodeList inner = InnerXml ("//*");
227 transform.LoadInnerXml (inner);
228 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
229 AssertEquals ("LoadInnerXml", inner, xnl);
233 public void UnsupportedInput ()
235 byte[] bad = { 0xBA, 0xD };
236 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
237 transform.LoadInput (bad);
241 [ExpectedException (typeof (ArgumentException))]
242 public void UnsupportedOutput ()
244 XmlDocument doc = new XmlDocument ();
245 object o = transform.GetOutput (doc.GetType ());
249 public void TransformSimple ()
251 XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
252 XmlDocument xpdoc = new XmlDocument ();
253 string ns = "http://www.w3.org/2000/09/xmldsig#";
254 string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>*|@*|namespace::*</XPath>"; // not absolute path.. so @* and namespace::* does not make sense.
255 xpdoc.LoadXml (xpath);
256 t.LoadInnerXml (xpdoc.ChildNodes);
257 XmlDocument doc = new XmlDocument ();
258 doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
260 XmlNodeList nl = (XmlNodeList) t.GetOutput ();
261 AssertEquals (XmlNodeType.Document, nl [0].NodeType);
262 AssertEquals (XmlNodeType.Element, nl [1].NodeType);
263 AssertEquals ("element", nl [1].LocalName);
264 AssertEquals (XmlNodeType.Element, nl [2].NodeType);
265 AssertEquals ("foo", nl [2].LocalName);
266 AssertEquals (XmlNodeType.Element, nl [3].NodeType);
267 AssertEquals ("bar", nl [3].LocalName);
268 // MS.NET bug - ms.net returns ns node even when the
269 // current node is ns node (it is like returning
270 // attribute from attribute nodes).
271 // AssertEquals (XmlNodeType.Attribute, nl [4].NodeType);
272 // AssertEquals ("xmlns", nl [4].LocalName);
276 [Category ("NotWorking")]
277 // MS.NET looks incorrect, or something incorrect in this test code; It turned out nothing to do with function here()
\r
279 [ExpectedException (typeof (XPathException))]
\r
281 public void FunctionHereObsolete ()
283 XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
284 XmlDocument xpdoc = new XmlDocument ();
285 string ns = "http://www.w3.org/2000/09/xmldsig#";
286 // string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>here()</XPath>";
287 string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'></XPath>";
288 xpdoc.LoadXml (xpath);
289 t.LoadInnerXml (xpdoc.ChildNodes);
290 XmlDocument doc = new XmlDocument ();
292 doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
294 // FX 2.0 throws at next line!
295 XmlNodeList nl = (XmlNodeList) t.GetOutput ();
296 AssertEquals (0, nl.Count);
298 doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
300 nl = (XmlNodeList) t.GetOutput ();
301 AssertEquals (1, nl.Count);
303 doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
305 nl = (XmlNodeList) t.GetOutput ();
306 AssertEquals (2, nl.Count);
308 doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
310 nl = (XmlNodeList) t.GetOutput ();
311 AssertEquals (3, nl.Count);
313 doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
315 nl = (XmlNodeList) t.GetOutput ();
316 AssertEquals (1, nl.Count);
317 AssertEquals (XmlNodeType.Attribute, nl [0].NodeType);