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 [Category ("NotDotNet")]
255 // see LoadInputAsXmlNodeList2 description
256 public void LoadInputAsXmlNodeList ()
258 XmlDocument doc = GetDoc ();
259 // Argument list just contains element Test.
260 transform.LoadInput (doc.ChildNodes);
261 Stream s = (Stream) transform.GetOutput ();
262 string output = Stream2String (s);
263 Assert.AreEqual ("<Test></Test>", output, "XmlChildNodes");
267 [Category ("NotDotNet")]
268 // MS has a bug that those namespace declaration nodes in
269 // the node-set are written to output. Related spec section is:
270 // http://www.w3.org/TR/2001/REC-xml-c14n-20010315#ProcessingModel
271 public void LoadInputAsXmlNodeList2 ()
273 XmlDocument doc = GetDoc ();
274 transform.LoadInput (doc.SelectNodes ("//*"));
275 Stream s = (Stream) transform.GetOutput ();
276 string output = Stream2String (s);
277 string expected = @"<Test><Toto></Toto></Test>";
278 Assert.AreEqual (expected, output, "XmlChildNodes");
282 public void LoadInputAsStream ()
284 MemoryStream ms = new MemoryStream ();
285 byte[] x = Encoding.ASCII.GetBytes (xml);
286 ms.Write (x, 0, x.Length);
288 transform.LoadInput (ms);
289 Stream s = (Stream) transform.GetOutput ();
290 string output = Stream2String (s);
291 Assert.AreEqual (c14xml2, output, "MemoryStream");
295 [Ignore ("LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)")]
296 public void LoadInputWithUnsupportedType ()
298 byte[] bad = { 0xBA, 0xD };
299 transform.LoadInput (bad);
303 [ExpectedException (typeof (ArgumentException))]
304 public void UnsupportedOutput ()
306 XmlDocument doc = new XmlDocument();
307 object o = transform.GetOutput (doc.GetType ());
311 public void ExcC14NSpecExample1 ()
313 using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
314 sw.Write ("<!-- presence, not content, required -->");
317 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample1Input);
318 Assert.AreEqual (ExcC14NSpecExample1Output, res, "Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (without comments)");
322 public void ExcC14NSpecExample2 ()
324 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample2Input);
325 Assert.AreEqual (ExcC14NSpecExample2Output, res, "Example 2 from c14n spec - Whitespace in Document Content (without comments)");
329 public void ExcC14NSpecExample3 ()
331 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample3Input);
332 Assert.AreEqual (ExcC14NSpecExample3Output, res, "Example 3 from c14n spec - Start and End Tags (without comments)");
336 // [Ignore ("This test should be fine, but it does not pass under MS.NET")]
337 public void ExcC14NSpecExample4 ()
339 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample4Input);
340 Assert.AreEqual (ExcC14NSpecExample4Output, res, "Example 4 from c14n spec - Character Modifications and Character References (without comments)");
344 public void ExcC14NSpecExample5 ()
346 using (StreamWriter sw = new StreamWriter ("world.txt", false, Encoding.ASCII)) {
350 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample5Input);
351 Assert.AreEqual (ExcC14NSpecExample5Output, res, "Example 5 from c14n spec - Entity References (without comments)");
355 public void ExcC14NSpecExample6 ()
357 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample6Input);
358 Assert.AreEqual (ExcC14NSpecExample6Output, res, "Example 6 from c14n spec - UTF-8 Encoding (without comments)");
361 private string ExecuteXmlDSigExcC14NTransform (string InputXml)
363 XmlDocument doc = new XmlDocument ();
364 doc.PreserveWhitespace = true;
365 doc.LoadXml (InputXml);
367 // Testing default attribute support with
368 // vreader.ValidationType = ValidationType.None.
370 UTF8Encoding utf8 = new UTF8Encoding ();
371 byte[] data = utf8.GetBytes (InputXml.ToString ());
372 Stream stream = new MemoryStream (data);
373 XmlTextReader reader = new XmlTextReader (stream);
374 XmlValidatingReader vreader = new XmlValidatingReader (reader);
375 vreader.ValidationType = ValidationType.None;
376 vreader.EntityHandling = EntityHandling.ExpandCharEntities;
379 transform.LoadInput (doc);
380 return Stream2String ((Stream)transform.GetOutput ());
384 // Example 1 from ExcC14N spec - PIs, Comments, and Outside of Document Element:
385 // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
388 // removed reference to an empty external DTD
390 static string ExcC14NSpecExample1Input =
391 "<?xml version=\"1.0\"?>\n" +
393 "<?xml-stylesheet href=\"doc.xsl\"\n" +
394 " type=\"text/xsl\" ?>\n" +
396 // "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
398 "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
400 "<?pi-without-data ?>\n\n" +
401 "<!-- Comment 2 -->\n\n" +
402 "<!-- Comment 3 -->\n";
403 static string ExcC14NSpecExample1Output =
404 "<?xml-stylesheet href=\"doc.xsl\"\n" +
405 " type=\"text/xsl\" ?>\n" +
406 "<doc>Hello, world!</doc>\n" +
407 "<?pi-without-data?>";
410 // Example 2 from ExcC14N spec - Whitespace in Document Content:
411 // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
413 static string ExcC14NSpecExample2Input =
415 " <clean> </clean>\n" +
416 " <dirty> A B </dirty>\n" +
419 " <clean> </clean>\n" +
421 " <dirty> A B </dirty>\n" +
425 static string ExcC14NSpecExample2Output =
427 " <clean> </clean>\n" +
428 " <dirty> A B </dirty>\n" +
431 " <clean> </clean>\n" +
433 " <dirty> A B </dirty>\n" +
439 // Example 3 from ExcC14N spec - Start and End Tags:
440 // http://www.w3.org/TR/xml-c14n#Example-SETags
442 static string ExcC14NSpecExample3Input =
443 "<!DOCTYPE doc [<!ATTLIST e9 attr CDATA \"default\">]>\n" +
447 " <e3 name = \"elem3\" id=\"elem3\" />\n" +
448 " <e4 name=\"elem4\" id=\"elem4\" ></e4>\n" +
449 " <e5 a:attr=\"out\" b:attr=\"sorted\" attr2=\"all\" attr=\"I\'m\"\n" +
450 " xmlns:b=\"http://www.ietf.org\" \n" +
451 " xmlns:a=\"http://www.w3.org\"\n" +
452 " xmlns=\"http://www.uvic.ca\"/>\n" +
453 " <e6 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
454 " <e7 xmlns=\"http://www.ietf.org\">\n" +
455 " <e8 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
456 " <e9 xmlns=\"\" xmlns:a=\"http://www.ietf.org\"/>\n" +
461 static string ExcC14NSpecExample3Output =
465 " <e3 id=\"elem3\" name=\"elem3\"></e3>\n" +
466 " <e4 id=\"elem4\" name=\"elem4\"></e4>\n" +
467 " <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" +
469 " <e7 xmlns=\"http://www.ietf.org\">\n" +
470 " <e8 xmlns=\"\">\n" +
471 " <e9 attr=\"default\"></e9>\n" +
472 // " <e9 xmlns:a=\"http://www.ietf.org\"></e9>\n" +
480 // Example 4 from ExcC14N spec - Character Modifications and Character References:
481 // http://www.w3.org/TR/xml-c14n#Example-Chars
484 // This test does not include "normId" element
485 // because it has an invalid ID attribute "id" which
486 // should be normalized by XML parser. Currently Mono
487 // does not support this (see comment after this example
489 static string ExcC14NSpecExample4Input =
490 "<!DOCTYPE doc [<!ATTLIST normId id ID #IMPLIED>]>\n" +
492 " <text>First line
 Second line</text>\n" +
