XmlDsigC14NWithCommentsTransformTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. //
  2. // XmlDsigC14NWithCommentsTransformTest.cs
  3. // - NUnit Test Cases for XmlDsigC14NWithCommentsTransform
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. #if !MOBILE
  12. using System;
  13. using System.IO;
  14. using System.Security;
  15. using System.Security.Cryptography.Xml;
  16. using System.Text;
  17. using System.Xml;
  18. using NUnit.Framework;
  19. namespace MonoTests.System.Security.Cryptography.Xml {
  20. // Note: GetInnerXml is protected in XmlDsigC14NWithCommentsTransform
  21. // making it difficult to test properly. This class "open it up" :-)
  22. public class UnprotectedXmlDsigC14NWithCommentsTransform : XmlDsigC14NWithCommentsTransform {
  23. public XmlNodeList UnprotectedGetInnerXml () {
  24. return base.GetInnerXml ();
  25. }
  26. }
  27. [TestFixture]
  28. public class XmlDsigC14NWithCommentsTransformTest {
  29. protected UnprotectedXmlDsigC14NWithCommentsTransform transform;
  30. [SetUp]
  31. protected void SetUp ()
  32. {
  33. transform = new UnprotectedXmlDsigC14NWithCommentsTransform ();
  34. }
  35. [TearDown]
  36. protected void CleanUp ()
  37. {
  38. try {
  39. if (File.Exists ("doc.dtd"))
  40. File.Delete ("doc.dtd");
  41. if (File.Exists ("world.txt"))
  42. File.Delete ("world.txt");
  43. }
  44. catch {}
  45. if (File.Exists ("doc.dtd"))
  46. throw new Exception ("File.Delete() is not working.");
  47. if (File.Exists ("world.txt"))
  48. throw new Exception ("File.Delete() is not working.");
  49. }
  50. [Test]
  51. public void Properties ()
  52. {
  53. Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", transform.Algorithm, "Algorithm");
  54. Type[] input = transform.InputTypes;
  55. Assert.IsTrue ((input.Length == 3), "Input #");
  56. // check presence of every supported input types
  57. bool istream = false;
  58. bool ixmldoc = false;
  59. bool ixmlnl = false;
  60. foreach (Type t in input) {
  61. if (t.ToString () == "System.IO.Stream")
  62. istream = true;
  63. if (t.ToString () == "System.Xml.XmlDocument")
  64. ixmldoc = true;
  65. if (t.ToString () == "System.Xml.XmlNodeList")
  66. ixmlnl = true;
  67. }
  68. Assert.IsTrue (istream, "Input Stream");
  69. Assert.IsTrue (ixmldoc, "Input XmlDocument");
  70. Assert.IsTrue (ixmlnl, "Input XmlNodeList");
  71. Type[] output = transform.OutputTypes;
  72. Assert.IsTrue ((output.Length == 1), "Output #");
  73. // check presence of every supported output types
  74. bool ostream = false;
  75. foreach (Type t in output) {
  76. if (t.ToString () == "System.IO.Stream")
  77. ostream = true;
  78. }
  79. Assert.IsTrue (ostream, "Output Stream");
  80. }
  81. [Test]
  82. public void GetInnerXml ()
  83. {
  84. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  85. Assert.AreEqual (null, xnl, "Default InnerXml");
  86. }
  87. [ExpectedException (typeof (ArgumentException))]
  88. public void LoadInputWithUnsupportedType ()
  89. {
  90. byte[] bad = { 0xBA, 0xD };
  91. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  92. transform.LoadInput (bad);
  93. }
  94. [Test]
  95. [ExpectedException (typeof (ArgumentException))]
  96. public void UnsupportedOutput ()
  97. {
  98. XmlDocument doc = new XmlDocument();
  99. object o = transform.GetOutput (doc.GetType ());
  100. }
  101. [Test]
  102. public void C14NSpecExample1 ()
  103. {
  104. using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
  105. sw.Write ("<!-- presence, not content, required -->");
  106. sw.Close ();
  107. }
  108. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample1Input, true);
  109. Assert.AreEqual (C14NSpecExample1Output, res, "Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (with comments)");
  110. }
  111. [Test]
  112. [ExpectedException (typeof (SecurityException))]
  113. public void C14NSpecExample1_WithoutResolver ()
  114. {
  115. using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
  116. sw.Write ("<!-- presence, not content, required -->");
  117. sw.Close ();
  118. }
  119. if (!SecurityManager.SecurityEnabled)
  120. NUnit.Framework.Assert.Ignore ("SecurityManager isn't enabled.");
  121. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample1Input, false);
  122. Assert.AreEqual (C14NSpecExample1Output, res, "Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (with comments)");
  123. }
  124. [Test]
  125. public void C14NSpecExample2 ()
  126. {
  127. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample2Input, false);
  128. Assert.AreEqual (C14NSpecExample2Output, res, "Example 2 from c14n spec - Whitespace in Document Content (with comments)");
  129. }
  130. [Test]
  131. public void C14NSpecExample3 ()
  132. {
  133. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample3Input, false);
  134. Assert.AreEqual (C14NSpecExample3Output, res, "Example 3 from c14n spec - Start and End Tags (with comments)");
  135. }
  136. [Test]
  137. public void C14NSpecExample4 ()
  138. {
  139. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample4Input, false);
  140. Assert.AreEqual (C14NSpecExample4Output, res, "Example 4 from c14n spec - Character Modifications and Character References (with comments)");
  141. }
  142. [Test]
  143. [Ignore(".NET DOM implementation does not match W3C DOM specification.")]
  144. public void C14NSpecExample5 ()
  145. {
  146. if (!File.Exists ("world.txt")) {
  147. using (StreamWriter sw = new StreamWriter ("world.txt", false, Encoding.ASCII)) {
  148. sw.Write ("world");
  149. sw.Close ();
  150. }
  151. }
  152. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample5Input, false);
  153. Assert.AreEqual (C14NSpecExample5Output, res, "Example 5 from c14n spec - Entity References (with comments)");
  154. }
  155. [Test]
  156. public void C14NSpecExample6 ()
  157. {
  158. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample6Input, false);
  159. Assert.AreEqual (C14NSpecExample6Output, res, "Example 6 from c14n spec - UTF-8 Encoding (with comments)");
  160. }
  161. private string ExecuteXmlDSigC14NTransform (string InputXml, bool resolver)
  162. {
  163. XmlDocument doc = new XmlDocument ();
  164. doc.PreserveWhitespace = true;
  165. doc.LoadXml (InputXml);
  166. // Testing default attribute support with
  167. // vreader.ValidationType = ValidationType.None.
  168. UTF8Encoding utf8 = new UTF8Encoding ();
  169. byte[] data = utf8.GetBytes (InputXml.ToString ());
  170. Stream stream = new MemoryStream (data);
  171. XmlTextReader reader = new XmlTextReader (stream);
  172. XmlValidatingReader vreader = new XmlValidatingReader (reader);
  173. vreader.ValidationType = ValidationType.None;
  174. vreader.EntityHandling = EntityHandling.ExpandCharEntities;
  175. doc.Load (vreader);
  176. if (resolver)
  177. transform.Resolver = new XmlUrlResolver ();
  178. transform.LoadInput (doc);
  179. return Stream2String ((Stream)transform.GetOutput ());
  180. }
  181. private string Stream2String (Stream s)
  182. {
  183. StringBuilder sb = new StringBuilder ();
  184. int b = s.ReadByte ();
  185. while (b != -1) {
  186. sb.Append (Convert.ToChar (b));
  187. b = s.ReadByte ();
  188. }
  189. return sb.ToString ();
  190. }
  191. //
  192. // Example 1 from C14N spec - PIs, Comments, and Outside of Document Element:
  193. // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
  194. //
  195. static string C14NSpecExample1Input =
  196. "<?xml version=\"1.0\"?>\n" +
  197. "\n" +
  198. "<?xml-stylesheet href=\"doc.xsl\"\n" +
  199. " type=\"text/xsl\" ?>\n" +
  200. "\n" +
  201. "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
  202. "\n" +
  203. "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
  204. "\n" +
  205. "<?pi-without-data ?>\n\n" +
  206. "<!-- Comment 2 -->\n\n" +
  207. "<!-- Comment 3 -->\n";
  208. static string C14NSpecExample1Output =
  209. "<?xml-stylesheet href=\"doc.xsl\"\n" +
  210. " type=\"text/xsl\" ?>\n" +
  211. "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
  212. "<?pi-without-data?>\n" +
  213. "<!-- Comment 2 -->\n" +
  214. "<!-- Comment 3 -->";
  215. //
  216. // Example 2 from C14N spec - Whitespace in Document Content:
  217. // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
  218. //
  219. static string C14NSpecExample2Input =
  220. "<doc>\n" +
  221. " <clean> </clean>\n" +
  222. " <dirty> A B </dirty>\n" +
  223. " <mixed>\n" +
  224. " A\n" +
  225. " <clean> </clean>\n" +
  226. " B\n" +
  227. " <dirty> A B </dirty>\n" +
  228. " C\n" +
  229. " </mixed>\n" +
  230. "</doc>\n";
  231. static string C14NSpecExample2Output =
  232. "<doc>\n" +
  233. " <clean> </clean>\n" +
  234. " <dirty> A B </dirty>\n" +
  235. " <mixed>\n" +
  236. " A\n" +
  237. " <clean> </clean>\n" +
  238. " B\n" +
  239. " <dirty> A B </dirty>\n" +
  240. " C\n" +
  241. " </mixed>\n" +
  242. "</doc>";
  243. //
  244. // Example 3 from C14N spec - Start and End Tags:
  245. // http://www.w3.org/TR/xml-c14n#Example-SETags
  246. //
  247. static string C14NSpecExample3Input =
  248. "<!DOCTYPE doc [<!ATTLIST e9 attr CDATA \"default\">]>\n" +
  249. "<doc>\n" +
  250. " <e1 />\n" +
  251. " <e2 ></e2>\n" +
  252. " <e3 name = \"elem3\" id=\"elem3\" />\n" +
  253. " <e4 name=\"elem4\" id=\"elem4\" ></e4>\n" +
  254. " <e5 a:attr=\"out\" b:attr=\"sorted\" attr2=\"all\" attr=\"I\'m\"\n" +
  255. " xmlns:b=\"http://www.ietf.org\" \n" +
  256. " xmlns:a=\"http://www.w3.org\"\n" +
  257. " xmlns=\"http://www.uvic.ca\"/>\n" +
  258. " <e6 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
  259. " <e7 xmlns=\"http://www.ietf.org\">\n" +
  260. " <e8 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
  261. " <e9 xmlns=\"\" xmlns:a=\"http://www.ietf.org\"/>\n" +
  262. " </e8>\n" +
  263. " </e7>\n" +
  264. " </e6>\n" +
  265. "</doc>\n";
  266. static string C14NSpecExample3Output =
  267. "<doc>\n" +
  268. " <e1></e1>\n" +
  269. " <e2></e2>\n" +
  270. " <e3 id=\"elem3\" name=\"elem3\"></e3>\n" +
  271. " <e4 id=\"elem4\" name=\"elem4\"></e4>\n" +
  272. " <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" +
  273. " <e6 xmlns:a=\"http://www.w3.org\">\n" +
  274. " <e7 xmlns=\"http://www.ietf.org\">\n" +
  275. " <e8 xmlns=\"\">\n" +
  276. " <e9 xmlns:a=\"http://www.ietf.org\" attr=\"default\"></e9>\n" +
  277. // " <e9 xmlns:a=\"http://www.ietf.org\"></e9>\n" +
  278. " </e8>\n" +
  279. " </e7>\n" +
  280. " </e6>\n" +
  281. "</doc>";
  282. //
  283. // Example 4 from C14N spec - Character Modifications and Character References:
  284. // http://www.w3.org/TR/xml-c14n#Example-Chars
  285. //
  286. // Aleksey:
  287. // This test does not include "normId" element
  288. // because it has an invalid ID attribute "id" which
  289. // should be normalized by XML parser. Currently Mono
  290. // does not support this (see comment after this example
  291. // in the spec).
  292. static string C14NSpecExample4Input =
  293. "<!DOCTYPE doc [<!ATTLIST normId id ID #IMPLIED>]>\n" +
  294. "<doc>\n" +
  295. " <text>First line&#x0d;&#10;Second line</text>\n" +
  296. " <value>&#x32;</value>\n" +
  297. " <compute><![CDATA[value>\"0\" && value<\"10\" ?\"valid\":\"error\"]]></compute>\n" +
  298. " <compute expr=\'value>\"0\" &amp;&amp; value&lt;\"10\" ?\"valid\":\"error\"\'>valid</compute>\n" +
  299. " <norm attr=\' &apos; &#x20;&#13;&#xa;&#9; &apos; \'/>\n" +
  300. // " <normId id=\' &apos; &#x20;&#13;&#xa;&#9; &apos; \'/>\n" +
  301. "</doc>\n";
  302. static string C14NSpecExample4Output =
  303. "<doc>\n" +
  304. " <text>First line&#xD;\n" +
  305. "Second line</text>\n" +
  306. " <value>2</value>\n" +
  307. " <compute>value&gt;\"0\" &amp;&amp; value&lt;\"10\" ?\"valid\":\"error\"</compute>\n" +
  308. " <compute expr=\"value>&quot;0&quot; &amp;&amp; value&lt;&quot;10&quot; ?&quot;valid&quot;:&quot;error&quot;\">valid</compute>\n" +
  309. " <norm attr=\" \' &#xD;&#xA;&#x9; \' \"></norm>\n" +
  310. // " <normId id=\"\' &#xD;&#xA;&#x9; \'\"></normId>\n" +
  311. "</doc>";
  312. //
  313. // Example 5 from C14N spec - Entity References:
  314. // http://www.w3.org/TR/xml-c14n#Example-Entities
  315. //
  316. static string C14NSpecExample5Input =
  317. "<!DOCTYPE doc [\n" +
  318. "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
  319. "<!ENTITY ent1 \"Hello\">\n" +
  320. "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
  321. "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
  322. "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
  323. "]>\n" +
  324. "<doc attrExtEnt=\"entExt\">\n" +
  325. " &ent1;, &ent2;!\n" +
  326. "</doc>\n" +
  327. "\n" +
  328. "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
  329. static string C14NSpecExample5Output =
  330. "<doc attrExtEnt=\"entExt\">\n" +
  331. " Hello, world!\n" +
  332. "</doc>\n" +
  333. "<!-- Let world.txt contain \"world\" (excluding the quotes) -->";
  334. //
  335. // Example 6 from C14N spec - UTF-8 Encoding:
  336. // http://www.w3.org/TR/xml-c14n#Example-UTF8
  337. //
  338. static string C14NSpecExample6Input =
  339. "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
  340. "<doc>&#169;</doc>\n";
  341. static string C14NSpecExample6Output =
  342. "<doc>\xC2\xA9</doc>";
  343. //
  344. // Example 7 from C14N spec - Document Subsets:
  345. // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
  346. //
  347. // Aleksey:
  348. // Well, XPath support in Mono is far from complete....
  349. // I was not able to simplify the xpath expression from this test
  350. // so it runs on Mono and still makes sense for testing this feature.
  351. // Thus this test is not included in the suite now.
  352. static string C14NSpecExample7Input =
  353. "<!DOCTYPE doc [\n" +
  354. "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
  355. "<!ATTLIST e3 id ID #IMPLIED>\n" +
  356. "]>\n" +
  357. "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
  358. " <e1>\n" +
  359. " <e2 xmlns=\"\">\n" +
  360. " <e3 id=\"E3\"/>\n" +
  361. " </e2>\n" +
  362. " </e1>\n" +
  363. "</doc>\n";
  364. static string C14NSpecExample7Xpath =
  365. "(//.|//@*|//namespace::*)\n" +
  366. "[\n" +
  367. "self::ietf:e1\n" +
  368. " or\n" +
  369. "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
  370. " or\n" +
  371. "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
  372. "]";
  373. static string C14NSpecExample7Output =
  374. "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
  375. }
  376. }
  377. #endif