2 // DSASignatureFormatterTest.cs - NUnit Test Cases for DSASignatureFormatter
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 using NUnit.Framework;
12 using System.Security;
13 using System.Security.Cryptography;
15 namespace MonoTests.System.Security.Cryptography {
17 public class DSASignatureFormatterTest : TestCase {
18 protected DSASignatureFormatter fmt;
19 protected static DSA dsa;
20 protected static RSA rsa;
22 protected override void SetUp ()
24 fmt = new DSASignatureFormatter ();
25 // key generation is VERY long so one time is enough
32 protected override void TearDown () {}
34 public void TestConstructors ()
37 DSASignatureFormatter fmt = new DSASignatureFormatter ();
38 AssertNotNull ("DSASignatureFormatter()", fmt);
39 // AsymmetricAlgorithm constructor (with null)
40 fmt = new DSASignatureFormatter (null);
41 AssertNotNull ("DSASignatureFormatter(null)", fmt);
42 // AsymmetricAlgorithm constructor (with DSA)
43 fmt = new DSASignatureFormatter (dsa);
44 AssertNotNull ("DSASignatureFormatter(dsa)", fmt);
45 // AsymmetricAlgorithm constructor (with RSA)
47 fmt = new DSASignatureFormatter (rsa);
48 Fail ("Expected InvalidCastException but got none");
50 catch (InvalidCastException) {
54 Fail ("Expected InvalidCastException but got " + e.ToString ());
58 public void TestSetHash ()
62 fmt.SetHashAlgorithm (null);
64 catch (ArgumentNullException) {
65 // do nothing, this is what we expect
68 Fail ("Expected ArgumentNullException but got " + e.ToString ());
72 fmt.SetHashAlgorithm ("SHA1");
75 Fail ("Unexpected exception: " + e.ToString ());
79 fmt.SetHashAlgorithm ("MD5");
81 catch (CryptographicUnexpectedOperationException) {
82 // do nothing, this is what we expect
85 Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
89 public void TestSetKey () {
95 Fail ("Unexpected exception: " + e.ToString ());
100 Fail ("Expected InvalidCastException but got none");
102 catch (InvalidCastException) {
103 // do nothing, this is what we expect
105 catch (Exception e) {
106 Fail ("Expected InvalidCastException but got: " + e.ToString ());
112 catch (Exception e) {
113 Fail ("Unexpected exception: " + e.ToString ());
117 // note: There's a bug in MS Framework where you can't re-import a key into
119 public void TestSignature ()
121 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
125 sign = fmt.CreateSignature (hash);
127 catch (CryptographicUnexpectedOperationException) {
128 // do nothing, this is what we expect
130 catch (Exception e) {
131 Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
134 // try a keypair without the private key
135 dsa.ImportParameters (AllTests.GetKey (false));
138 sign = fmt.CreateSignature (hash);
139 Fail ("Expected CryptographicException but got none");
141 catch (CryptographicException) {
142 // do nothing, this is what we expect
144 catch (Exception e) {
145 Fail ("Expected CryptographicException but got " + e.ToString ());
149 dsa.ImportParameters (AllTests.GetKey (true));
154 byte[] h = null; // overloaded method
155 sign = fmt.CreateSignature (h);
156 Fail ("Expected ArgumentNullException but got none");
158 catch (ArgumentNullException) {
159 // do nothing, this is what we expect
161 catch (Exception e) {
162 Fail ("Expected ArgumentNullException but got " + e.ToString ());
166 sign = fmt.CreateSignature (hash);
167 Assert ("verified signature", dsa.VerifySignature (hash, sign));