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.
26 using System.Security.Cryptography.Xml;
30 using NUnit.Framework;
32 namespace MonoTests.System.Security.Cryptography.Xml {
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 ()
41 public UnprotectedXmlDsigExcC14NTransform (bool includeComments)
42 : base (includeComments)
46 public UnprotectedXmlDsigExcC14NTransform (string inclusiveNamespacesPrefixList)
47 : base (inclusiveNamespacesPrefixList)
51 public UnprotectedXmlDsigExcC14NTransform (bool includeComments, string inclusiveNamespacesPrefixList)
52 : base (includeComments, inclusiveNamespacesPrefixList)
56 public XmlNodeList UnprotectedGetInnerXml () {
57 return base.GetInnerXml ();
62 public class XmlDsigExcC14NTransformTest {
64 protected UnprotectedXmlDsigExcC14NTransform transform;
67 protected void SetUp ()
69 transform = new UnprotectedXmlDsigExcC14NTransform ();
73 protected void CleanUp ()
76 if (File.Exists ("doc.dtd"))
77 File.Delete ("doc.dtd");
78 if (File.Exists ("world.txt"))
79 File.Delete ("world.txt");
85 public void Constructor1 ()
87 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
88 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
89 CheckProperties (transform);
92 [Test] // ctor (Boolean)
93 public void Constructor2 ()
95 transform = new UnprotectedXmlDsigExcC14NTransform (true);
96 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#WithComments", transform.Algorithm, "Algorithm");
97 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
98 CheckProperties (transform);
100 transform = new UnprotectedXmlDsigExcC14NTransform (false);
101 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
102 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
103 CheckProperties (transform);
106 [Test] // ctor (String)
107 public void Constructor3 ()
109 transform = new UnprotectedXmlDsigExcC14NTransform (null);
110 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
111 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
112 CheckProperties (transform);
114 transform = new UnprotectedXmlDsigExcC14NTransform (string.Empty);
115 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
116 Assert.AreEqual (string.Empty, transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
117 CheckProperties (transform);
119 transform = new UnprotectedXmlDsigExcC14NTransform ("#default xsd");
120 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
121 Assert.AreEqual ("#default xsd", transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
122 CheckProperties (transform);
125 [Test] // ctor (Boolean, String)
126 public void Constructor4 ()
128 transform = new UnprotectedXmlDsigExcC14NTransform (true, null);
129 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#WithComments", transform.Algorithm, "Algorithm");
130 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
131 CheckProperties (transform);
133 transform = new UnprotectedXmlDsigExcC14NTransform (true, string.Empty);
134 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#WithComments", transform.Algorithm, "Algorithm");
135 Assert.AreEqual (string.Empty, transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
136 CheckProperties (transform);
138 transform = new UnprotectedXmlDsigExcC14NTransform (true, "#default xsd");
139 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#WithComments", transform.Algorithm, "Algorithm");
140 Assert.AreEqual ("#default xsd", transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
141 CheckProperties (transform);
143 transform = new UnprotectedXmlDsigExcC14NTransform (false, null);
144 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
145 Assert.IsNull (transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
146 CheckProperties (transform);
148 transform = new UnprotectedXmlDsigExcC14NTransform (false, string.Empty);
149 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
150 Assert.AreEqual (string.Empty, transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
151 CheckProperties (transform);
153 transform = new UnprotectedXmlDsigExcC14NTransform (false, "#default xsd");
154 Assert.AreEqual ("http://www.w3.org/2001/10/xml-exc-c14n#", transform.Algorithm, "Algorithm");
155 Assert.AreEqual ("#default xsd", transform.InclusiveNamespacesPrefixList, "InclusiveNamespacesPrefixList");
156 CheckProperties (transform);
159 void CheckProperties (XmlDsigExcC14NTransform transform)
161 Type[] input = transform.InputTypes;
162 Assert.IsTrue ((input.Length == 3), "Input #");
163 // check presence of every supported input types
164 bool istream = false;
165 bool ixmldoc = false;
167 foreach (Type t in input) {
168 if (t.ToString () == "System.IO.Stream")
170 if (t.ToString () == "System.Xml.XmlDocument")
172 if (t.ToString () == "System.Xml.XmlNodeList")
175 Assert.IsTrue (istream, "Input Stream");
176 Assert.IsTrue (ixmldoc, "Input XmlDocument");
177 Assert.IsTrue (ixmlnl, "Input XmlNodeList");
179 Type[] output = transform.OutputTypes;
180 Assert.IsTrue ((output.Length == 1), "Output #");
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")
187 Assert.IsTrue (ostream, "Output Stream");
193 Type [] input = transform.InputTypes;
197 // property does not return a clone
198 foreach (Type t in transform.InputTypes) {
201 // it's not a static array
202 XmlDsigExcC14NTransform t2 = new XmlDsigExcC14NTransform ();
203 foreach (Type t in t2.InputTypes) {
204 Assert.IsNotNull (t);
209 public void GetInnerXml ()
211 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
212 Assert.IsNull (xnl, "Default InnerXml");
215 private string Stream2String (Stream s)
217 StringBuilder sb = new StringBuilder ();
218 int b = s.ReadByte ();
220 sb.Append (Convert.ToChar (b));
223 return sb.ToString ();
226 static string xml = "<Test attrib='at ' xmlns=\"http://www.go-mono.com/\" > \r\n 
 <Toto/> text & </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 & </Test>";
229 // GOOD for Stream input
230 static string c14xml2 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \n 
 <Toto></Toto> text & </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 
, so every \r must be handled as 
.
234 static string c14xml3 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> 
\n 
 <Toto></Toto> text & </Test>";
236 private XmlDocument GetDoc ()
238 XmlDocument doc = new XmlDocument ();
239 doc.PreserveWhitespace = true;
245 public void LoadInputAsXmlDocument ()
247 XmlDocument doc = GetDoc ();
248 transform.LoadInput (doc);
249 Stream s = (Stream) transform.GetOutput ();
250 string output = Stream2String (s);
252 Assert.AreEqual (c14xml3, output, "XmlDocument");
254 // .NET 1.0 keeps the \r\n (0x0D, 0x0A) - bug
255 Assert.AreEqual (c14xml1, output, "XmlDocument");
261 [Category ("NotDotNet")]
262 // see LoadInputAsXmlNodeList2 description
264 public void LoadInputAsXmlNodeList ()
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 Assert.AreEqual ("<Test></Test>", output, "XmlChildNodes");
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 ()
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 Assert.AreEqual (expected, output, "XmlChildNodes");
290 public void LoadInputAsStream ()
292 MemoryStream ms = new MemoryStream ();
293 byte[] x = Encoding.ASCII.GetBytes (xml);
294 ms.Write (x, 0, x.Length);
296 transform.LoadInput (ms);
297 Stream s = (Stream) transform.GetOutput ();
298 string output = Stream2String (s);
299 Assert.AreEqual (c14xml2, output, "MemoryStream");
303 [Ignore ("LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)")]
304 public void LoadInputWithUnsupportedType ()
306 byte[] bad = { 0xBA, 0xD };
307 transform.LoadInput (bad);
311 [ExpectedException (typeof (ArgumentException))]
312 public void UnsupportedOutput ()
314 XmlDocument doc = new XmlDocument();
315 object o = transform.GetOutput (doc.GetType ());
319 public void ExcC14NSpecExample1 ()
321 using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
322 sw.Write ("<!-- presence, not content, required -->");
325 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample1Input);
326 Assert.AreEqual (ExcC14NSpecExample1Output, res, "Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (without comments)");
330 public void ExcC14NSpecExample2 ()
332 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample2Input);
333 Assert.AreEqual (ExcC14NSpecExample2Output, res, "Example 2 from c14n spec - Whitespace in Document Content (without comments)");
337 public void ExcC14NSpecExample3 ()
339 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample3Input);
340 Assert.AreEqual (ExcC14NSpecExample3Output, res, "Example 3 from c14n spec - Start and End Tags (without comments)");
344 // [Ignore ("This test should be fine, but it does not pass under MS.NET")]
345 public void ExcC14NSpecExample4 ()
347 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample4Input);
348 Assert.AreEqual (ExcC14NSpecExample4Output, res, "Example 4 from c14n spec - Character Modifications and Character References (without comments)");
352 public void ExcC14NSpecExample5 ()
354 using (StreamWriter sw = new StreamWriter ("world.txt", false, Encoding.ASCII)) {
358 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample5Input);
359 Assert.AreEqual (ExcC14NSpecExample5Output, res, "Example 5 from c14n spec - Entity References (without comments)");
363 public void ExcC14NSpecExample6 ()
365 string res = ExecuteXmlDSigExcC14NTransform (ExcC14NSpecExample6Input);
366 Assert.AreEqual (ExcC14NSpecExample6Output, res, "Example 6 from c14n spec - UTF-8 Encoding (without comments)");
369 private string ExecuteXmlDSigExcC14NTransform (string InputXml)
371 XmlDocument doc = new XmlDocument ();
372 doc.PreserveWhitespace = true;
373 doc.LoadXml (InputXml);
375 // Testing default attribute support with
376 // vreader.ValidationType = ValidationType.None.
378 UTF8Encoding utf8 = new UTF8Encoding ();
379 byte[] data = utf8.GetBytes (InputXml.ToString ());
380 Stream stream = new MemoryStream (data);
381 XmlTextReader reader = new XmlTextReader (stream);
382 XmlValidatingReader vreader = new XmlValidatingReader (reader);
383 vreader.ValidationType = ValidationType.None;
384 vreader.EntityHandling = EntityHandling.ExpandCharEntities;
387 transform.LoadInput (doc);
388 return Stream2String ((Stream)transform.GetOutput ());
392 // Example 1 from ExcC14N spec - PIs, Comments, and Outside of Document Element:
393 // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
396 // removed reference to an empty external DTD
398 static string ExcC14NSpecExample1Input =
399 "<?xml version=\"1.0\"?>\n" +
401 "<?xml-stylesheet href=\"doc.xsl\"\n" +
402 " type=\"text/xsl\" ?>\n" +
404 // "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
406 "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
408 "<?pi-without-data ?>\n\n" +
409 "<!-- Comment 2 -->\n\n" +
410 "<!-- Comment 3 -->\n";
411 static string ExcC14NSpecExample1Output =
412 "<?xml-stylesheet href=\"doc.xsl\"\n" +
413 " type=\"text/xsl\" ?>\n" +
414 "<doc>Hello, world!</doc>\n" +
415 "<?pi-without-data?>";
418 // Example 2 from ExcC14N spec - Whitespace in Document Content:
419 // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
421 static string ExcC14NSpecExample2Input =
423 " <clean> </clean>\n" +
424 " <dirty> A B </dirty>\n" +
427 " <clean> </clean>\n" +
429 " <dirty> A B </dirty>\n" +
433 static string ExcC14NSpecExample2Output =
435 " <clean> </clean>\n" +
436 " <dirty> A B </dirty>\n" +
439 " <clean> </clean>\n" +
441 " <dirty> A B </dirty>\n" +
447 // Example 3 from ExcC14N spec - Start and End Tags:
448 // http://www.w3.org/TR/xml-c14n#Example-SETags
450 static string ExcC14NSpecExample3Input =
451 "<!DOCTYPE doc [<!ATTLIST e9 attr CDATA \"default\">]>\n" +
455 " <e3 name = \"elem3\" id=\"elem3\" />\n" +
456 " <e4 name=\"elem4\" id=\"elem4\" ></e4>\n" +
457 " <e5 a:attr=\"out\" b:attr=\"sorted\" attr2=\"all\" attr=\"I\'m\"\n" +
458 " xmlns:b=\"http://www.ietf.org\" \n" +
459 " xmlns:a=\"http://www.w3.org\"\n" +
460 " xmlns=\"http://www.uvic.ca\"/>\n" +
461 " <e6 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
462 " <e7 xmlns=\"http://www.ietf.org\">\n" +
463 " <e8 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
464 " <e9 xmlns=\"\" xmlns:a=\"http://www.ietf.org\"/>\n" +
469 static string ExcC14NSpecExample3Output =
473 " <e3 id=\"elem3\" name=\"elem3\"></e3>\n" +
474 " <e4 id=\"elem4\" name=\"elem4\"></e4>\n" +
475 " <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" +
477 " <e7 xmlns=\"http://www.ietf.org\">\n" +
478 " <e8 xmlns=\"\">\n" +
479 " <e9 attr=\"default\"></e9>\n" +
480 // " <e9 xmlns:a=\"http://www.ietf.org\"></e9>\n" +
488 // Example 4 from ExcC14N spec - Character Modifications and Character References:
489 // http://www.w3.org/TR/xml-c14n#Example-Chars
492 // This test does not include "normId" element
493 // because it has an invalid ID attribute "id" which
494 // should be normalized by XML parser. Currently Mono
495 // does not support this (see comment after this example
497 static string ExcC14NSpecExample4Input =
498 "<!DOCTYPE doc [<!ATTLIST normId id ID #IMPLIED>]>\n" +
500 " <text>First line
 Second line</text>\n" +
501 " <value>2</value>\n" +
502 " <compute><![CDATA[value>\"0\" && value<\"10\" ?\"valid\":\"error\"]]></compute>\n" +
503 " <compute expr=\'value>\"0\" && value<\"10\" ?\"valid\":\"error\"\'>valid</compute>\n" +
504 " <norm attr=\' '   
	 ' \'/>\n" +
505 // " <normId id=\' '   
	 ' \'/>\n" +
507 static string ExcC14NSpecExample4Output =
509 " <text>First line
\n" +
510 "Second line</text>\n" +
511 " <value>2</value>\n" +
512 " <compute>value>\"0\" && value<\"10\" ?\"valid\":\"error\"</compute>\n" +
513 " <compute expr=\"value>"0" && value<"10" ?"valid":"error"\">valid</compute>\n" +
514 " <norm attr=\" \' 
	 \' \"></norm>\n" +
515 // " <normId id=\"\' 
	 \'\"></normId>\n" +
519 // Example 5 from ExcC14N spec - Entity References:
520 // http://www.w3.org/TR/xml-c14n#Example-Entities
522 static string ExcC14NSpecExample5Input =
523 "<!DOCTYPE doc [\n" +
524 "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
525 "<!ENTITY ent1 \"Hello\">\n" +
526 "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
527 "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
528 "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
530 "<doc attrExtEnt=\"entExt\">\n" +
531 " &ent1;, &ent2;!\n" +
534 "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
535 static string ExcC14NSpecExample5Output =
536 "<doc attrExtEnt=\"entExt\">\n" +
541 // Example 6 from ExcC14N spec - UTF-8 Encoding:
542 // http://www.w3.org/TR/xml-c14n#Example-UTF8
544 static string ExcC14NSpecExample6Input =
545 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
546 "<doc>©</doc>\n";
547 static string ExcC14NSpecExample6Output =
548 "<doc>\xC2\xA9</doc>";
552 // Example 7 from ExcC14N spec - Document Subsets:
553 // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
556 // Well, XPath support in Mono is far from complete....
557 // I was not able to simplify the xpath expression from this test
558 // so it runs on Mono and still makes sense for testing this feature.
559 // Thus this test is not included in the suite now.
560 static string ExcC14NSpecExample7Input =
561 "<!DOCTYPE doc [\n" +
562 "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
563 "<!ATTLIST e3 id ID #IMPLIED>\n" +
565 "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
567 " <e2 xmlns=\"\">\n" +
568 " <e3 id=\"E3\"/>\n" +
573 static string ExcC14NSpecExample7Xpath =
574 "(//.|//@*|//namespace::*)\n" +
578 "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
580 "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
582 static string ExcC14NSpecExample7Output =
583 "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
586 public void SimpleNamespacePrefixes ()
588 string input = "<a:Action xmlns:a='urn:foo'>http://tempuri.org/IFoo/Echo</a:Action>";
589 string expected = @"<a:Action xmlns:a=""urn:foo"">http://tempuri.org/IFoo/Echo</a:Action>";
591 XmlDocument doc = new XmlDocument ();
593 XmlDsigExcC14NTransform t = new XmlDsigExcC14NTransform ();
595 Stream s = t.GetOutput () as Stream;
596 Assert.AreEqual (new StreamReader (s, Encoding.UTF8).ReadToEnd (), expected);
600 [ExpectedException (typeof (NullReferenceException))]
601 public void GetDigestedOutput_Null ()
603 new XmlDsigExcC14NTransform ().GetDigestedOutput (null);