**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Security.Cryptography / DSASignatureFormatterTest.cs
blob14e060bc9e99e451b7ad6e5b37b0c246340a73ec
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.
30 using NUnit.Framework;
31 using System;
32 using System.Security;
33 using System.Security.Cryptography;
35 namespace MonoTests.System.Security.Cryptography {
37 [TestFixture]
38 public class DSASignatureFormatterTest {
39 protected DSASignatureFormatter fmt;
40 protected static DSA dsa;
41 protected static RSA rsa;
43 [TestFixtureSetUp]
44 public void FixtureSetUp ()
46 // key generation is VERY long so one time is enough
47 dsa = DSA.Create ();
48 rsa = RSA.Create ();
51 [SetUp]
52 public void SetUp ()
54 fmt = new DSASignatureFormatter ();
57 [Test]
58 public void Constructor_Empty ()
60 DSASignatureFormatter fmt = new DSASignatureFormatter ();
61 Assert.IsNotNull (fmt);
64 [Test]
65 public void Constructor_DSA ()
67 DSASignatureFormatter fmt = new DSASignatureFormatter (dsa);
68 Assert.IsNotNull (fmt);
71 [Test]
72 #if NET_2_0
73 [ExpectedException (typeof (ArgumentNullException))]
74 #endif
75 public void Constructor_Null ()
77 DSASignatureFormatter fmt = new DSASignatureFormatter (null);
78 Assert.IsNotNull (fmt);
81 [Test]
82 [ExpectedException (typeof (InvalidCastException))]
83 public void Constructor_RSA ()
85 DSASignatureFormatter fmt = new DSASignatureFormatter (rsa);
88 [Test]
89 [ExpectedException (typeof (ArgumentNullException))]
90 public void SetHash_Null ()
92 fmt.SetHashAlgorithm (null);
95 [Test]
96 public void SetHash_SHA1 ()
98 fmt.SetHashAlgorithm ("SHA1");
101 [Test]
102 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
103 public void SetHash_MD5 ()
105 fmt.SetHashAlgorithm ("MD5");
108 [Test]
109 #if NET_2_0
110 [ExpectedException (typeof (ArgumentNullException))]
111 #endif
112 public void SetKey_Null ()
114 fmt.SetKey (null);
117 [Test]
118 [ExpectedException (typeof (InvalidCastException))]
119 public void SetKey_RSA ()
121 fmt.SetKey (rsa);
124 [Test]
125 public void SetKey_DSA ()
127 fmt.SetKey (dsa);
130 // note: There's a bug in MS Framework where you can't re-import a key into
131 // the same object
133 [Test]
134 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
135 public void Signature_NoKeyPair ()
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);
141 [Test]
142 [ExpectedException (typeof (CryptographicException))]
143 public void Signature_OnlyPublicKey ()
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);
151 [Test]
152 public void Signature ()
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));
161 [Test]
162 [ExpectedException (typeof (ArgumentNullException))]
163 public void Signature_NullHash ()
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);
169 byte[] h = null; // overloaded method
170 byte[] sign = fmt.CreateSignature (h);