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