2 // XmlDsigC14NTransformTest.cs - NUnit Test Cases for XmlDsigC14NTransform
5 // Sebastien Pouliot <sebastien@ximian.com>
6 // Aleksey Sanin (aleksey@aleksey.com)
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)
15 using System.Security.Cryptography.Xml;
19 using NUnit.Framework;
21 namespace MonoTests.System.Security.Cryptography.Xml {
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 public UnprotectedXmlDsigC14NTransform ()
30 public UnprotectedXmlDsigC14NTransform (bool includeComments)
31 : base (includeComments)
35 public XmlNodeList UnprotectedGetInnerXml () {
36 return base.GetInnerXml ();
41 public class XmlDsigC14NTransformTest : Assertion {
43 protected UnprotectedXmlDsigC14NTransform transform;
46 protected void SetUp ()
48 transform = new UnprotectedXmlDsigC14NTransform ();
52 protected void CleanUp ()
55 if (File.Exists ("doc.dtd"))
56 File.Delete ("doc.dtd");
57 if (File.Exists ("world.txt"))
58 File.Delete ("world.txt");
64 public void Constructor1 ()
66 AssertEquals ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", transform.Algorithm);
67 CheckProperties (transform);
70 [Test] // ctor (Boolean)
71 public void Constructor2 ()
73 transform = new UnprotectedXmlDsigC14NTransform (true);
74 AssertEquals ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", transform.Algorithm);
75 CheckProperties (transform);
77 transform = new UnprotectedXmlDsigC14NTransform (false);
78 AssertEquals ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", transform.Algorithm);
79 CheckProperties (transform);
82 void CheckProperties (XmlDsigC14NTransform transform)
84 Type[] input = transform.InputTypes;
85 Assert ("Input #", (input.Length == 3));
86 // check presence of every supported input types
90 foreach (Type t in input) {
91 if (t.ToString () == "System.IO.Stream")
93 if (t.ToString () == "System.Xml.XmlDocument")
95 if (t.ToString () == "System.Xml.XmlNodeList")
98 Assert ("Input Stream", istream);
99 Assert ("Input XmlDocument", ixmldoc);
100 Assert ("Input XmlNodeList", ixmlnl);
102 Type[] output = transform.OutputTypes;
103 Assert ("Output #", (output.Length == 1));
104 // check presence of every supported output types
105 bool ostream = false;
106 foreach (Type t in output) {
107 if (t.ToString () == "System.IO.Stream")
110 Assert ("Output Stream", ostream);
114 public void GetInnerXml ()
116 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
117 AssertNull ("Default InnerXml", xnl);
120 private string Stream2String (Stream s)
122 StringBuilder sb = new StringBuilder ();
123 int b = s.ReadByte ();
125 sb.Append (Convert.ToChar (b));
128 return sb.ToString ();
131 static string xml = "<Test attrib='at ' xmlns=\"http://www.go-mono.com/\" > \r\n 
 <Toto/> text & </Test >";
132 // BAD for XmlDocument input (framework 1.0 result)
133 static string c14xml1 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \r\n \r <Toto></Toto> text & </Test>";
134 // GOOD for Stream input
135 static string c14xml2 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \n 
 <Toto></Toto> text & </Test>";
136 // GOOD for XmlDocument input. The difference is because once
137 // xml string is loaded to XmlDocument, there is no difference
138 // between \r and 
, so every \r must be handled as 
.
139 static string c14xml3 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> 
\n 
 <Toto></Toto> text & </Test>";
141 private XmlDocument GetDoc ()
143 XmlDocument doc = new XmlDocument ();
144 doc.PreserveWhitespace = true;
150 public void LoadInputAsXmlDocument ()
152 XmlDocument doc = GetDoc ();
153 transform.LoadInput (doc);
154 Stream s = (Stream) transform.GetOutput ();
155 string output = Stream2String (s);
157 AssertEquals("XmlDocument", c14xml3, output);
159 // .NET 1.0 keeps the \r\n (0x0D, 0x0A) - bug
160 AssertEquals("XmlDocument", c14xml1, output);
166 [Category ("NotDotNet")]
167 // see LoadInputAsXmlNodeList2 description
169 public void LoadInputAsXmlNodeList ()
171 XmlDocument doc = GetDoc ();
172 // Argument list just contains element Test.
173 transform.LoadInput (doc.ChildNodes);
174 Stream s = (Stream) transform.GetOutput ();
175 string output = Stream2String (s);
176 AssertEquals ("XmlChildNodes", "<Test></Test>", output);
180 [Category ("NotDotNet")]
181 // MS has a bug that those namespace declaration nodes in
182 // the node-set are written to output. Related spec section is:
183 // http://www.w3.org/TR/2001/REC-xml-c14n-20010315#ProcessingModel
184 public void LoadInputAsXmlNodeList2 ()
186 XmlDocument doc = GetDoc ();
187 transform.LoadInput (doc.SelectNodes ("//*"));
188 Stream s = (Stream) transform.GetOutput ();
189 string output = Stream2String (s);
190 string expected = @"<Test><Toto></Toto></Test>";
191 AssertEquals ("XmlChildNodes", expected, output);
195 public void LoadInputAsStream ()
197 MemoryStream ms = new MemoryStream ();
198 byte[] x = Encoding.ASCII.GetBytes (xml);
199 ms.Write (x, 0, x.Length);
201 transform.LoadInput (ms);
202 Stream s = (Stream) transform.GetOutput ();
203 string output = Stream2String (s);
204 AssertEquals ("MemoryStream", c14xml2, output);
209 [ExpectedException (typeof (ArgumentException))]
211 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
213 public void LoadInputWithUnsupportedType ()
215 byte[] bad = { 0xBA, 0xD };
216 transform.LoadInput (bad);
220 [ExpectedException (typeof (ArgumentException))]
221 public void UnsupportedOutput ()
223 XmlDocument doc = new XmlDocument();
224 object o = transform.GetOutput (doc.GetType ());
228 public void C14NSpecExample1 ()
230 using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
231 sw.Write ("<!-- presence, not content, required -->");
234 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample1Input);
235 AssertEquals ("Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (without comments)",
236 C14NSpecExample1Output, res);
240 public void C14NSpecExample2 ()
242 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample2Input);
243 AssertEquals ("Example 2 from c14n spec - Whitespace in Document Content (without comments)",
244 C14NSpecExample2Output, res);
248 public void C14NSpecExample3 ()
250 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample3Input);
251 AssertEquals ("Example 3 from c14n spec - Start and End Tags (without comments)",
252 C14NSpecExample3Output, res);
256 // [Ignore ("This test should be fine, but it does not pass under MS.NET")]
257 public void C14NSpecExample4 ()
259 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample4Input);
260 AssertEquals ("Example 4 from c14n spec - Character Modifications and Character References (without comments)",
261 C14NSpecExample4Output, res);
265 public void C14NSpecExample5 ()
267 using (StreamWriter sw = new StreamWriter ("world.txt", false, Encoding.ASCII)) {
271 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample5Input);
272 AssertEquals ("Example 5 from c14n spec - Entity References (without comments)",
273 C14NSpecExample5Output, res);
277 public void C14NSpecExample6 ()
279 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample6Input);
280 AssertEquals ("Example 6 from c14n spec - UTF-8 Encoding (without comments)",
281 C14NSpecExample6Output, res);
284 private string ExecuteXmlDSigC14NTransform (string InputXml)
286 XmlDocument doc = new XmlDocument ();
287 doc.PreserveWhitespace = true;
288 doc.LoadXml (InputXml);
290 // Testing default attribute support with
291 // vreader.ValidationType = ValidationType.None.
293 UTF8Encoding utf8 = new UTF8Encoding ();
294 byte[] data = utf8.GetBytes (InputXml.ToString ());
295 Stream stream = new MemoryStream (data);
296 XmlTextReader reader = new XmlTextReader (stream);
297 XmlValidatingReader vreader = new XmlValidatingReader (reader);
298 vreader.ValidationType = ValidationType.None;
299 vreader.EntityHandling = EntityHandling.ExpandCharEntities;
302 transform.LoadInput (doc);
303 return Stream2String ((Stream)transform.GetOutput ());
307 // Example 1 from C14N spec - PIs, Comments, and Outside of Document Element:
308 // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
311 // removed reference to an empty external DTD
313 static string C14NSpecExample1Input =
314 "<?xml version=\"1.0\"?>\n" +
316 "<?xml-stylesheet href=\"doc.xsl\"\n" +
317 " type=\"text/xsl\" ?>\n" +
319 // "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
321 "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
323 "<?pi-without-data ?>\n\n" +
324 "<!-- Comment 2 -->\n\n" +
325 "<!-- Comment 3 -->\n";
326 static string C14NSpecExample1Output =
327 "<?xml-stylesheet href=\"doc.xsl\"\n" +
328 " type=\"text/xsl\" ?>\n" +
329 "<doc>Hello, world!</doc>\n" +
330 "<?pi-without-data?>";
333 // Example 2 from C14N spec - Whitespace in Document Content:
334 // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
336 static string C14NSpecExample2Input =
338 " <clean> </clean>\n" +
339 " <dirty> A B </dirty>\n" +
342 " <clean> </clean>\n" +
344 " <dirty> A B </dirty>\n" +
348 static string C14NSpecExample2Output =
350 " <clean> </clean>\n" +
351 " <dirty> A B </dirty>\n" +
354 " <clean> </clean>\n" +
356 " <dirty> A B </dirty>\n" +
362 // Example 3 from C14N spec - Start and End Tags:
363 // http://www.w3.org/TR/xml-c14n#Example-SETags
365 static string C14NSpecExample3Input =
366 "<!DOCTYPE doc [<!ATTLIST e9 attr CDATA \"default\">]>\n" +
370 " <e3 name = \"elem3\" id=\"elem3\" />\n" +
371 " <e4 name=\"elem4\" id=\"elem4\" ></e4>\n" +
372 " <e5 a:attr=\"out\" b:attr=\"sorted\" attr2=\"all\" attr=\"I\'m\"\n" +
373 " xmlns:b=\"http://www.ietf.org\" \n" +
374 " xmlns:a=\"http://www.w3.org\"\n" +
375 " xmlns=\"http://www.uvic.ca\"/>\n" +
376 " <e6 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
377 " <e7 xmlns=\"http://www.ietf.org\">\n" +
378 " <e8 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
379 " <e9 xmlns=\"\" xmlns:a=\"http://www.ietf.org\"/>\n" +
384 static string C14NSpecExample3Output =
388 " <e3 id=\"elem3\" name=\"elem3\"></e3>\n" +
389 " <e4 id=\"elem4\" name=\"elem4\"></e4>\n" +
390 " <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" +
391 " <e6 xmlns:a=\"http://www.w3.org\">\n" +
392 " <e7 xmlns=\"http://www.ietf.org\">\n" +
393 " <e8 xmlns=\"\">\n" +
394 " <e9 xmlns:a=\"http://www.ietf.org\" attr=\"default\"></e9>\n" +
395 // " <e9 xmlns:a=\"http://www.ietf.org\"></e9>\n" +
403 // Example 4 from C14N spec - Character Modifications and Character References:
404 // http://www.w3.org/TR/xml-c14n#Example-Chars
407 // This test does not include "normId" element
408 // because it has an invalid ID attribute "id" which
409 // should be normalized by XML parser. Currently Mono
410 // does not support this (see comment after this example
412 static string C14NSpecExample4Input =
413 "<!DOCTYPE doc [<!ATTLIST normId id ID #IMPLIED>]>\n" +
415 " <text>First line
 Second line</text>\n" +
