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;
43 public DSASignatureFormatterTest ()
45 // key generation is VERY long so one time is enough
53 fmt = new DSASignatureFormatter ();
57 public void Constructor_Empty ()
59 DSASignatureFormatter fmt = new DSASignatureFormatter ();
60 Assert.IsNotNull (fmt);
64 public void Constructor_DSA ()
66 DSASignatureFormatter fmt = new DSASignatureFormatter (dsa);
67 Assert.IsNotNull (fmt);
72 [ExpectedException (typeof (ArgumentNullException))]
74 public void Constructor_Null ()
76 DSASignatureFormatter fmt = new DSASignatureFormatter (null);
77 Assert.IsNotNull (fmt);
81 [ExpectedException (typeof (InvalidCastException))]
82 public void Constructor_RSA ()
84 DSASignatureFormatter fmt = new DSASignatureFormatter (rsa);
88 [ExpectedException (typeof (ArgumentNullException))]
89 public void SetHash_Null ()
91 fmt.SetHashAlgorithm (null);
95 public void SetHash_SHA1 ()
97 fmt.SetHashAlgorithm ("SHA1");
101 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
102 public void SetHash_MD5 ()
104 fmt.SetHashAlgorithm ("MD5");
109 [ExpectedException (typeof (ArgumentNullException))]
111 public void SetKey_Null ()
117 [ExpectedException (typeof (InvalidCastException))]
118 public void SetKey_RSA ()
124 public void SetKey_DSA ()
129 // note: There's a bug in MS Framework where you can't re-import a key into
133 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
134 public void Signature_NoKeyPair ()
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);
141 [ExpectedException (typeof (CryptographicException))]
142 public void Signature_OnlyPublicKey ()
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));
147 byte[] sign = fmt.CreateSignature (hash);
151 public void Signature ()
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));
156 byte[] sign = fmt.CreateSignature (hash);
157 Assert.IsTrue (dsa.VerifySignature (hash, sign));
161 [ExpectedException (typeof (ArgumentNullException))]
162 public void Signature_NullHash ()
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));
168 byte[] h = null; // overloaded method
169 byte[] sign = fmt.CreateSignature (h);