XmlDsigC14NWithCommentsTransformTest.cs 15 KB

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