SignatureTest.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // SignatureTest.cs - NUnit Test Cases for SignedXml
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Security.Cryptography.Xml;
  13. using System.Xml;
  14. namespace MonoTests.System.Security.Cryptography.Xml {
  15. public class SignatureTest : TestCase {
  16. public SignatureTest () : base ("System.Security.Cryptography.Xml.Signature testsuite") {}
  17. public SignatureTest (string name) : base (name) {}
  18. protected Signature signature;
  19. protected override void SetUp ()
  20. {
  21. signature = new Signature ();
  22. }
  23. protected override void TearDown () {}
  24. public static ITest Suite {
  25. get {
  26. return new TestSuite (typeof (SignatureTest));
  27. }
  28. }
  29. public void TestSignature ()
  30. {
  31. XmlElement xel = null;
  32. // empty - missing SignedInfo
  33. try {
  34. xel = signature.GetXml ();
  35. Fail ("Expected CryptographicException but got none");
  36. }
  37. catch (CryptographicException) {
  38. // this is expected
  39. }
  40. catch (Exception e) {
  41. Fail ("Expected CryptographicException but got :" + e.ToString ());
  42. }
  43. SignedInfo info = new SignedInfo ();
  44. signature.SignedInfo = info;
  45. info.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
  46. signature.SignatureValue = new byte [128];
  47. try {
  48. xel = signature.GetXml ();
  49. }
  50. catch (Exception e) {
  51. Fail ("Expected ... but got :" + e.ToString ());
  52. }
  53. }
  54. public void TestLoad ()
  55. {
  56. string value = "<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>";
  57. XmlDocument doc = new XmlDocument ();
  58. doc.LoadXml (value);
  59. signature.LoadXml (doc.DocumentElement);
  60. string s = signature.GetXml ().OuterXml;
  61. AssertEquals ("Load", value, s);
  62. }
  63. }
  64. }