26cdfaab39556cc88511d8596a134aebc5b0e40d
[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)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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;
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 #if ONLY_1_1
101                 [ExpectedException (typeof (NullReferenceException))]
102 #endif
103                 public void OnlyInner () 
104                 {
105                         XmlNodeList inner = InnerXml (""); // empty
106                         transform.LoadInnerXml (inner);
107                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
108 #if NET_2_0
109                         AssertEquals ("Count", 0, xnl.Count);
110 #endif
111                 }
112
113                 private XmlDocument GetDoc () 
114                 {
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 ();
122                         doc.LoadXml (test);
123                         return doc;
124                 }
125
126                 private XmlNodeList InnerXml (string xpathExpr) 
127                 {
128                         string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
129                         XmlDocument doc = new XmlDocument ();
130                         doc.LoadXml (xpath);
131                         return doc.ChildNodes;
132                 }
133
134                 [Test]
135 #if NET_2_0
136                 [Category ("NotWorking")]
137 #endif
138                 public void LoadInputAsXmlDocument () 
139                 {
140                         XmlDocument doc = GetDoc ();
141                         transform.LoadInput (doc);
142                         XmlNodeList inner = InnerXml ("//*/title");
143                         transform.LoadInnerXml (inner);
144                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
145 #if NET_2_0
146                         AssertEquals (73, xnl.Count);
147 #else
148                         AssertEquals (47, xnl.Count);
149 #endif
150                 }
151
152                 [Test]
153                 public void LoadInputAsXmlDocument_EmptyXPath () 
154                 {
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);
160                 }
161
162                 [Test]
163                 [Category ("NotWorking")]
164                 public void LoadInputAsXmlNodeList () 
165                 {
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);
172                 }
173
174                 [Test]
175                 public void LoadInputAsXmlNodeList_EmptyXPath () 
176                 {
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);
182                 }
183
184                 [Test]
185 #if NET_2_0
186                 [Category ("NotWorking")]
187 #endif
188                 public void LoadInputAsStream () 
189                 {
190                         XmlDocument doc = GetDoc ();
191                         doc.PreserveWhitespace = true;
192                         MemoryStream ms = new MemoryStream ();
193                         doc.Save (ms);
194                         ms.Position = 0;
195                         transform.LoadInput (ms);
196                         XmlNodeList inner = InnerXml ("//*/title");
197                         transform.LoadInnerXml (inner);
198                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
199 #if NET_2_0
200                         AssertEquals (73, xnl.Count);
201 #else
202                         AssertEquals (47, xnl.Count);
203 #endif
204                 }
205
206                 [Test]
207                 public void LoadInputAsStream_EmptyXPath () 
208                 {
209                         XmlDocument doc = GetDoc ();
210                         MemoryStream ms = new MemoryStream ();
211                         doc.Save (ms);
212                         ms.Position = 0;
213                         transform.LoadInput (ms);
214                         // empty means no LoadInnerXml
215                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
216                         AssertEquals ("Empy Result", 0, xnl.Count);
217                 }
218
219                 [Test]
220                 public void LoadInnerXml () 
221                 {
222                         XmlNodeList inner = InnerXml ("//*");
223                         transform.LoadInnerXml (inner);
224                         XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
225                         AssertEquals ("LoadInnerXml", inner, xnl);
226                 }
227
228                 [Test]
229                 public void UnsupportedInput () 
230                 {
231                         byte[] bad = { 0xBA, 0xD };
232                         // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
233                         transform.LoadInput (bad);
234                 }
235
236                 [Test]
237                 [ExpectedException (typeof (ArgumentException))]
238                 public void UnsupportedOutput () 
239                 {
240                         XmlDocument doc = new XmlDocument ();
241                         object o = transform.GetOutput (doc.GetType ());
242                 }
243
244                 [Test]
245                 public void TransformSimple ()
246                 {
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>");
255                         t.LoadInput (doc);
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);
269                 }
270
271                 [Test]
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 ()
275                 {
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 ();
284
285                         doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
286                         t.LoadInput (doc);
287
288                         XmlNodeList nl = (XmlNodeList) t.GetOutput ();
289                         AssertEquals ("0", 0, nl.Count);
290
291                         doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
292                         t.LoadInput (doc);
293                         nl = (XmlNodeList) t.GetOutput ();
294 #if NET_2_0
295                         AssertEquals ("1", 0, nl.Count);
296 #else
297                         AssertEquals ("1", 1, nl.Count);
298 #endif
299
300                         doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
301                         t.LoadInput (doc);
302                         nl = (XmlNodeList) t.GetOutput ();
303 #if NET_2_0
304                         AssertEquals ("2", 0, nl.Count);
305 #else
306                         AssertEquals ("2", 2, nl.Count);
307 #endif
308
309                         doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
310                         t.LoadInput (doc);
311                         nl = (XmlNodeList) t.GetOutput ();
312 #if NET_2_0
313                         AssertEquals ("3", 0, nl.Count);
314 #else
315                         AssertEquals ("3", 3, nl.Count);
316 #endif
317
318                         doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
319                         t.LoadInput (doc);
320                         nl = (XmlNodeList) t.GetOutput ();
321 #if NET_2_0
322                         AssertEquals ("4", 0, nl.Count);
323 #else
324                         AssertEquals ("4", 1, nl.Count);
325                         AssertEquals ("NodeType", XmlNodeType.Attribute, nl [0].NodeType);
326 #endif
327                 }
328         }
329 }