493 " <value>2</value>\n" +
494 " <compute><![CDATA[value>\"0\" && value<\"10\" ?\"valid\":\"error\"]]></compute>\n" +
495 " <compute expr=\'value>\"0\" && value<\"10\" ?\"valid\":\"error\"\'>valid</compute>\n" +
496 " <norm attr=\' '   
	 ' \'/>\n" +
497 // " <normId id=\' '   
	 ' \'/>\n" +
499 static string ExcC14NSpecExample4Output =
501 " <text>First line
\n" +
502 "Second line</text>\n" +
503 " <value>2</value>\n" +
504 " <compute>value>\"0\" && value<\"10\" ?\"valid\":\"error\"</compute>\n" +
505 " <compute expr=\"value>"0" && value<"10" ?"valid":"error"\">valid</compute>\n" +
506 " <norm attr=\" \' 
	 \' \"></norm>\n" +
507 // " <normId id=\"\' 
	 \'\"></normId>\n" +
511 // Example 5 from ExcC14N spec - Entity References:
512 // http://www.w3.org/TR/xml-c14n#Example-Entities
514 static string ExcC14NSpecExample5Input =
515 "<!DOCTYPE doc [\n" +
516 "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
517 "<!ENTITY ent1 \"Hello\">\n" +
518 "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
519 "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
520 "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
522 "<doc attrExtEnt=\"entExt\">\n" +
523 " &ent1;, &ent2;!\n" +
526 "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
527 static string ExcC14NSpecExample5Output =
528 "<doc attrExtEnt=\"entExt\">\n" +
533 // Example 6 from ExcC14N spec - UTF-8 Encoding:
534 // http://www.w3.org/TR/xml-c14n#Example-UTF8
536 static string ExcC14NSpecExample6Input =
537 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
538 "<doc>©</doc>\n";
539 static string ExcC14NSpecExample6Output =
540 "<doc>\xC2\xA9</doc>";
544 // Example 7 from ExcC14N spec - Document Subsets:
545 // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
548 // Well, XPath support in Mono is far from complete....
549 // I was not able to simplify the xpath expression from this test
550 // so it runs on Mono and still makes sense for testing this feature.
551 // Thus this test is not included in the suite now.
552 static string ExcC14NSpecExample7Input =
553 "<!DOCTYPE doc [\n" +
554 "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
555 "<!ATTLIST e3 id ID #IMPLIED>\n" +
557 "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
559 " <e2 xmlns=\"\">\n" +
560 " <e3 id=\"E3\"/>\n" +
565 static string ExcC14NSpecExample7Xpath =
566 "(//.|//@*|//namespace::*)\n" +
570 "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
572 "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
574 static string ExcC14NSpecExample7Output =
575 "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
578 public void SimpleNamespacePrefixes ()
580 string input = "<a:Action xmlns:a='urn:foo'>http://tempuri.org/IFoo/Echo</a:Action>";
581 string expected = @"<a:Action xmlns:a=""urn:foo"">http://tempuri.org/IFoo/Echo</a:Action>";
583 XmlDocument doc = new XmlDocument ();
585 XmlDsigExcC14NTransform t = new XmlDsigExcC14NTransform ();
587 Stream s = t.GetOutput () as Stream;
588 Assert.AreEqual (new StreamReader (s, Encoding.UTF8).ReadToEnd (), expected);
592 [ExpectedException (typeof (NullReferenceException))]
593 public void GetDigestedOutput_Null ()
595 new XmlDsigExcC14NTransform ().GetDigestedOutput (null);