2 // XmlDsigC14NWithCommentsTransformTest.cs
3 // - NUnit Test Cases for XmlDsigC14NWithCommentsTransform
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
\r
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
\r
14 using System.Security;
\r
15 using System.Security.Cryptography.Xml;
19 using NUnit.Framework;
21 namespace MonoTests.System.Security.Cryptography.Xml {
23 // Note: GetInnerXml is protected in XmlDsigC14NWithCommentsTransform
24 // making it difficult to test properly. This class "open it up" :-)
25 public class UnprotectedXmlDsigC14NWithCommentsTransform : XmlDsigC14NWithCommentsTransform {
27 public XmlNodeList UnprotectedGetInnerXml () {
28 return base.GetInnerXml ();
33 public class XmlDsigC14NWithCommentsTransformTest : Assertion {
35 protected UnprotectedXmlDsigC14NWithCommentsTransform transform;
38 protected void SetUp ()
40 transform = new UnprotectedXmlDsigC14NWithCommentsTransform ();
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");
53 if (File.Exists ("doc.dtd"))
54 throw new Exception ("File.Delete() is not working.");
55 if (File.Exists ("world.txt"))
56 throw new Exception ("File.Delete() is not working.");
60 public void Properties ()
62 AssertEquals ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", transform.Algorithm);
64 Type[] input = transform.InputTypes;
65 Assert ("Input #", (input.Length == 3));
66 // check presence of every supported input types
70 foreach (Type t in input) {
71 if (t.ToString () == "System.IO.Stream")
73 if (t.ToString () == "System.Xml.XmlDocument")
75 if (t.ToString () == "System.Xml.XmlNodeList")
78 Assert ("Input Stream", istream);
79 Assert ("Input XmlDocument", ixmldoc);
80 Assert ("Input XmlNodeList", ixmlnl);
82 Type[] output = transform.OutputTypes;
83 Assert ("Output #", (output.Length == 1));
84 // check presence of every supported output types
86 foreach (Type t in output) {
87 if (t.ToString () == "System.IO.Stream")
90 Assert ("Output Stream", ostream);
94 public void GetInnerXml ()
96 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
97 AssertEquals ("Default InnerXml", null, xnl);
101 [ExpectedException (typeof (ArgumentException))]
103 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
105 public void LoadInputWithUnsupportedType ()
107 byte[] bad = { 0xBA, 0xD };
108 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
109 transform.LoadInput (bad);
113 [ExpectedException (typeof (ArgumentException))]
114 public void UnsupportedOutput ()
116 XmlDocument doc = new XmlDocument();
117 object o = transform.GetOutput (doc.GetType ());
121 public void C14NSpecExample1 ()
123 using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
124 sw.Write ("<!-- presence, not content, required -->");
127 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample1Input, true);
128 AssertEquals ("Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (with comments)",
129 C14NSpecExample1Output, res);
134 [ExpectedException (typeof (SecurityException))]
\r
136 public void C14NSpecExample1_WithoutResolver ()
\r
138 using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
\r
139 sw.Write ("<!-- presence, not content, required -->");
\r
143 if (!SecurityManager.SecurityEnabled)
\r
144 NUnit.Framework.Assert.Ignore ("SecurityManager isn't enabled.");
\r
146 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample1Input, false);
\r
147 AssertEquals ("Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (with comments)",
\r
148 C14NSpecExample1Output, res);
\r
152 public void C14NSpecExample2 ()
154 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample2Input, false);
155 AssertEquals ("Example 2 from c14n spec - Whitespace in Document Content (with comments)",
156 C14NSpecExample2Output, res);
160 public void C14NSpecExample3 ()
162 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample3Input, false);
163 AssertEquals ("Example 3 from c14n spec - Start and End Tags (with comments)",
164 C14NSpecExample3Output, res);
168 public void C14NSpecExample4 ()
170 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample4Input, false);
171 AssertEquals ("Example 4 from c14n spec - Character Modifications and Character References (with comments)",
172 C14NSpecExample4Output, res);
176 public void C14NSpecExample5 ()
178 if (!File.Exists ("world.txt")) {
179 using (StreamWriter sw = new StreamWriter ("world.txt", false, Encoding.ASCII)) {
184 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample5Input, false);
185 AssertEquals ("Example 5 from c14n spec - Entity References (with comments)",
186 C14NSpecExample5Output, res);
190 public void C14NSpecExample6 ()
192 string res = ExecuteXmlDSigC14NTransform (C14NSpecExample6Input, false);
193 AssertEquals ("Example 6 from c14n spec - UTF-8 Encoding (with comments)",
194 C14NSpecExample6Output, res);
197 private string ExecuteXmlDSigC14NTransform (string InputXml, bool resolver)
199 XmlDocument doc = new XmlDocument ();
200 doc.PreserveWhitespace = true;
201 doc.LoadXml (InputXml);
203 // Testing default attribute support with
204 // vreader.ValidationType = ValidationType.None.
205 UTF8Encoding utf8 = new UTF8Encoding ();
206 byte[] data = utf8.GetBytes (InputXml.ToString ());
207 Stream stream = new MemoryStream (data);
208 XmlTextReader reader = new XmlTextReader (stream);
209 XmlValidatingReader vreader = new XmlValidatingReader (reader);
210 vreader.ValidationType = ValidationType.None;
211 vreader.EntityHandling = EntityHandling.ExpandCharEntities;
215 transform.Resolver = new XmlUrlResolver ();
217 transform.LoadInput (doc);
218 return Stream2String ((Stream)transform.GetOutput ());
221 private string Stream2String (Stream s)
223 StringBuilder sb = new StringBuilder ();
224 int b = s.ReadByte ();
226 sb.Append (Convert.ToChar (b));
229 return sb.ToString ();
233 // Example 1 from C14N spec - PIs, Comments, and Outside of Document Element:
234 // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
236 static string C14NSpecExample1Input =
237 "<?xml version=\"1.0\"?>\n" +
239 "<?xml-stylesheet href=\"doc.xsl\"\n" +
240 " type=\"text/xsl\" ?>\n" +
242 "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
244 "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
246 "<?pi-without-data ?>\n\n" +
247 "<!-- Comment 2 -->\n\n" +
248 "<!-- Comment 3 -->\n";
249 static string C14NSpecExample1Output =
250 "<?xml-stylesheet href=\"doc.xsl\"\n" +
251 " type=\"text/xsl\" ?>\n" +
252 "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
253 "<?pi-without-data?>\n" +
254 "<!-- Comment 2 -->\n" +
255 "<!-- Comment 3 -->";
258 // Example 2 from C14N spec - Whitespace in Document Content:
259 // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
261 static string C14NSpecExample2Input =
263 " <clean> </clean>\n" +
264 " <dirty> A B </dirty>\n" +
267 " <clean> </clean>\n" +
269 " <dirty> A B </dirty>\n" +
273 static string C14NSpecExample2Output =
275 " <clean> </clean>\n" +
276 " <dirty> A B </dirty>\n" +
279 " <clean> </clean>\n" +
281 " <dirty> A B </dirty>\n" +
287 // Example 3 from C14N spec - Start and End Tags:
288 // http://www.w3.org/TR/xml-c14n#Example-SETags
290 static string C14NSpecExample3Input =
291 "<!DOCTYPE doc [<!ATTLIST e9 attr CDATA \"default\">]>\n" +
295 " <e3 name = \"elem3\" id=\"elem3\" />\n" +
296 " <e4 name=\"elem4\" id=\"elem4\" ></e4>\n" +
297 " <e5 a:attr=\"out\" b:attr=\"sorted\" attr2=\"all\" attr=\"I\'m\"\n" +
298 " xmlns:b=\"http://www.ietf.org\" \n" +
299 " xmlns:a=\"http://www.w3.org\"\n" +
300 " xmlns=\"http://www.uvic.ca\"/>\n" +
301 " <e6 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
302 " <e7 xmlns=\"http://www.ietf.org\">\n" +
303 " <e8 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
304 " <e9 xmlns=\"\" xmlns:a=\"http://www.ietf.org\"/>\n" +
309 static string C14NSpecExample3Output =
313 " <e3 id=\"elem3\" name=\"elem3\"></e3>\n" +
314 " <e4 id=\"elem4\" name=\"elem4\"></e4>\n" +
315 " <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" +
316 " <e6 xmlns:a=\"http://www.w3.org\">\n" +
317 " <e7 xmlns=\"http://www.ietf.org\">\n" +
318 " <e8 xmlns=\"\">\n" +
319 " <e9 xmlns:a=\"http://www.ietf.org\" attr=\"default\"></e9>\n" +
320 // " <e9 xmlns:a=\"http://www.ietf.org\"></e9>\n" +
328 // Example 4 from C14N spec - Character Modifications and Character References:
329 // http://www.w3.org/TR/xml-c14n#Example-Chars
332 // This test does not include "normId" element
333 // because it has an invalid ID attribute "id" which
334 // should be normalized by XML parser. Currently Mono
335 // does not support this (see comment after this example
337 static string C14NSpecExample4Input =
338 "<!DOCTYPE doc [<!ATTLIST normId id ID #IMPLIED>]>\n" +
340 " <text>First line
 Second line</text>\n" +
341 " <value>2</value>\n" +
342 " <compute><![CDATA[value>\"0\" && value<\"10\" ?\"valid\":\"error\"]]></compute>\n" +
343 " <compute expr=\'value>\"0\" && value<\"10\" ?\"valid\":\"error\"\'>valid</compute>\n" +
344 " <norm attr=\' '   
	 ' \'/>\n" +
345 // " <normId id=\' '   
	 ' \'/>\n" +
347 static string C14NSpecExample4Output =
349 " <text>First line
\n" +
350 "Second line</text>\n" +
351 " <value>2</value>\n" +
352 " <compute>value>\"0\" && value<\"10\" ?\"valid\":\"error\"</compute>\n" +
353 " <compute expr=\"value>"0" && value<"10" ?"valid":"error"\">valid</compute>\n" +
354 " <norm attr=\" \' 
	 \' \"></norm>\n" +
355 // " <normId id=\"\' 
	 \'\"></normId>\n" +
359 // Example 5 from C14N spec - Entity References:
360 // http://www.w3.org/TR/xml-c14n#Example-Entities
362 static string C14NSpecExample5Input =
363 "<!DOCTYPE doc [\n" +
364 "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
365 "<!ENTITY ent1 \"Hello\">\n" +
366 "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
367 "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
368 "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
370 "<doc attrExtEnt=\"entExt\">\n" +
371 " &ent1;, &ent2;!\n" +
374 "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
375 static string C14NSpecExample5Output =
376 "<doc attrExtEnt=\"entExt\">\n" +
379 "<!-- Let world.txt contain \"world\" (excluding the quotes) -->";
382 // Example 6 from C14N spec - UTF-8 Encoding:
383 // http://www.w3.org/TR/xml-c14n#Example-UTF8
385 static string C14NSpecExample6Input =
386 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
387 "<doc>©</doc>\n";
388 static string C14NSpecExample6Output =
389 "<doc>\xC2\xA9</doc>";
393 // Example 7 from C14N spec - Document Subsets:
394 // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
397 // Well, XPath support in Mono is far from complete....
398 // I was not able to simplify the xpath expression from this test
399 // so it runs on Mono and still makes sense for testing this feature.
400 // Thus this test is not included in the suite now.
401 static string C14NSpecExample7Input =
402 "<!DOCTYPE doc [\n" +
403 "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
404 "<!ATTLIST e3 id ID #IMPLIED>\n" +
406 "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
408 " <e2 xmlns=\"\">\n" +
409 " <e3 id=\"E3\"/>\n" +
414 static string C14NSpecExample7Xpath =
415 "(//.|//@*|//namespace::*)\n" +
419 "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
421 "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
423 static string C14NSpecExample7Output =
424 "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";