2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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  <sebastien@ximian.com>
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Security;
33 using System.Security.Cryptography;
34
35 namespace MonoTests.System.Security.Cryptography {
36
37 [TestFixture]
38 public class DSASignatureDeformatterTest {
39         protected DSASignatureDeformatter def;
40         protected static DSA dsa;
41         protected static RSA rsa;
42
43         static byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
44         static byte[] sign = { 0x50, 0xd2, 0xb0, 0x8b, 0xcd, 0x5e, 0xb2, 0xc2, 0x35, 0x82, 0xd3, 0x76, 0x07, 0x79, 0xbb, 0x55, 0x98, 0x72, 0x43, 0xe8,
45                                0x74, 0xc9, 0x35, 0xf8, 0xc9, 0xbd, 0x69, 0x2f, 0x08, 0x34, 0xfa, 0x5a, 0x59, 0x23, 0x2a, 0x85, 0x7b, 0xa3, 0xb3, 0x82 };
46
47         [TestFixtureSetUp]
48         public void FixtureSetUp () 
49         {
50                 // key generation is VERY long so one time is enough
51                 dsa = DSA.Create ();
52                 rsa = RSA.Create ();
53         }
54
55         [SetUp]
56         public void SetUp () 
57         {
58                 def = new DSASignatureDeformatter ();
59         }
60
61         [Test]
62         public void Constructor_Empty () 
63         {
64                 DSASignatureDeformatter def = new DSASignatureDeformatter ();
65                 Assert.IsNotNull (def);
66         }
67
68         [Test]
69 #if NET_2_0
70         [ExpectedException (typeof (ArgumentNullException))]
71 #endif
72         public void Constructor_Null ()
73         {
74                 DSASignatureDeformatter def = new DSASignatureDeformatter (null);
75                 Assert.IsNotNull (def);
76         }
77
78         [Test]
79         public void Constructor_DSA ()
80         {
81                 DSASignatureDeformatter def = new DSASignatureDeformatter (dsa);
82                 Assert.IsNotNull (def);
83         }
84
85         [Test]
86         [ExpectedException (typeof (InvalidCastException))]
87         public void Constructor_RSA ()
88         {
89                 DSASignatureDeformatter def = new DSASignatureDeformatter (rsa);
90         }
91
92         [Test]
93         [ExpectedException (typeof (ArgumentNullException))]
94         public void SetHash_Null ()
95         {
96                 def.SetHashAlgorithm (null);
97         }
98
99         [Test]
100         public void SetHash_SHA1 ()
101         {
102                 def.SetHashAlgorithm ("SHA1");
103         }
104
105         [Test]
106         [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
107         public void SetHash_MD5 ()
108         {
109                 def.SetHashAlgorithm ("MD5");
110         }
111
112         [Test]
113 #if NET_2_0
114         [ExpectedException (typeof (ArgumentNullException))]
115 #endif
116         public void SetKey_Null () 
117         {
118                 def.SetKey (null);
119         }
120
121         [Test]
122         [ExpectedException (typeof (InvalidCastException))]
123         public void SetKey_RSA ()
124         {
125                 def.SetKey (rsa);
126         }
127
128         [Test]
129         public void SetKey_DSA ()
130         {
131                 def.SetKey (dsa);
132         }
133
134         [Test]
135         [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
136         public void Verify_NoKeyPair () 
137         {
138                 def.VerifySignature (hash, sign);
139         }
140
141         [Test]
142         [ExpectedException (typeof (ArgumentNullException))]
143         public void Verify_NullSignature ()
144         {
145                 dsa.ImportParameters (AllTests.GetKey (false));
146                 def.SetKey (dsa);
147                 def.VerifySignature (hash, null);
148         }
149
150         [Test]
151         [ExpectedException (typeof (ArgumentNullException))]
152         public void Verify_NullHash ()
153         {
154                 dsa.ImportParameters (AllTests.GetKey (false));
155                 def.SetKey (dsa);
156                 byte[] s = null; // overloaded method
157                 def.VerifySignature (s, sign);
158         }
159
160         [Test]
161         public void Verify ()
162         {
163                 dsa.ImportParameters (AllTests.GetKey (false));
164                 def.SetKey (dsa);
165                 Assert.IsTrue (def.VerifySignature (hash, sign));
166         }
167
168         [Test]
169         public void Verify_Bad ()
170         {
171                 dsa.ImportParameters (AllTests.GetKey (false));
172                 def.SetKey (dsa);
173                 byte[] badSign = { 0x49, 0xd2, 0xb0, 0x8b, 0xcd, 0x5e, 0xb2, 0xc2, 0x35, 0x82, 0xd3, 0x76, 0x07, 0x79, 0xbb, 0x55, 0x98, 0x72, 0x43, 0xe8,
174                                    0x74, 0xc9, 0x35, 0xf8, 0xc9, 0xbd, 0x69, 0x2f, 0x08, 0x34, 0xfa, 0x5a, 0x59, 0x23, 0x2a, 0x85, 0x7b, 0xa3, 0xb3, 0x82 };
175                 Assert.IsFalse (def.VerifySignature (hash, badSign));
176         }
177 }
178
179 }