XmlDsigC14NTransformTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 &#xD; <Toto/> text &amp; </Test >";
  93. // BAD for XmlDocument input (framework 1.0 result)
  94. static string c14xml1 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \r\n \r <Toto></Toto> text &amp; </Test>";
  95. // GOOD for Stream input
  96. static string c14xml2 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> \n &#xD; <Toto></Toto> text &amp; </Test>";
  97. // GOOD for XmlDocument input. The difference is because once
  98. // xml string is loaded to XmlDocument, there is no difference
  99. // between \r and &#xD;, so every \r must be handled as &#xD;.
  100. static string c14xml3 = "<Test xmlns=\"http://www.go-mono.com/\" attrib=\"at \"> &#xD;\n &#xD; <Toto></Toto> text &amp; </Test>";
  101. private XmlDocument GetDoc ()
  102. {
  103. XmlDocument doc = new XmlDocument ();
  104. doc.PreserveWhitespace = true;
  105. doc.LoadXml (xml);
  106. return doc;
  107. }
  108. [Test]
  109. public void LoadInputAsXmlDocument ()
  110. {
  111. XmlDocument doc = GetDoc ();
  112. transform.LoadInput (doc);
  113. Stream s = (Stream) transform.GetOutput ();
  114. string output = Stream2String (s);
  115. #if NET_1_1
  116. AssertEquals("XmlDocument", c14xml3, output);
  117. #else
  118. // .NET 1.0 keeps the \r\n (0x0D, 0x0A) - bug
  119. AssertEquals("XmlDocument", c14xml1, output);
  120. #endif
  121. }
  122. [Test]
  123. #if NET_2_0
  124. [Category ("NotDotNet")]
  125. // see LoadInputAsXmlNodeList2 description
  126. #endif
  127. public void LoadInputAsXmlNodeList ()
  128. {
  129. XmlDocument doc = GetDoc ();
  130. // Argument list just contains element Test.
  131. transform.LoadInput (doc.ChildNodes);
  132. Stream s = (Stream) transform.GetOutput ();
  133. string output = Stream2String (s);
  134. AssertEquals ("XmlChildNodes", "<Test></Test>", output);
  135. }
  136. [Test]
  137. [Category ("NotDotNet")]
  138. // MS has a bug that those namespace declaration nodes in
  139. // the node-set are written to output. Related spec section is:
  140. // http://www.w3.org/TR/2001/REC-xml-c14n-20010315#ProcessingModel
  141. public void LoadInputAsXmlNodeList2 ()
  142. {
  143. XmlDocument doc = GetDoc ();
  144. transform.LoadInput (doc.SelectNodes ("//*"));
  145. Stream s = (Stream) transform.GetOutput ();
  146. string output = Stream2String (s);
  147. string expected = @"<Test><Toto></Toto></Test>";
  148. AssertEquals ("XmlChildNodes", expected, output);
  149. }
  150. [Test]
  151. public void LoadInputAsStream ()
  152. {
  153. MemoryStream ms = new MemoryStream ();
  154. byte[] x = Encoding.ASCII.GetBytes (xml);
  155. ms.Write (x, 0, x.Length);
  156. ms.Position = 0;
  157. transform.LoadInput (ms);
  158. Stream s = (Stream) transform.GetOutput ();
  159. string output = Stream2String (s);
  160. AssertEquals ("MemoryStream", c14xml2, output);
  161. }
  162. [Test]
  163. #if NET_2_0
  164. [ExpectedException (typeof (ArgumentException))]
  165. #else
  166. // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
  167. #endif
  168. public void LoadInputWithUnsupportedType ()
  169. {
  170. byte[] bad = { 0xBA, 0xD };
  171. transform.LoadInput (bad);
  172. }
  173. [Test]
  174. [ExpectedException (typeof (ArgumentException))]
  175. public void UnsupportedOutput ()
  176. {
  177. XmlDocument doc = new XmlDocument();
  178. object o = transform.GetOutput (doc.GetType ());
  179. }
  180. [Test]
  181. public void C14NSpecExample1 ()
  182. {
  183. using (StreamWriter sw = new StreamWriter ("doc.dtd", false, Encoding.ASCII)) {
  184. sw.Write ("<!-- presence, not content, required -->");
  185. sw.Close ();
  186. }
  187. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample1Input);
  188. AssertEquals ("Example 1 from c14n spec - PIs, Comments, and Outside of Document Element (without comments)",
  189. C14NSpecExample1Output, res);
  190. }
  191. [Test]
  192. public void C14NSpecExample2 ()
  193. {
  194. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample2Input);
  195. AssertEquals ("Example 2 from c14n spec - Whitespace in Document Content (without comments)",
  196. C14NSpecExample2Output, res);
  197. }
  198. [Test]
  199. public void C14NSpecExample3 ()
  200. {
  201. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample3Input);
  202. AssertEquals ("Example 3 from c14n spec - Start and End Tags (without comments)",
  203. C14NSpecExample3Output, res);
  204. }
  205. [Test]
  206. // [Ignore ("This test should be fine, but it does not pass under MS.NET")]
  207. public void C14NSpecExample4 ()
  208. {
  209. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample4Input);
  210. AssertEquals ("Example 4 from c14n spec - Character Modifications and Character References (without comments)",
  211. C14NSpecExample4Output, res);
  212. }
  213. [Test]
  214. public void C14NSpecExample5 ()
  215. {
  216. using (StreamWriter sw = new StreamWriter ("world.txt", false, Encoding.ASCII)) {
  217. sw.Write ("world");
  218. sw.Close ();
  219. }
  220. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample5Input);
  221. AssertEquals ("Example 5 from c14n spec - Entity References (without comments)",
  222. C14NSpecExample5Output, res);
  223. }
  224. [Test]
  225. public void C14NSpecExample6 ()
  226. {
  227. string res = ExecuteXmlDSigC14NTransform (C14NSpecExample6Input);
  228. AssertEquals ("Example 6 from c14n spec - UTF-8 Encoding (without comments)",
  229. C14NSpecExample6Output, res);
  230. }
  231. private string ExecuteXmlDSigC14NTransform (string InputXml)
  232. {
  233. XmlDocument doc = new XmlDocument ();
  234. doc.PreserveWhitespace = true;
  235. doc.LoadXml (InputXml);
  236. // Testing default attribute support with
  237. // vreader.ValidationType = ValidationType.None.
  238. //
  239. UTF8Encoding utf8 = new UTF8Encoding ();
  240. byte[] data = utf8.GetBytes (InputXml.ToString ());
  241. Stream stream = new MemoryStream (data);
  242. XmlTextReader reader = new XmlTextReader (stream);
  243. XmlValidatingReader vreader = new XmlValidatingReader (reader);
  244. vreader.ValidationType = ValidationType.None;
  245. vreader.EntityHandling = EntityHandling.ExpandCharEntities;
  246. doc.Load (vreader);
  247. transform.LoadInput (doc);
  248. return Stream2String ((Stream)transform.GetOutput ());
  249. }
  250. //
  251. // Example 1 from C14N spec - PIs, Comments, and Outside of Document Element:
  252. // http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
  253. //
  254. // Aleksey:
  255. // removed reference to an empty external DTD
  256. //
  257. static string C14NSpecExample1Input =
  258. "<?xml version=\"1.0\"?>\n" +
  259. "\n" +
  260. "<?xml-stylesheet href=\"doc.xsl\"\n" +
  261. " type=\"text/xsl\" ?>\n" +
  262. "\n" +
  263. // "<!DOCTYPE doc SYSTEM \"doc.dtd\">\n" +
  264. "\n" +
  265. "<doc>Hello, world!<!-- Comment 1 --></doc>\n" +
  266. "\n" +
  267. "<?pi-without-data ?>\n\n" +
  268. "<!-- Comment 2 -->\n\n" +
  269. "<!-- Comment 3 -->\n";
  270. static string C14NSpecExample1Output =
  271. "<?xml-stylesheet href=\"doc.xsl\"\n" +
  272. " type=\"text/xsl\" ?>\n" +
  273. "<doc>Hello, world!</doc>\n" +
  274. "<?pi-without-data?>";
  275. //
  276. // Example 2 from C14N spec - Whitespace in Document Content:
  277. // http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
  278. //
  279. static string C14NSpecExample2Input =
  280. "<doc>\n" +
  281. " <clean> </clean>\n" +
  282. " <dirty> A B </dirty>\n" +
  283. " <mixed>\n" +
  284. " A\n" +
  285. " <clean> </clean>\n" +
  286. " B\n" +
  287. " <dirty> A B </dirty>\n" +
  288. " C\n" +
  289. " </mixed>\n" +
  290. "</doc>\n";
  291. static string C14NSpecExample2Output =
  292. "<doc>\n" +
  293. " <clean> </clean>\n" +
  294. " <dirty> A B </dirty>\n" +
  295. " <mixed>\n" +
  296. " A\n" +
  297. " <clean> </clean>\n" +
  298. " B\n" +
  299. " <dirty> A B </dirty>\n" +
  300. " C\n" +
  301. " </mixed>\n" +
  302. "</doc>";
  303. //
  304. // Example 3 from C14N spec - Start and End Tags:
  305. // http://www.w3.org/TR/xml-c14n#Example-SETags
  306. //
  307. static string C14NSpecExample3Input =
  308. "<!DOCTYPE doc [<!ATTLIST e9 attr CDATA \"default\">]>\n" +
  309. "<doc>\n" +
  310. " <e1 />\n" +
  311. " <e2 ></e2>\n" +
  312. " <e3 name = \"elem3\" id=\"elem3\" />\n" +
  313. " <e4 name=\"elem4\" id=\"elem4\" ></e4>\n" +
  314. " <e5 a:attr=\"out\" b:attr=\"sorted\" attr2=\"all\" attr=\"I\'m\"\n" +
  315. " xmlns:b=\"http://www.ietf.org\" \n" +
  316. " xmlns:a=\"http://www.w3.org\"\n" +
  317. " xmlns=\"http://www.uvic.ca\"/>\n" +
  318. " <e6 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
  319. " <e7 xmlns=\"http://www.ietf.org\">\n" +
  320. " <e8 xmlns=\"\" xmlns:a=\"http://www.w3.org\">\n" +
  321. " <e9 xmlns=\"\" xmlns:a=\"http://www.ietf.org\"/>\n" +
  322. " </e8>\n" +
  323. " </e7>\n" +
  324. " </e6>\n" +
  325. "</doc>\n";
  326. static string C14NSpecExample3Output =
  327. "<doc>\n" +
  328. " <e1></e1>\n" +
  329. " <e2></e2>\n" +
  330. " <e3 id=\"elem3\" name=\"elem3\"></e3>\n" +
  331. " <e4 id=\"elem4\" name=\"elem4\"></e4>\n" +
  332. " <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" +
  333. " <e6 xmlns:a=\"http://www.w3.org\">\n" +
  334. " <e7 xmlns=\"http://www.ietf.org\">\n" +
  335. " <e8 xmlns=\"\">\n" +
  336. " <e9 xmlns:a=\"http://www.ietf.org\" attr=\"default\"></e9>\n" +
  337. // " <e9 xmlns:a=\"http://www.ietf.org\"></e9>\n" +
  338. " </e8>\n" +
  339. " </e7>\n" +
  340. " </e6>\n" +
  341. "</doc>";
  342. //
  343. // Example 4 from C14N spec - Character Modifications and Character References:
  344. // http://www.w3.org/TR/xml-c14n#Example-Chars
  345. //
  346. // Aleksey:
  347. // This test does not include "normId" element
  348. // because it has an invalid ID attribute "id" which
  349. // should be normalized by XML parser. Currently Mono
  350. // does not support this (see comment after this example
  351. // in the spec).
  352. static string C14NSpecExample4Input =
  353. "<!DOCTYPE doc [<!ATTLIST normId id ID #IMPLIED>]>\n" +
  354. "<doc>\n" +
  355. " <text>First line&#x0d;&#10;Second line</text>\n" +
  356. " <value>&#x32;</value>\n" +
  357. " <compute><![CDATA[value>\"0\" && value<\"10\" ?\"valid\":\"error\"]]></compute>\n" +
  358. " <compute expr=\'value>\"0\" &amp;&amp; value&lt;\"10\" ?\"valid\":\"error\"\'>valid</compute>\n" +
  359. " <norm attr=\' &apos; &#x20;&#13;&#xa;&#9; &apos; \'/>\n" +
  360. // " <normId id=\' &apos; &#x20;&#13;&#xa;&#9; &apos; \'/>\n" +
  361. "</doc>\n";
  362. static string C14NSpecExample4Output =
  363. "<doc>\n" +
  364. " <text>First line&#xD;\n" +
  365. "Second line</text>\n" +
  366. " <value>2</value>\n" +
  367. " <compute>value&gt;\"0\" &amp;&amp; value&lt;\"10\" ?\"valid\":\"error\"</compute>\n" +
  368. " <compute expr=\"value>&quot;0&quot; &amp;&amp; value&lt;&quot;10&quot; ?&quot;valid&quot;:&quot;error&quot;\">valid</compute>\n" +
  369. " <norm attr=\" \' &#xD;&#xA;&#x9; \' \"></norm>\n" +
  370. // " <normId id=\"\' &#xD;&#xA;&#x9; \'\"></normId>\n" +
  371. "</doc>";
  372. //
  373. // Example 5 from C14N spec - Entity References:
  374. // http://www.w3.org/TR/xml-c14n#Example-Entities
  375. //
  376. static string C14NSpecExample5Input =
  377. "<!DOCTYPE doc [\n" +
  378. "<!ATTLIST doc attrExtEnt ENTITY #IMPLIED>\n" +
  379. "<!ENTITY ent1 \"Hello\">\n" +
  380. "<!ENTITY ent2 SYSTEM \"world.txt\">\n" +
  381. "<!ENTITY entExt SYSTEM \"earth.gif\" NDATA gif>\n" +
  382. "<!NOTATION gif SYSTEM \"viewgif.exe\">\n" +
  383. "]>\n" +
  384. "<doc attrExtEnt=\"entExt\">\n" +
  385. " &ent1;, &ent2;!\n" +
  386. "</doc>\n" +
  387. "\n" +
  388. "<!-- Let world.txt contain \"world\" (excluding the quotes) -->\n";
  389. static string C14NSpecExample5Output =
  390. "<doc attrExtEnt=\"entExt\">\n" +
  391. " Hello, world!\n" +
  392. "</doc>";
  393. //
  394. // Example 6 from C14N spec - UTF-8 Encoding:
  395. // http://www.w3.org/TR/xml-c14n#Example-UTF8
  396. //
  397. static string C14NSpecExample6Input =
  398. "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
  399. "<doc>&#169;</doc>\n";
  400. static string C14NSpecExample6Output =
  401. "<doc>\xC2\xA9</doc>";
  402. //
  403. // Example 7 from C14N spec - Document Subsets:
  404. // http://www.w3.org/TR/xml-c14n#Example-DocSubsets
  405. //
  406. // Aleksey:
  407. // Well, XPath support in Mono is far from complete....
  408. // I was not able to simplify the xpath expression from this test
  409. // so it runs on Mono and still makes sense for testing this feature.
  410. // Thus this test is not included in the suite now.
  411. static string C14NSpecExample7Input =
  412. "<!DOCTYPE doc [\n" +
  413. "<!ATTLIST e2 xml:space (default|preserve) \'preserve\'>\n" +
  414. "<!ATTLIST e3 id ID #IMPLIED>\n" +
  415. "]>\n" +
  416. "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" +
  417. " <e1>\n" +
  418. " <e2 xmlns=\"\">\n" +
  419. " <e3 id=\"E3\"/>\n" +
  420. " </e2>\n" +
  421. " </e1>\n" +
  422. "</doc>\n";
  423. static string C14NSpecExample7Xpath =
  424. "(//.|//@*|//namespace::*)\n" +
  425. "[\n" +
  426. "self::ietf:e1\n" +
  427. " or\n" +
  428. "(parent::ietf:e1 and not(self::text() or self::e2))\n" +
  429. " or\n" +
  430. "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" +
  431. "]";
  432. static string C14NSpecExample7Output =
  433. "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>";
  434. }
  435. }