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 {
19 public DSASignatureFormatterTest () : base ("System.Security.Cryptography.DSASignatureFormatter testsuite") {}
20 public DSASignatureFormatterTest (string name) : base (name) {}
22 protected DSASignatureFormatter fmt;
23 protected static DSA dsa;
24 protected static RSA rsa;
26 protected override void SetUp ()
28 fmt = new DSASignatureFormatter ();
29 // key generation is VERY long so one time is enough
36 protected override void TearDown () {}
38 public static ITest Suite {
40 return new TestSuite (typeof (DSASignatureFormatterTest));
44 public void TestConstructors ()
47 DSASignatureFormatter fmt = new DSASignatureFormatter ();
48 AssertNotNull ("DSASignatureFormatter()", fmt);
49 // AsymmetricAlgorithm constructor (with null)
50 fmt = new DSASignatureFormatter (null);
51 AssertNotNull ("DSASignatureFormatter(null)", fmt);
52 // AsymmetricAlgorithm constructor (with DSA)
53 fmt = new DSASignatureFormatter (dsa);
54 AssertNotNull ("DSASignatureFormatter(dsa)", fmt);
55 // AsymmetricAlgorithm constructor (with RSA)
57 fmt = new DSASignatureFormatter (rsa);
58 Fail ("Expected InvalidCastException but got none");
60 catch (InvalidCastException) {
64 Fail ("Expected InvalidCastException but got " + e.ToString ());
68 public void TestSetHash ()
72 fmt.SetHashAlgorithm (null);
74 catch (ArgumentNullException) {
75 // do nothing, this is what we expect
78 Fail ("Expected ArgumentNullException but got " + e.ToString ());
82 fmt.SetHashAlgorithm ("SHA1");
85 Fail ("Unexpected exception: " + e.ToString ());
89 fmt.SetHashAlgorithm ("MD5");
91 catch (CryptographicUnexpectedOperationException) {
92 // do nothing, this is what we expect
95 Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
99 public void TestSetKey () {
104 catch (Exception e) {
105 Fail ("Unexpected exception: " + e.ToString ());
110 Fail ("Expected InvalidCastException but got none");
112 catch (InvalidCastException) {
113 // do nothing, this is what we expect
115 catch (Exception e) {
116 Fail ("Expected InvalidCastException but got: " + e.ToString ());
122 catch (Exception e) {
123 Fail ("Unexpected exception: " + e.ToString ());
127 // note: There's a bug in MS Framework where you can't re-import a key into
129 public void TestSignature ()
131 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
135 sign = fmt.CreateSignature (hash);
137 catch (CryptographicUnexpectedOperationException) {
138 // do nothing, this is what we expect
140 catch (Exception e) {
141 Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
144 // try a keypair without the private key
145 dsa.ImportParameters (AllTests.GetKey (false));
148 sign = fmt.CreateSignature (hash);
149 Fail ("Expected CryptographicException but got none");
151 catch (CryptographicException) {
152 // do nothing, this is what we expect
154 catch (Exception e) {
155 Fail ("Expected CryptographicException but got " + e.ToString ());
159 dsa.ImportParameters (AllTests.GetKey (true));
164 byte[] h = null; // overloaded method
165 sign = fmt.CreateSignature (h);
166 Fail ("Expected ArgumentNullException but got none");
168 catch (ArgumentNullException) {
169 // do nothing, this is what we expect
171 catch (Exception e) {
172 Fail ("Expected ArgumentNullException but got " + e.ToString ());
176 sign = fmt.CreateSignature (hash);
177 Assert ("verified signature", dsa.VerifySignature (hash, sign));