XmlDsigC14NWithCommentsTransformTest.cs 14 KB

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