SignedInfoTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // SignedInfoTest.cs - NUnit Test Cases for SignedInfo
  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 SignedInfoTest : TestCase {
  16. public SignedInfoTest () : base ("System.Security.Cryptography.Xml.SignedInfo testsuite") {}
  17. public SignedInfoTest (string name) : base (name) {}
  18. protected SignedInfo info;
  19. protected override void SetUp ()
  20. {
  21. info = new SignedInfo ();
  22. }
  23. protected override void TearDown () {}
  24. public static ITest Suite {
  25. get {
  26. return new TestSuite (typeof (SignedInfoTest));
  27. }
  28. }
  29. public void TestEmpty ()
  30. {
  31. AssertEquals ("CanonicalizationMethod", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod);
  32. // NON-WORKING ??? AssertEquals ("Id", "", info.Id);
  33. AssertNotNull ("References", info.References);
  34. AssertEquals ("References.Count", 0, info.References.Count);
  35. // NON-WORKING ??? AssertEquals ("SignatureLength", "", info.SignatureLength);
  36. // NON-WORKING ??? AssertEquals ("SignatureMethod", "", info.SignatureMethod);
  37. AssertEquals ("ToString()", "System.Security.Cryptography.Xml.SignedInfo", info.ToString ());
  38. try {
  39. string xml = info.GetXml().OuterXml;
  40. Fail ("Empty Xml: Expected CryptographicException but got none");
  41. }
  42. catch (CryptographicException) {
  43. // this is expected
  44. }
  45. catch (Exception e) {
  46. Fail ("Empty Xml: Expected CryptographicException but got: " + e.ToString ());
  47. }
  48. }
  49. public void TestProperties ()
  50. {
  51. info.CanonicalizationMethod = "http://www.go-mono.com/";
  52. AssertEquals ("CanonicalizationMethod", "http://www.go-mono.com/", info.CanonicalizationMethod);
  53. info.Id = "Mono::";
  54. AssertEquals ("Id", "Mono::", info.Id);
  55. }
  56. public void TestReferences ()
  57. {
  58. Reference r1 = new Reference ();
  59. r1.Uri = "http://www.go-mono.com/";
  60. r1.AddTransform (new XmlDsigBase64Transform ());
  61. info.AddReference (r1);
  62. AssertEquals ("References.Count 1", 1, info.References.Count);
  63. Reference r2 = new Reference ("http://www.motus.com/");
  64. r2.AddTransform (new XmlDsigBase64Transform ());
  65. info.AddReference (r2);
  66. AssertEquals ("References.Count 2", 2, info.References.Count);
  67. info.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
  68. }
  69. public void TestLoad ()
  70. {
  71. 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>";
  72. XmlDocument doc = new XmlDocument ();
  73. doc.LoadXml (xml);
  74. info.LoadXml (doc.DocumentElement);
  75. AssertEquals ("LoadXml", xml, (info.GetXml ().OuterXml));
  76. AssertEquals ("LoadXml-C14N", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod);
  77. AssertEquals ("LoadXml-Algo", "http://www.w3.org/2000/09/xmldsig#rsa-sha1", info.SignatureMethod);
  78. AssertEquals ("LoadXml-Ref1", 1, info.References.Count);
  79. }
  80. // there are many (documented) not supported methods in SignedInfo
  81. public void TestNotSupported ()
  82. {
  83. try {
  84. int n = info.Count;
  85. Fail ("Count: Expected NotSupportedException but got none");
  86. }
  87. catch (NotSupportedException) {
  88. // this is expected
  89. }
  90. catch (Exception e) {
  91. Fail ("Count: Expected NotSupportedException but got: " + e.ToString ());
  92. }
  93. try {
  94. bool b = info.IsReadOnly;
  95. Fail ("IsReadOnly: Expected NotSupportedException but got none");
  96. }
  97. catch (NotSupportedException) {
  98. // this is expected
  99. }
  100. catch (Exception e) {
  101. Fail ("IsReadOnly: Expected NotSupportedException but got: " + e.ToString ());
  102. }
  103. try {
  104. bool b = info.IsSynchronized;
  105. Fail ("IsSynchronized: Expected NotSupportedException but got none");
  106. }
  107. catch (NotSupportedException) {
  108. // this is expected
  109. }
  110. catch (Exception e) {
  111. Fail ("IsSynchronized: Expected NotSupportedException but got: " + e.ToString ());
  112. }
  113. try {
  114. object o = info.SyncRoot;
  115. Fail ("SyncRoot: Expected NotSupportedException but got none");
  116. }
  117. catch (NotSupportedException) {
  118. // this is expected
  119. }
  120. catch (Exception e) {
  121. Fail ("SyncRoot: Expected NotSupportedException but got: " + e.ToString ());
  122. }
  123. try {
  124. info.CopyTo (null, 0);
  125. Fail ("CopyTo: Expected NotSupportedException but got none");
  126. }
  127. catch (NotSupportedException) {
  128. // this is expected
  129. }
  130. catch (Exception e) {
  131. Fail ("CopyTo: Expected NotSupportedException but got: " + e.ToString ());
  132. }
  133. }
  134. }
  135. }