XmlDsigEnvelopedSignatureTransformTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // XmlDsigEnvelopedSignatureTransformTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2004 Novell Inc.
  8. //
  9. #if !MOBILE
  10. using System;
  11. using System.IO;
  12. using System.Security.Cryptography;
  13. using System.Security.Cryptography.Xml;
  14. using System.Text;
  15. using System.Xml;
  16. using System.Xml.Xsl;
  17. using NUnit.Framework;
  18. namespace MonoTests.System.Security.Cryptography.Xml {
  19. // Note: GetInnerXml is protected in XmlDsigEnvelopedSignatureTransform making it
  20. // difficult to test properly. This class "open it up" :-)
  21. public class UnprotectedXmlDsigEnvelopedSignatureTransform : XmlDsigEnvelopedSignatureTransform {
  22. public UnprotectedXmlDsigEnvelopedSignatureTransform ()
  23. {
  24. }
  25. public UnprotectedXmlDsigEnvelopedSignatureTransform (bool includeComments)
  26. : base (includeComments)
  27. {
  28. }
  29. public XmlNodeList UnprotectedGetInnerXml ()
  30. {
  31. return base.GetInnerXml ();
  32. }
  33. }
  34. [TestFixture]
  35. public class XmlDsigEnvelopedSignatureTransformTest {
  36. private UnprotectedXmlDsigEnvelopedSignatureTransform transform;
  37. [SetUp]
  38. public void SetUp ()
  39. {
  40. transform = new UnprotectedXmlDsigEnvelopedSignatureTransform ();
  41. }
  42. [Test] // ctor ()
  43. public void Constructor1 ()
  44. {
  45. CheckProperties (transform);
  46. }
  47. [Test] // ctor (Boolean)
  48. public void Constructor2 ()
  49. {
  50. transform = new UnprotectedXmlDsigEnvelopedSignatureTransform (true);
  51. CheckProperties (transform);
  52. transform = new UnprotectedXmlDsigEnvelopedSignatureTransform (false);
  53. CheckProperties (transform);
  54. }
  55. void CheckProperties (XmlDsigEnvelopedSignatureTransform transform)
  56. {
  57. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#enveloped-signature",
  58. transform.Algorithm, "Algorithm");
  59. Type [] input = transform.InputTypes;
  60. Assert.AreEqual (3, input.Length, "Input Length");
  61. // check presence of every supported input types
  62. bool istream = false;
  63. bool ixmldoc = false;
  64. bool ixmlnl = false;
  65. foreach (Type t in input) {
  66. if (t == typeof (XmlDocument))
  67. ixmldoc = true;
  68. if (t == typeof (XmlNodeList))
  69. ixmlnl = true;
  70. if (t == typeof (Stream))
  71. istream = true;
  72. }
  73. Assert.IsTrue (istream, "Input Stream");
  74. Assert.IsTrue (ixmldoc, "Input XmlDocument");
  75. Assert.IsTrue (ixmlnl, "Input XmlNodeList");
  76. Type [] output = transform.OutputTypes;
  77. Assert.AreEqual (2, output.Length, "Output Length");
  78. // check presence of every supported output types
  79. bool oxmlnl = false;
  80. bool oxmldoc = false;
  81. foreach (Type t in output) {
  82. if (t == typeof (XmlNodeList))
  83. oxmlnl = true;
  84. if (t == typeof (XmlDocument))
  85. oxmldoc = true;
  86. }
  87. Assert.IsTrue (oxmlnl, "Output XmlNodeList");
  88. Assert.IsTrue (oxmldoc, "Output XmlDocument");
  89. }
  90. void AssertEquals (XmlNodeList expected, XmlNodeList actual, string msg)
  91. {
  92. for (int i = 0; i < expected.Count; i++) {
  93. if (expected [i].OuterXml != actual [i].OuterXml)
  94. Assert.Fail (msg + " [" + i + "] expected " + expected [i].OuterXml + " bug got " + actual [i].OuterXml);
  95. }
  96. }
  97. [Test]
  98. public void GetInnerXml ()
  99. {
  100. // Always returns null
  101. Assert.IsNull (transform.UnprotectedGetInnerXml ());
  102. }
  103. private XmlDocument GetDoc ()
  104. {
  105. 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>";
  106. string test = "<Envelope> " + dsig + " </Envelope>";
  107. XmlDocument doc = new XmlDocument ();
  108. doc.LoadXml (test);
  109. return doc;
  110. }
  111. [Test]
  112. public void LoadInputAsXmlDocument ()
  113. {
  114. XmlDocument doc = GetDoc ();
  115. transform.LoadInput (doc);
  116. object o = transform.GetOutput ();
  117. Assert.AreEqual (doc, o, "EnvelopedSignature result");
  118. }
  119. [Test]
  120. public void LoadInputAsXmlNodeList ()
  121. {
  122. XmlDocument doc = GetDoc ();
  123. transform.LoadInput (doc.ChildNodes);
  124. XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
  125. AssertEquals (doc.ChildNodes, xnl, "EnvelopedSignature result");
  126. }
  127. }
  128. }
  129. #endif