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 {
37 protected UnprotectedXmlDsigXPathTransform transform;
40 protected void SetUp ()
42 transform = new UnprotectedXmlDsigXPathTransform ();
46 public void Properties ()
48 Assert.AreEqual ("http://www.w3.org/TR/1999/REC-xpath-19991116", transform.Algorithm, "Algorithm");
50 Type[] input = transform.InputTypes;
51 Assert.IsTrue ((input.Length == 3), "Input #");
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.IsTrue (istream, "Input Stream");
65 Assert.IsTrue (ixmldoc, "Input XmlDocument");
66 Assert.IsTrue (ixmlnl, "Input XmlNodeList");
68 Type[] output = transform.OutputTypes;
69 Assert.IsTrue ((output.Length == 1), "Output #");
70 // check presence of every supported output types
72 foreach (Type t in output) {
73 if (t.ToString () == "System.Xml.XmlNodeList")
76 Assert.IsTrue (oxmlnl, "Output XmlNodeList");
79 protected void AreEqual (string msg, XmlNodeList expected, XmlNodeList actual)
81 for (int i=0; i < expected.Count; i++) {
82 if (expected [i].OuterXml != actual [i].OuterXml)
83 Assert.Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
85 Assert.AreEqual (expected.Count, actual.Count);
89 [Ignore ("throws a NullReferenceException - but it's (kind of internal)")]
90 public void GetInnerXml ()
92 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
93 Assert.AreEqual (1, xnl.Count, "Default InnerXml.Count");
94 Assert.AreEqual ("<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\"></XPath>", xnl [0].OuterXml, "Default InnerXml.OuterXml");
99 [ExpectedException (typeof (NullReferenceException))]
101 public void OnlyInner ()
103 XmlNodeList inner = InnerXml (""); // empty
104 transform.LoadInnerXml (inner);
105 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
106 Assert.AreEqual (0, xnl.Count, "Count");
109 private XmlDocument GetDoc ()
111 string test = "<catalog><cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><price>10.90</price>";
112 test += "<year>1985</year></cd><cd><title>Hide your heart</title><artist>Bonnie Tyler</artist><price>9.90</price>";
113 test += "<year>1988</year></cd><cd><title>Greatest Hits</title><artist>Dolly Parton</artist><price>9.90</price>";
114 test += "<year>1982</year></cd><cd><title>Still got the blues</title><artist>Gary Moore</artist><price>10.20</price>";
115 test += "<year>1990</year></cd><cd><title>Eros</title><artist>Eros Ramazzotti</artist><price>9.90</price>";
116 test += "<year>1997</year></cd></catalog>";
117 XmlDocument doc = new XmlDocument ();
122 private XmlNodeList InnerXml (string xpathExpr)
124 string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
125 XmlDocument doc = new XmlDocument ();
127 return doc.ChildNodes;
131 [Category ("NotWorking")]
132 public void LoadInputAsXmlDocument ()
134 XmlDocument doc = GetDoc ();
135 transform.LoadInput (doc);
136 XmlNodeList inner = InnerXml ("//*/title");
137 transform.LoadInnerXml (inner);
138 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
139 Assert.AreEqual (73, xnl.Count);
143 public void LoadInputAsXmlDocument_EmptyXPath ()
145 XmlDocument doc = GetDoc ();
146 transform.LoadInput (doc);
147 // empty means no LoadInnerXml
148 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
149 Assert.AreEqual (0, xnl.Count, "Empy Result");
153 [Category ("NotWorking")]
154 public void LoadInputAsXmlNodeList ()
156 XmlDocument doc = GetDoc ();
157 transform.LoadInput (doc.ChildNodes);
158 XmlNodeList inner = InnerXml ("//*/title");
159 transform.LoadInnerXml (inner);
160 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
161 Assert.AreEqual (1, xnl.Count);
165 public void LoadInputAsXmlNodeList_EmptyXPath ()
167 XmlDocument doc = GetDoc ();
168 transform.LoadInput (doc.ChildNodes);
169 // empty means no LoadInnerXml
170 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
171 Assert.AreEqual (0, xnl.Count, "Empy Result");
175 [Category ("NotWorking")]
176 public void LoadInputAsStream ()
178 XmlDocument doc = GetDoc ();
179 doc.PreserveWhitespace = true;
180 MemoryStream ms = new MemoryStream ();
183 transform.LoadInput (ms);
184 XmlNodeList inner = InnerXml ("//*/title");
185 transform.LoadInnerXml (inner);
186 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
187 Assert.AreEqual (73, xnl.Count);
191 public void LoadInputAsStream_EmptyXPath ()
193 XmlDocument doc = GetDoc ();
194 MemoryStream ms = new MemoryStream ();
197 transform.LoadInput (ms);
198 // empty means no LoadInnerXml
199 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
200 Assert.AreEqual (0, xnl.Count, "Empy Result");
204 public void LoadInnerXml ()
206 XmlNodeList inner = InnerXml ("//*");
207 transform.LoadInnerXml (inner);
208 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
209 Assert.AreEqual (inner, xnl, "LoadInnerXml");
213 public void UnsupportedInput ()
215 byte[] bad = { 0xBA, 0xD };
216 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
217 transform.LoadInput (bad);
221 [ExpectedException (typeof (ArgumentException))]
222 public void UnsupportedOutput ()
224 XmlDocument doc = new XmlDocument ();
225 object o = transform.GetOutput (doc.GetType ());
229 public void TransformSimple ()
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'>*|@*|namespace::*</XPath>"; // not absolute path.. so @* and namespace::* does not make sense.
235 xpdoc.LoadXml (xpath);
236 t.LoadInnerXml (xpdoc.ChildNodes);
237 XmlDocument doc = new XmlDocument ();
238 doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
240 XmlNodeList nl = (XmlNodeList) t.GetOutput ();
241 Assert.AreEqual (XmlNodeType.Document, nl [0].NodeType);
242 Assert.AreEqual (XmlNodeType.Element, nl [1].NodeType);
243 Assert.AreEqual ("element", nl [1].LocalName);
244 Assert.AreEqual (XmlNodeType.Element, nl [2].NodeType);
245 Assert.AreEqual ("foo", nl [2].LocalName);
246 Assert.AreEqual (XmlNodeType.Element, nl [3].NodeType);
247 Assert.AreEqual ("bar", nl [3].LocalName);
248 // MS.NET bug - ms.net returns ns node even when the
249 // current node is ns node (it is like returning
250 // attribute from attribute nodes).
251 // Assert.AreEqual (XmlNodeType.Attribute, nl [4].NodeType);
252 // Assert.AreEqual ("xmlns", nl [4].LocalName);
256 [Category ("NotWorking")]
257 // MS.NET looks incorrect, or something incorrect in this test code; It turned out nothing to do with function here()
258 public void FunctionHereObsolete ()
260 XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
261 XmlDocument xpdoc = new XmlDocument ();
262 string ns = "http://www.w3.org/2000/09/xmldsig#";
263 // string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>here()</XPath>";
264 string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'></XPath>";
265 xpdoc.LoadXml (xpath);
266 t.LoadInnerXml (xpdoc.ChildNodes);
267 XmlDocument doc = new XmlDocument ();
269 doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
272 XmlNodeList nl = (XmlNodeList) t.GetOutput ();
273 Assert.AreEqual (0, nl.Count, "0");
275 doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
277 nl = (XmlNodeList) t.GetOutput ();
278 Assert.AreEqual (0, nl.Count, "1");
280 doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
282 nl = (XmlNodeList) t.GetOutput ();
283 Assert.AreEqual (0, nl.Count, "2");
285 doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
287 nl = (XmlNodeList) t.GetOutput ();
288 Assert.AreEqual (0, nl.Count, "3");
290 doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
292 nl = (XmlNodeList) t.GetOutput ();
293 Assert.AreEqual (0, nl.Count, "4");