[bcl] Remove more NET_2_0 checks from class libs
[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 {
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                         Assert.AreEqual ("http://www.w3.org/TR/1999/REC-xpath-19991116", transform.Algorithm, "Algorithm");
49
50                         Type[] input = transform.InputTypes;
51                         Assert.IsTrue ((input.Length == 3), "Input #");
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.IsTrue (istream, "Input Stream");
65                         Assert.IsTrue (ixmldoc, "Input XmlDocument");
66                         Assert.IsTrue (ixmlnl, "Input XmlNodeList");
67
68                         Type[] output = transform.OutputTypes;
69                         Assert.IsTrue ((output.Length == 1), "Output #");
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.IsTrue (oxmlnl, "Output XmlNodeList");
77                 }
78
79                 protected void AreEqual (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                                         Assert.Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
84                         }
85                         Assert.AreEqual (expected.Count, actual.Count);
86                 }
87
88                 [Test]
89                 [Ignore ("throws a NullReferenceException - but it's (kind of internal)")]
90                 public void GetInnerXml () 
91                 {
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");
95                 }
96
97                 [Test]
98 #if ONLY_1_1
99                 [ExpectedException (typeof (NullReferenceException))]
100 #endif
101                 public void OnlyInner () 
102                 {
103                         XmlNodeList inner = InnerXml (""); // empty
104                         transform.LoadInnerXml (inner);
105                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
106                         Assert.AreEqual (0, xnl.Count, "Count");
107                 }
108
109                 private XmlDocument GetDoc () 
110                 {
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 ();
118                         doc.LoadXml (test);
119                         return doc;
120                 }
121
122                 private XmlNodeList InnerXml (string xpathExpr) 
123                 {
124                         string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
125                         XmlDocument doc = new XmlDocument ();
126                         doc.LoadXml (xpath);
127                         return doc.ChildNodes;
128                 }
129
130                 [Test]
131                 [Category ("NotWorking")]
132                 public void LoadInputAsXmlDocument () 
133                 {
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);
140                 }
141
142                 [Test]
143                 public void LoadInputAsXmlDocument_EmptyXPath () 
144                 {
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");
150                 }
151
152                 [Test]
153                 [Category ("NotWorking")]
154                 public void LoadInputAsXmlNodeList () 
155                 {
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);
162                 }
163
164                 [Test]
165                 public void LoadInputAsXmlNodeList_EmptyXPath () 
166                 {
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");
172                 }
173
174                 [Test]
175                 [Category ("NotWorking")]
176                 public void LoadInputAsStream () 
177                 {
178                         XmlDocument doc = GetDoc ();
179                         doc.PreserveWhitespace = true;
180                         MemoryStream ms = new MemoryStream ();
181                         doc.Save (ms);
182                         ms.Position = 0;
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);
188                 }
189
190                 [Test]
191                 public void LoadInputAsStream_EmptyXPath () 
192                 {
193                         XmlDocument doc = GetDoc ();
194                         MemoryStream ms = new MemoryStream ();
195                         doc.Save (ms);
196                         ms.Position = 0;
197                         transform.LoadInput (ms);
198                         // empty means no LoadInnerXml
199                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
200                         Assert.AreEqual (0, xnl.Count, "Empy Result");
201                 }
202
203                 [Test]
204                 public void LoadInnerXml () 
205                 {
206                         XmlNodeList inner = InnerXml ("//*");
207                         transform.LoadInnerXml (inner);
208                         XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
209                         Assert.AreEqual (inner, xnl, "LoadInnerXml");
210                 }
211
212                 [Test]
213                 public void UnsupportedInput () 
214                 {
215                         byte[] bad = { 0xBA, 0xD };
216                         // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
217                         transform.LoadInput (bad);
218                 }
219
220                 [Test]
221                 [ExpectedException (typeof (ArgumentException))]
222                 public void UnsupportedOutput () 
223                 {
224                         XmlDocument doc = new XmlDocument ();
225                         object o = transform.GetOutput (doc.GetType ());
226                 }
227
228                 [Test]
229                 public void TransformSimple ()
230                 {
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>");
239                         t.LoadInput (doc);
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);
253                 }
254
255                 [Test]
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 ()
259                 {
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 ();
268
269                         doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
270                         t.LoadInput (doc);
271
272                         XmlNodeList nl = (XmlNodeList) t.GetOutput ();
273                         Assert.AreEqual (0, nl.Count, "0");
274
275                         doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
276                         t.LoadInput (doc);
277                         nl = (XmlNodeList) t.GetOutput ();
278                         Assert.AreEqual (0, nl.Count, "1");
279
280                         doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
281                         t.LoadInput (doc);
282                         nl = (XmlNodeList) t.GetOutput ();
283                         Assert.AreEqual (0, nl.Count, "2");
284
285                         doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
286                         t.LoadInput (doc);
287                         nl = (XmlNodeList) t.GetOutput ();
288                         Assert.AreEqual (0, nl.Count, "3");
289
290                         doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
291                         t.LoadInput (doc);
292                         nl = (XmlNodeList) t.GetOutput ();
293                         Assert.AreEqual (0, nl.Count, "4");
294                 }
295         }
296 }