416 " <value>2</value>\n" +
417 " <compute><![CDATA[value>\"0\" && value<\"10\" ?\"valid\":\"error\"]]></compute>\n" +
418 " <compute expr=\'value>\"0\" && value<\"10\" ?\"valid\":\"error\"\'>valid</compute>\n" +
419 " <norm attr=\' '   
	 ' \'/>\n" +
420 // " <normId id=\' '   
	 ' \'/>\n" +
422 static string C14NSpecExample4Output =
424 " <text>First line
\n" +
425 "Second line</text>\n" +
426 " <value>2</value>\n" +
427 " <compute>value>\"0\" && value<\"10\" ?\"valid\":\"error\"</compute>\n" +
428 " <compute expr=\"value>"0" && value<"10" ?"valid":"error"\">valid</compute>\n" +
429 " <norm attr=\" \' 
	 \' \"></norm>\n" +
430 // " <normId id=\"\' 
	 \'\"></normId>\n" +
434 // Example 5 from C14N spec - Entity References:
435 // http://www.w3.org/TR/xml-c14n#Example-Entities
437 static string C14NSpecExample5Input =
438 "<!DOCTYPE doc [\n" +
439 "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
440 "<!ENTITY ent1 \"Hello\">\n" +
441 "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
442 "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
443 "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
445 "<doc attrExtEnt=\"entExt\">\n" +
446 " &ent1;, &ent2;!\n" +
449 "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
450 static string C14NSpecExample5Output =
451 "<doc attrExtEnt=\"entExt\">\n" +
456 // Example 6 from C14N spec - UTF-8 Encoding:
457 // http://www.w3.org/TR/xml-c14n#Example-UTF8
459 static string C14NSpecExample6Input =
460 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
461 "<doc>©</doc>\n";
462 static string C14NSpecExample6Output =
463 "<doc>\xC2\xA9</doc>";
467 // Example 7 from C14N spec - Document Subsets:
468 // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
471 // Well, XPath support in Mono is far from complete....
472 // I was not able to simplify the xpath expression from this test
473 // so it runs on Mono and still makes sense for testing this feature.
474 // Thus this test is not included in the suite now.
475 static string C14NSpecExample7Input =
476 "<!DOCTYPE doc [\n" +
477 "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
478 "<!ATTLIST e3 id ID #IMPLIED>\n" +
480 "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
482 " <e2 xmlns=\"\">\n" +
483 " <e3 id=\"E3\"/>\n" +
488 static string C14NSpecExample7Xpath =
489 "(//.|//@*|//namespace::*)\n" +
493 "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
495 "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
497 static string C14NSpecExample7Output =
498 "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
501 public void SimpleNamespacePrefixes ()
503 string input = "<a:Action xmlns:a='urn:foo'>http://tempuri.org/IFoo/Echo</a:Action>";
504 string expected = @"<a:Action xmlns:a=""urn:foo"">http://tempuri.org/IFoo/Echo</a:Action>";
506 XmlDocument doc = new XmlDocument ();
508 XmlDsigC14NTransform t = new XmlDsigC14NTransform ();
510 Stream s = t.GetOutput () as Stream;
511 AssertEquals (expected, new StreamReader (s, Encoding.UTF8).ReadToEnd ());
516 public void PrefixlessNamespaceOutput ()
518 XmlDocument doc = new XmlDocument ();
519 doc.AppendChild (doc.CreateElement ("foo", "urn:foo"));
520 doc.DocumentElement.AppendChild (doc.CreateElement ("bar", "urn:bar"));
521 AssertEquals ("#1", String.Empty, doc.DocumentElement.GetAttribute ("xmlns"));
522 XmlDsigC14NTransform t = new XmlDsigC14NTransform ();
524 Stream s = t.GetOutput () as Stream;
525 AssertEquals ("<foo xmlns=\"urn:foo\"><bar xmlns=\"urn:bar\"></bar></foo>", new StreamReader (s, Encoding.UTF8).ReadToEnd ());
526 AssertEquals ("#2", "urn:foo", doc.DocumentElement.GetAttribute ("xmlns"));
530 [Ignore ("find out how PropagatedNamespaces returns non-null instance on .NET")]
531 public void PropagatedNamespaces ()
533 XmlDocument doc = new XmlDocument ();
534 doc.AppendChild (doc.CreateElement ("foo", "urn:foo"));
535 doc.DocumentElement.AppendChild (doc.CreateElement ("bar", "urn:bar"));
536 AssertEquals ("#1", String.Empty, doc.DocumentElement.GetAttribute ("xmlns:f"));
537 XmlDsigExcC14NTransform t = new XmlDsigExcC14NTransform ();
539 t.PropagatedNamespaces.Add ("f", "urn:foo");
540 t.PropagatedNamespaces.Add ("b", "urn:bar");
541 Stream s = t.GetOutput () as Stream;
542 AssertEquals ("<f:foo xmlns:f=\"urn:foo\"><b:bar xmlns:b=\"urn:bar\"></b:bar></f:foo>", new StreamReader (s, Encoding.UTF8).ReadToEnd ());
543 AssertEquals ("#2", "urn:foo", doc.DocumentElement.GetAttribute ("xmlns:f"));
547 [ExpectedException (typeof (NullReferenceException))]
548 public void GetDigestedOutput_Null ()
550 new XmlDsigExcC14NTransform ().GetDigestedOutput (null);