This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / DSASignatureDeformatterTest.cs
1 //
2 // DSASignatureDeformatterTest.cs - NUnit Test Cases for DSASignatureDeformatter
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 DSASignatureDeformatterTest : TestCase {
18         protected DSASignatureDeformatter def;
19         protected static DSA dsa;
20         protected static RSA rsa;
21
22         protected override void SetUp () 
23         {
24                 def = new DSASignatureDeformatter ();
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                 DSASignatureDeformatter def = new DSASignatureDeformatter ();
38                 AssertNotNull ("DSASignatureDeformatter()", def);
39                 // AsymmetricAlgorithm constructor (with null)
40                 def = new DSASignatureDeformatter (null);
41                 AssertNotNull ("DSASignatureDeformatter(null)", def);
42                 // AsymmetricAlgorithm constructor (with DSA)
43                 def = new DSASignatureDeformatter (dsa);
44                 AssertNotNull ("DSASignatureDeformatter(dsa)", def);
45                 // AsymmetricAlgorithm constructor (with RSA)
46                 try {
47                         def = new DSASignatureDeformatter (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         // Method is documented as unused so...
59         public void TestSetHash () 
60         {
61                 // null is ok
62                 try {
63                         def.SetHashAlgorithm (null);
64                 }
65                 catch (ArgumentNullException) {
66                         // do nothing, this is what we expect
67                 }
68                 catch (Exception e) {
69                         Fail ("Expected ArgumentNullException but got " + e.ToString ());
70                 }
71                 // SHA1
72                 try {
73                         def.SetHashAlgorithm ("SHA1");
74                 }
75                 catch (Exception e) {
76                         Fail ("Unexpected exception: " + e.ToString ());
77                 }
78                 // MD5 (bad)
79                 try {
80                         def.SetHashAlgorithm ("MD5");
81                 }
82                 catch (CryptographicUnexpectedOperationException) {
83                         // do nothing, this is what we expect
84                 }
85                 catch (Exception e) {
86                         Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
87                 }
88         }
89
90         public void TestSetKey () 
91         {
92                 // here null is ok 
93                 try {
94                         def.SetKey (null);
95                 }
96                 catch (Exception e) {
97                         Fail ("Unexpected exception: " + e.ToString ());
98                 }
99                 // RSA (bad)
100                 try {
101                         def.SetKey (rsa);
102                         Fail ("Expected InvalidCastException but got none");
103                 }
104                 catch (InvalidCastException) {
105                         // do nothing, this is what we expect 
106                 }
107                 catch (Exception e) {
108                         Fail ("Expected InvalidCastException but got: " + e.ToString ());
109                 }
110                 // DSA
111                 try {
112                         def.SetKey (dsa);
113                 }
114                 catch (Exception e) {
115                         Fail ("Unexpected exception: " + e.ToString ());
116                 }
117         }
118
119         public void TestVerify () 
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 = { 0x50, 0xd2, 0xb0, 0x8b, 0xcd, 0x5e, 0xb2, 0xc2, 0x35, 0x82, 0xd3, 0x76, 0x07, 0x79, 0xbb, 0x55, 0x98, 0x72, 0x43, 0xe8,
123                                       0x74, 0xc9, 0x35, 0xf8, 0xc9, 0xbd, 0x69, 0x2f, 0x08, 0x34, 0xfa, 0x5a, 0x59, 0x23, 0x2a, 0x85, 0x7b, 0xa3, 0xb3, 0x82 };
124                 bool ok = false;
125                 try {
126                         ok = def.VerifySignature (hash, sign);
127                 }
128                 catch (CryptographicUnexpectedOperationException) {
129                         // do nothing, this is what we expect 
130                 }
131                 catch (Exception e) {
132                         Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
133                 }
134
135                 dsa.ImportParameters (AllTests.GetKey (false));
136                 def.SetKey (dsa);
137
138                 // missing signature
139                 try {
140                         ok = def.VerifySignature (hash, null);
141                         Fail ("Expected ArgumentNullException but got none");
142                 }
143                 catch (ArgumentNullException) {
144                         // do nothing, this is what we expect 
145                 }
146                 catch (Exception e) {
147                         Fail ("Expected ArgumentNullException but got " + e.ToString ());
148                 }
149
150                 // missing hash
151                 try {
152                         byte[] s = null; // overloaded method
153                         ok = def.VerifySignature (s, sign);
154                         Fail ("Expected ArgumentNullException but got none");
155                 }
156                 catch (ArgumentNullException) {
157                         // do nothing, this is what we expect 
158                 }
159                 catch (Exception e) {
160                         Fail ("Expected ArgumentNullException but got " + e.ToString ());
161                 }
162
163                 ok = def.VerifySignature (hash, sign);
164                 Assert ("verified signature", ok);
165
166                 byte[] badSign = { 0x49, 0xd2, 0xb0, 0x8b, 0xcd, 0x5e, 0xb2, 0xc2, 0x35, 0x82, 0xd3, 0x76, 0x07, 0x79, 0xbb, 0x55, 0x98, 0x72, 0x43, 0xe8,
167                                          0x74, 0xc9, 0x35, 0xf8, 0xc9, 0xbd, 0x69, 0x2f, 0x08, 0x34, 0xfa, 0x5a, 0x59, 0x23, 0x2a, 0x85, 0x7b, 0xa3, 0xb3, 0x82 };
168                 ok = def.VerifySignature (hash, badSign);
169                 Assert ("didn't verified bad signature", !ok);
170         }
171 }
172
173 }