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 (framework 1.0 result)
109 static string c14xml1 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \r\n <Toto></Toto> text & </Test>";
110 // BAD (framework 1.1 result for Stream)
111 static string c14xml2 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \n <Toto></Toto> text & </Test>";
112 // GOOD (framework 1.1 for XmlDocument and Mono::)
113 static string c14xml3 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> 
\n <Toto></Toto> text & </Test>";
115 private XmlDocument GetDoc ()
117 XmlDocument doc = new XmlDocument ();
118 doc.PreserveWhitespace = true;
124 public void LoadInputAsXmlDocument ()
126 XmlDocument doc = GetDoc ();
127 transform.LoadInput (doc);
128 Stream s = (Stream) transform.GetOutput ();
129 string output = Stream2String (s);
131 // .NET 1.0 keeps the \r\n (0x0D, 0x0A) - bug
132 AssertEquals("XmlDocument", c14xml1, output);
134 AssertEquals("XmlDocument", c14xml3, output);
139 public void LoadInputAsXmlNodeList ()
141 XmlDocument doc = GetDoc ();
142 // Argument list just contains element Test.
143 transform.LoadInput (doc.ChildNodes);
144 Stream s = (Stream) transform.GetOutput ();
145 string output = Stream2String (s);
146 AssertEquals ("XmlChildNodes", "<Test></Test>", output);
150 public void LoadInputAsXmlNodeList2 ()
152 XmlDocument doc = GetDoc ();
153 transform.LoadInput (doc.SelectNodes ("//*"));
154 Stream s = (Stream) transform.GetOutput ();
155 string output = Stream2String (s);
156 string expected = @"<Test><Toto></Toto></Test>";
157 AssertEquals ("XmlChildNodes", expected, output);
161 public void LoadInputAsStream ()
163 MemoryStream ms = new MemoryStream ();
164 byte[] x = Encoding.ASCII.GetBytes (xml);
165 ms.Write (x, 0, x.Length);
167 transform.LoadInput (ms);
168 Stream s = (Stream) transform.GetOutput ();
169 string output = Stream2String (s);
170 // ARGH! HOW CAN MS RETURN SOMETHING DIFFERENT IF A
171 // STREAM IS USED THAN IF A XMLDOCUMENT IS USED :-(
172 bool result = ((output == c14xml2) || (output == c14xml3));
173 Assert ("MemoryStream", result);
177 public void LoadInputWithUnsupportedType ()
179 byte[] bad = { 0xBA, 0xD };
180 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
181 transform.LoadInput (bad);
185 [ExpectedException (typeof (ArgumentException))]
186 public void UnsupportedOutput ()
188 XmlDocument doc = new XmlDocument();
189 object o = transform.GetOutput (doc.GetType ());
193 public void C14NSpecExample1 ()
195 using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
196 sw.Write ("<!-- presence, not content, required -->");
199 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample1Input);
200 AssertEquals ("Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (without comments)",
201 C14NSpecExample1Output, res);
205 public void C14NSpecExample2 ()
207 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample2Input);
208 AssertEquals ("Example 2 from c14n spec - Whitespace in Document Content (without comments)",
209 C14NSpecExample2Output, res);
213 public void C14NSpecExample3 ()
215 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample3Input);
216 AssertEquals ("Example 3 from c14n spec - Start and End Tags (without comments)",
217 C14NSpecExample3Output, res);
221 // [Ignore ("This test should be fine, but it does not pass under MS.NET")]
222 public void C14NSpecExample4 ()
224 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample4Input);
225 AssertEquals ("Example 4 from c14n spec - Character Modifications and Character References (without comments)",
226 C14NSpecExample4Output, res);
230 public void C14NSpecExample5 ()
232 using (StreamWriter sw = new StreamWriter ("world.txt", false, Encoding.ASCII)) {
236 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample5Input);
237 AssertEquals ("Example 5 from c14n spec - Entity References (without comments)",
238 C14NSpecExample5Output, res);
242 public void C14NSpecExample6 ()
244 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample6Input);
245 AssertEquals ("Example 6 from c14n spec - UTF-8 Encoding (without comments)",
246 C14NSpecExample6Output, res);
249 private string ExecuteXmlDSigC14NTransform (string InputXml)
251 XmlDocument doc = new XmlDocument ();
252 doc.PreserveWhitespace = true;
253 doc.LoadXml (InputXml);
255 // Testing default attribute support with
256 // vreader.ValidationType = ValidationType.None.
258 UTF8Encoding utf8 = new UTF8Encoding ();
259 byte[] data = utf8.GetBytes (InputXml.ToString ());
260 Stream stream = new MemoryStream (data);
261 XmlTextReader reader = new XmlTextReader (stream);
262 XmlValidatingReader vreader = new XmlValidatingReader (reader);
263 vreader.ValidationType = ValidationType.None;
264 vreader.EntityHandling = EntityHandling.ExpandCharEntities;
267 transform.LoadInput (doc);
268 return Stream2String ((Stream)transform.GetOutput ());
272 // Example 1 from C14N spec - PIs, Comments, and Outside of Document Element:
273 // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
276 // removed reference to an empty external DTD
278 static string C14NSpecExample1Input =
279 "<?xml version=\"1.0\"?>\n" +
281 "<?xml-stylesheet href=\"doc.xsl\"\n" +
282 " type=\"text/xsl\" ?>\n" +
284 // "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
286 "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
288 "<?pi-without-data ?>\n\n" +
289 "<!-- Comment 2 -->\n\n" +
290 "<!-- Comment 3 -->\n";
291 static string C14NSpecExample1Output =
292 "<?xml-stylesheet href=\"doc.xsl\"\n" +
293 " type=\"text/xsl\" ?>\n" +
294 "<doc>Hello, world!</doc>\n" +
295 "<?pi-without-data?>";
298 // Example 2 from C14N spec - Whitespace in Document Content:
299 // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
301 static string C14NSpecExample2Input =
303 " <clean> </clean>\n" +
304 " <dirty> A B </dirty>\n" +
307 " <clean> </clean>\n" +
309 " <dirty> A B </dirty>\n" +
313 static string C14NSpecExample2Output =
315 " <clean> </clean>\n" +
316 " <dirty> A B </dirty>\n" +
319 " <clean> </clean>\n" +
321 " <dirty> A B </dirty>\n" +
327 // Example 3 from C14N spec - Start and End Tags:
328 // http://www.w3.org/TR/xml-c14n#Example-SETags
330 static string C14NSpecExample3Input =
331 "<!DOCTYPE doc [<!ATTLIST e9 attr CDATA \"default\">]>\n" +
335 " <e3 name = \"elem3\" id=\"elem3\" />\n" +
336 " <e4 name=\"elem4\" id=\"elem4\" ></e4>\n" +
337 " <e5 a:attr=\"out\" b:attr=\"sorted\" attr2=\"all\" attr=\"I\'m\"\n" +
338 " xmlns:b=\"http://www.ietf.org\" \n" +
339 " xmlns:a=\"http://www.w3.org\"\n" +
340 " xmlns=\"http://www.uvic.ca\"/>\n" +
341 " <e6 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
342 " <e7 xmlns=\"http://www.ietf.org\">\n" +
343 " <e8 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
344 " <e9 xmlns=\"\" xmlns:a=\"http://www.ietf.org\"/>\n" +
349 static string C14NSpecExample3Output =
353 " <e3 id=\"elem3\" name=\"elem3\"></e3>\n" +
354 " <e4 id=\"elem4\" name=\"elem4\"></e4>\n" +
355 " <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" +
356 " <e6 xmlns:a=\"http://www.w3.org\">\n" +
357 " <e7 xmlns=\"http://www.ietf.org\">\n" +
358 " <e8 xmlns=\"\">\n" +
359 " <e9 xmlns:a=\"http://www.ietf.org\" attr=\"default\"></e9>\n" +
360 // " <e9 xmlns:a=\"http://www.ietf.org\"></e9>\n" +
368 // Example 4 from C14N spec - Character Modifications and Character References:
369 // http://www.w3.org/TR/xml-c14n#Example-Chars
372 // This test does not include "normId" element
373 // because it has an invalid ID attribute "id" which
374 // should be normalized by XML parser. Currently Mono
375 // does not support this (see comment after this example
377 static string C14NSpecExample4Input =
378 "<!DOCTYPE doc [<!ATTLIST normId id ID #IMPLIED>]>\n" +
380 " <text>First line
 Second line</text>\n" +
