872aa161ab0517380850036b27b2f7b56a95096e
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / XmlDsigXPathTransformTest.cs
1 //
2 // XmlDsigXPathTransformTest.cs - NUnit Test Cases for XmlDsigXPathTransform
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)\r
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)\r
9 //
10
11 using System;
12 using System.IO;
13 using System.Security.Cryptography;
14 using System.Security.Cryptography.Xml;
15 using System.Text;
16 using System.Xml;
17 using System.Xml.Xsl;\r
18 using System.Xml.XPath;
19
20 using NUnit.Framework;
21
22 namespace MonoTests.System.Security.Cryptography.Xml {
23
24         // Note: GetInnerXml is protected in XmlDsigXPathTransform making it
25         // difficult to test properly. This class "open it up" :-)
26         public class UnprotectedXmlDsigXPathTransform : XmlDsigXPathTransform {
27
28                 public XmlNodeList UnprotectedGetInnerXml () 
29                 {
30                         return base.GetInnerXml ();
31                 }
32         }
33
34         [TestFixture]
35         public class XmlDsigXPathTransformTest : Assertion {
36
37                 protected UnprotectedXmlDsigXPathTransform transform;
38
39                 [SetUp]
40                 protected void SetUp () 
41                 {
42                         transform = new UnprotectedXmlDsigXPathTransform ();
43                 }
44
45                 [Test]
46                 public void Properties () 
47                 {
48                         AssertEquals ("Algorithm", "http://www.w3.org/TR/1999/REC-xpath-19991116", transform.Algorithm);
49
50                         Type[] input = transform.InputTypes;
51                         Assert ("Input #", (input.Length == 3));
52                         // check presence of every supported input types
53                         bool istream = false;
54                         bool ixmldoc = false;
55                         bool ixmlnl = false;
56                         foreach (Type t in input) {
57                                 if (t.ToString () == "System.IO.Stream")
58                                         istream = true;
59                                 if (t.ToString () == "System.Xml.XmlDocument")
60                                         ixmldoc = true;
61                                 if (t.ToString () == "System.Xml.XmlNodeList")
62                                         ixmlnl = true;
63                         }
64                         Assert ("Input Stream", istream);
65                         Assert ("Input XmlDocument", ixmldoc);
66                         Assert ("Input XmlNodeList", ixmlnl);
67
68                         Type[] output = transform.OutputTypes;
69                         Assert ("Output #", (output.Length == 1));
70                         // check presence of every supported output types
71                         bool oxmlnl = false;
72                         foreach (Type t in output) {
73                                 if (t.ToString () == "System.Xml.XmlNodeList")
74                                         oxmlnl = true;
75                         }
76                         Assert ("Output XmlNodeList", oxmlnl);
77                 }
78
79                 protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual) 
80                 {
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);
84                         }
85                         AssertEquals (expected.Count, actual.Count);
86                 }
87
88                 [Test]
89 #if NET_2_0
90                 [Ignore ("throws a NullReferenceException - but it's (kind of internal)")]
91 #endif
92                 public void GetInnerXml () 
93                 {
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);
97                 }
98
99                 [Test]
100                 [ExpectedException (typeof (NullReferenceException))]
101                 public void OnlyInner () 
102                 {
103                         XmlNodeList inner = InnerXml (""); // empty
104                         transform.LoadInnerXml (inner);
105                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
106                 }
107
108                 private XmlDocument GetDoc () 
109                 {
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 ();
117                         doc.LoadXml (test);
118                         return doc;
119                 }
120
121                 private XmlNodeList InnerXml (string xpathExpr) 
122                 {
123                         string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
124                         XmlDocument doc = new XmlDocument ();
125                         doc.LoadXml (xpath);
126                         return doc.ChildNodes;
127                 }
128
129                 [Test]
130 #if NET_2_0
131                 [Category ("NotWorking")]
132 #endif
133                 public void LoadInputAsXmlDocument () 
134                 {
135                         XmlDocument doc = GetDoc ();
136                         transform.LoadInput (doc);
137                         XmlNodeList inner = InnerXml ("//*/title");
138                         transform.LoadInnerXml (inner);
139                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
140 #if NET_2_0
141                         AssertEquals (73, xnl.Count);
142 #else
143                         AssertEquals (47, xnl.Count);
144 #endif
145                 }
146
147                 [Test]\r
148 #if NET_2_0\r
149                 [ExpectedException (typeof (XPathException))]\r
150 #endif\r
151                 public void LoadInputAsXmlDocument_EmptyXPath () 
152                 {
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);
158                 }
159
160                 [Test]
161                 [Category ("NotWorking")]
162                 public void LoadInputAsXmlNodeList () 
163                 {
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);
170                 }
171
172                 [Test]\r
173 #if NET_2_0\r
174                 [ExpectedException (typeof (XPathException))]\r
175 #endif\r
176                 public void LoadInputAsXmlNodeList_EmptyXPath () 
177                 {
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);
183                 }
184
185                 [Test]
186 #if NET_2_0
187                 [Category ("NotWorking")]
188 #endif
189                 public void LoadInputAsStream () 
190                 {
191                         XmlDocument doc = GetDoc ();
192                         doc.PreserveWhitespace = true;
193                         MemoryStream ms = new MemoryStream ();
194                         doc.Save (ms);
195                         ms.Position = 0;
196                         transform.LoadInput (ms);
197                         XmlNodeList inner = InnerXml ("//*/title");
198                         transform.LoadInnerXml (inner);
199                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
200 #if NET_2_0\r
201                         AssertEquals (73, xnl.Count);\r
202 #else
203                         AssertEquals (47, xnl.Count);
204 #endif
205                 }
206
207                 [Test]
208 #if NET_2_0
209                 [ExpectedException (typeof (XPathException))]
210 #endif
211                 public void LoadInputAsStream_EmptyXPath () 
212                 {
213                         XmlDocument doc = GetDoc ();
214                         MemoryStream ms = new MemoryStream ();
215                         doc.Save (ms);
216                         ms.Position = 0;
217                         transform.LoadInput (ms);
218                         // empty means no LoadInnerXml
219                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
220                         AssertEquals ("Empy Result", 0, xnl.Count);
221                 }
222
223                 [Test]
224                 public void LoadInnerXml () 
225                 {
226                         XmlNodeList inner = InnerXml ("//*");
227                         transform.LoadInnerXml (inner);
228                         XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
229                         AssertEquals ("LoadInnerXml", inner, xnl);
230                 }
231
232                 [Test]
233                 public void UnsupportedInput () 
234                 {
235                         byte[] bad = { 0xBA, 0xD };
236                         // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
237                         transform.LoadInput (bad);
238                 }
239
240                 [Test]
241                 [ExpectedException (typeof (ArgumentException))]
242                 public void UnsupportedOutput () 
243                 {
244                         XmlDocument doc = new XmlDocument ();
245                         object o = transform.GetOutput (doc.GetType ());
246                 }
247
248                 [Test]
249                 public void TransformSimple ()
250                 {
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>");
259                         t.LoadInput (doc);
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);
273                 }
274
275                 [Test]
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
278 #if NET_2_0\r
279                 [ExpectedException (typeof (XPathException))]\r
280 #endif\r
281                 public void FunctionHereObsolete ()
282                 {
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 ();
291
292                         doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
293                         t.LoadInput (doc);
294                         // FX 2.0 throws at next line!
295                         XmlNodeList nl = (XmlNodeList) t.GetOutput ();
296                         AssertEquals (0, nl.Count);
297
298                         doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
299                         t.LoadInput (doc);
300                         nl = (XmlNodeList) t.GetOutput ();
301                         AssertEquals (1, nl.Count);
302
303                         doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
304                         t.LoadInput (doc);
305                         nl = (XmlNodeList) t.GetOutput ();
306                         AssertEquals (2, nl.Count);
307
308                         doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
309                         t.LoadInput (doc);
310                         nl = (XmlNodeList) t.GetOutput ();
311                         AssertEquals (3, nl.Count);
312
313                         doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
314                         t.LoadInput (doc);
315                         nl = (XmlNodeList) t.GetOutput ();
316                         AssertEquals (1, nl.Count);
317                         AssertEquals (XmlNodeType.Attribute, nl [0].NodeType);
318                 }
319         }
320 }