2 // DSASignatureDeformatterTest.cs - NUnit Test Cases for DSASignatureDeformatter
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 using NUnit.Framework;
12 using System.Security;
13 using System.Security.Cryptography;
15 namespace MonoTests.System.Security.Cryptography {
17 public class DSASignatureDeformatterTest : TestCase {
18 protected DSASignatureDeformatter def;
19 protected static DSA dsa;
20 protected static RSA rsa;
22 protected override void SetUp ()
24 def = new DSASignatureDeformatter ();
25 // key generation is VERY long so one time is enough
32 protected override void TearDown () {}
34 public void TestConstructors ()
37 DSASignatureDeformatter def = new DSASignatureDeformatter ();
38 AssertNotNull ("DSASignatureDeformatter()", def);
39 // AsymmetricAlgorithm constructor (with null)
40 def = new DSASignatureDeformatter (null);
41 AssertNotNull ("DSASignatureDeformatter(null)", def);
42 // AsymmetricAlgorithm constructor (with DSA)
43 def = new DSASignatureDeformatter (dsa);
44 AssertNotNull ("DSASignatureDeformatter(dsa)", def);
45 // AsymmetricAlgorithm constructor (with RSA)
47 def = new DSASignatureDeformatter (rsa);
48 Fail ("Expected InvalidCastException but got none");
50 catch (InvalidCastException) {
54 Fail ("Expected InvalidCastException but got " + e.ToString ());
58 // Method is documented as unused so...
59 public void TestSetHash ()
63 def.SetHashAlgorithm (null);
65 catch (ArgumentNullException) {
66 // do nothing, this is what we expect
69 Fail ("Expected ArgumentNullException but got " + e.ToString ());
73 def.SetHashAlgorithm ("SHA1");
76 Fail ("Unexpected exception: " + e.ToString ());
80 def.SetHashAlgorithm ("MD5");
82 catch (CryptographicUnexpectedOperationException) {
83 // do nothing, this is what we expect
86 Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
90 public void TestSetKey ()
97 Fail ("Unexpected exception: " + e.ToString ());
102 Fail ("Expected InvalidCastException but got none");
104 catch (InvalidCastException) {
105 // do nothing, this is what we expect
107 catch (Exception e) {
108 Fail ("Expected InvalidCastException but got: " + e.ToString ());
114 catch (Exception e) {
115 Fail ("Unexpected exception: " + e.ToString ());
119 public void TestVerify ()
121 byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
122 byte[] sign = { 0x50, 0xd2, 0xb0, 0x8b, 0xcd, 0x5e, 0xb2, 0xc2, 0x35, 0x82, 0xd3, 0x76, 0x07, 0x79, 0xbb, 0x55, 0x98, 0x72, 0x43, 0xe8,
123 0x74, 0xc9, 0x35, 0xf8, 0xc9, 0xbd, 0x69, 0x2f, 0x08, 0x34, 0xfa, 0x5a, 0x59, 0x23, 0x2a, 0x85, 0x7b, 0xa3, 0xb3, 0x82 };
126 ok = def.VerifySignature (hash, sign);
128 catch (CryptographicUnexpectedOperationException) {
129 // do nothing, this is what we expect
131 catch (Exception e) {
132 Fail ("Expected CryptographicUnexpectedOperationException but got " + e.ToString ());
135 dsa.ImportParameters (AllTests.GetKey (false));
140 ok = def.VerifySignature (hash, null);
141 Fail ("Expected ArgumentNullException but got none");
143 catch (ArgumentNullException) {
144 // do nothing, this is what we expect
146 catch (Exception e) {
147 Fail ("Expected ArgumentNullException but got " + e.ToString ());
152 byte[] s = null; // overloaded method
153 ok = def.VerifySignature (s, sign);
154 Fail ("Expected ArgumentNullException but got none");
156 catch (ArgumentNullException) {
157 // do nothing, this is what we expect
159 catch (Exception e) {
160 Fail ("Expected ArgumentNullException but got " + e.ToString ());
163 ok = def.VerifySignature (hash, sign);
164 Assert ("verified signature", ok);
166 byte[] badSign = { 0x49, 0xd2, 0xb0, 0x8b, 0xcd, 0x5e, 0xb2, 0xc2, 0x35, 0x82, 0xd3, 0x76, 0x07, 0x79, 0xbb, 0x55, 0x98, 0x72, 0x43, 0xe8,
167 0x74, 0xc9, 0x35, 0xf8, 0xc9, 0xbd, 0x69, 0x2f, 0x08, 0x34, 0xfa, 0x5a, 0x59, 0x23, 0x2a, 0x85, 0x7b, 0xa3, 0xb3, 0x82 };
168 ok = def.VerifySignature (hash, badSign);
169 Assert ("didn't verified bad signature", !ok);