381 " <value>2</value>\n" +
382 " <compute><![CDATA[value>\"0\" && value<\"10\" ?\"valid\":\"error\"]]></compute>\n" +
383 " <compute expr=\'value>\"0\" && value<\"10\" ?\"valid\":\"error\"\'>valid</compute>\n" +
384 " <norm attr=\' '   
	 ' \'/>\n" +
385 // " <normId id=\' '   
	 ' \'/>\n" +
387 static string C14NSpecExample4Output =
389 " <text>First line
\n" +
390 "Second line</text>\n" +
391 " <value>2</value>\n" +
392 " <compute>value>\"0\" && value<\"10\" ?\"valid\":\"error\"</compute>\n" +
393 " <compute expr=\"value>"0" && value<"10" ?"valid":"error"\">valid</compute>\n" +
394 " <norm attr=\" \' 
	 \' \"></norm>\n" +
395 // " <normId id=\"\' 
	 \'\"></normId>\n" +
399 // Example 5 from C14N spec - Entity References:
400 // http://www.w3.org/TR/xml-c14n#Example-Entities
402 static string C14NSpecExample5Input =
403 "<!DOCTYPE doc [\n" +
404 "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
405 "<!ENTITY ent1 \"Hello\">\n" +
406 "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
407 "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
408 "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
410 "<doc attrExtEnt=\"entExt\">\n" +
411 " &ent1;, &ent2;!\n" +
414 "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
415 static string C14NSpecExample5Output =
416 "<doc attrExtEnt=\"entExt\">\n" +
421 // Example 6 from C14N spec - UTF-8 Encoding:
422 // http://www.w3.org/TR/xml-c14n#Example-UTF8
424 static string C14NSpecExample6Input =
425 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
426 "<doc>©</doc>\n";
427 static string C14NSpecExample6Output =
428 "<doc>\xC2\xA9</doc>";
432 // Example 7 from C14N spec - Document Subsets:
433 // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
436 // Well, XPath support in Mono is far from complete....
437 // I was not able to simplify the xpath expression from this test
438 // so it runs on Mono and still makes sense for testing this feature.
439 // Thus this test is not included in the suite now.
440 static string C14NSpecExample7Input =
441 "<!DOCTYPE doc [\n" +
442 "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
443 "<!ATTLIST e3 id ID #IMPLIED>\n" +
445 "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
447 " <e2 xmlns=\"\">\n" +
448 " <e3 id=\"E3\"/>\n" +
453 static string C14NSpecExample7Xpath =
454 "(//.|//@*|//namespace::*)\n" +
458 "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
460 "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
462 static string C14NSpecExample7Output =
463 "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";