SignatureTest.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // SignatureTest.cs - NUnit Test Cases for SignedXml
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // (C) 2004 Novell (http://www.novell.com)
  9. //
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Security.Cryptography.Xml;
  13. using System.Xml;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Security.Cryptography.Xml {
  16. [TestFixture]
  17. public class SignatureTest : Assertion {
  18. protected Signature signature;
  19. [SetUp]
  20. protected void SetUp ()
  21. {
  22. signature = new Signature ();
  23. }
  24. [Test]
  25. [ExpectedException (typeof (CryptographicException))]
  26. public void Signature1 ()
  27. {
  28. // empty - missing SignedInfo
  29. XmlElement xel = signature.GetXml ();
  30. }
  31. [Test]
  32. [ExpectedException (typeof (CryptographicException))]
  33. public void Signature2 ()
  34. {
  35. SignedInfo info = new SignedInfo ();
  36. signature.SignedInfo = info;
  37. info.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
  38. signature.SignatureValue = new byte [128];
  39. // no reference element are present
  40. XmlElement xel = signature.GetXml ();
  41. }
  42. [Test]
  43. public void Load ()
  44. {
  45. string test1 = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>A6XuE8Cy9iOffRXaW9b0+dUcMUJQnlmwLsiqtQnADbCtZXnXAaeJ6nGnQ4Mm0IGi0AJc7/2CoJReXl7iW4hltmFguG1e3nl0VxCyCTHKGOCo1u8R3K+B1rTaenFbSxs42EM7/D9KETsPlzfYfis36yM3PqatiCUOsoMsAiMGzlc=</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>tI8QYIpbG/m6JLyvP+S3X8mzcaAIayxomyTimSh9UCpEucRnGvLw0P73uStNpiF7wltTZA1HEsv+Ha39dY/0j/Wiy3RAodGDRNuKQao1wu34aNybZ673brbsbHFUfw/o7nlKD2xO84fbajBZmKtBBDy63NHt+QL+grSrREPfCTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
  46. // Mono's result is XML equivalent but a little different from the original
  47. string test2 = test1.Replace ("<Object Id=\"MyObjectId\">", "<Object Id=\"MyObjectId\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\">");
  48. XmlDocument doc = new XmlDocument ();
  49. doc.LoadXml (test1);
  50. signature.LoadXml (doc.DocumentElement);
  51. string result = signature.GetXml ().OuterXml;
  52. Assert ("Load", ((test1 == result) || (test2 == result)));
  53. }
  54. [Test]
  55. [ExpectedException (typeof (CryptographicException))]
  56. public void LoadXmlMalformed1 ()
  57. {
  58. SignedXml s = new SignedXml ();
  59. XmlDocument doc = new XmlDocument ();
  60. doc.LoadXml ("<root/>");
  61. s.LoadXml (doc.DocumentElement);
  62. }
  63. [Test]
  64. [ExpectedException (typeof (CryptographicException))]
  65. public void LoadXmlMalformed2 ()
  66. {
  67. SignedXml s = new SignedXml ();
  68. XmlDocument doc = new XmlDocument ();
  69. doc.LoadXml ("<ds:Signature xmlns:ds='http://www.w3.org/2000/09/xmldsig#'><foo/><bar/></ds:Signature>");
  70. s.LoadXml (doc.DocumentElement);
  71. }
  72. }
  73. }