SignedInfoTest.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // SignedInfoTest.cs - NUnit Test Cases for SignedInfo
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System;
  10. using System.Security.Cryptography;
  11. using System.Security.Cryptography.Xml;
  12. using System.Xml;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Security.Cryptography.Xml {
  15. [TestFixture]
  16. public class SignedInfoTest : Assertion {
  17. protected SignedInfo info;
  18. [SetUp]
  19. protected void SetUp ()
  20. {
  21. info = new SignedInfo ();
  22. }
  23. [Test]
  24. public void Empty ()
  25. {
  26. AssertEquals ("CanonicalizationMethod", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod);
  27. AssertNull ("Id", info.Id);
  28. AssertNotNull ("References", info.References);
  29. AssertEquals ("References.Count", 0, info.References.Count);
  30. AssertNull ("SignatureLength", info.SignatureLength);
  31. AssertNull ("SignatureMethod", info.SignatureMethod);
  32. AssertEquals ("ToString()", "System.Security.Cryptography.Xml.SignedInfo", info.ToString ());
  33. }
  34. [Test]
  35. [ExpectedException (typeof (CryptographicException))]
  36. public void EmptyException ()
  37. {
  38. string xml = info.GetXml ().OuterXml;
  39. }
  40. [Test]
  41. public void Properties ()
  42. {
  43. info.CanonicalizationMethod = "http://www.go-mono.com/";
  44. AssertEquals ("CanonicalizationMethod", "http://www.go-mono.com/", info.CanonicalizationMethod);
  45. info.Id = "Mono::";
  46. AssertEquals ("Id", "Mono::", info.Id);
  47. }
  48. [Test]
  49. public void References ()
  50. {
  51. Reference r1 = new Reference ();
  52. r1.Uri = "http://www.go-mono.com/";
  53. r1.AddTransform (new XmlDsigBase64Transform ());
  54. info.AddReference (r1);
  55. AssertEquals ("References.Count 1", 1, info.References.Count);
  56. Reference r2 = new Reference ("http://www.motus.com/");
  57. r2.AddTransform (new XmlDsigBase64Transform ());
  58. info.AddReference (r2);
  59. AssertEquals ("References.Count 2", 2, info.References.Count);
  60. info.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
  61. }
  62. [Test]
  63. public void Load ()
  64. {
  65. 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>";
  66. XmlDocument doc = new XmlDocument ();
  67. doc.LoadXml (xml);
  68. info.LoadXml (doc.DocumentElement);
  69. AssertEquals ("LoadXml", xml, (info.GetXml ().OuterXml));
  70. AssertEquals ("LoadXml-C14N", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod);
  71. AssertEquals ("LoadXml-Algo", "http://www.w3.org/2000/09/xmldsig#rsa-sha1", info.SignatureMethod);
  72. AssertEquals ("LoadXml-Ref1", 1, info.References.Count);
  73. }
  74. // there are many (documented) not supported methods in SignedInfo
  75. [Test]
  76. [ExpectedException (typeof (NotSupportedException))]
  77. public void NotSupportedCount ()
  78. {
  79. int n = info.Count;
  80. }
  81. [Test]
  82. [ExpectedException (typeof (NotSupportedException))]
  83. public void NotSupportedIsReadOnly ()
  84. {
  85. bool b = info.IsReadOnly;
  86. }
  87. [Test]
  88. [ExpectedException (typeof (NotSupportedException))]
  89. public void NotSupportedIsSynchronized ()
  90. {
  91. bool b = info.IsSynchronized;
  92. }
  93. [Test]
  94. [ExpectedException (typeof (NotSupportedException))]
  95. public void NotSupportedSyncRoot ()
  96. {
  97. object o = info.SyncRoot;
  98. }
  99. [Test]
  100. [ExpectedException (typeof (NotSupportedException))]
  101. public void NotSupportedCopyTo ()
  102. {
  103. info.CopyTo (null, 0);
  104. }
  105. // from phaos testcase
  106. const string xmlForGetXml = @"<player bats=""left"" id=""10012"" throws=""right"">
  107. <!-- Here&apos;s a comment -->
  108. <name>Alfonso Soriano</name>
  109. <position>2B</position>
  110. <team>New York Yankees</team>
  111. <dsig:Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"" xmlns:dsig=""http://www.w3.org/2000/09/xmldsig#"">"
  112. + @"<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""/>"
  113. + @"<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>";
  114. [Test]
  115. public void GetXmlWithoutSetProperty ()
  116. {
  117. 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>";
  118. XmlDocument doc = new XmlDocument ();
  119. doc.LoadXml (xmlForGetXml);
  120. SignedInfo sig = new SignedInfo ();
  121. sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
  122. XmlElement el = sig.GetXml ();
  123. AssertEquals ("#GetXmlWOSetProperty.document", doc, el.OwnerDocument);
  124. AssertEquals ("#GetXmlWOSetProperty.outerxml", result, el.OuterXml);
  125. }
  126. [Test]
  127. public void GetXmlWithSetProperty ()
  128. {
  129. XmlDocument doc = new XmlDocument ();
  130. doc.LoadXml (xmlForGetXml);
  131. SignedInfo sig = new SignedInfo ();
  132. sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
  133. sig.CanonicalizationMethod = "urn:foo";
  134. XmlElement el = sig.GetXml ();
  135. Assert ("#GetXmlWithSetProperty.document", doc != el.OwnerDocument);
  136. }
  137. [Test] // never fails
  138. public void EmptyReferenceWithoutSetProperty ()
  139. {
  140. XmlDocument doc = new XmlDocument ();
  141. doc.LoadXml (xmlForGetXml);
  142. XmlNode n = doc.SelectSingleNode ("//*[local-name()='Reference']");
  143. n.ParentNode.RemoveChild (n);
  144. SignedInfo sig = new SignedInfo ();
  145. sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
  146. XmlElement el = sig.GetXml ();
  147. }
  148. [Test]
  149. [ExpectedException (typeof (CryptographicException))]
  150. public void EmptyReferenceWithSetProperty ()
  151. {
  152. XmlDocument doc = new XmlDocument ();
  153. doc.LoadXml (xmlForGetXml);
  154. XmlNode n = doc.SelectSingleNode ("//*[local-name()='Reference']");
  155. n.ParentNode.RemoveChild (n);
  156. SignedInfo sig = new SignedInfo ();
  157. sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
  158. sig.CanonicalizationMethod = "urn:foo";
  159. XmlElement el = sig.GetXml ();
  160. }
  161. }
  162. }