Merge pull request #249 from pcc/xgetinputfocus
[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 #if NET_2_0
72         [ExpectedException (typeof (ArgumentNullException))]
73 #endif
74         public void Constructor_Null () 
75         {
76                 DSASignatureFormatter fmt = new DSASignatureFormatter (null);
77                 Assert.IsNotNull (fmt);
78         }
79
80         [Test]
81         [ExpectedException (typeof (InvalidCastException))]
82         public void Constructor_RSA ()
83         {
84                 DSASignatureFormatter fmt = new DSASignatureFormatter (rsa);
85         }
86
87         [Test]
88         [ExpectedException (typeof (ArgumentNullException))]
89         public void SetHash_Null ()
90         {
91                 fmt.SetHashAlgorithm (null);
92         }
93
94         [Test]
95         public void SetHash_SHA1 ()
96         {
97                 fmt.SetHashAlgorithm ("SHA1");
98         }
99
100         [Test]
101         [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
102         public void SetHash_MD5 ()
103         {
104                 fmt.SetHashAlgorithm ("MD5");
105         }
106
107         [Test]
108 #if NET_2_0
109         [ExpectedException (typeof (ArgumentNullException))]
110 #endif
111         public void SetKey_Null ()
112         {
113                 fmt.SetKey (null);
114         }
115
116         [Test]
117         [ExpectedException (typeof (InvalidCastException))]
118         public void SetKey_RSA ()
119         {
120                 fmt.SetKey (rsa);
121         }
122
123         [Test]
124         public void SetKey_DSA ()
125         {
126                 fmt.SetKey (dsa);
127         }
128
129         // note: There's a bug in MS Framework where you can't re-import a key into
130         // the same object
131
132         [Test]
133         [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
134         public void Signature_NoKeyPair ()
135         {
136                 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
137                 byte[] sign = fmt.CreateSignature (hash);
138         }
139
140         [Test]
141         [ExpectedException (typeof (CryptographicException))]
142         public void Signature_OnlyPublicKey ()
143         {
144                 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
145                 dsa.ImportParameters (AllTests.GetKey (false));
146                 fmt.SetKey (dsa);
147                 byte[] sign = fmt.CreateSignature (hash);
148         }
149
150         [Test]
151         public void Signature ()
152         {
153                 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
154                 dsa.ImportParameters (AllTests.GetKey (true));
155                 fmt.SetKey (dsa);
156                 byte[] sign = fmt.CreateSignature (hash);
157                 Assert.IsTrue (dsa.VerifySignature (hash, sign));
158         }
159
160         [Test]
161         [ExpectedException (typeof (ArgumentNullException))]
162         public void Signature_NullHash ()
163         {
164                 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
165                 dsa.ImportParameters (AllTests.GetKey (true));
166                 fmt.SetKey (dsa);
167
168                 byte[] h = null; // overloaded method
169                 byte[] sign = fmt.CreateSignature (h); 
170         }
171 }
172
173 }