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)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
13 using System.Security.Cryptography;
14 using System.Security.Cryptography.Xml;
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);
101 [ExpectedException (typeof (NullReferenceException))]
103 public void OnlyInner ()
105 XmlNodeList inner = InnerXml (""); // empty
106 transform.LoadInnerXml (inner);
107 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
109 AssertEquals ("Count", 0, xnl.Count);
113 private XmlDocument GetDoc ()
115 string test = "<catalog><cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><price>10.90</price>";
116 test += "<year>1985</year></cd><cd><title>Hide your heart</title><artist>Bonnie Tyler</artist><price>9.90</price>";
117 test += "<year>1988</year></cd><cd><title>Greatest Hits</title><artist>Dolly Parton</artist><price>9.90</price>";
118 test += "<year>1982</year></cd><cd><title>Still got the blues</title><artist>Gary Moore</artist><price>10.20</price>";
119 test += "<year>1990</year></cd><cd><title>Eros</title><artist>Eros Ramazzotti</artist><price>9.90</price>";
120 test += "<year>1997</year></cd></catalog>";
121 XmlDocument doc = new XmlDocument ();
126 private XmlNodeList InnerXml (string xpathExpr)
128 string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
129 XmlDocument doc = new XmlDocument ();
131 return doc.ChildNodes;
136 [Category ("NotWorking")]
138 public void LoadInputAsXmlDocument ()
140 XmlDocument doc = GetDoc ();
141 transform.LoadInput (doc);
142 XmlNodeList inner = InnerXml ("//*/title");
143 transform.LoadInnerXml (inner);
144 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
146 AssertEquals (73, xnl.Count);
148 AssertEquals (47, xnl.Count);
153 public void LoadInputAsXmlDocument_EmptyXPath ()
155 XmlDocument doc = GetDoc ();
156 transform.LoadInput (doc);
157 // empty means no LoadInnerXml
158 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
159 AssertEquals ("Empy Result", 0, xnl.Count);
163 [Category ("NotWorking")]
164 public void LoadInputAsXmlNodeList ()
166 XmlDocument doc = GetDoc ();
167 transform.LoadInput (doc.ChildNodes);
168 XmlNodeList inner = InnerXml ("//*/title");
169 transform.LoadInnerXml (inner);
170 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
171 AssertEquals (1, xnl.Count);
175 public void LoadInputAsXmlNodeList_EmptyXPath ()
177 XmlDocument doc = GetDoc ();
178 transform.LoadInput (doc.ChildNodes);
179 // empty means no LoadInnerXml
180 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
181 AssertEquals ("Empy Result", 0, xnl.Count);
186 [Category ("NotWorking")]
188 public void LoadInputAsStream ()
190 XmlDocument doc = GetDoc ();
191 doc.PreserveWhitespace = true;
192 MemoryStream ms = new MemoryStream ();
195 transform.LoadInput (ms);
196 XmlNodeList inner = InnerXml ("//*/title");
197 transform.LoadInnerXml (inner);
198 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
200 AssertEquals (73, xnl.Count);
202 AssertEquals (47, xnl.Count);
207 public void LoadInputAsStream_EmptyXPath ()
209 XmlDocument doc = GetDoc ();
210 MemoryStream ms = new MemoryStream ();
213 transform.LoadInput (ms);
214 // empty means no LoadInnerXml
215 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
216 AssertEquals ("Empy Result", 0, xnl.Count);
220 public void LoadInnerXml ()
222 XmlNodeList inner = InnerXml ("//*");
223 transform.LoadInnerXml (inner);
224 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
225 AssertEquals ("LoadInnerXml", inner, xnl);
229 public void UnsupportedInput ()
231 byte[] bad = { 0xBA, 0xD };
232 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
233 transform.LoadInput (bad);
237 [ExpectedException (typeof (ArgumentException))]
238 public void UnsupportedOutput ()
240 XmlDocument doc = new XmlDocument ();
241 object o = transform.GetOutput (doc.GetType ());
245 public void TransformSimple ()
247 XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
248 XmlDocument xpdoc = new XmlDocument ();
249 string ns = "http://www.w3.org/2000/09/xmldsig#";
250 string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>*|@*|namespace::*</XPath>"; // not absolute path.. so @* and namespace::* does not make sense.
251 xpdoc.LoadXml (xpath);
252 t.LoadInnerXml (xpdoc.ChildNodes);
253 XmlDocument doc = new XmlDocument ();
254 doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
256 XmlNodeList nl = (XmlNodeList) t.GetOutput ();
257 AssertEquals (XmlNodeType.Document, nl [0].NodeType);
258 AssertEquals (XmlNodeType.Element, nl [1].NodeType);
259 AssertEquals ("element", nl [1].LocalName);
260 AssertEquals (XmlNodeType.Element, nl [2].NodeType);
261 AssertEquals ("foo", nl [2].LocalName);
262 AssertEquals (XmlNodeType.Element, nl [3].NodeType);
263 AssertEquals ("bar", nl [3].LocalName);
264 // MS.NET bug - ms.net returns ns node even when the
265 // current node is ns node (it is like returning
266 // attribute from attribute nodes).
267 // AssertEquals (XmlNodeType.Attribute, nl [4].NodeType);
268 // AssertEquals ("xmlns", nl [4].LocalName);
272 [Category ("NotWorking")]
273 // MS.NET looks incorrect, or something incorrect in this test code; It turned out nothing to do with function here()
274 public void FunctionHereObsolete ()
276 XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
277 XmlDocument xpdoc = new XmlDocument ();
278 string ns = "http://www.w3.org/2000/09/xmldsig#";
279 // string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>here()</XPath>";
280 string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'></XPath>";
281 xpdoc.LoadXml (xpath);
282 t.LoadInnerXml (xpdoc.ChildNodes);
283 XmlDocument doc = new XmlDocument ();
285 doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
288 XmlNodeList nl = (XmlNodeList) t.GetOutput ();
289 AssertEquals ("0", 0, nl.Count);
291 doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
293 nl = (XmlNodeList) t.GetOutput ();
295 AssertEquals ("1", 0, nl.Count);
297 AssertEquals ("1", 1, nl.Count);
300 doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
302 nl = (XmlNodeList) t.GetOutput ();
304 AssertEquals ("2", 0, nl.Count);
306 AssertEquals ("2", 2, nl.Count);
309 doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
311 nl = (XmlNodeList) t.GetOutput ();
313 AssertEquals ("3", 0, nl.Count);
315 AssertEquals ("3", 3, nl.Count);
318 doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
320 nl = (XmlNodeList) t.GetOutput ();
322 AssertEquals ("4", 0, nl.Count);
324 AssertEquals ("4", 1, nl.Count);
325 AssertEquals ("NodeType", XmlNodeType.Attribute, nl [0].NodeType);