2002-11-16 Sebastien Pouliot <spouliot@videotron.ca>
[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
19         public DSASignatureFormatterTest () : base ("System.Security.Cryptography.DSASignatureFormatter testsuite") {}
20         public DSASignatureFormatterTest (string name) : base (name) {}
21
22         protected DSASignatureFormatter fmt;
23         protected static DSA dsa;
24         protected static RSA rsa;
25
26         protected override void SetUp () 
27         {
28                 fmt = new DSASignatureFormatter ();
29                 // key generation is VERY long so one time is enough
30                 if (dsa == null)
31                         dsa = DSA.Create ();
32                 if (rsa == null)
33                         rsa = RSA.Create ();
34         }
35
36         protected override void TearDown () {}
37
38         public static ITest Suite {
39                 get { 
40                         return new TestSuite (typeof (DSASignatureFormatterTest)); 
41                 }
42         }
43
44         public void TestConstructors () 
45         {
46                 // empty constructor
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)
56                 try {
57                         fmt = new DSASignatureFormatter (rsa);
58                         Fail ("Expected InvalidCastException but got none");
59                 }
60                 catch (InvalidCastException) {
61                         // this is expected
62                 }
63                 catch (Exception e) {
64                         Fail ("Expected InvalidCastException but got " + e.ToString ());
65                 }
66         }
67
68         public void TestSetHash () 
69         {
70                 // null is ok
71                 try {
72                         fmt.SetHashAlgorithm (null);
73                 }
74                 catch (ArgumentNullException) {
75                         // do nothing, this is what we expect
76                 }
77                 catch (Exception e) {
78                         Fail ("Expected ArgumentNullException but got " + e.ToString ());
79                 }
80                 // SHA1
81                 try {
82                         fmt.SetHashAlgorithm ("SHA1");
83                 }
84                 catch (Exception e) {
85                         Fail ("Unexpected exception: " + e.ToString ());
86                 }
87                 // MD5 (bad)
88                 try {
89                         fmt.SetHashAlgorithm ("MD5");
90                 }
91                 catch (CryptographicUnexpectedOperationException) {
92                         // do nothing, this is what we expect
93                 }
94                 catch (Exception e) {
95                         Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
96                 }
97         }
98
99         public void TestSetKey () {
100                 // here null is ok 
101                 try {
102                         fmt.SetKey (null);
103                 }
104                 catch (Exception e) {
105                         Fail ("Unexpected exception: " + e.ToString ());
106                 }
107                 // RSA (bad)
108                 try {
109                         fmt.SetKey (rsa);
110                         Fail ("Expected InvalidCastException but got none");
111                 }
112                 catch (InvalidCastException) {
113                         // do nothing, this is what we expect 
114                 }
115                 catch (Exception e) {
116                         Fail ("Expected InvalidCastException but got: " + e.ToString ());
117                 }
118                 // DSA
119                 try {
120                         fmt.SetKey (dsa);
121                 }
122                 catch (Exception e) {
123                         Fail ("Unexpected exception: " + e.ToString ());
124                 }
125         }
126
127         // note: There's a bug in MS Framework where you can't re-import a key into
128         // the same object
129         public void TestSignature () 
130         {
131                 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
132                 byte[] sign = null;
133                 // no keypair
134                 try {
135                         sign = fmt.CreateSignature (hash);
136                 }
137                 catch (CryptographicUnexpectedOperationException) {
138                         // do nothing, this is what we expect 
139                 }
140                 catch (Exception e) {
141                         Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
142                 }
143
144                 // try a keypair without the private key
145                 dsa.ImportParameters (AllTests.GetKey (false));
146                 fmt.SetKey (dsa);
147                 try {
148                         sign = fmt.CreateSignature (hash);
149                         Fail ("Expected CryptographicException but got none");
150                 }
151                 catch (CryptographicException) {
152                         // do nothing, this is what we expect 
153                 }
154                 catch (Exception e) {
155                         Fail ("Expected CryptographicException but got " + e.ToString ());
156                 }
157
158                 // complete keypair
159                 dsa.ImportParameters (AllTests.GetKey (true));
160                 fmt.SetKey (dsa);
161
162                 // null hash
163                 try {
164                         byte[] h = null; // overloaded method
165                         sign = fmt.CreateSignature (h); 
166                         Fail ("Expected ArgumentNullException but got none");
167                 }
168                 catch (ArgumentNullException) {
169                         // do nothing, this is what we expect 
170                 }
171                 catch (Exception e) {
172                         Fail ("Expected ArgumentNullException but got " + e.ToString ());
173                 }
174
175                 // valid
176                 sign = fmt.CreateSignature (hash);
177                 Assert ("verified signature", dsa.VerifySignature (hash, sign));
178         }
179 }
180
181 }