2005-04-27 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  <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 DSASignatureFormatterTest {
39         protected DSASignatureFormatter fmt;
40         protected static DSA dsa;
41         protected static RSA rsa;
42
43         [TestFixtureSetUp]
44         public void FixtureSetUp () 
45         {
46                 // key generation is VERY long so one time is enough
47                 dsa = DSA.Create ();
48                 rsa = RSA.Create ();
49         }
50
51         [SetUp]
52         public void SetUp () 
53         {
54                 fmt = new DSASignatureFormatter ();
55         }
56
57         [Test]
58         public void Constructor_Empty () 
59         {
60                 DSASignatureFormatter fmt = new DSASignatureFormatter ();
61                 Assert.IsNotNull (fmt);
62         }
63
64         [Test]
65         public void Constructor_DSA ()
66         {
67                 DSASignatureFormatter fmt = new DSASignatureFormatter (dsa);
68                 Assert.IsNotNull (fmt);
69         }
70
71         [Test]
72 #if NET_2_0
73         [ExpectedException (typeof (ArgumentNullException))]
74 #endif
75         public void Constructor_Null () 
76         {
77                 DSASignatureFormatter fmt = new DSASignatureFormatter (null);
78                 Assert.IsNotNull (fmt);
79         }
80
81         [Test]
82         [ExpectedException (typeof (InvalidCastException))]
83         public void Constructor_RSA ()
84         {
85                 DSASignatureFormatter fmt = new DSASignatureFormatter (rsa);
86         }
87
88         [Test]
89         [ExpectedException (typeof (ArgumentNullException))]
90         public void SetHash_Null ()
91         {
92                 fmt.SetHashAlgorithm (null);
93         }
94
95         [Test]
96         public void SetHash_SHA1 ()
97         {
98                 fmt.SetHashAlgorithm ("SHA1");
99         }
100
101         [Test]
102         [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
103         public void SetHash_MD5 ()
104         {
105                 fmt.SetHashAlgorithm ("MD5");
106         }
107
108         [Test]
109 #if NET_2_0
110         [ExpectedException (typeof (ArgumentNullException))]
111 #endif
112         public void SetKey_Null ()
113         {
114                 fmt.SetKey (null);
115         }
116
117         [Test]
118         [ExpectedException (typeof (InvalidCastException))]
119         public void SetKey_RSA ()
120         {
121                 fmt.SetKey (rsa);
122         }
123
124         [Test]
125         public void SetKey_DSA ()
126         {
127                 fmt.SetKey (dsa);
128         }
129
130         // note: There's a bug in MS Framework where you can't re-import a key into
131         // the same object
132
133         [Test]
134         [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
135         public void Signature_NoKeyPair ()
136         {
137                 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
138                 byte[] sign = fmt.CreateSignature (hash);
139         }
140
141         [Test]
142         [ExpectedException (typeof (CryptographicException))]
143         public void Signature_OnlyPublicKey ()
144         {
145                 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
146                 dsa.ImportParameters (AllTests.GetKey (false));
147                 fmt.SetKey (dsa);
148                 byte[] sign = fmt.CreateSignature (hash);
149         }
150
151         [Test]
152         public void Signature ()
153         {
154                 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
155                 dsa.ImportParameters (AllTests.GetKey (true));
156                 fmt.SetKey (dsa);
157                 byte[] sign = fmt.CreateSignature (hash);
158                 Assert.IsTrue (dsa.VerifySignature (hash, sign));
159         }
160
161         [Test]
162         [ExpectedException (typeof (ArgumentNullException))]
163         public void Signature_NullHash ()
164         {
165                 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
166                 dsa.ImportParameters (AllTests.GetKey (true));
167                 fmt.SetKey (dsa);
168
169                 byte[] h = null; // overloaded method
170                 byte[] sign = fmt.CreateSignature (h); 
171         }
172 }
173
174 }