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 {
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
37 public void Constructor_Default ()
40 SignatureDescription sig = new SignatureDescription ();
44 [ExpectedException (typeof (ArgumentNullException))]
45 public void Constructor_Null ()
48 SignatureDescription sig = new SignatureDescription (null);
49 // LAMESPEC: Documented as CryptographicException
53 public void Constructor_SecurityElement_Empty ()
55 // (empty) SecurityElement constructor
56 SecurityElement se = new SecurityElement ("xml");
57 SignatureDescription sig = new SignatureDescription (se);
61 public void Constructor_SecurityElement_DSA ()
63 SecurityElement se = new SecurityElement ("DSASignature");
64 se.AddChild (new SecurityElement ("Key", "System.Security.Cryptography.DSACryptoServiceProvider"));
65 se.AddChild (new SecurityElement ("Digest", "System.Security.Cryptography.SHA1CryptoServiceProvider"));
66 se.AddChild (new SecurityElement ("Formatter", "System.Security.Cryptography.DSASignatureFormatter"));
67 se.AddChild (new SecurityElement ("Deformatter", "System.Security.Cryptography.DSASignatureDeformatter"));
69 SignatureDescription sig = new SignatureDescription (se);
70 Assert.AreEqual ("System.Security.Cryptography.DSACryptoServiceProvider", sig.KeyAlgorithm);
71 Assert.AreEqual ("System.Security.Cryptography.SHA1CryptoServiceProvider", sig.DigestAlgorithm);
72 Assert.AreEqual ("System.Security.Cryptography.DSASignatureFormatter", sig.FormatterAlgorithm);
73 Assert.AreEqual ("System.Security.Cryptography.DSASignatureDeformatter", sig.DeformatterAlgorithm);
77 public void Constructor_SecurityElement_RSA ()
79 SecurityElement se = new SecurityElement ("RSASignature");
80 se.AddChild (new SecurityElement ("Key", "System.Security.Cryptography.RSACryptoServiceProvider"));
81 se.AddChild (new SecurityElement ("Digest", "System.Security.Cryptography.SHA1CryptoServiceProvider"));
82 se.AddChild (new SecurityElement ("Formatter", "System.Security.Cryptography.RSAPKCS1SignatureFormatter"));
83 se.AddChild (new SecurityElement ("Deformatter", "System.Security.Cryptography.RSAPKCS1SignatureDeformatter"));
85 SignatureDescription sig = new SignatureDescription (se);
86 Assert.AreEqual ("System.Security.Cryptography.RSACryptoServiceProvider", sig.KeyAlgorithm);
87 Assert.AreEqual ("System.Security.Cryptography.SHA1CryptoServiceProvider", sig.DigestAlgorithm);
88 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureFormatter", sig.FormatterAlgorithm);
89 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureDeformatter", sig.DeformatterAlgorithm);
93 public void Properties ()
95 string invalid = "invalid";
96 Assert.IsNull (sig.DeformatterAlgorithm, "DeformatterAlgorithm 1");
97 sig.DeformatterAlgorithm = invalid;
98 Assert.IsNotNull (sig.DeformatterAlgorithm, "DeformatterAlgorithm 2");
99 Assert.AreEqual (invalid, sig.DeformatterAlgorithm, "DeformatterAlgorithm 3");
100 sig.DeformatterAlgorithm = null;
101 Assert.IsNull (sig.DeformatterAlgorithm, "DeformatterAlgorithm 4");
103 Assert.IsNull (sig.DigestAlgorithm, "DigestAlgorithm 1");
104 sig.DigestAlgorithm = invalid;
105 Assert.IsNotNull (sig.DigestAlgorithm, "DigestAlgorithm 2");
106 Assert.AreEqual (invalid, sig.DigestAlgorithm, "DigestAlgorithm 3");
107 sig.DigestAlgorithm = null;
108 Assert.IsNull (sig.DigestAlgorithm, "DigestAlgorithm 4");
110 Assert.IsNull (sig.FormatterAlgorithm, "FormatterAlgorithm 1");
111 sig.FormatterAlgorithm = invalid;
112 Assert.IsNotNull (sig.FormatterAlgorithm, "FormatterAlgorithm 2");
113 Assert.AreEqual (invalid, sig.FormatterAlgorithm, "FormatterAlgorithm 3");
114 sig.FormatterAlgorithm = null;
115 Assert.IsNull (sig.FormatterAlgorithm, "FormatterAlgorithm 4");
117 Assert.IsNull (sig.KeyAlgorithm, "KeyAlgorithm 1");
118 sig.KeyAlgorithm = invalid;
119 Assert.IsNotNull (sig.KeyAlgorithm, "KeyAlgorithm 2");
120 Assert.AreEqual (invalid, sig.KeyAlgorithm, "KeyAlgorithm 3");
121 sig.KeyAlgorithm = null;
122 Assert.IsNull (sig.KeyAlgorithm, "KeyAlgorithm 4");
126 public void Deformatter ()
128 AsymmetricSignatureDeformatter def = null;
129 // Deformatter with all properties null
131 def = sig.CreateDeformatter (dsa);
132 Assert.Fail ("Expected ArgumentNullException but got none");
134 catch (ArgumentNullException) {
135 // this is what we expect
137 catch (Exception e) {
138 Assert.Fail ("Expected ArgumentNullException but got: " + e.ToString ());
140 // Deformatter with invalid DeformatterAlgorithm property
141 sig.DeformatterAlgorithm = "DSA";
143 def = sig.CreateDeformatter (dsa);
144 Assert.Fail ("Expected InvalidCastException but got none");
146 catch (InvalidCastException) {
147 // this is what we expect
149 catch (Exception e) {
150 Assert.Fail ("Expected InvalidCastException but got: " + e.ToString ());
152 // Deformatter with valid DeformatterAlgorithm property
153 sig.DeformatterAlgorithm = "DSASignatureDeformatter";
155 def = sig.CreateDeformatter (dsa);
156 Assert.Fail ("Expected NullReferenceException but got none");
158 catch (NullReferenceException) {
159 // this is what we expect
161 catch (Exception e) {
162 Assert.Fail ("Expected NullReferenceException but got: " + e.ToString ());
164 // Deformatter with valid DeformatterAlgorithm property
165 sig.KeyAlgorithm = "DSA";
166 sig.DigestAlgorithm = "SHA1";
167 sig.DeformatterAlgorithm = "DSASignatureDeformatter";
169 def = sig.CreateDeformatter (dsa);
170 Assert.Fail ("Expected NullReferenceException but got none");
172 catch (NullReferenceException) {
173 // this is what we expect
175 catch (Exception e) {
176 Assert.Fail ("Expected NullReferenceException but got: " + e.ToString ());
181 public void Digest ()
183 bool rightClass = false;
184 HashAlgorithm hash = null;
187 hash = sig.CreateDigest ();
188 Assert.Fail ("Expected ArgumentNullException but got none");
190 catch (ArgumentNullException) {
191 // this is what we expect
193 catch (Exception e) {
194 Assert.Fail ("Expected ArgumentNullException but got: " + e.ToString ());
197 sig.DigestAlgorithm = "SHA1";
198 hash = sig.CreateDigest ();
199 Assert.IsNotNull (hash, "CreateDigest(SHA1)");
200 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
201 Assert.IsTrue (rightClass, "CreateDigest(SHA1)");
203 sig.DigestAlgorithm = "MD5";
204 hash = sig.CreateDigest ();
205 Assert.IsNotNull (hash, "CreateDigest(MD5)");
206 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
207 Assert.IsTrue (rightClass, "CreateDigest(MD5)");
209 sig.DigestAlgorithm = "SHA256";
210 hash = sig.CreateDigest ();
211 Assert.IsNotNull (hash, "CreateDigest(SHA256)");
212 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
213 Assert.IsTrue (rightClass, "CreateDigest(SHA256)");
215 sig.DigestAlgorithm = "SHA384";
216 hash = sig.CreateDigest ();
217 Assert.IsNotNull (hash, "CreateDigest(SHA384)");
218 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
219 Assert.IsTrue (rightClass, "CreateDigest(SHA384)");
221 sig.DigestAlgorithm = "SHA512";
222 hash = sig.CreateDigest ();
223 Assert.IsNotNull (hash, "CreateDigest(SHA512)");
224 rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
225 Assert.IsTrue (rightClass, "CreateDigest(SHA512)");
227 sig.DigestAlgorithm = "bad";
228 hash = sig.CreateDigest ();
229 Assert.IsNull (hash, "CreateDigest(bad)");
233 public void Formatter ()
235 AsymmetricSignatureFormatter fmt = null;
236 // Formatter with all properties null
238 fmt = sig.CreateFormatter (dsa);
239 Assert.Fail ("Expected ArgumentNullException but got none");
241 catch (ArgumentNullException) {
242 // this is what we expect
244 catch (Exception e) {
245 Assert.Fail ("Expected ArgumentNullException but got: " + e.ToString ());
247 // Formatter with invalid FormatterAlgorithm property
248 sig.FormatterAlgorithm = "DSA";
250 fmt = sig.CreateFormatter (dsa);
251 Assert.Fail ("Expected InvalidCastException but got none");
253 catch (InvalidCastException) {
254 // this is what we expect
256 catch (Exception e) {
257 Assert.Fail ("Expected InvalidCastException but got: " + e.ToString ());
259 // Formatter with valid FormatterAlgorithm property
260 sig.FormatterAlgorithm = "DSASignatureFormatter";
262 fmt = sig.CreateFormatter (dsa);
263 Assert.Fail ("Expected NullReferenceException but got none");
265 catch (NullReferenceException) {
266 // this is what we expect
268 catch (Exception e) {
269 Assert.Fail ("Expected NullReferenceException but got: " + e.ToString ());
271 // Deformatter with valid DeformatterAlgorithm property
272 sig.KeyAlgorithm = "DSA";
273 sig.DigestAlgorithm = "SHA1";
274 sig.FormatterAlgorithm = "DSASignatureFormatter";
276 fmt = sig.CreateFormatter (dsa);
277 Assert.Fail ("Expected NullReferenceException but got none");
279 catch (NullReferenceException) {
280 // this is what we expect
282 catch (Exception e) {
283 Assert.Fail ("Expected NullReferenceException but got: " + e.ToString ());
288 public void DSASignatureDescription ()
290 // internal class - we cannot create one without CryptoConfig
291 SignatureDescription sd = (SignatureDescription) CryptoConfig.CreateFromName ("http://www.w3.org/2000/09/xmldsig#dsa-sha1");
292 Assert.AreEqual ("System.Security.Cryptography.SHA1CryptoServiceProvider", sd.DigestAlgorithm);
293 Assert.AreEqual ("System.Security.Cryptography.DSASignatureDeformatter", sd.DeformatterAlgorithm);
294 Assert.AreEqual ("System.Security.Cryptography.DSASignatureFormatter", sd.FormatterAlgorithm);
295 Assert.AreEqual ("System.Security.Cryptography.DSACryptoServiceProvider", sd.KeyAlgorithm);
297 HashAlgorithm hash = sd.CreateDigest();
298 Assert.AreEqual ("System.Security.Cryptography.SHA1CryptoServiceProvider", hash.ToString ());
300 Assert.AreEqual (dsa.ToString (), sd.KeyAlgorithm);
302 AsymmetricSignatureDeformatter asd = sd.CreateDeformatter (dsa);
303 Assert.AreEqual ("System.Security.Cryptography.DSASignatureDeformatter", asd.ToString ());
305 AsymmetricSignatureFormatter asf = sd.CreateFormatter (dsa);
306 Assert.AreEqual ("System.Security.Cryptography.DSASignatureFormatter", asf.ToString ());
310 public void RSASignatureDescription ()
312 // TODO: this would be cleaner with NUnit TestCase'es but they're NUnit 2.5+ :(
313 #if FULL_AOT_RUNTIME || MONOTOUCH || MONODROID
314 RSASignatureDescriptionCore ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", "System.Security.Cryptography.SHA1Cng", "System.Security.Cryptography.SHA1CryptoServiceProvider");
315 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", "System.Security.Cryptography.SHA256Cng", "System.Security.Cryptography.SHA256Managed");
316 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384", "System.Security.Cryptography.SHA384Cng", "System.Security.Cryptography.SHA384Managed");
317 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512", "System.Security.Cryptography.SHA512Cng", "System.Security.Cryptography.SHA512Managed");
319 RSASignatureDescriptionCore ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", "System.Security.Cryptography.SHA1Cng", "System.Security.Cryptography.SHA1Cng");
320 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", "System.Security.Cryptography.SHA256Cng", "System.Security.Cryptography.SHA256Cng");
321 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384", "System.Security.Cryptography.SHA384Cng", "System.Security.Cryptography.SHA384Cng");
322 RSASignatureDescriptionCore ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512", "System.Security.Cryptography.SHA512Cng", "System.Security.Cryptography.SHA512Cng");
326 void RSASignatureDescriptionCore (string name, string expectedDigestAlgorithm, string expectedSelectedDigestAlgorithm)
328 // internal class - we cannot create one without CryptoConfig
329 SignatureDescription sd = (SignatureDescription) CryptoConfig.CreateFromName (name);
330 Assert.AreEqual (expectedDigestAlgorithm, sd.DigestAlgorithm);
331 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureDeformatter", sd.DeformatterAlgorithm);
332 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureFormatter", sd.FormatterAlgorithm);
333 Assert.AreEqual ("System.Security.Cryptography.RSA", sd.KeyAlgorithm);
335 HashAlgorithm hash = sd.CreateDigest();
336 Assert.AreEqual (expectedSelectedDigestAlgorithm, hash.ToString ());
338 Assert.AreEqual ("System.Security.Cryptography.RSA", sd.KeyAlgorithm);
340 AsymmetricSignatureDeformatter asd = sd.CreateDeformatter (rsa);
341 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureDeformatter", asd.ToString ());
343 AsymmetricSignatureFormatter asf = sd.CreateFormatter (rsa);
344 Assert.AreEqual ("System.Security.Cryptography.RSAPKCS1SignatureFormatter", asf.ToString ());