2007-12-18 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / XmlDsigC14NTransformTest.cs
1 //
2 // XmlDsigC14NTransformTest.cs - NUnit Test Cases for XmlDsigC14NTransform
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //      Aleksey Sanin (aleksey@aleksey.com)
7 //
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // (C) 2003 Aleksey Sanin (aleksey@aleksey.com)
10 // (C) 2004 Novell (http://www.novell.com)
11 //
12
13 using System;
14 using System.IO;
15 using System.Security.Cryptography.Xml;
16 using System.Text;
17 using System.Xml;
18
19 using NUnit.Framework;
20
21 namespace MonoTests.System.Security.Cryptography.Xml {
22
23         // Note: GetInnerXml is protected in XmlDsigC14NTransform making it
24         // difficult to test properly. This class "open it up" :-)
25         public class UnprotectedXmlDsigC14NTransform : XmlDsigC14NTransform {
26
27                 public XmlNodeList UnprotectedGetInnerXml () {
28                         return base.GetInnerXml ();
29                 }
30         }
31
32         [TestFixture]
33         public class XmlDsigC14NTransformTest : Assertion {
34
35                 protected UnprotectedXmlDsigC14NTransform transform;
36
37                 [SetUp]
38                 protected void SetUp () 
39                 {
40                         transform = new UnprotectedXmlDsigC14NTransform ();
41                 }
42
43                 [TearDown]
44                 protected void CleanUp () 
45                 {
46                         try {
47                                 if (File.Exists ("doc.dtd"))
48                                         File.Delete ("doc.dtd");
49                                 if (File.Exists ("world.txt"))
50                                         File.Delete ("world.txt");
51                         }
52                         catch {}
53                 }
54
55                 [Test]
56                 public void Properties () 
57                 {
58                         AssertEquals ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", transform.Algorithm);
59
60                         Type[] input = transform.InputTypes;
61                         Assert ("Input #", (input.Length == 3));
62                         // check presence of every supported input types
63                         bool istream = false;
64                         bool ixmldoc = false;
65                         bool ixmlnl = false;
66                         foreach (Type t in input) {
67                                 if (t.ToString () == "System.IO.Stream")
68                                         istream = true;
69                                 if (t.ToString () == "System.Xml.XmlDocument")
70                                         ixmldoc = true;
71                                 if (t.ToString () == "System.Xml.XmlNodeList")
72                                         ixmlnl = true;
73                         }
74                         Assert ("Input Stream", istream);
75                         Assert ("Input XmlDocument", ixmldoc);
76                         Assert ("Input XmlNodeList", ixmlnl);
77
78                         Type[] output = transform.OutputTypes;
79                         Assert ("Output #", (output.Length == 1));
80                         // check presence of every supported output types
81                         bool ostream = false;
82                         foreach (Type t in output) {
83                                 if (t.ToString () == "System.IO.Stream")
84                                         ostream = true;
85                         }
86                         Assert ("Output Stream", ostream);
87                 }
88
89                 [Test]
90                 public void GetInnerXml () 
91                 {
92                         XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
93                         AssertNull ("Default InnerXml", xnl);
94                 }
95
96                 private string Stream2String (Stream s) 
97                 {
98                         StringBuilder sb = new StringBuilder ();
99                         int b = s.ReadByte ();
100                         while (b != -1) {
101                                 sb.Append (Convert.ToChar (b));
102                                 b = s.ReadByte ();
103                         }
104                         return sb.ToString ();
105                 }
106
107                 static string xml = "<Test  attrib='at ' xmlns=\"http://www.go-mono.com/\" > \r\n &#xD; <Toto/> text &amp; </Test   >";
108                 // BAD for XmlDocument input (framework 1.0 result)
109                 static string c14xml1 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \r\n \r <Toto></Toto> text &amp; </Test>";
110                 // GOOD for Stream input
111                 static string c14xml2 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \n &#xD; <Toto></Toto> text &amp; </Test>";
112                 // GOOD for XmlDocument input. The difference is because once
113                 // xml string is loaded to XmlDocument, there is no difference
114                 // between \r and &#xD;, so every \r must be handled as &#xD;.
115                 static string c14xml3 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> &#xD;\n &#xD; <Toto></Toto> text &amp; </Test>";
116
117                 private XmlDocument GetDoc () 
118                 {
119                         XmlDocument doc = new XmlDocument ();
120                         doc.PreserveWhitespace = true;
121                         doc.LoadXml (xml);
122                         return doc;
123                 }
124
125                 [Test]
126                 public void LoadInputAsXmlDocument () 
127                 {
128                         XmlDocument doc = GetDoc ();
129                         transform.LoadInput (doc);
130                         Stream s = (Stream) transform.GetOutput ();
131                         string output = Stream2String (s);
132 #if NET_1_1
133                         AssertEquals("XmlDocument", c14xml3, output);
134 #else
135                         // .NET 1.0 keeps the \r\n (0x0D, 0x0A) - bug
136                         AssertEquals("XmlDocument", c14xml1, output);
137 #endif
138                 }
139
140                 [Test]
141 #if NET_2_0
142                 [Category ("NotDotNet")]\r
143                 // see LoadInputAsXmlNodeList2 description
144 #endif
145                 public void LoadInputAsXmlNodeList () 
146                 {
147                         XmlDocument doc = GetDoc ();
148                         // Argument list just contains element Test.
149                         transform.LoadInput (doc.ChildNodes);
150                         Stream s = (Stream) transform.GetOutput ();
151                         string output = Stream2String (s);
152                         AssertEquals ("XmlChildNodes", "<Test></Test>", output);
153                 }
154
155                 [Test]
156                 [Category ("NotDotNet")]
157                 // MS has a bug that those namespace declaration nodes in
158                 // the node-set are written to output. Related spec section is:
159                 // http://www.w3.org/TR/2001/REC-xml-c14n-20010315#ProcessingModel
160                 public void LoadInputAsXmlNodeList2 () 
161                 {
162                         XmlDocument doc = GetDoc ();
163                         transform.LoadInput (doc.SelectNodes ("//*"));
164                         Stream s = (Stream) transform.GetOutput ();
165                         string output = Stream2String (s);
166                         string expected = @"<Test><Toto></Toto></Test>";
167                         AssertEquals ("XmlChildNodes", expected, output);
168                 }
169
170                 [Test]
171                 public void LoadInputAsStream () 
172                 {
173                         MemoryStream ms = new MemoryStream ();
174                         byte[] x = Encoding.ASCII.GetBytes (xml);
175                         ms.Write (x, 0, x.Length);
176                         ms.Position = 0;
177                         transform.LoadInput (ms);
178                         Stream s = (Stream) transform.GetOutput ();
179                         string output = Stream2String (s);
180                         AssertEquals ("MemoryStream", c14xml2, output);
181                 }
182
183                 [Test]
184 #if NET_2_0
185                 [ExpectedException (typeof (ArgumentException))]
186 #else
187                 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
188 #endif
189                 public void LoadInputWithUnsupportedType () 
190                 {
191                         byte[] bad = { 0xBA, 0xD };
192                         transform.LoadInput (bad);
193                 }
194
195                 [Test]
196                 [ExpectedException (typeof (ArgumentException))]
197                 public void UnsupportedOutput () 
198                 {
199                         XmlDocument doc = new XmlDocument();
200                         object o = transform.GetOutput (doc.GetType ());
201                 }
202
203                 [Test]
204                 public void C14NSpecExample1 ()
205                 {
206                         using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
207                                 sw.Write ("<!-- presence, not content, required -->");
208                                 sw.Close ();
209                         }
210                         string res = ExecuteXmlDSigC14NTransform (C14NSpecExample1Input);
211                         AssertEquals ("Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (without comments)", 
212                                         C14NSpecExample1Output, res);
213                 }
214
215                 [Test]
216                 public void C14NSpecExample2 ()
217                 {
218                         string res = ExecuteXmlDSigC14NTransform (C14NSpecExample2Input);
219                         AssertEquals ("Example 2 from c14n spec - Whitespace in Document Content (without comments)", 
220                                         C14NSpecExample2Output, res);
221                 }
222
223                 [Test]
224                 public void C14NSpecExample3 ()
225                 {
226                         string res = ExecuteXmlDSigC14NTransform (C14NSpecExample3Input);
227                         AssertEquals ("Example 3 from c14n spec - Start and End Tags (without comments)", 
228                                         C14NSpecExample3Output, res);
229                 }
230             
231                 [Test]
232 //              [Ignore ("This test should be fine, but it does not pass under MS.NET")]
233                 public void C14NSpecExample4 ()
234                 {
235                         string res = ExecuteXmlDSigC14NTransform (C14NSpecExample4Input);
236                         AssertEquals ("Example 4 from c14n spec - Character Modifications and Character References (without comments)", 
237                                         C14NSpecExample4Output, res);
238                 }
239             
240                 [Test]
241                 public void C14NSpecExample5 ()
242                 {
243                         using (StreamWriter sw = new StreamWriter ("world.txt", false, Encoding.ASCII)) {
244                                 sw.Write ("world");
245                                 sw.Close ();
246                         }
247                         string res = ExecuteXmlDSigC14NTransform (C14NSpecExample5Input);
248                         AssertEquals ("Example 5 from c14n spec - Entity References (without comments)", 
249                                         C14NSpecExample5Output, res);
250                 }
251     
252                 [Test]
253                 public void C14NSpecExample6 ()
254                 {
255                         string res = ExecuteXmlDSigC14NTransform (C14NSpecExample6Input);
256                         AssertEquals ("Example 6 from c14n spec - UTF-8 Encoding (without comments)", 
257                                         C14NSpecExample6Output, res);
258                 }
259
260                 private string ExecuteXmlDSigC14NTransform (string InputXml)
261                 {
262                         XmlDocument doc = new XmlDocument ();
263                         doc.PreserveWhitespace = true;
264                         doc.LoadXml (InputXml);
265                 
266                         // Testing default attribute support with
267                         // vreader.ValidationType = ValidationType.None.
268                         //
269                         UTF8Encoding utf8 = new UTF8Encoding ();
270                         byte[] data = utf8.GetBytes (InputXml.ToString ());
271                         Stream stream = new MemoryStream (data);
272                         XmlTextReader reader = new XmlTextReader (stream);
273                         XmlValidatingReader vreader = new XmlValidatingReader (reader);
274                         vreader.ValidationType = ValidationType.None;
275                         vreader.EntityHandling = EntityHandling.ExpandCharEntities;
276                         doc.Load (vreader);
277
278                         transform.LoadInput (doc);
279                         return Stream2String ((Stream)transform.GetOutput ());
280                 }
281
282                 //
283                 // Example 1 from C14N spec - PIs, Comments, and Outside of Document Element: 
284                 // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
285                 // 
286                 // Aleksey: 
287                 // removed reference to an empty external DTD
288                 //
289                 static string C14NSpecExample1Input =  
290                         "<?xml version=\"1.0\"?>\n" +
291                         "\n" +
292                         "<?xml-stylesheet   href=\"doc.xsl\"\n" +
293                         "   type=\"text/xsl\"   ?>\n" +
294                         "\n" +
295                         // "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
296                         "\n" +
297                         "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
298                         "\n" +
299                         "<?pi-without-data     ?>\n\n" +
300                         "<!-- Comment 2 -->\n\n" +
301                         "<!-- Comment 3 -->\n";
302                 static string C14NSpecExample1Output =   
303                         "<?xml-stylesheet href=\"doc.xsl\"\n" +
304                         "   type=\"text/xsl\"   ?>\n" +
305                         "<doc>Hello, world!</doc>\n" +
306                         "<?pi-without-data?>";
307                 
308                 //
309                 // Example 2 from C14N spec - Whitespace in Document Content: 
310                 // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
311                 // 
312                 static string C14NSpecExample2Input =  
313                         "<doc>\n" +
314                         "  <clean>   </clean>\n" +
315                         "   <dirty>   A   B   </dirty>\n" +
316                         "   <mixed>\n" +
317                         "      A\n" +
318                         "      <clean>   </clean>\n" +
319                         "      B\n" +
320                         "      <dirty>   A   B   </dirty>\n" +
321                         "      C\n" +
322                         "   </mixed>\n" +
323                         "</doc>\n";
324                 static string C14NSpecExample2Output =   
325                         "<doc>\n" +
326                         "  <clean>   </clean>\n" +
327                         "   <dirty>   A   B   </dirty>\n" +
328                         "   <mixed>\n" +
329                         "      A\n" +
330                         "      <clean>   </clean>\n" +
331                         "      B\n" +
332                         "      <dirty>   A   B   </dirty>\n" +
333                         "      C\n" +
334                         "   </mixed>\n" +
335                         "</doc>";
336             
337                 //
338                 // Example 3 from C14N spec - Start and End Tags: 
339                 // http://www.w3.org/TR/xml-c14n#Example-SETags
340                 //
341                 static string C14NSpecExample3Input =  
342                         "<!DOCTYPE doc [<!ATTLIST e9 attr CDATA \"default\">]>\n" +
343                         "<doc>\n" +
344                         "   <e1   />\n" +
345                         "   <e2   ></e2>\n" +
346                         "   <e3    name = \"elem3\"   id=\"elem3\"    />\n" +
347                         "   <e4    name=\"elem4\"   id=\"elem4\"    ></e4>\n" +
348                         "   <e5 a:attr=\"out\" b:attr=\"sorted\" attr2=\"all\" attr=\"I\'m\"\n" +
349                         "       xmlns:b=\"http://www.ietf.org\" \n" +
350                         "       xmlns:a=\"http://www.w3.org\"\n" +
351                         "       xmlns=\"http://www.uvic.ca\"/>\n" +
352                         "   <e6 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
353                         "       <e7 xmlns=\"http://www.ietf.org\">\n" +
354                         "           <e8 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
355                         "               <e9 xmlns=\"\" xmlns:a=\"http://www.ietf.org\"/>\n" +
356                         "           </e8>\n" +
357                         "       </e7>\n" +
358                         "   </e6>\n" +
359                         "</doc>\n";
360                 static string C14NSpecExample3Output =   
361                         "<doc>\n" +
362                         "   <e1></e1>\n" +
363                         "   <e2></e2>\n" +
364                         "   <e3 id=\"elem3\" name=\"elem3\"></e3>\n" +
365                         "   <e4 id=\"elem4\" name=\"elem4\"></e4>\n" +
366                         "   <e5 xmlns=\"http://www.uvic.ca\" xmlns:a=\"http://www.w3.org\" xmlns:b=\"http://www.ietf.org\" attr=\"I\'m\" attr2=\"all\" b:attr=\"sorted\" a:attr=\"out\"></e5>\n" +
367                         "   <e6 xmlns:a=\"http://www.w3.org\">\n" +
368                         "       <e7 xmlns=\"http://www.ietf.org\">\n" +
369                         "           <e8 xmlns=\"\">\n" +
370                         "               <e9 xmlns:a=\"http://www.ietf.org\" attr=\"default\"></e9>\n" +
371 //                      "               <e9 xmlns:a=\"http://www.ietf.org\"></e9>\n" +
372                         "           </e8>\n" +
373                         "       </e7>\n" +
374                         "   </e6>\n" +
375                         "</doc>";
376             
377             
378                 //
379                 // Example 4 from C14N spec - Character Modifications and Character References: 
380                 // http://www.w3.org/TR/xml-c14n#Example-Chars
381                 //
382                 // Aleksey: 
383                 // This test does not include "normId" element
384                 // because it has an invalid ID attribute "id" which
385                 // should be normalized by XML parser. Currently Mono
386                 // does not support this (see comment after this example
387                 // in the spec).
388                 static string C14NSpecExample4Input =  
389                         "<!DOCTYPE doc [<!ATTLIST normId id ID #IMPLIED>]>\n" +
390                         "<doc>\n" +
391                         "   <text>First line&#x0d;&#10;Second line</text>\n" +
392                         "   <value>&#x32;</value>\n" +
393                         "   <compute><![CDATA[value>\"0\" && value<\"10\" ?\"valid\":\"error\"]]></compute>\n" +
394                         "   <compute expr=\'value>\"0\" &amp;&amp; value&lt;\"10\" ?\"valid\":\"error\"\'>valid</compute>\n" +
395                         "   <norm attr=\' &apos;   &#x20;&#13;&#xa;&#9;   &apos; \'/>\n" +
396                         // "   <normId id=\' &apos;   &#x20;&#13;&#xa;&#9;   &apos; \'/>\n" +
397                         "</doc>\n";
398                 static string C14NSpecExample4Output =   
399                         "<doc>\n" +
400                         "   <text>First line&#xD;\n" +
401                         "Second line</text>\n" +
402                         "   <value>2</value>\n" +
403                         "   <compute>value&gt;\"0\" &amp;&amp; value&lt;\"10\" ?\"valid\":\"error\"</compute>\n" +
404                         "   <compute expr=\"value>&quot;0&quot; &amp;&amp; value&lt;&quot;10&quot; ?&quot;valid&quot;:&quot;error&quot;\">valid</compute>\n" +
405                         "   <norm attr=\" \'    &#xD;&#xA;&#x9;   \' \"></norm>\n" +
406                         // "   <normId id=\"\' &#xD;&#xA;&#x9; \'\"></normId>\n" +
407                         "</doc>";
408             
409                 //
410                 // Example 5 from C14N spec - Entity References: 
411                 // http://www.w3.org/TR/xml-c14n#Example-Entities
412                 //
413                 static string C14NSpecExample5Input =  
414                         "<!DOCTYPE doc [\n" +
415                         "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
416                         "<!ENTITY ent1 \"Hello\">\n" +
417                         "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
418                         "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
419                         "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
420                         "]>\n" +
421                         "<doc attrExtEnt=\"entExt\">\n" +
422                         "   &ent1;, &ent2;!\n" +
423                         "</doc>\n" +
424                         "\n" +
425                         "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
426                 static string C14NSpecExample5Output =  
427                         "<doc attrExtEnt=\"entExt\">\n" +
428                         "   Hello, world!\n" +
429                         "</doc>";           
430     
431                 //
432                 // Example 6 from C14N spec - UTF-8 Encoding: 
433                 // http://www.w3.org/TR/xml-c14n#Example-UTF8
434                 // 
435                 static string C14NSpecExample6Input =  
436                         "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
437                         "<doc>&#169;</doc>\n";
438                 static string C14NSpecExample6Output =  
439                         "<doc>\xC2\xA9</doc>";
440             
441
442                 //
443                 // Example 7 from C14N spec - Document Subsets: 
444                 // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
445                 // 
446                 // Aleksey:
447                 // Well, XPath support in Mono is far from complete....
448                 // I was not able to simplify the xpath expression from this test
449                 // so it runs on Mono and still makes sense for testing this feature.
450                 // Thus this test is not included in the suite now.
451                 static string C14NSpecExample7Input =  
452                         "<!DOCTYPE doc [\n" +
453                         "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
454                         "<!ATTLIST e3 id ID #IMPLIED>\n" +
455                         "]>\n" +
456                         "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
457                         "   <e1>\n" +
458                         "      <e2 xmlns=\"\">\n" +
459                         "         <e3 id=\"E3\"/>\n" +
460                         "      </e2>\n" +
461                         "   </e1>\n" +
462                         "</doc>\n";
463
464                 static string C14NSpecExample7Xpath =  
465                         "(//.|//@*|//namespace::*)\n" +
466                         "[\n" +
467                         "self::ietf:e1\n" +
468                         "    or\n" +
469                         "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
470                         "    or\n" +
471                         "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
472                         "]";
473                 static string C14NSpecExample7Output =  
474                         "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
475
476                 [Test]
477                 public void SimpleNamespacePrefixes ()
478                 {
479                         string input = "<a:Action xmlns:a='urn:foo'>http://tempuri.org/IFoo/Echo</a:Action>";
480                         string expected = @"<a:Action xmlns:a=""urn:foo"">http://tempuri.org/IFoo/Echo</a:Action>";
481
482                         XmlDocument doc = new XmlDocument ();
483                         doc.LoadXml (input);
484                         XmlDsigC14NTransform t = new XmlDsigC14NTransform ();
485                         t.LoadInput (doc);
486                         Stream s = t.GetOutput () as Stream;
487                         AssertEquals (expected, new StreamReader (s, Encoding.UTF8).ReadToEnd ());
488                 }
489
490 #if NET_2_0
491                 [Test]
492                 public void PrefixlessNamespaceOutput ()
493                 {
494                         XmlDocument doc = new XmlDocument ();
495                         doc.AppendChild (doc.CreateElement ("foo", "urn:foo"));
496                         doc.DocumentElement.AppendChild (doc.CreateElement ("bar", "urn:bar"));
497                         AssertEquals ("#1", String.Empty, doc.DocumentElement.GetAttribute ("xmlns"));
498                         XmlDsigC14NTransform t = new XmlDsigC14NTransform ();
499                         t.LoadInput (doc);
500                         Stream s = t.GetOutput () as Stream;
501                         AssertEquals ("<foo xmlns=\"urn:foo\"><bar xmlns=\"urn:bar\"></bar></foo>", new StreamReader (s, Encoding.UTF8).ReadToEnd ());
502                         AssertEquals ("#2", "urn:foo", doc.DocumentElement.GetAttribute ("xmlns"));
503                 }
504
505                 [Test]
506                 [Ignore ("find out how PropagatedNamespaces returns non-null instance on .NET")]
507                 public void PropagatedNamespaces ()
508                 {
509                         XmlDocument doc = new XmlDocument ();
510                         doc.AppendChild (doc.CreateElement ("foo", "urn:foo"));
511                         doc.DocumentElement.AppendChild (doc.CreateElement ("bar", "urn:bar"));
512                         AssertEquals ("#1", String.Empty, doc.DocumentElement.GetAttribute ("xmlns:f"));
513                         XmlDsigExcC14NTransform t = new XmlDsigExcC14NTransform ();
514                         t.LoadInput (doc);
515                         t.PropagatedNamespaces.Add ("f", "urn:foo");
516                         t.PropagatedNamespaces.Add ("b", "urn:bar");
517                         Stream s = t.GetOutput () as Stream;
518                         AssertEquals ("<f:foo xmlns:f=\"urn:foo\"><b:bar xmlns:b=\"urn:bar\"></b:bar></f:foo>", new StreamReader (s, Encoding.UTF8).ReadToEnd ());
519                         AssertEquals ("#2", "urn:foo", doc.DocumentElement.GetAttribute ("xmlns:f"));
520                 }
521 #endif
522         }    
523 }