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