DSASignatureFormatterTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // DSASignatureFormatterTest.cs - NUnit Test Cases for DSASignatureFormatter
  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;
  12. using System.Security.Cryptography;
  13. namespace MonoTests.System.Security.Cryptography {
  14. public class DSASignatureFormatterTest : TestCase {
  15. protected DSASignatureFormatter fmt;
  16. protected static DSA dsa;
  17. protected static RSA rsa;
  18. protected override void SetUp ()
  19. {
  20. fmt = new DSASignatureFormatter ();
  21. // key generation is VERY long so one time is enough
  22. if (dsa == null)
  23. dsa = DSA.Create ();
  24. if (rsa == null)
  25. rsa = RSA.Create ();
  26. }
  27. protected override void TearDown () {}
  28. public void TestConstructors ()
  29. {
  30. // empty constructor
  31. DSASignatureFormatter fmt = new DSASignatureFormatter ();
  32. AssertNotNull ("DSASignatureFormatter()", fmt);
  33. // AsymmetricAlgorithm constructor (with null)
  34. fmt = new DSASignatureFormatter (null);
  35. AssertNotNull ("DSASignatureFormatter(null)", fmt);
  36. // AsymmetricAlgorithm constructor (with DSA)
  37. fmt = new DSASignatureFormatter (dsa);
  38. AssertNotNull ("DSASignatureFormatter(dsa)", fmt);
  39. // AsymmetricAlgorithm constructor (with RSA)
  40. try {
  41. fmt = new DSASignatureFormatter (rsa);
  42. Fail ("Expected InvalidCastException but got none");
  43. }
  44. catch (InvalidCastException) {
  45. // this is expected
  46. }
  47. catch (Exception e) {
  48. Fail ("Expected InvalidCastException but got " + e.ToString ());
  49. }
  50. }
  51. public void TestSetHash ()
  52. {
  53. // null is ok
  54. try {
  55. fmt.SetHashAlgorithm (null);
  56. }
  57. catch (ArgumentNullException) {
  58. // do nothing, this is what we expect
  59. }
  60. catch (Exception e) {
  61. Fail ("Expected ArgumentNullException but got " + e.ToString ());
  62. }
  63. // SHA1
  64. try {
  65. fmt.SetHashAlgorithm ("SHA1");
  66. }
  67. catch (Exception e) {
  68. Fail ("Unexpected exception: " + e.ToString ());
  69. }
  70. // MD5 (bad)
  71. try {
  72. fmt.SetHashAlgorithm ("MD5");
  73. }
  74. catch (CryptographicUnexpectedOperationException) {
  75. // do nothing, this is what we expect
  76. }
  77. catch (Exception e) {
  78. Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
  79. }
  80. }
  81. public void TestSetKey () {
  82. // here null is ok
  83. try {
  84. fmt.SetKey (null);
  85. }
  86. catch (Exception e) {
  87. Fail ("Unexpected exception: " + e.ToString ());
  88. }
  89. // RSA (bad)
  90. try {
  91. fmt.SetKey (rsa);
  92. Fail ("Expected InvalidCastException but got none");
  93. }
  94. catch (InvalidCastException) {
  95. // do nothing, this is what we expect
  96. }
  97. catch (Exception e) {
  98. Fail ("Expected InvalidCastException but got: " + e.ToString ());
  99. }
  100. // DSA
  101. try {
  102. fmt.SetKey (dsa);
  103. }
  104. catch (Exception e) {
  105. Fail ("Unexpected exception: " + e.ToString ());
  106. }
  107. }
  108. // note: There's a bug in MS Framework where you can't re-import a key into
  109. // the same object
  110. public void TestSignature ()
  111. {
  112. byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
  113. byte[] sign = null;
  114. // no keypair
  115. try {
  116. sign = fmt.CreateSignature (hash);
  117. }
  118. catch (CryptographicUnexpectedOperationException) {
  119. // do nothing, this is what we expect
  120. }
  121. catch (Exception e) {
  122. Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
  123. }
  124. // try a keypair without the private key
  125. dsa.ImportParameters (AllTests.GetKey (false));
  126. fmt.SetKey (dsa);
  127. try {
  128. sign = fmt.CreateSignature (hash);
  129. Fail ("Expected CryptographicException but got none");
  130. }
  131. catch (CryptographicException) {
  132. // do nothing, this is what we expect
  133. }
  134. catch (Exception e) {
  135. Fail ("Expected CryptographicException but got " + e.ToString ());
  136. }
  137. // complete keypair
  138. dsa.ImportParameters (AllTests.GetKey (true));
  139. fmt.SetKey (dsa);
  140. // null hash
  141. try {
  142. byte[] h = null; // overloaded method
  143. sign = fmt.CreateSignature (h);
  144. Fail ("Expected ArgumentNullException but got none");
  145. }
  146. catch (ArgumentNullException) {
  147. // do nothing, this is what we expect
  148. }
  149. catch (Exception e) {
  150. Fail ("Expected ArgumentNullException but got " + e.ToString ());
  151. }
  152. // valid
  153. sign = fmt.CreateSignature (hash);
  154. Assert ("verified signature", dsa.VerifySignature (hash, sign));
  155. }
  156. }
  157. }