2 // XmlDsigExcC14NTransformTest.cs - NUnit Test Cases for XmlDsigExcC14NTransform
6 // Sebastien Pouliot <sebastien@ximian.com>
7 // Aleksey Sanin (aleksey@aleksey.com)
9 // Atsushi Enomoto <atsushi@ximian.com>
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)
18 // This file is simply replaced C14N->ExcC14N, and then replaced expected
19 // output XML. So, they are *not* from c14n specification.
25 using System.Security.Cryptography.Xml;
29 using NUnit.Framework;
31 namespace MonoTests.System.Security.Cryptography.Xml {
33 // Note: GetInnerXml is protected in XmlDsigExcC14NTransform making it
34 // difficult to test properly. This class "open it up" :-)
35 public class UnprotectedXmlDsigExcC14NTransform : XmlDsigExcC14NTransform {
36 public UnprotectedXmlDsigExcC14NTransform ()
40 public UnprotectedXmlDsigExcC14NTransform (bool includeComments)
41 : base (includeComments)
45 public UnprotectedXmlDsigExcC14NTransform (string inclusiveNamespacesPrefixList)
46 : base (inclusiveNamespacesPrefixList)
50 public UnprotectedXmlDsigExcC14NTransform (bool includeComments, string inclusiveNamespacesPrefixList)
51 : base (includeComments, inclusiveNamespacesPrefixList)
55 public XmlNodeList UnprotectedGetInnerXml () {
56 return base.GetInnerXml ();
61 public class XmlDsigExcC14NTransformTest {
63 protected UnprotectedXmlDsigExcC14NTransform transform;
66 protected void SetUp ()
68 transform = new UnprotectedXmlDsigExcC14NTransform ();
72 protected void CleanUp ()
75 if (File.Exists ("doc.dtd"))
76 File.Delete ("doc.dtd");
77 if (File.Exists ("world.txt"))
78 File.Delete ("world.txt");
84 public void Constructor1 ()
86 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
87 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
88 CheckProperties (transform);
91 [Test] // ctor (Boolean)
92 public void Constructor2 ()
94 transform = new UnprotectedXmlDsigExcC14NTransform (true);
95 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#WithComments", transform.Algorithm, "Algorithm");
96 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
97 CheckProperties (transform);
99 transform = new UnprotectedXmlDsigExcC14NTransform (false);
100 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
101 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
102 CheckProperties (transform);
105 [Test] // ctor (String)
106 public void Constructor3 ()
108 transform = new UnprotectedXmlDsigExcC14NTransform (null);
109 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
110 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
111 CheckProperties (transform);
113 transform = new UnprotectedXmlDsigExcC14NTransform (string.Empty);
114 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
115 Assert.AreEqual (string.Empty, transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
116 CheckProperties (transform);
118 transform = new UnprotectedXmlDsigExcC14NTransform ("#default xsd");
119 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
120 Assert.AreEqual ("#default xsd", transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
121 CheckProperties (transform);
124 [Test] // ctor (Boolean, String)
125 public void Constructor4 ()
127 transform = new UnprotectedXmlDsigExcC14NTransform (true, null);
128 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#WithComments", transform.Algorithm, "Algorithm");
129 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
130 CheckProperties (transform);
132 transform = new UnprotectedXmlDsigExcC14NTransform (true, string.Empty);
133 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#WithComments", transform.Algorithm, "Algorithm");
134 Assert.AreEqual (string.Empty, transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
135 CheckProperties (transform);
137 transform = new UnprotectedXmlDsigExcC14NTransform (true, "#default xsd");
138 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#WithComments", transform.Algorithm, "Algorithm");
139 Assert.AreEqual ("#default xsd", transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
140 CheckProperties (transform);
142 transform = new UnprotectedXmlDsigExcC14NTransform (false, null);
143 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
144 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
145 CheckProperties (transform);
147 transform = new UnprotectedXmlDsigExcC14NTransform (false, string.Empty);
148 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
149 Assert.AreEqual (string.Empty, transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
150 CheckProperties (transform);
152 transform = new UnprotectedXmlDsigExcC14NTransform (false, "#default xsd");
153 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
154 Assert.AreEqual ("#default xsd", transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
155 CheckProperties (transform);
158 void CheckProperties (XmlDsigExcC14NTransform transform)
160 Type[] input = transform.InputTypes;
161 Assert.IsTrue ((input.Length == 3), "Input #");
162 // check presence of every supported input types
163 bool istream = false;
164 bool ixmldoc = false;
166 foreach (Type t in input) {
167 if (t.ToString () == "System.IO.Stream")
169 if (t.ToString () == "System.Xml.XmlDocument")
171 if (t.ToString () == "System.Xml.XmlNodeList")
174 Assert.IsTrue (istream, "Input Stream");
175 Assert.IsTrue (ixmldoc, "Input XmlDocument");
176 Assert.IsTrue (ixmlnl, "Input XmlNodeList");
178 Type[] output = transform.OutputTypes;
179 Assert.IsTrue ((output.Length == 1), "Output #");
180 // check presence of every supported output types
181 bool ostream = false;
182 foreach (Type t in output) {
183 if (t.ToString () == "System.IO.Stream")
186 Assert.IsTrue (ostream, "Output Stream");
192 Type [] input = transform.InputTypes;
196 // property does not return a clone
197 foreach (Type t in transform.InputTypes) {
200 // it's not a static array
201 XmlDsigExcC14NTransform t2 = new XmlDsigExcC14NTransform ();
202 foreach (Type t in t2.InputTypes) {
203 Assert.IsNotNull (t);
208 public void GetInnerXml ()
210 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
211 Assert.IsNull (xnl, "Default InnerXml");
214 private string Stream2String (Stream s)
216 StringBuilder sb = new StringBuilder ();
217 int b = s.ReadByte ();
219 sb.Append (Convert.ToChar (b));
222 return sb.ToString ();
225 static string xml = "<Test attrib='at ' xmlns=\"http://www.go-mono.com/\" > \r\n 
 <Toto/> text & </Test >";
226 // BAD for XmlDocument input (framework 1.0 result)
227 static string c14xml1 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \r\n \r <Toto></Toto> text & </Test>";
228 // GOOD for Stream input
229 static string c14xml2 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \n 
 <Toto></Toto> text & </Test>";
230 // GOOD for XmlDocument input. The difference is because once
231 // xml string is loaded to XmlDocument, there is no difference
232 // between \r and 
, so every \r must be handled as 
.
233 static string c14xml3 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> 
\n 
 <Toto></Toto> text & </Test>";
235 private XmlDocument GetDoc ()
237 XmlDocument doc = new XmlDocument ();
238 doc.PreserveWhitespace = true;
244 public void LoadInputAsXmlDocument ()
246 XmlDocument doc = GetDoc ();
247 transform.LoadInput (doc);
248 Stream s = (Stream) transform.GetOutput ();
249 string output = Stream2String (s);
250 Assert.AreEqual (c14xml3, output, "XmlDocument");
254 // see LoadInputAsXmlNodeList2 description
255 public void LoadInputAsXmlNodeList ()
257 XmlDocument doc = GetDoc ();
258 // Argument list just contains element Test.
259 transform.LoadInput (doc.ChildNodes);
260 Stream s = (Stream) transform.GetOutput ();
261 string output = Stream2String (s);
262 Assert.AreEqual ("<Test xmlns=\"http://www.go-mono.com/\"></Test>", output, "XmlChildNodes");
266 // MS has a bug that those namespace declaration nodes in
267 // the node-set are written to output. Related spec section is:
268 // http://www.w3.org/TR/2001/REC-xml-c14n-20010315#ProcessingModel
269 public void LoadInputAsXmlNodeList2 ()
271 XmlDocument doc = GetDoc ();
272 transform.LoadInput (doc.SelectNodes ("//*"));
273 Stream s = (Stream) transform.GetOutput ();
274 string output = Stream2String (s);
275 string expected = "<Test xmlns=\"http://www.go-mono.com/\"><Toto></Toto></Test>";
276 Assert.AreEqual (expected, output, "XmlChildNodes");
280 public void LoadInputAsStream ()
282 MemoryStream ms = new MemoryStream ();
283 byte[] x = Encoding.ASCII.GetBytes (xml);
284 ms.Write (x, 0, x.Length);
286 transform.LoadInput (ms);
287 Stream s = (Stream) transform.GetOutput ();
288 string output = Stream2String (s);
289 Assert.AreEqual (c14xml2, output, "MemoryStream");
293 [Ignore ("LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)")]
294 public void LoadInputWithUnsupportedType ()
296 byte[] bad = { 0xBA, 0xD };
297 transform.LoadInput (bad);
301 [ExpectedException (typeof (ArgumentException))]
302 public void UnsupportedOutput ()
304 XmlDocument doc = new XmlDocument();
305 object o = transform.GetOutput (doc.GetType ());
309 public void ExcC14NSpecExample1 ()
311 using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
312 sw.Write ("<!-- presence, not content, required -->");
315 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample1Input);
316 Assert.AreEqual (ExcC14NSpecExample1Output, res, "Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (without comments)");
320 public void ExcC14NSpecExample2 ()
322 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample2Input);
323 Assert.AreEqual (ExcC14NSpecExample2Output, res, "Example 2 from c14n spec - Whitespace in Document Content (without comments)");
327 public void ExcC14NSpecExample3 ()
329 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample3Input);
330 Assert.AreEqual (ExcC14NSpecExample3Output, res, "Example 3 from c14n spec - Start and End Tags (without comments)");
334 // [Ignore ("This test should be fine, but it does not pass under MS.NET")]
335 public void ExcC14NSpecExample4 ()
337 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample4Input);
338 Assert.AreEqual (ExcC14NSpecExample4Output, res, "Example 4 from c14n spec - Character Modifications and Character References (without comments)");
342 public void ExcC14NSpecExample5 ()
344 using (StreamWriter sw = new StreamWriter ("world.txt", false, Encoding.ASCII)) {
348 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample5Input);
349 Assert.AreEqual (ExcC14NSpecExample5Output, res, "Example 5 from c14n spec - Entity References (without comments)");
353 public void ExcC14NSpecExample6 ()
355 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample6Input);
356 Assert.AreEqual (ExcC14NSpecExample6Output, res, "Example 6 from c14n spec - UTF-8 Encoding (without comments)");
359 private string ExecuteXmlDSigExcC14NTransform (string InputXml)
361 XmlDocument doc = new XmlDocument ();
362 doc.PreserveWhitespace = true;
363 doc.LoadXml (InputXml);
365 // Testing default attribute support with
366 // vreader.ValidationType = ValidationType.None.
368 UTF8Encoding utf8 = new UTF8Encoding ();
369 byte[] data = utf8.GetBytes (InputXml.ToString ());
370 Stream stream = new MemoryStream (data);
371 XmlTextReader reader = new XmlTextReader (stream);
372 XmlValidatingReader vreader = new XmlValidatingReader (reader);
373 vreader.ValidationType = ValidationType.None;
374 vreader.EntityHandling = EntityHandling.ExpandCharEntities;
377 transform.LoadInput (doc);
378 return Stream2String ((Stream)transform.GetOutput ());
382 // Example 1 from ExcC14N spec - PIs, Comments, and Outside of Document Element:
383 // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
386 // removed reference to an empty external DTD
388 static string ExcC14NSpecExample1Input =
389 "<?xml version=\"1.0\"?>\n" +
391 "<?xml-stylesheet href=\"doc.xsl\"\n" +
392 " type=\"text/xsl\" ?>\n" +
394 // "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
396 "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
398 "<?pi-without-data ?>\n\n" +
399 "<!-- Comment 2 -->\n\n" +
400 "<!-- Comment 3 -->\n";
401 static string ExcC14NSpecExample1Output =
402 "<?xml-stylesheet href=\"doc.xsl\"\n" +
403 " type=\"text/xsl\" ?>\n" +
404 "<doc>Hello, world!</doc>\n" +
405 "<?pi-without-data?>";
408 // Example 2 from ExcC14N spec - Whitespace in Document Content:
409 // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
411 static string ExcC14NSpecExample2Input =
413 " <clean> </clean>\n" +
414 " <dirty> A B </dirty>\n" +
417 " <clean> </clean>\n" +
419 " <dirty> A B </dirty>\n" +
423 static string ExcC14NSpecExample2Output =
425 " <clean> </clean>\n" +
426 " <dirty> A B </dirty>\n" +
429 " <clean> </clean>\n" +
431 " <dirty> A B </dirty>\n" +
437 // Example 3 from ExcC14N spec - Start and End Tags:
438 // http://www.w3.org/TR/xml-c14n#Example-SETags
440 static string ExcC14NSpecExample3Input =
441 "<!DOCTYPE doc [<!ATTLIST e9 attr CDATA \"default\">]>\n" +
445 " <e3 name = \"elem3\" id=\"elem3\" />\n" +
446 " <e4 name=\"elem4\" id=\"elem4\" ></e4>\n" +
447 " <e5 a:attr=\"out\" b:attr=\"sorted\" attr2=\"all\" attr=\"I\'m\"\n" +
448 " xmlns:b=\"http://www.ietf.org\" \n" +
449 " xmlns:a=\"http://www.w3.org\"\n" +
450 " xmlns=\"http://www.uvic.ca\"/>\n" +
451 " <e6 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
452 " <e7 xmlns=\"http://www.ietf.org\">\n" +
453 " <e8 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
454 " <e9 xmlns=\"\" xmlns:a=\"http://www.ietf.org\"/>\n" +
459 static string ExcC14NSpecExample3Output =
463 " <e3 id=\"elem3\" name=\"elem3\"></e3>\n" +
464 " <e4 id=\"elem4\" name=\"elem4\"></e4>\n" +
465 " <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" +
467 " <e7 xmlns=\"http://www.ietf.org\">\n" +
468 " <e8 xmlns=\"\">\n" +
469 " <e9 attr=\"default\"></e9>\n" +
470 // " <e9 xmlns:a=\"http://www.ietf.org\"></e9>\n" +
478 // Example 4 from ExcC14N spec - Character Modifications and Character References:
479 // http://www.w3.org/TR/xml-c14n#Example-Chars
482 // This test does not include "normId" element
483 // because it has an invalid ID attribute "id" which
484 // should be normalized by XML parser. Currently Mono
485 // does not support this (see comment after this example
487 static string ExcC14NSpecExample4Input =
488 "<!DOCTYPE doc [<!ATTLIST normId id ID #IMPLIED>]>\n" +
490 " <text>First line
 Second line</text>\n" +
491 " <value>2</value>\n" +
492 " <compute><![CDATA[value>\"0\" && value<\"10\" ?\"valid\":\"error\"]]></compute>\n" +
493 " <compute expr=\'value>\"0\" && value<\"10\" ?\"valid\":\"error\"\'>valid</compute>\n" +
494 " <norm attr=\' '   
	 ' \'/>\n" +
495 // " <normId id=\' '   
	 ' \'/>\n" +
497 static string ExcC14NSpecExample4Output =
499 " <text>First line
\n" +
500 "Second line</text>\n" +
501 " <value>2</value>\n" +
502 " <compute>value>\"0\" && value<\"10\" ?\"valid\":\"error\"</compute>\n" +
503 " <compute expr=\"value>"0" && value<"10" ?"valid":"error"\">valid</compute>\n" +
504 " <norm attr=\" \' 
	 \' \"></norm>\n" +
505 // " <normId id=\"\' 
	 \'\"></normId>\n" +
509 // Example 5 from ExcC14N spec - Entity References:
510 // http://www.w3.org/TR/xml-c14n#Example-Entities
512 static string ExcC14NSpecExample5Input =
513 "<!DOCTYPE doc [\n" +
514 "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
515 "<!ENTITY ent1 \"Hello\">\n" +
516 "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
517 "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
518 "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
520 "<doc attrExtEnt=\"entExt\">\n" +
521 " &ent1;, &ent2;!\n" +
524 "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
525 static string ExcC14NSpecExample5Output =
526 "<doc attrExtEnt=\"entExt\">\n" +
531 // Example 6 from ExcC14N spec - UTF-8 Encoding:
532 // http://www.w3.org/TR/xml-c14n#Example-UTF8
534 static string ExcC14NSpecExample6Input =
535 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
536 "<doc>©</doc>\n";
537 static string ExcC14NSpecExample6Output =
538 "<doc>\xC2\xA9</doc>";
542 // Example 7 from ExcC14N spec - Document Subsets:
543 // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
546 // Well, XPath support in Mono is far from complete....
547 // I was not able to simplify the xpath expression from this test
548 // so it runs on Mono and still makes sense for testing this feature.
549 // Thus this test is not included in the suite now.
550 static string ExcC14NSpecExample7Input =
551 "<!DOCTYPE doc [\n" +
552 "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
553 "<!ATTLIST e3 id ID #IMPLIED>\n" +
555 "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
557 " <e2 xmlns=\"\">\n" +
558 " <e3 id=\"E3\"/>\n" +
563 static string ExcC14NSpecExample7Xpath =
564 "(//.|//@*|//namespace::*)\n" +
568 "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
570 "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
572 static string ExcC14NSpecExample7Output =
573 "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
576 public void SimpleNamespacePrefixes ()
578 string input = "<a:Action xmlns:a='urn:foo'>http://tempuri.org/IFoo/Echo</a:Action>";
579 string expected = @"<a:Action xmlns:a=""urn:foo"">http://tempuri.org/IFoo/Echo</a:Action>";
581 XmlDocument doc = new XmlDocument ();
583 XmlDsigExcC14NTransform t = new XmlDsigExcC14NTransform ();
585 Stream s = t.GetOutput () as Stream;
586 Assert.AreEqual (new StreamReader (s, Encoding.UTF8).ReadToEnd (), expected);
590 [ExpectedException (typeof (NullReferenceException))]
591 public void GetDigestedOutput_Null ()
593 new XmlDsigExcC14NTransform ().GetDigestedOutput (null);