XmlDsigC14NTransformTest.cs 16 KB

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