XmlDsigC14NWithCommentsTransformTest.cs 15 KB

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