2 // SignatureDescriptionTest.cs - NUnit Test Cases for SignatureDescription
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell http://www.novell.com
11 using NUnit.Framework;
13 using System.Security;
14 using System.Security.Cryptography;
16 namespace MonoTests.System.Security.Cryptography {
19 public class SignatureDescriptionTest : Assertion {
21 protected SignatureDescription sig;
22 protected static DSA dsa;
23 protected static RSA rsa;
28 sig = new SignatureDescription();
29 // key generation is VERY long so one time is enough
36 public void AssertEquals (string msg, byte[] array1, byte[] array2)
38 AllTests.AssertEquals (msg, array1, array2);
42 public void Constructor_Default ()
45 SignatureDescription sig = new SignatureDescription ();
46 AssertNotNull ("SignatureDescription()", sig);
50 [ExpectedException (typeof (ArgumentNullException))]
51 public void Constructor_Null ()
54 SignatureDescription sig = new SignatureDescription (null);
55 // LAMESPEC: Documented as CryptographicException
59 public void Constructor_SecurityElement_Empty ()
61 // (empty) SecurityElement constructor
62 SecurityElement se = new SecurityElement ("xml");
63 SignatureDescription sig = new SignatureDescription (se);
64 AssertNotNull ("SignatureDescription(SecurityElement)", sig);
68 public void Constructor_SecurityElement_DSA ()
70 SecurityElement se = new SecurityElement ("DSASignature");
71 se.AddChild (new SecurityElement ("Key", "System.Security.Cryptography.DSACryptoServiceProvider"));
72 se.AddChild (new SecurityElement ("Digest", "System.Security.Cryptography.SHA1CryptoServiceProvider"));
73 se.AddChild (new SecurityElement ("Formatter", "System.Security.Cryptography.DSASignatureFormatter"));
74 se.AddChild (new SecurityElement ("Deformatter", "System.Security.Cryptography.DSASignatureDeformatter"));
76 SignatureDescription sig = new SignatureDescription (se);
77 AssertNotNull ("SignatureDescription(SecurityElement)", sig);
78 AssertEquals ("Key", "System.Security.Cryptography.DSACryptoServiceProvider", sig.KeyAlgorithm);
79 AssertEquals ("Digest", "System.Security.Cryptography.SHA1CryptoServiceProvider", sig.DigestAlgorithm);
80 AssertEquals ("Formatter", "System.Security.Cryptography.DSASignatureFormatter", sig.FormatterAlgorithm);
81 AssertEquals ("Deformatter", "System.Security.Cryptography.DSASignatureDeformatter", sig.DeformatterAlgorithm);
85 public void Constructor_SecurityElement_RSA ()
87 SecurityElement se = new SecurityElement ("RSASignature");
88 se.AddChild (new SecurityElement ("Key", "System.Security.Cryptography.RSACryptoServiceProvider"));
89 se.AddChild (new SecurityElement ("Digest", "System.Security.Cryptography.SHA1CryptoServiceProvider"));
90 se.AddChild (new SecurityElement ("Formatter", "System.Security.Cryptography.RSAPKCS1SignatureFormatter"));
91 se.AddChild (new SecurityElement ("Deformatter", "System.Security.Cryptography.RSAPKCS1SignatureDeformatter"));
93 SignatureDescription sig = new SignatureDescription (se);
94 AssertNotNull ("SignatureDescription(SecurityElement)", sig);
95 AssertEquals ("Key", "System.Security.Cryptography.RSACryptoServiceProvider", sig.KeyAlgorithm);
96 AssertEquals ("Digest", "System.Security.Cryptography.SHA1CryptoServiceProvider", sig.DigestAlgorithm);
97 AssertEquals ("Formatter", "System.Security.Cryptography.RSAPKCS1SignatureFormatter", sig.FormatterAlgorithm);
98 AssertEquals ("Deformatter", "System.Security.Cryptography.RSAPKCS1SignatureDeformatter", sig.DeformatterAlgorithm);
102 public void Properties ()
104 string invalid = "invalid";
105 AssertNull ("DeformatterAlgorithm 1", sig.DeformatterAlgorithm);
106 sig.DeformatterAlgorithm = invalid;
107 AssertNotNull ("DeformatterAlgorithm 2", sig.DeformatterAlgorithm);
108 AssertEquals ("DeformatterAlgorithm 3", invalid, sig.DeformatterAlgorithm);
109 sig.DeformatterAlgorithm = null;
110 AssertNull ("DeformatterAlgorithm 4", sig.DeformatterAlgorithm);
112 AssertNull ("DigestAlgorithm 1", sig.DigestAlgorithm);
113 sig.DigestAlgorithm = invalid;
114 AssertNotNull ("DigestAlgorithm 2", sig.DigestAlgorithm);
115 AssertEquals ("DigestAlgorithm 3", invalid, sig.DigestAlgorithm);
116 sig.DigestAlgorithm = null;
117 AssertNull ("DigestAlgorithm 4", sig.DigestAlgorithm);
119 AssertNull ("FormatterAlgorithm 1", sig.FormatterAlgorithm);
120 sig.FormatterAlgorithm = invalid;
121 AssertNotNull ("FormatterAlgorithm 2", sig.FormatterAlgorithm);
122 AssertEquals ("FormatterAlgorithm 3", invalid, sig.FormatterAlgorithm);
123 sig.FormatterAlgorithm = null;
124 AssertNull ("FormatterAlgorithm 4", sig.FormatterAlgorithm);
126 AssertNull ("KeyAlgorithm 1", sig.KeyAlgorithm);
127 sig.KeyAlgorithm = invalid;
128 AssertNotNull ("KeyAlgorithm 2", sig.KeyAlgorithm);
129 AssertEquals ("KeyAlgorithm 3", invalid, sig.KeyAlgorithm);
130 sig.KeyAlgorithm = null;
131 AssertNull ("KeyAlgorithm 4", sig.KeyAlgorithm);
135 public void Deformatter ()
137 AsymmetricSignatureDeformatter def = null;
138 // Deformatter with all properties null
140 def = sig.CreateDeformatter (dsa);
141 Fail ("Expected ArgumentNullException but got none");
143 catch (ArgumentNullException) {
144 // this is what we expect
146 catch (Exception e) {
147 Fail ("Expected ArgumentNullException but got: " + e.ToString ());
149 // Deformatter with invalid DeformatterAlgorithm property
150 sig.DeformatterAlgorithm = "DSA";
152 def = sig.CreateDeformatter (dsa);
153 Fail ("Expected InvalidCastException but got none");
155 catch (InvalidCastException) {
156 // this is what we expect
158 catch (Exception e) {
159 Fail ("Expected InvalidCastException but got: " + e.ToString ());
161 // Deformatter with valid DeformatterAlgorithm property
162 sig.DeformatterAlgorithm = "DSASignatureDeformatter";
164 def = sig.CreateDeformatter (dsa);
165 Fail ("Expected NullReferenceException but got none");
167 catch (NullReferenceException) {
168 // this is what we expect
170 catch (Exception e) {
171 Fail ("Expected NullReferenceException but got: " + e.ToString ());
173 // Deformatter with valid DeformatterAlgorithm property
174 sig.KeyAlgorithm = "DSA";
175 sig.DigestAlgorithm = "SHA1";
176 sig.DeformatterAlgorithm = "DSASignatureDeformatter";
178 def = sig.CreateDeformatter (dsa);
179 Fail ("Expected NullReferenceException but got none");
181 catch (NullReferenceException) {
182 // this is what we expect
184 catch (Exception e) {
185 Fail ("Expected NullReferenceException but got: " + e.ToString ());
190 public void Digest ()
192 bool rightClass = false;
193 HashAlgorithm hash = null;
196 hash = sig.CreateDigest ();
197 Fail ("Expected ArgumentNullException but got none");
199 catch (ArgumentNullException) {
200 // this is what we expect
202 catch (Exception e) {
203 Fail ("Expected ArgumentNullException but got: " + e.ToString ());
206 sig.DigestAlgorithm = "SHA1";
207 hash = sig.CreateDigest ();
208 AssertNotNull ("CreateDigest(SHA1)", hash);
209 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
210 Assert ("CreateDigest(SHA1)", rightClass);
212 sig.DigestAlgorithm = "MD5";
213 hash = sig.CreateDigest ();
214 AssertNotNull ("CreateDigest(MD5)", hash);
215 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
216 Assert ("CreateDigest(MD5)", rightClass);
218 sig.DigestAlgorithm = "SHA256";
219 hash = sig.CreateDigest ();
220 AssertNotNull ("CreateDigest(SHA256)", hash);
221 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
222 Assert ("CreateDigest(SHA256)", rightClass);
224 sig.DigestAlgorithm = "SHA384";
225 hash = sig.CreateDigest ();
226 AssertNotNull ("CreateDigest(SHA384)", hash);
227 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
228 Assert ("CreateDigest(SHA384)", rightClass);
230 sig.DigestAlgorithm = "SHA512";
231 hash = sig.CreateDigest ();
232 AssertNotNull ("CreateDigest(SHA512)", hash);
233 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
234 Assert ("CreateDigest(SHA512)", rightClass);
236 sig.DigestAlgorithm = "bad";
237 hash = sig.CreateDigest ();
238 AssertNull ("CreateDigest(bad)", hash);
242 public void Formatter ()
244 AsymmetricSignatureFormatter fmt = null;
245 // Formatter with all properties null
247 fmt = sig.CreateFormatter (dsa);
248 Fail ("Expected ArgumentNullException but got none");
250 catch (ArgumentNullException) {
251 // this is what we expect
253 catch (Exception e) {
254 Fail ("Expected ArgumentNullException but got: " + e.ToString ());
256 // Formatter with invalid FormatterAlgorithm property
257 sig.FormatterAlgorithm = "DSA";
259 fmt = sig.CreateFormatter (dsa);
260 Fail ("Expected InvalidCastException but got none");
262 catch (InvalidCastException) {
263 // this is what we expect
265 catch (Exception e) {
266 Fail ("Expected InvalidCastException but got: " + e.ToString ());
268 // Formatter with valid FormatterAlgorithm property
269 sig.FormatterAlgorithm = "DSASignatureFormatter";
271 fmt = sig.CreateFormatter (dsa);
272 Fail ("Expected NullReferenceException but got none");
274 catch (NullReferenceException) {
275 // this is what we expect
277 catch (Exception e) {
278 Fail ("Expected NullReferenceException but got: " + e.ToString ());
280 // Deformatter with valid DeformatterAlgorithm property
281 sig.KeyAlgorithm = "DSA";
282 sig.DigestAlgorithm = "SHA1";
283 sig.FormatterAlgorithm = "DSASignatureFormatter";
285 fmt = sig.CreateFormatter (dsa);
286 Fail ("Expected NullReferenceException but got none");
288 catch (NullReferenceException) {
289 // this is what we expect
291 catch (Exception e) {
292 Fail ("Expected NullReferenceException but got: " + e.ToString ());
297 public void DSASignatureDescription ()
299 // internal class - we cannot create one without CryptoConfig
300 SignatureDescription sd = (SignatureDescription) CryptoConfig.CreateFromName ("http://www.w3.org/2000/09/xmldsig#dsa-sha1");
301 AssertEquals ("DSA.DigestAlgorithm", "System.Security.Cryptography.SHA1CryptoServiceProvider", sd.DigestAlgorithm);
302 AssertEquals ("DSA.DeformatterAlgorithm", "System.Security.Cryptography.DSASignatureDeformatter", sd.DeformatterAlgorithm);
303 AssertEquals ("DSA.FormatterAlgorithm", "System.Security.Cryptography.DSASignatureFormatter", sd.FormatterAlgorithm);
304 AssertEquals ("DSA.KeyAlgorithm", "System.Security.Cryptography.DSACryptoServiceProvider", sd.KeyAlgorithm);
306 HashAlgorithm hash = sd.CreateDigest();
307 AssertEquals ("DSA.CreateDigest", "System.Security.Cryptography.SHA1CryptoServiceProvider", hash.ToString ());
309 AssertEquals ("DSA.Create", dsa.ToString (), sd.KeyAlgorithm);
311 AsymmetricSignatureDeformatter asd = sd.CreateDeformatter (dsa);
312 AssertEquals ("DSA.CreateDeformatter", "System.Security.Cryptography.DSASignatureDeformatter", asd.ToString ());
314 AsymmetricSignatureFormatter asf = sd.CreateFormatter (dsa);
315 AssertEquals ("DSA.CreateFormatter", "System.Security.Cryptography.DSASignatureFormatter", asf.ToString ());
319 public void RSASignatureDescription ()
321 // internal class - we cannot create one without CryptoConfig
322 SignatureDescription sd = (SignatureDescription) CryptoConfig.CreateFromName ("http://www.w3.org/2000/09/xmldsig#rsa-sha1");
323 AssertEquals ("RSA.DigestAlgorithm", "System.Security.Cryptography.SHA1CryptoServiceProvider", sd.DigestAlgorithm);
324 AssertEquals ("RSA.DeformatterAlgorithm", "System.Security.Cryptography.RSAPKCS1SignatureDeformatter", sd.DeformatterAlgorithm);
325 AssertEquals ("RSA.FormatterAlgorithm", "System.Security.Cryptography.RSAPKCS1SignatureFormatter", sd.FormatterAlgorithm);
326 AssertEquals ("RSA.KeyAlgorithm", "System.Security.Cryptography.RSACryptoServiceProvider", sd.KeyAlgorithm);
328 HashAlgorithm hash = sd.CreateDigest();
329 AssertEquals ("RSA.CreateDigest", "System.Security.Cryptography.SHA1CryptoServiceProvider", hash.ToString ());
331 AssertEquals ("RSA.Create", rsa.ToString (), sd.KeyAlgorithm);
333 AsymmetricSignatureDeformatter asd = sd.CreateDeformatter (rsa);
334 AssertEquals ("RSA.CreateDeformatter", "System.Security.Cryptography.RSAPKCS1SignatureDeformatter", asd.ToString ());
336 AsymmetricSignatureFormatter asf = sd.CreateFormatter (rsa);
337 AssertEquals ("RSA.CreateFormatter", "System.Security.Cryptography.RSAPKCS1SignatureFormatter", asf.ToString ());