XmlDsigC14NWithCommentsTransformTest.cs 14 KB

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