XmlDsigEnvelopedSignatureTransformTest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // XmlDsigEnvelopedSignatureTransformTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2004 Novell Inc.
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Security.Cryptography;
  12. using System.Security.Cryptography.Xml;
  13. using System.Text;
  14. using System.Xml;
  15. using System.Xml.Xsl;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.Security.Cryptography.Xml {
  18. // Note: GetInnerXml is protected in XmlDsigEnvelopedSignatureTransform making it
  19. // difficult to test properly. This class "open it up" :-)
  20. public class UnprotectedXmlDsigEnvelopedSignatureTransform : XmlDsigEnvelopedSignatureTransform {
  21. public XmlNodeList UnprotectedGetInnerXml ()
  22. {
  23. return base.GetInnerXml ();
  24. }
  25. }
  26. [TestFixture]
  27. public class XmlDsigEnvelopedSignatureTransformTest : Assertion {
  28. protected UnprotectedXmlDsigEnvelopedSignatureTransform transform;
  29. [SetUp]
  30. protected void SetUp ()
  31. {
  32. transform = new UnprotectedXmlDsigEnvelopedSignatureTransform ();
  33. }
  34. [Test]
  35. public void Properties ()
  36. {
  37. AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature", transform.Algorithm);
  38. Type[] input = transform.InputTypes;
  39. AssertEquals ("Input Length", 3, input.Length);
  40. // check presence of every supported input types
  41. bool istream = false;
  42. bool ixmldoc = false;
  43. bool ixmlnl = false;
  44. foreach (Type t in input) {
  45. if (t.ToString () == "System.Xml.XmlDocument")
  46. ixmldoc = true;
  47. if (t.ToString () == "System.Xml.XmlNodeList")
  48. ixmlnl = true;
  49. }
  50. Assert ("No Input Stream", !istream);
  51. Assert ("Input XmlDocument", ixmldoc);
  52. Assert ("Input XmlNodeList", ixmlnl);
  53. Type[] output = transform.OutputTypes;
  54. AssertEquals ("Output Length", 2, output.Length);
  55. // check presence of every supported output types
  56. bool oxmlnl = false;
  57. bool oxmldoc = false;
  58. foreach (Type t in output) {
  59. if (t == null)
  60. throw new InvalidOperationException ();
  61. if (t.ToString () == "System.Xml.XmlNodeList")
  62. oxmlnl = true;
  63. if (t.ToString () == "System.Xml.XmlDocument")
  64. oxmldoc = true;
  65. }
  66. Assert ("Output XmlNodeList", oxmlnl);
  67. Assert ("Output XmlDocument", oxmldoc);
  68. }
  69. protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual)
  70. {
  71. for (int i=0; i < expected.Count; i++) {
  72. if (expected [i].OuterXml != actual [i].OuterXml)
  73. Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
  74. }
  75. }
  76. [Test]
  77. public void GetInnerXml ()
  78. {
  79. // Always returns null
  80. AssertNull (transform.UnprotectedGetInnerXml ());
  81. }
  82. private XmlDocument GetDoc ()
  83. {
  84. string dsig = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#dsa-sha1\" /><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>fdy6S2NLpnT4fMdokUHSHsmpcvo=</DigestValue></Reference></Signature>";
  85. string test = "<Envelope> " + dsig + " </Envelope>";
  86. XmlDocument doc = new XmlDocument ();
  87. doc.LoadXml (test);
  88. return doc;
  89. }
  90. [Test]
  91. public void LoadInputAsXmlDocument ()
  92. {
  93. XmlDocument doc = GetDoc ();
  94. transform.LoadInput (doc);
  95. object o = transform.GetOutput ();
  96. AssertEquals ("EnvelopedSignature result", doc, o);
  97. }
  98. [Test]
  99. public void LoadInputAsXmlNodeList ()
  100. {
  101. XmlDocument doc = GetDoc ();
  102. transform.LoadInput (doc.ChildNodes);
  103. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  104. AssertEquals ("EnvelopedSignature result", doc.ChildNodes, xnl);
  105. }
  106. }
  107. }