2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / SignedInfoTest.cs
1 //
2 // SignedInfoTest.cs - NUnit Test Cases for SignedInfo
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10
11 using System;
12 using System.Security.Cryptography;
13 using System.Security.Cryptography.Xml;
14 using System.Xml;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Security.Cryptography.Xml {
19
20         [TestFixture]
21         public class SignedInfoTest {
22
23                 protected SignedInfo info;
24
25                 [SetUp]
26                 protected void SetUp () 
27                 {
28                         info = new SignedInfo ();
29                 }
30
31                 [Test]
32                 public void Empty () 
33                 {
34                         Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod, "CanonicalizationMethod");
35                         Assert.IsNull (info.Id, "Id");
36                         Assert.IsNotNull (info.References, "References");
37                         Assert.AreEqual (0, info.References.Count, "References.Count");
38                         Assert.IsNull (info.SignatureLength, "SignatureLength");
39                         Assert.IsNull (info.SignatureMethod, "SignatureMethod");
40                         Assert.AreEqual ("System.Security.Cryptography.Xml.SignedInfo", info.ToString (), "ToString()");
41                 }
42
43                 [Test]
44                 [ExpectedException (typeof (CryptographicException))]
45                 public void EmptyException () 
46                 {
47                         string xml = info.GetXml ().OuterXml;
48                 }
49
50                 [Test]
51                 public void Properties () 
52                 {
53                         info.CanonicalizationMethod = "http://www.go-mono.com/";
54                         Assert.AreEqual ("http://www.go-mono.com/", info.CanonicalizationMethod, "CanonicalizationMethod");
55                         info.Id = "Mono::";
56                         Assert.AreEqual ("Mono::", info.Id, "Id");
57                 }
58
59                 [Test]
60                 public void References () 
61                 {
62                         Reference r1 = new Reference ();
63                         r1.Uri = "http://www.go-mono.com/";
64                         r1.AddTransform (new XmlDsigBase64Transform ());
65                         info.AddReference (r1);
66                         Assert.AreEqual (1, info.References.Count, "References.Count 1");
67
68                         Reference r2 = new Reference ("http://www.motus.com/");
69                         r2.AddTransform (new XmlDsigBase64Transform ());
70                         info.AddReference (r2);
71                         Assert.AreEqual (2, info.References.Count, "References.Count 2");
72
73                         info.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
74                 }
75
76                 [Test]
77                 public void Load () 
78                 {
79                         string xml = "<SignedInfo 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#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo>";
80                         XmlDocument doc = new XmlDocument ();
81                         doc.LoadXml (xml);
82                         info.LoadXml (doc.DocumentElement);
83                         Assert.AreEqual (xml, (info.GetXml ().OuterXml), "LoadXml");
84                         Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod, "LoadXml-C14N");
85                         Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", info.SignatureMethod, "LoadXml-Algo");
86                         Assert.AreEqual (1, info.References.Count, "LoadXml-Ref1");
87                 }
88
89                 // there are many (documented) not supported methods in SignedInfo
90
91                 [Test]
92                 [ExpectedException (typeof (NotSupportedException))]
93                 public void NotSupportedCount () 
94                 {
95                         int n = info.Count;
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (NotSupportedException))]
100                 public void NotSupportedIsReadOnly () 
101                 {
102                         bool b = info.IsReadOnly;
103                 }
104
105                 [Test]
106                 [ExpectedException (typeof (NotSupportedException))]
107                 public void NotSupportedIsSynchronized () 
108                 {
109                         bool b = info.IsSynchronized;
110                 }
111
112                 [Test]
113                 [ExpectedException (typeof (NotSupportedException))]
114                 public void NotSupportedSyncRoot () 
115                 {
116                         object o = info.SyncRoot;
117                 }
118
119                 [Test]
120                 [ExpectedException (typeof (NotSupportedException))]
121                 public void NotSupportedCopyTo () 
122                 {
123                         info.CopyTo (null, 0);
124                 }
125                 
126                 // from phaos testcase
127                 const string xmlForGetXml = @"<player bats=""left"" id=""10012"" throws=""right"">
128         <!-- Here&apos;s a comment -->
129         <name>Alfonso Soriano</name>
130         <position>2B</position>
131         <team>New York Yankees</team>
132 <dsig:Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"" xmlns:dsig=""http://www.w3.org/2000/09/xmldsig#"">"
133 + @"<dsig:SignedInfo><dsig:CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-withcomments-20010315""/><dsig:SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#rsa-sha1""/>"
134 + @"<dsig:Reference URI=""""><dsig:Transforms><dsig:Transform Algorithm=""http://www.w3.org/2000/09/xmldsig#enveloped-signature""/></dsig:Transforms><dsig:DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1""/><dsig:DigestValue>nDF2V/bzRd0VE3EwShWtsBzTEDc=</dsig:DigestValue></dsig:Reference></dsig:SignedInfo><dsig:SignatureValue>fbye4Xm//RPUTsLd1dwJPo0gPZYX6gVYCEB/gz2348EARNk/nCCch1fFfpuqAGMKg4ayVC0yWkUyE5V4QB33jaGlh9wuNQSjxs6TIvFwSsT+0ioDgVgFv0gVeasbyNL4rFEHuAWL8QKwDT9L6b2wUvJC90DmpBs9GMR2jTZIWlM=</dsig:SignatureValue><dsig:KeyInfo><dsig:X509Data><dsig:X509Certificate>MIIC0DCCAjmgAwIBAgIDD0JBMA0GCSqGSIb3DQEBBAUAMHwxCzAJBgNVBAYTAlVTMREwDwYDVQQIEwhOZXcgWW9yazERMA8GA1UEBxMITmV3IFlvcmsxGTAXBgNVBAoTEFBoYW9zIFRlY2hub2xvZ3kxFDASBgNVBAsTC0VuZ2luZWVyaW5nMRYwFAYDVQQDEw1UZXN0IENBIChSU0EpMB4XDTAyMDQyOTE5MTY0MFoXDTEyMDQyNjE5MTY0MFowgYAxCzAJBgNVBAYTAlVTMREwDwYDVQQIEwhOZXcgWW9yazERMA8GA1UEBxMITmV3IFlvcmsxGTAXBgNVBAoTEFBoYW9zIFRlY2hub2xvZ3kxFDASBgNVBAsTC0VuZ2luZWVyaW5nMRowGAYDVQQDExFUZXN0IENsaWVudCAoUlNBKTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAgIb6nAB9oS/AI5jIj6WymvQhRxiMlE07G4abmMliYi5zWzvaFE2tnU+RZIBgtoXcgDEIU/vsLQut7nzCn9mHxC8JEaV4D4U91j64AyZakShqJw7qjJfqUxxPL0yJv2oFiouPDjGuJ9JPi0NrsZq+yfWfM54s4b9SNkcOIVMybZUCAwEAAaNbMFkwDAYDVR0TAQH/BAIwADAPBgNVHQ8BAf8EBQMDB9gAMBkGA1UdEQQSMBCBDnRlY2hAcGhhb3MuY29tMB0GA1UdDgQWBBQT58rBCxPmVLeZaYGRqVROnQlFbzANBgkqhkiG9w0BAQQFAAOBgQCxbCovFST25t+ryN1RipqozxJQcguKfeCwbfgBNobzcRvoW0kSIf7zi4mtQajDM0NfslFF51/dex5Rn64HmFFshSwSvQQMyf5Cfaqv2XQ60OXq6nAFG6WbHoge6RqfIez2MWDLoSB6plsjKtMmL3mcybBhROtX5GGuLx1NtfhNFQ==</dsig:X509Certificate><dsig:X509IssuerSerial><dsig:X509IssuerName>CN=Test CA (RSA),OU=Engineering,O=Phaos Technology,L=New York,ST=New York,C=US</dsig:X509IssuerName><dsig:X509SerialNumber>1000001</dsig:X509SerialNumber></dsig:X509IssuerSerial><dsig:X509SubjectName>CN=Test Client (RSA),OU=Engineering,O=Phaos Technology,L=New York,ST=New York,C=US</dsig:X509SubjectName><dsig:X509SKI>E+fKwQsT5lS3mWmBkalUTp0JRW8=</dsig:X509SKI></dsig:X509Data></dsig:KeyInfo></dsig:Signature></player>";
135
136                 [Test]
137                 public void GetXmlWithoutSetProperty ()
138                 {
139                         string result = @"<dsig:SignedInfo xmlns:dsig=""http://www.w3.org/2000/09/xmldsig#""><dsig:CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-withcomments-20010315"" /><dsig:SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#rsa-sha1"" /><dsig:Reference URI=""""><dsig:Transforms><dsig:Transform Algorithm=""http://www.w3.org/2000/09/xmldsig#enveloped-signature"" /></dsig:Transforms><dsig:DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><dsig:DigestValue>nDF2V/bzRd0VE3EwShWtsBzTEDc=</dsig:DigestValue></dsig:Reference></dsig:SignedInfo>";
140                         XmlDocument doc = new XmlDocument ();
141                         doc.LoadXml (xmlForGetXml);
142                         SignedInfo sig = new SignedInfo ();
143                         sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
144                         XmlElement el = sig.GetXml ();
145                         Assert.AreEqual (doc, el.OwnerDocument, "#GetXmlWOSetProperty.document");
146                         Assert.AreEqual (result, el.OuterXml, "#GetXmlWOSetProperty.outerxml");
147                 }
148
149                 [Test]
150 #if NET_2_0
151                 // urn:foo is'nt accepted when calling GetXml
152                 [ExpectedException (typeof (CryptographicException))]
153                 [Category ("NotWorking")]
154 #endif
155                 public void GetXmlWithSetProperty ()
156                 {
157                         XmlDocument doc = new XmlDocument ();
158                         doc.LoadXml (xmlForGetXml);
159                         SignedInfo sig = new SignedInfo ();
160                         sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
161                         sig.CanonicalizationMethod = "urn:foo";
162                         XmlElement el = sig.GetXml ();
163                         Assert.IsTrue (doc != el.OwnerDocument, "#GetXmlWithSetProperty.document");
164                 }
165
166                 [Test] // never fails
167                 public void EmptyReferenceWithoutSetProperty ()
168                 {
169                         XmlDocument doc = new XmlDocument ();
170                         doc.LoadXml (xmlForGetXml);
171                         XmlNode n = doc.SelectSingleNode ("//*[local-name()='Reference']");
172                         n.ParentNode.RemoveChild (n);
173
174                         SignedInfo sig = new SignedInfo ();
175                         sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
176                         XmlElement el = sig.GetXml ();
177                 }
178
179                 [Test]
180                 [ExpectedException (typeof (CryptographicException))]
181                 public void EmptyReferenceWithSetProperty ()
182                 {
183                         XmlDocument doc = new XmlDocument ();
184                         doc.LoadXml (xmlForGetXml);
185                         XmlNode n = doc.SelectSingleNode ("//*[local-name()='Reference']");
186                         n.ParentNode.RemoveChild (n);
187
188                         SignedInfo sig = new SignedInfo ();
189                         sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
190                         sig.CanonicalizationMethod = "urn:foo";
191                         XmlElement el = sig.GetXml ();
192                 }
193         }
194 }