2 // SignatureTest.cs - NUnit Test Cases for SignedXml
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 using System.Security.Cryptography;
13 using System.Security.Cryptography.Xml;
16 using NUnit.Framework;
18 namespace MonoTests.System.Security.Cryptography.Xml {
21 public class SignatureTest : Assertion {
23 protected Signature signature;
26 protected void SetUp ()
28 signature = new Signature ();
32 [ExpectedException (typeof (CryptographicException))]
33 public void Signature1 ()
35 // empty - missing SignedInfo
36 XmlElement xel = signature.GetXml ();
40 [ExpectedException (typeof (CryptographicException))]
41 public void Signature2 ()
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 // no reference element are present
48 XmlElement xel = signature.GetXml ();
54 string expected = "<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>";
55 XmlDocument doc = new XmlDocument ();
56 doc.LoadXml (expected);
57 signature.LoadXml (doc.DocumentElement);
58 string result = signature.GetXml ().OuterXml;
59 AssertCrypto.AssertXmlEquals ("Load", expected, result);
63 [ExpectedException (typeof (CryptographicException))]
64 public void LoadXmlMalformed1 ()
66 SignedXml s = new SignedXml ();
67 XmlDocument doc = new XmlDocument ();
68 doc.LoadXml ("<root/>");
69 s.LoadXml (doc.DocumentElement);
73 [ExpectedException (typeof (CryptographicException))]
74 public void LoadXmlMalformed2 ()
76 SignedXml s = new SignedXml ();
77 XmlDocument doc = new XmlDocument ();
78 doc.LoadXml ("<ds:Signature xmlns:ds='http://www.w3.org/2000/09/xmldsig#'><foo/><bar/></ds:Signature>");
79 s.LoadXml (doc.DocumentElement);