XmlDsigC14NTransformTest.cs 16 KB

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