XmlDsigC14NTransformTest.cs 20 KB

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