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 {
27 public XmlNodeList UnprotectedGetInnerXml () {
28 return base.GetInnerXml ();
33 public class XmlDsigC14NTransformTest : Assertion {
35 protected UnprotectedXmlDsigC14NTransform transform;
38 protected void SetUp ()
40 transform = new UnprotectedXmlDsigC14NTransform ();
44 protected void CleanUp ()
47 if (File.Exists ("doc.dtd"))
48 File.Delete ("doc.dtd");
49 if (File.Exists ("world.txt"))
50 File.Delete ("world.txt");
56 public void Properties ()
58 AssertEquals ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", transform.Algorithm);
60 Type[] input = transform.InputTypes;
61 Assert ("Input #", (input.Length == 3));
62 // check presence of every supported input types
66 foreach (Type t in input) {
67 if (t.ToString () == "System.IO.Stream")
69 if (t.ToString () == "System.Xml.XmlDocument")
71 if (t.ToString () == "System.Xml.XmlNodeList")
74 Assert ("Input Stream", istream);
75 Assert ("Input XmlDocument", ixmldoc);
76 Assert ("Input XmlNodeList", ixmlnl);
78 Type[] output = transform.OutputTypes;
79 Assert ("Output #", (output.Length == 1));
80 // check presence of every supported output types
82 foreach (Type t in output) {
83 if (t.ToString () == "System.IO.Stream")
86 Assert ("Output Stream", ostream);
90 public void GetInnerXml ()
92 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
93 AssertNull ("Default InnerXml", xnl);
96 private string Stream2String (Stream s)
98 StringBuilder sb = new StringBuilder ();
99 int b = s.ReadByte ();
101 sb.Append (Convert.ToChar (b));
104 return sb.ToString ();
107 static string xml = "<Test attrib='at ' xmlns=\"http://www.go-mono.com/\" > \r\n 
 <Toto/> text & </Test >";
108 // BAD for XmlDocument input (framework 1.0 result)
109 static string c14xml1 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \r\n \r <Toto></Toto> text & </Test>";
110 // GOOD for Stream input
111 static string c14xml2 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \n 
 <Toto></Toto> text & </Test>";
112 // GOOD for XmlDocument input. The difference is because once
113 // xml string is loaded to XmlDocument, there is no difference
114 // between \r and 
, so every \r must be handled as 
.
115 static string c14xml3 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> 
\n 
 <Toto></Toto> text & </Test>";
117 private XmlDocument GetDoc ()
119 XmlDocument doc = new XmlDocument ();
120 doc.PreserveWhitespace = true;
126 public void LoadInputAsXmlDocument ()
128 XmlDocument doc = GetDoc ();
129 transform.LoadInput (doc);
130 Stream s = (Stream) transform.GetOutput ();
131 string output = Stream2String (s);
133 AssertEquals("XmlDocument", c14xml3, output);
135 // .NET 1.0 keeps the \r\n (0x0D, 0x0A) - bug
136 AssertEquals("XmlDocument", c14xml1, output);
142 [Category ("NotDotNet")]
\r
143 // see LoadInputAsXmlNodeList2 description
145 public void LoadInputAsXmlNodeList ()
147 XmlDocument doc = GetDoc ();
148 // Argument list just contains element Test.
149 transform.LoadInput (doc.ChildNodes);
150 Stream s = (Stream) transform.GetOutput ();
151 string output = Stream2String (s);
152 AssertEquals ("XmlChildNodes", "<Test></Test>", output);
156 [Category ("NotDotNet")]
157 // MS has a bug that those namespace declaration nodes in
158 // the node-set are written to output. Related spec section is:
159 // http://www.w3.org/TR/2001/REC-xml-c14n-20010315#ProcessingModel
160 public void LoadInputAsXmlNodeList2 ()
162 XmlDocument doc = GetDoc ();
163 transform.LoadInput (doc.SelectNodes ("//*"));
164 Stream s = (Stream) transform.GetOutput ();
165 string output = Stream2String (s);
166 string expected = @"<Test><Toto></Toto></Test>";
167 AssertEquals ("XmlChildNodes", expected, output);
171 public void LoadInputAsStream ()
173 MemoryStream ms = new MemoryStream ();
174 byte[] x = Encoding.ASCII.GetBytes (xml);
175 ms.Write (x, 0, x.Length);
177 transform.LoadInput (ms);
178 Stream s = (Stream) transform.GetOutput ();
179 string output = Stream2String (s);
180 AssertEquals ("MemoryStream", c14xml2, output);
185 [ExpectedException (typeof (ArgumentException))]
187 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
189 public void LoadInputWithUnsupportedType ()
191 byte[] bad = { 0xBA, 0xD };
192 transform.LoadInput (bad);
196 [ExpectedException (typeof (ArgumentException))]
197 public void UnsupportedOutput ()
199 XmlDocument doc = new XmlDocument();
200 object o = transform.GetOutput (doc.GetType ());
204 public void C14NSpecExample1 ()
206 using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
207 sw.Write ("<!-- presence, not content, required -->");
210 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample1Input);
211 AssertEquals ("Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (without comments)",
212 C14NSpecExample1Output, res);
216 public void C14NSpecExample2 ()
218 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample2Input);
219 AssertEquals ("Example 2 from c14n spec - Whitespace in Document Content (without comments)",
220 C14NSpecExample2Output, res);
224 public void C14NSpecExample3 ()
226 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample3Input);
227 AssertEquals ("Example 3 from c14n spec - Start and End Tags (without comments)",
228 C14NSpecExample3Output, res);
232 // [Ignore ("This test should be fine, but it does not pass under MS.NET")]
233 public void C14NSpecExample4 ()
235 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample4Input);
236 AssertEquals ("Example 4 from c14n spec - Character Modifications and Character References (without comments)",
237 C14NSpecExample4Output, res);
241 public void C14NSpecExample5 ()
243 using (StreamWriter sw = new StreamWriter ("world.txt", false, Encoding.ASCII)) {
247 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample5Input);
248 AssertEquals ("Example 5 from c14n spec - Entity References (without comments)",
249 C14NSpecExample5Output, res);
253 public void C14NSpecExample6 ()
255 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample6Input);
256 AssertEquals ("Example 6 from c14n spec - UTF-8 Encoding (without comments)",
257 C14NSpecExample6Output, res);
260 private string ExecuteXmlDSigC14NTransform (string InputXml)
262 XmlDocument doc = new XmlDocument ();
263 doc.PreserveWhitespace = true;
264 doc.LoadXml (InputXml);
266 // Testing default attribute support with
267 // vreader.ValidationType = ValidationType.None.
269 UTF8Encoding utf8 = new UTF8Encoding ();
270 byte[] data = utf8.GetBytes (InputXml.ToString ());
271 Stream stream = new MemoryStream (data);
272 XmlTextReader reader = new XmlTextReader (stream);
273 XmlValidatingReader vreader = new XmlValidatingReader (reader);
274 vreader.ValidationType = ValidationType.None;
275 vreader.EntityHandling = EntityHandling.ExpandCharEntities;
278 transform.LoadInput (doc);
279 return Stream2String ((Stream)transform.GetOutput ());
283 // Example 1 from C14N spec - PIs, Comments, and Outside of Document Element:
284 // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
287 // removed reference to an empty external DTD
289 static string C14NSpecExample1Input =
290 "<?xml version=\"1.0\"?>\n" +
292 "<?xml-stylesheet href=\"doc.xsl\"\n" +
293 " type=\"text/xsl\" ?>\n" +
295 // "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
297 "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
299 "<?pi-without-data ?>\n\n" +
300 "<!-- Comment 2 -->\n\n" +
301 "<!-- Comment 3 -->\n";
302 static string C14NSpecExample1Output =
303 "<?xml-stylesheet href=\"doc.xsl\"\n" +
304 " type=\"text/xsl\" ?>\n" +
305 "<doc>Hello, world!</doc>\n" +
306 "<?pi-without-data?>";
309 // Example 2 from C14N spec - Whitespace in Document Content:
310 // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
312 static string C14NSpecExample2Input =
314 " <clean> </clean>\n" +
315 " <dirty> A B </dirty>\n" +
318 " <clean> </clean>\n" +
320 " <dirty> A B </dirty>\n" +
324 static string C14NSpecExample2Output =
326 " <clean> </clean>\n" +
327 " <dirty> A B </dirty>\n" +
330 " <clean> </clean>\n" +
332 " <dirty> A B </dirty>\n" +
338 // Example 3 from C14N spec - Start and End Tags:
339 // http://www.w3.org/TR/xml-c14n#Example-SETags
341 static string C14NSpecExample3Input =
342 "<!DOCTYPE doc [<!ATTLIST e9 attr CDATA \"default\">]>\n" +
346 " <e3 name = \"elem3\" id=\"elem3\" />\n" +
347 " <e4 name=\"elem4\" id=\"elem4\" ></e4>\n" +
348 " <e5 a:attr=\"out\" b:attr=\"sorted\" attr2=\"all\" attr=\"I\'m\"\n" +
349 " xmlns:b=\"http://www.ietf.org\" \n" +
350 " xmlns:a=\"http://www.w3.org\"\n" +
351 " xmlns=\"http://www.uvic.ca\"/>\n" +
352 " <e6 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
353 " <e7 xmlns=\"http://www.ietf.org\">\n" +
354 " <e8 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
355 " <e9 xmlns=\"\" xmlns:a=\"http://www.ietf.org\"/>\n" +
360 static string C14NSpecExample3Output =
364 " <e3 id=\"elem3\" name=\"elem3\"></e3>\n" +
365 " <e4 id=\"elem4\" name=\"elem4\"></e4>\n" +
366 " <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" +
367 " <e6 xmlns:a=\"http://www.w3.org\">\n" +
368 " <e7 xmlns=\"http://www.ietf.org\">\n" +
369 " <e8 xmlns=\"\">\n" +
370 " <e9 xmlns:a=\"http://www.ietf.org\" attr=\"default\"></e9>\n" +
371 // " <e9 xmlns:a=\"http://www.ietf.org\"></e9>\n" +
379 // Example 4 from C14N spec - Character Modifications and Character References:
380 // http://www.w3.org/TR/xml-c14n#Example-Chars
383 // This test does not include "normId" element
384 // because it has an invalid ID attribute "id" which
385 // should be normalized by XML parser. Currently Mono
386 // does not support this (see comment after this example
388 static string C14NSpecExample4Input =
389 "<!DOCTYPE doc [<!ATTLIST normId id ID #IMPLIED>]>\n" +
391 " <text>First line
 Second line</text>\n" +
