XmlDsigC14NWithCommentsTransformTest.cs 14 KB

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