XmlDsigExcC14NTransformTest.cs 23 KB

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