392 " <value>2</value>\n" +
393 " <compute><![CDATA[value>\"0\" && value<\"10\" ?\"valid\":\"error\"]]></compute>\n" +
394 " <compute expr=\'value>\"0\" && value<\"10\" ?\"valid\":\"error\"\'>valid</compute>\n" +
395 " <norm attr=\' '   
	 ' \'/>\n" +
396 // " <normId id=\' '   
	 ' \'/>\n" +
398 static string C14NSpecExample4Output =
400 " <text>First line
\n" +
401 "Second line</text>\n" +
402 " <value>2</value>\n" +
403 " <compute>value>\"0\" && value<\"10\" ?\"valid\":\"error\"</compute>\n" +
404 " <compute expr=\"value>"0" && value<"10" ?"valid":"error"\">valid</compute>\n" +
405 " <norm attr=\" \' 
	 \' \"></norm>\n" +
406 // " <normId id=\"\' 
	 \'\"></normId>\n" +
410 // Example 5 from C14N spec - Entity References:
411 // http://www.w3.org/TR/xml-c14n#Example-Entities
413 static string C14NSpecExample5Input =
414 "<!DOCTYPE doc [\n" +
415 "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
416 "<!ENTITY ent1 \"Hello\">\n" +
417 "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
418 "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
419 "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
421 "<doc attrExtEnt=\"entExt\">\n" +
422 " &ent1;, &ent2;!\n" +
425 "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
426 static string C14NSpecExample5Output =
427 "<doc attrExtEnt=\"entExt\">\n" +
432 // Example 6 from C14N spec - UTF-8 Encoding:
433 // http://www.w3.org/TR/xml-c14n#Example-UTF8
435 static string C14NSpecExample6Input =
436 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
437 "<doc>©</doc>\n";
438 static string C14NSpecExample6Output =
439 "<doc>\xC2\xA9</doc>";
443 // Example 7 from C14N spec - Document Subsets:
444 // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
447 // Well, XPath support in Mono is far from complete....
448 // I was not able to simplify the xpath expression from this test
449 // so it runs on Mono and still makes sense for testing this feature.
450 // Thus this test is not included in the suite now.
451 static string C14NSpecExample7Input =
452 "<!DOCTYPE doc [\n" +
453 "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
454 "<!ATTLIST e3 id ID #IMPLIED>\n" +
456 "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
458 " <e2 xmlns=\"\">\n" +
459 " <e3 id=\"E3\"/>\n" +
464 static string C14NSpecExample7Xpath =
465 "(//.|//@*|//namespace::*)\n" +
469 "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
471 "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
473 static string C14NSpecExample7Output =
474 "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
477 public void SimpleNamespacePrefixes ()
479 string input = "<a:Action xmlns:a='urn:foo'>http://tempuri.org/IFoo/Echo</a:Action>";
480 string expected = @"<a:Action xmlns:a=""urn:foo"">http://tempuri.org/IFoo/Echo</a:Action>";
482 XmlDocument doc = new XmlDocument ();
484 XmlDsigC14NTransform t = new XmlDsigC14NTransform ();
486 Stream s = t.GetOutput () as Stream;
487 AssertEquals (expected, new StreamReader (s, Encoding.UTF8).ReadToEnd ());
492 public void PrefixlessNamespaceOutput ()
494 XmlDocument doc = new XmlDocument ();
495 doc.AppendChild (doc.CreateElement ("foo", "urn:foo"));
496 doc.DocumentElement.AppendChild (doc.CreateElement ("bar", "urn:bar"));
497 AssertEquals ("#1", String.Empty, doc.DocumentElement.GetAttribute ("xmlns"));
498 XmlDsigC14NTransform t = new XmlDsigC14NTransform ();
500 Stream s = t.GetOutput () as Stream;
501 AssertEquals ("<foo xmlns=\"urn:foo\"><bar xmlns=\"urn:bar\"></bar></foo>", new StreamReader (s, Encoding.UTF8).ReadToEnd ());
502 AssertEquals ("#2", "urn:foo", doc.DocumentElement.GetAttribute ("xmlns"));
506 [Ignore ("find out how PropagatedNamespaces returns non-null instance on .NET")]
507 public void PropagatedNamespaces ()
509 XmlDocument doc = new XmlDocument ();
510 doc.AppendChild (doc.CreateElement ("foo", "urn:foo"));
511 doc.DocumentElement.AppendChild (doc.CreateElement ("bar", "urn:bar"));
512 AssertEquals ("#1", String.Empty, doc.DocumentElement.GetAttribute ("xmlns:f"));
513 XmlDsigExcC14NTransform t = new XmlDsigExcC14NTransform ();
515 t.PropagatedNamespaces.Add ("f", "urn:foo");
516 t.PropagatedNamespaces.Add ("b", "urn:bar");
517 Stream s = t.GetOutput () as Stream;
518 AssertEquals ("<f:foo xmlns:f=\"urn:foo\"><b:bar xmlns:b=\"urn:bar\"></b:bar></f:foo>", new StreamReader (s, Encoding.UTF8).ReadToEnd ());
519 AssertEquals ("#2", "urn:foo", doc.DocumentElement.GetAttribute ("xmlns:f"));