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