* Makefile: Don't build make-map.exe.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / SignatureDescriptionTest.cs
1 //
2 // SignatureDescriptionTest.cs - NUnit Test Cases for SignatureDescription
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell  http://www.novell.com
9 //
10
11 using NUnit.Framework;
12 using System;
13 using System.Security;
14 using System.Security.Cryptography;
15
16 namespace MonoTests.System.Security.Cryptography {
17
18 [TestFixture]
19 public class SignatureDescriptionTest : Assertion {
20
21         protected SignatureDescription sig;
22         protected static DSA dsa;
23         protected static RSA rsa;
24
25         [SetUp]
26         public void SetUp () 
27         {
28                 sig = new SignatureDescription();
29                 // key generation is VERY long so one time is enough
30                 if (dsa == null)
31                         dsa = DSA.Create ();
32                 if (rsa == null)
33                         rsa = RSA.Create ();
34         }
35
36         public void AssertEquals (string msg, byte[] array1, byte[] array2) 
37         {
38                 AllTests.AssertEquals (msg, array1, array2);
39         }
40
41         [Test]
42         public void Constructor_Default () 
43         {
44                 // empty constructor
45                 SignatureDescription sig = new SignatureDescription ();
46                 AssertNotNull ("SignatureDescription()", sig);
47         }
48         
49         [Test]
50         [ExpectedException (typeof (ArgumentNullException))]
51         public void Constructor_Null () 
52         {
53                 // null constructor
54                 SignatureDescription sig = new SignatureDescription (null);
55                 // LAMESPEC: Documented as CryptographicException
56         }
57         
58         [Test]
59         public void Constructor_SecurityElement_Empty () 
60         {
61                 // (empty) SecurityElement constructor
62                 SecurityElement se = new SecurityElement ("xml");
63                 SignatureDescription sig = new SignatureDescription (se);
64                 AssertNotNull ("SignatureDescription(SecurityElement)", sig);
65         }
66
67         [Test]
68         public void Constructor_SecurityElement_DSA ()
69         {
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"));
75
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);
82         }
83
84         [Test]
85         public void Constructor_SecurityElement_RSA ()
86         {
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"));
92
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);
99         }
100
101         [Test]
102         public void Properties () 
103         {
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);
111
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);
118
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);
125
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);
132         }
133
134         [Test]
135         public void Deformatter () 
136         {
137                 AsymmetricSignatureDeformatter def = null;
138                 // Deformatter with all properties null
139                 try {
140                         def = sig.CreateDeformatter (dsa);
141                         Fail ("Expected ArgumentNullException but got none");
142                 }
143                 catch (ArgumentNullException) {
144                         // this is what we expect
145                 }
146                 catch (Exception e) {
147                         Fail ("Expected ArgumentNullException but got: " + e.ToString ());
148                 }
149                 // Deformatter with invalid DeformatterAlgorithm property
150                 sig.DeformatterAlgorithm = "DSA";
151                 try {
152                         def = sig.CreateDeformatter (dsa);
153                         Fail ("Expected InvalidCastException but got none");
154                 }
155                 catch (InvalidCastException) {
156                         // this is what we expect
157                 }
158                 catch (Exception e) {
159                         Fail ("Expected InvalidCastException but got: " + e.ToString ());
160                 }
161                 // Deformatter with valid DeformatterAlgorithm property
162                 sig.DeformatterAlgorithm = "DSASignatureDeformatter";
163                 try {
164                         def = sig.CreateDeformatter (dsa);
165                         Fail ("Expected NullReferenceException but got none");
166                 }
167                 catch (NullReferenceException) {
168                         // this is what we expect
169                 }
170                 catch (Exception e) {
171                         Fail ("Expected NullReferenceException but got: " + e.ToString ());
172                 }
173                 // Deformatter with valid DeformatterAlgorithm property
174                 sig.KeyAlgorithm = "DSA";
175                 sig.DigestAlgorithm = "SHA1";
176                 sig.DeformatterAlgorithm = "DSASignatureDeformatter";
177                 try {
178                         def = sig.CreateDeformatter (dsa);
179                         Fail ("Expected NullReferenceException but got none");
180                 }
181                 catch (NullReferenceException) {
182                         // this is what we expect
183                 }
184                 catch (Exception e) {
185                         Fail ("Expected NullReferenceException but got: " + e.ToString ());
186                 }
187         }
188
189         [Test]
190         public void Digest ()
191         {
192                 bool rightClass = false;
193                 HashAlgorithm hash = null;
194                 // null hash
195                 try {
196                         hash = sig.CreateDigest ();
197                         Fail ("Expected ArgumentNullException but got none");
198                 }
199                 catch (ArgumentNullException) {
200                         // this is what we expect
201                 }
202                 catch (Exception e) {
203                         Fail ("Expected ArgumentNullException but got: " + e.ToString ());
204                 }
205
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);
211
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);
217
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);
223
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);
229
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);
235
236                 sig.DigestAlgorithm = "bad";
237                 hash = sig.CreateDigest ();
238                 AssertNull ("CreateDigest(bad)", hash);
239         }
240
241         [Test]
242         public void Formatter () 
243         {
244                 AsymmetricSignatureFormatter fmt = null;
245                 // Formatter with all properties null
246                 try {
247                         fmt = sig.CreateFormatter (dsa);
248                         Fail ("Expected ArgumentNullException but got none");
249                 }
250                 catch (ArgumentNullException) {
251                         // this is what we expect
252                 }
253                 catch (Exception e) {
254                         Fail ("Expected ArgumentNullException but got: " + e.ToString ());
255                 }
256                 // Formatter with invalid FormatterAlgorithm property
257                 sig.FormatterAlgorithm = "DSA";
258                 try {
259                         fmt = sig.CreateFormatter (dsa);
260                         Fail ("Expected InvalidCastException but got none");
261                 }
262                 catch (InvalidCastException) {
263                         // this is what we expect
264                 }
265                 catch (Exception e) {
266                         Fail ("Expected InvalidCastException but got: " + e.ToString ());
267                 }
268                 // Formatter with valid FormatterAlgorithm property
269                 sig.FormatterAlgorithm = "DSASignatureFormatter";
270                 try {
271                         fmt = sig.CreateFormatter (dsa);
272                         Fail ("Expected NullReferenceException but got none");
273                 }
274                 catch (NullReferenceException) {
275                         // this is what we expect
276                 }
277                 catch (Exception e) {
278                         Fail ("Expected NullReferenceException but got: " + e.ToString ());
279                 }
280                 // Deformatter with valid DeformatterAlgorithm property
281                 sig.KeyAlgorithm = "DSA";
282                 sig.DigestAlgorithm = "SHA1";
283                 sig.FormatterAlgorithm = "DSASignatureFormatter";
284                 try {
285                         fmt = sig.CreateFormatter (dsa);
286                         Fail ("Expected NullReferenceException but got none");
287                 }
288                 catch (NullReferenceException) {
289                         // this is what we expect
290                 }
291                 catch (Exception e) {
292                         Fail ("Expected NullReferenceException but got: " + e.ToString ());
293                 }
294         }
295
296         [Test]
297         public void DSASignatureDescription ()  
298         {
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);
305
306                 HashAlgorithm hash = sd.CreateDigest();
307                 AssertEquals ("DSA.CreateDigest", "System.Security.Cryptography.SHA1CryptoServiceProvider", hash.ToString ());
308
309                 AssertEquals ("DSA.Create", dsa.ToString (), sd.KeyAlgorithm);
310
311                 AsymmetricSignatureDeformatter asd = sd.CreateDeformatter (dsa);
312                 AssertEquals ("DSA.CreateDeformatter", "System.Security.Cryptography.DSASignatureDeformatter", asd.ToString ());
313
314                 AsymmetricSignatureFormatter asf = sd.CreateFormatter (dsa);
315                 AssertEquals ("DSA.CreateFormatter", "System.Security.Cryptography.DSASignatureFormatter", asf.ToString ());
316         }
317
318         [Test]
319         public void RSASignatureDescription () 
320         {
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);
327
328                 HashAlgorithm hash = sd.CreateDigest();
329                 AssertEquals ("RSA.CreateDigest", "System.Security.Cryptography.SHA1CryptoServiceProvider", hash.ToString ());
330
331                 AssertEquals ("RSA.Create", rsa.ToString (), sd.KeyAlgorithm);
332
333                 AsymmetricSignatureDeformatter asd = sd.CreateDeformatter (rsa);
334                 AssertEquals ("RSA.CreateDeformatter", "System.Security.Cryptography.RSAPKCS1SignatureDeformatter", asd.ToString ());
335
336                 AsymmetricSignatureFormatter asf = sd.CreateFormatter (rsa);
337                 AssertEquals ("RSA.CreateFormatter", "System.Security.Cryptography.RSAPKCS1SignatureFormatter", asf.ToString ());
338         }
339 }
340
341 }