XmlDsigC14NWithCommentsTransformTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. [Test]
  34. public void Properties ()
  35. {
  36. AssertEquals ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", transform.Algorithm);
  37. Type[] input = transform.InputTypes;
  38. Assert ("Input #", (input.Length == 3));
  39. // check presence of every supported input types
  40. bool istream = false;
  41. bool ixmldoc = false;
  42. bool ixmlnl = false;
  43. foreach (Type t in input) {
  44. if (t.ToString () == "System.IO.Stream")
  45. istream = true;
  46. if (t.ToString () == "System.Xml.XmlDocument")
  47. ixmldoc = true;
  48. if (t.ToString () == "System.Xml.XmlNodeList")
  49. ixmlnl = true;
  50. }
  51. Assert ("Input Stream", istream);
  52. Assert ("Input XmlDocument", ixmldoc);
  53. Assert ("Input XmlNodeList", ixmlnl);
  54. Type[] output = transform.OutputTypes;
  55. Assert ("Output #", (output.Length == 1));
  56. // check presence of every supported output types
  57. bool ostream = false;
  58. foreach (Type t in output) {
  59. if (t.ToString () == "System.IO.Stream")
  60. ostream = true;
  61. }
  62. Assert ("Output Stream", ostream);
  63. }
  64. [Test]
  65. public void GetInnerXml ()
  66. {
  67. XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
  68. AssertEquals ("Default InnerXml", null, xnl);
  69. }
  70. [Test]
  71. public void LoadInputWithUnsupportedType ()
  72. {
  73. byte[] bad = { 0xBA, 0xD };
  74. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  75. transform.LoadInput (bad);
  76. }
  77. [Test]
  78. [ExpectedException (typeof (ArgumentException))]
  79. public void UnsupportedOutput ()
  80. {
  81. XmlDocument doc = new XmlDocument();
  82. object o = transform.GetOutput (doc.GetType ());
  83. }
  84. [Test]
  85. public void C14NSpecExample1 ()
  86. {
  87. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample1Input);
  88. AssertEquals ("Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (with comments)",
  89. C14NSpecExample1Output, res);
  90. }
  91. [Test]
  92. public void C14NSpecExample2 ()
  93. {
  94. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample2Input);
  95. AssertEquals ("Example 2 from c14n spec - Whitespace in Document Content (with comments)",
  96. C14NSpecExample2Output, res);
  97. }
  98. [Test]
  99. public void C14NSpecExample3 ()
  100. {
  101. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample3Input);
  102. AssertEquals ("Example 3 from c14n spec - Start and End Tags (with comments)",
  103. C14NSpecExample3Output, res);
  104. }
  105. [Test]
  106. public void C14NSpecExample4 ()
  107. {
  108. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample4Input);
  109. AssertEquals ("Example 4 from c14n spec - Character Modifications and Character References (with comments)",
  110. C14NSpecExample4Output, res);
  111. }
  112. [Test]
  113. public void C14NSpecExample5 ()
  114. {
  115. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample5Input);
  116. AssertEquals ("Example 5 from c14n spec - Entity References (with comments)",
  117. C14NSpecExample5Output, res);
  118. }
  119. [Test]
  120. public void C14NSpecExample6 ()
  121. {
  122. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample6Input);
  123. AssertEquals ("Example 6 from c14n spec - UTF-8 Encoding (with comments)",
  124. C14NSpecExample6Output, res);
  125. }
  126. private string ExecuteXmlDSigC14NTransform (string InputXml)
  127. {
  128. XmlDocument doc = new XmlDocument ();
  129. doc.PreserveWhitespace = true;
  130. doc.LoadXml (InputXml);
  131. // Testing default attribute support with
  132. // vreader.ValidationType = ValidationType.None.
  133. UTF8Encoding utf8 = new UTF8Encoding ();
  134. byte[] data = utf8.GetBytes (InputXml.ToString ());
  135. Stream stream = new MemoryStream (data);
  136. XmlTextReader reader = new XmlTextReader (stream);
  137. XmlValidatingReader vreader = new XmlValidatingReader (reader);
  138. vreader.ValidationType = ValidationType.None;
  139. vreader.EntityHandling = EntityHandling.ExpandCharEntities;
  140. doc.Load (vreader);
  141. transform.LoadInput (doc);
  142. return Stream2String ((Stream)transform.GetOutput ());
  143. }
  144. private string Stream2String (Stream s)
  145. {
  146. StringBuilder sb = new StringBuilder ();
  147. int b = s.ReadByte ();
  148. while (b != -1) {
  149. sb.Append (Convert.ToChar (b));
  150. b = s.ReadByte ();
  151. }
  152. return sb.ToString ();
  153. }
  154. //
  155. // Example 1 from C14N spec - PIs, Comments, and Outside of Document Element:
  156. // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
  157. //
  158. static string C14NSpecExample1Input =
  159. "<?xml version=\"1.0\"?>\n" +
  160. "\n" +
  161. "<?xml-stylesheet href=\"doc.xsl\"\n" +
  162. " type=\"text/xsl\" ?>\n" +
  163. "\n" +
  164. "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
  165. "\n" +
  166. "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
  167. "\n" +
  168. "<?pi-without-data ?>\n\n" +
  169. "<!-- Comment 2 -->\n\n" +
  170. "<!-- Comment 3 -->\n";
  171. static string C14NSpecExample1Output =
  172. "<?xml-stylesheet href=\"doc.xsl\"\n" +
  173. " type=\"text/xsl\" ?>\n" +
  174. "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
  175. "<?pi-without-data?>\n" +
  176. "<!-- Comment 2 -->\n" +
  177. "<!-- Comment 3 -->";
  178. //
  179. // Example 2 from C14N spec - Whitespace in Document Content:
  180. // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
  181. //
  182. static string C14NSpecExample2Input =
  183. "<doc>\n" +
  184. " <clean> </clean>\n" +
  185. " <dirty> A B </dirty>\n" +
  186. " <mixed>\n" +
  187. " A\n" +
  188. " <clean> </clean>\n" +
  189. " B\n" +
  190. " <dirty> A B </dirty>\n" +
  191. " C\n" +
  192. " </mixed>\n" +
  193. "</doc>\n";
  194. static string C14NSpecExample2Output =
  195. "<doc>\n" +
  196. " <clean> </clean>\n" +
  197. " <dirty> A B </dirty>\n" +
  198. " <mixed>\n" +
  199. " A\n" +
  200. " <clean> </clean>\n" +
  201. " B\n" +
  202. " <dirty> A B </dirty>\n" +
  203. " C\n" +
  204. " </mixed>\n" +
  205. "</doc>";
  206. //
  207. // Example 3 from C14N spec - Start and End Tags:
  208. // http://www.w3.org/TR/xml-c14n#Example-SETags
  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. static string C14NSpecExample5Input =
  280. "<!DOCTYPE doc [\n" +
  281. "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
  282. "<!ENTITY ent1 \"Hello\">\n" +
  283. "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
  284. "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
  285. "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
  286. "]>\n" +
  287. "<doc attrExtEnt=\"entExt\">\n" +
  288. " &ent1;, &ent2;!\n" +
  289. "</doc>\n" +
  290. "\n" +
  291. "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
  292. static string C14NSpecExample5Output =
  293. "<doc attrExtEnt=\"entExt\">\n" +
  294. " Hello, world!\n" +
  295. "</doc>\n" +
  296. "<!-- Let world.txt contain \"world\" (excluding the quotes) -->";
  297. //
  298. // Example 6 from C14N spec - UTF-8 Encoding:
  299. // http://www.w3.org/TR/xml-c14n#Example-UTF8
  300. //
  301. static string C14NSpecExample6Input =
  302. "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
  303. "<doc>&#169;</doc>\n";
  304. static string C14NSpecExample6Output =
  305. "<doc>\xC2\xA9</doc>";
  306. //
  307. // Example 7 from C14N spec - Document Subsets:
  308. // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
  309. //
  310. // Aleksey:
  311. // Well, XPath support in Mono is far from complete....
  312. // I was not able to simplify the xpath expression from this test
  313. // so it runs on Mono and still makes sense for testing this feature.
  314. // Thus this test is not included in the suite now.
  315. static string C14NSpecExample7Input =
  316. "<!DOCTYPE doc [\n" +
  317. "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
  318. "<!ATTLIST e3 id ID #IMPLIED>\n" +
  319. "]>\n" +
  320. "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
  321. " <e1>\n" +
  322. " <e2 xmlns=\"\">\n" +
  323. " <e3 id=\"E3\"/>\n" +
  324. " </e2>\n" +
  325. " </e1>\n" +
  326. "</doc>\n";
  327. static string C14NSpecExample7Xpath =
  328. "(//.|//@*|//namespace::*)\n" +
  329. "[\n" +
  330. "self::ietf:e1\n" +
  331. " or\n" +
  332. "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
  333. " or\n" +
  334. "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
  335. "]";
  336. static string C14NSpecExample7Output =
  337. "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
  338. }
  339. }