XmlDsigExcC14NTransformTest.cs 18 KB

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