2 // DSASignatureFormatterTest.cs - NUnit Test Cases for DSASignatureFormatter
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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;
32 using System.Security;
33 using System.Security.Cryptography;
35 namespace MonoTests.System.Security.Cryptography {
38 public class DSASignatureFormatterTest {
39 protected DSASignatureFormatter fmt;
40 protected static DSA dsa;
41 protected static RSA rsa;
44 public void FixtureSetUp ()
46 // key generation is VERY long so one time is enough
54 fmt = new DSASignatureFormatter ();
58 public void Constructor_Empty ()
60 DSASignatureFormatter fmt = new DSASignatureFormatter ();
61 Assert.IsNotNull (fmt);
65 public void Constructor_DSA ()
67 DSASignatureFormatter fmt = new DSASignatureFormatter (dsa);
68 Assert.IsNotNull (fmt);
73 [ExpectedException (typeof (ArgumentNullException))]
75 public void Constructor_Null ()
77 DSASignatureFormatter fmt = new DSASignatureFormatter (null);
78 Assert.IsNotNull (fmt);
82 [ExpectedException (typeof (InvalidCastException))]
83 public void Constructor_RSA ()
85 DSASignatureFormatter fmt = new DSASignatureFormatter (rsa);
89 [ExpectedException (typeof (ArgumentNullException))]
90 public void SetHash_Null ()
92 fmt.SetHashAlgorithm (null);
96 public void SetHash_SHA1 ()
98 fmt.SetHashAlgorithm ("SHA1");
102 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
103 public void SetHash_MD5 ()
105 fmt.SetHashAlgorithm ("MD5");
110 [ExpectedException (typeof (ArgumentNullException))]
112 public void SetKey_Null ()
118 [ExpectedException (typeof (InvalidCastException))]
119 public void SetKey_RSA ()
125 public void SetKey_DSA ()
130 // note: There's a bug in MS Framework where you can't re-import a key into
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);
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));
148 byte[] sign = fmt.CreateSignature (hash);
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));
157 byte[] sign = fmt.CreateSignature (hash);
158 Assert.IsTrue (dsa.VerifySignature (hash, sign));
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));
169 byte[] h = null; // overloaded method
170 byte[] sign = fmt.CreateSignature (h);