XmlDsigEnvelopedSignatureTransformTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 UnprotectedXmlDsigEnvelopedSignatureTransform ()
  22. {
  23. }
  24. public UnprotectedXmlDsigEnvelopedSignatureTransform (bool includeComments)
  25. : base (includeComments)
  26. {
  27. }
  28. public XmlNodeList UnprotectedGetInnerXml ()
  29. {
  30. return base.GetInnerXml ();
  31. }
  32. }
  33. [TestFixture]
  34. public class XmlDsigEnvelopedSignatureTransformTest {
  35. private UnprotectedXmlDsigEnvelopedSignatureTransform transform;
  36. [SetUp]
  37. public void SetUp ()
  38. {
  39. transform = new UnprotectedXmlDsigEnvelopedSignatureTransform ();
  40. }
  41. [Test] // ctor ()
  42. public void Constructor1 ()
  43. {
  44. CheckProperties (transform);
  45. }
  46. [Test] // ctor (Boolean)
  47. public void Constructor2 ()
  48. {
  49. transform = new UnprotectedXmlDsigEnvelopedSignatureTransform (true);
  50. CheckProperties (transform);
  51. transform = new UnprotectedXmlDsigEnvelopedSignatureTransform (false);
  52. CheckProperties (transform);
  53. }
  54. void CheckProperties (XmlDsigEnvelopedSignatureTransform transform)
  55. {
  56. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#enveloped-signature",
  57. transform.Algorithm, "Algorithm");
  58. Type [] input = transform.InputTypes;
  59. Assert.AreEqual (3, input.Length, "Input Length");
  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 == typeof (XmlDocument))
  66. ixmldoc = true;
  67. if (t == typeof (XmlNodeList))
  68. ixmlnl = true;
  69. if (t == typeof (Stream))
  70. istream = true;
  71. }
  72. Assert.IsTrue (istream, "Input Stream");
  73. Assert.IsTrue (ixmldoc, "Input XmlDocument");
  74. Assert.IsTrue (ixmlnl, "Input XmlNodeList");
  75. Type [] output = transform.OutputTypes;
  76. Assert.AreEqual (2, output.Length, "Output Length");
  77. // check presence of every supported output types
  78. bool oxmlnl = false;
  79. bool oxmldoc = false;
  80. foreach (Type t in output) {
  81. if (t == typeof (XmlNodeList))
  82. oxmlnl = true;
  83. if (t == typeof (XmlDocument))
  84. oxmldoc = true;
  85. }
  86. Assert.IsTrue (oxmlnl, "Output XmlNodeList");
  87. Assert.IsTrue (oxmldoc, "Output XmlDocument");
  88. }
  89. void AssertEquals (XmlNodeList expected, XmlNodeList actual, string msg)
  90. {
  91. for (int i = 0; i < expected.Count; i++) {
  92. if (expected [i].OuterXml != actual [i].OuterXml)
  93. Assert.Fail (msg + " [" + i + "] expected " + expected [i].OuterXml + " bug got " + actual [i].OuterXml);
  94. }
  95. }
  96. [Test]
  97. public void GetInnerXml ()
  98. {
  99. // Always returns null
  100. Assert.IsNull (transform.UnprotectedGetInnerXml ());
  101. }
  102. private XmlDocument GetDoc ()
  103. {
  104. 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>";
  105. string test = "<Envelope> " + dsig + " </Envelope>";
  106. XmlDocument doc = new XmlDocument ();
  107. doc.LoadXml (test);
  108. return doc;
  109. }
  110. [Test]
  111. public void LoadInputAsXmlDocument ()
  112. {
  113. XmlDocument doc = GetDoc ();
  114. transform.LoadInput (doc);
  115. object o = transform.GetOutput ();
  116. Assert.AreEqual (doc, o, "EnvelopedSignature result");
  117. }
  118. [Test]
  119. public void LoadInputAsXmlNodeList ()
  120. {
  121. XmlDocument doc = GetDoc ();
  122. transform.LoadInput (doc.ChildNodes);
  123. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  124. AssertEquals (doc.ChildNodes, xnl, "EnvelopedSignature result");
  125. }
  126. }
  127. }