New test.
[mono.git] / mcs / class / System / Test / System.Security.Cryptography.X509Certificates / X509StoreTest.cs
1 //
2 // X509StoreTest.cs - NUnit tests for X509Store
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 #if NET_2_0
11
12 using NUnit.Framework;
13
14 using System;
15 using System.Collections;
16 using System.Security.Cryptography;
17 using System.Security.Cryptography.X509Certificates;
18 using System.Text;
19
20 namespace MonoTests.System.Security.Cryptography.X509Certificates {
21
22         [TestFixture]
23         public class X509StoreTest {
24
25                 private X509Certificate2 cert_empty;
26                 private X509Certificate2 cert1;
27                 private X509Certificate2 cert2;
28                 private X509Certificate2Collection coll_empty;
29                 private X509Certificate2Collection coll;
30
31                 [TestFixtureSetUp]
32                 public void FixtureSetUp ()
33                 {
34                         cert_empty = new X509Certificate2 ();
35                         cert1 = new X509Certificate2 (X509Certificate2Test.farscape_pfx, "farscape", X509KeyStorageFlags.Exportable);
36                         cert2 = new X509Certificate2 (Encoding.ASCII.GetBytes (X509Certificate2Test.base64_cert));
37                         coll_empty = new X509Certificate2Collection ();
38                         coll = new X509Certificate2Collection ();
39                         coll.Add (cert1);
40                         coll.Add (cert2);
41
42                         CleanUpStore ("ReadOnlyStore");
43                 }
44
45                 [SetUp]
46                 public void SetUp ()
47                 {
48                         CleanUpStore ("ReadWriteStore");
49                 }
50
51                 private void CleanUpStore (string s)
52                 {
53                         X509Store xs = new X509Store (s);
54                         xs.Open (OpenFlags.ReadWrite);
55                         int n = xs.Certificates.Count;
56                         if (n > 0) {
57                                 X509Certificate2[] array = new X509Certificate2[n];
58                                 xs.Certificates.CopyTo (array, 0);
59                                 foreach (X509Certificate2 x in array)
60                                         xs.Remove (x);
61                         }
62                         xs.Close ();
63                 }
64
65                 private void CheckDefaults (X509Store xs)
66                 {
67                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
68                         Assert.AreEqual ("MY", xs.Name, "Name");
69                         Assert.IsNotNull (xs.Certificates, "Certificates");
70                         // always IntPtr.Zero for Mono, IntPtr.Zero before being opened on Windows
71                         Assert.AreEqual (IntPtr.Zero, xs.StoreHandle, "StoreHandle");
72                 }
73
74                 [Test]
75                 public void ConstructorEmpty () 
76                 {
77                         X509Store xs = new X509Store ();
78                         CheckDefaults (xs);
79                 }
80
81                 [Test]
82                 [ExpectedException (typeof (ArgumentNullException))]
83                 public void ConstructorIntPtr ()
84                 {
85                         new X509Store (IntPtr.Zero);
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (ArgumentException))]
90                 public void ConstructorStoreLocation_Invalid ()
91                 {
92                         new X509Store ((StoreLocation) Int32.MinValue);
93                 }
94                 
95                 [Test]
96                 public void ConstructorStoreLocationCurrentUser () 
97                 {
98                         X509Store xs = new X509Store (StoreLocation.CurrentUser);
99                         CheckDefaults (xs);
100                 }
101
102                 [Test]
103                 public void ConstructorStoreLocationLocalMachine () 
104                 {
105                         X509Store xs = new X509Store (StoreLocation.LocalMachine);
106                         // default properties
107                         Assert.AreEqual (StoreLocation.LocalMachine, xs.Location, "Location");
108                         Assert.AreEqual ("MY", xs.Name, "Name");
109                         Assert.IsNotNull (xs.Certificates, "Certificates");
110                 }
111
112                 [Test]
113                 public void ConstructorStoreString_Null ()
114                 {
115                         X509Store xs = new X509Store (null);
116                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
117                         Assert.IsNull (xs.Name, "Name");
118                         Assert.IsNotNull (xs.Certificates, "Certificates");
119                 }
120
121                 [Test]
122                 public void ConstructorStoreString_Empty ()
123                 {
124                         X509Store xs = new X509Store (String.Empty);
125                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
126                         Assert.AreEqual (String.Empty, xs.Name, "Name");
127                         Assert.IsNotNull (xs.Certificates, "Certificates");
128                 }
129
130                 [Test]
131                 public void ConstructorStoreStringAddressBook () 
132                 {
133                         X509Store xs = new X509Store ("AddressBook");
134                         // default properties
135                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
136                         Assert.AreEqual ("AddressBook", xs.Name, "Name");
137                         Assert.IsNotNull (xs.Certificates, "Certificates");
138                 }
139
140                 [Test]
141                 public void ConstructorStoreStringAuthRoot () 
142                 {
143                         X509Store xs = new X509Store ("AuthRoot");
144                         // default properties
145                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
146                         Assert.AreEqual ("AuthRoot", xs.Name, "Name");
147                         Assert.IsNotNull (xs.Certificates, "Certificates");
148                 }
149
150                 [Test]
151                 public void ConstructorStoreStringCertificateAuthority () 
152                 {
153                         X509Store xs = new X509Store ("CA");
154                         // default properties
155                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
156                         Assert.AreEqual ("CA", xs.Name, "Name");
157                         Assert.IsNotNull (xs.Certificates, "Certificates");
158                 }
159
160                 [Test]
161                 public void ConstructorStoreStringDisallowed () 
162                 {
163                         X509Store xs = new X509Store ("Disallowed");
164                         // default properties
165                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
166                         Assert.AreEqual ("Disallowed", xs.Name, "Name");
167                         Assert.IsNotNull (xs.Certificates, "Certificates");
168                 }
169
170                 [Test]
171                 public void ConstructorStoreStringMy () 
172                 {
173                         X509Store xs = new X509Store ("My");
174                         // default properties
175                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
176                         Assert.AreEqual ("My", xs.Name, "Name");
177                         Assert.IsNotNull (xs.Certificates, "Certificates");
178                 }
179
180                 [Test]
181                 public void ConstructorStoreStringRoot () 
182                 {
183                         X509Store xs = new X509Store ("Root");
184                         // default properties
185                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
186                         Assert.AreEqual ("Root", xs.Name, "Name");
187                         Assert.IsNotNull (xs.Certificates, "Certificates");
188                 }
189
190                 [Test]
191                 public void ConstructorStoreStringTrustedPeople () 
192                 {
193                         X509Store xs = new X509Store ("TrustedPeople");
194                         // default properties
195                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
196                         Assert.AreEqual ("TrustedPeople", xs.Name, "Name");
197                         Assert.IsNotNull (xs.Certificates, "Certificates");
198                 }
199
200                 [Test]
201                 public void ConstructorStoreStringTrustedPublisher () 
202                 {
203                         X509Store xs = new X509Store ("TrustedPublisher");
204                         // default properties
205                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
206                         Assert.AreEqual ("TrustedPublisher", xs.Name, "Name");
207                         Assert.IsNotNull (xs.Certificates, "Certificates");
208                 }
209
210                 [Test]
211                 public void ConstructorStoreStringMono () 
212                 {
213                         // mono isn't defined the StoreName
214                         X509Store xs = new X509Store ("Mono");
215                         // default properties
216                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
217                         Assert.AreEqual ("Mono", xs.Name, "Name");
218                         Assert.IsNotNull (xs.Certificates, "Certificates");
219                 }
220
221                 [Test]
222                 [ExpectedException (typeof (ArgumentException))]
223                 public void ConstructorStoreName_Invalid ()
224                 {
225                         new X509Store ((StoreName) Int32.MinValue);
226                 }
227
228                 [Test]
229                 public void ConstructorStoreNameAddressBook () 
230                 {
231                         X509Store xs = new X509Store (StoreName.AddressBook);
232                         // default properties
233                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
234                         Assert.AreEqual ("AddressBook", xs.Name, "Name");
235                         Assert.IsNotNull (xs.Certificates, "Certificates");
236                 }
237
238                 [Test]
239                 public void ConstructorStoreNameAuthRoot () 
240                 {
241                         X509Store xs = new X509Store (StoreName.AuthRoot);
242                         // default properties
243                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
244                         Assert.AreEqual ("AuthRoot", xs.Name, "Name");
245                         Assert.IsNotNull (xs.Certificates, "Certificates");
246                 }
247
248                 [Test]
249                 public void ConstructorStoreNameCertificateAuthority () 
250                 {
251                         X509Store xs = new X509Store (StoreName.CertificateAuthority);
252                         // default properties
253                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
254                         Assert.AreEqual ("CA", xs.Name, "Name");
255                         Assert.IsNotNull (xs.Certificates, "Certificates");
256                 }
257
258                 [Test]
259                 public void ConstructorStoreNameDisallowed () 
260                 {
261                         X509Store xs = new X509Store (StoreName.Disallowed);
262                         // default properties
263                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
264                         Assert.AreEqual ("Disallowed", xs.Name, "Name");
265                         Assert.IsNotNull (xs.Certificates, "Certificates");
266                 }
267
268                 [Test]
269                 public void ConstructorStoreNameMy () 
270                 {
271                         X509Store xs = new X509Store (StoreName.My);
272                         // default properties
273                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
274                         Assert.AreEqual ("My", xs.Name, "Name");
275                         Assert.IsNotNull (xs.Certificates, "Certificates");
276                 }
277
278                 [Test]
279                 public void ConstructorStoreNameRoot () 
280                 {
281                         X509Store xs = new X509Store (StoreName.Root);
282                         // default properties
283                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
284                         Assert.AreEqual ("Root", xs.Name, "Name");
285                         Assert.IsNotNull (xs.Certificates, "Certificates");
286                 }
287
288                 [Test]
289                 public void ConstructorStoreNameTrustedPeople () 
290                 {
291                         X509Store xs = new X509Store (StoreName.TrustedPeople);
292                         // default properties
293                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
294                         Assert.AreEqual ("TrustedPeople", xs.Name, "Name");
295                         Assert.IsNotNull (xs.Certificates, "Certificates");
296                 }
297
298                 [Test]
299                 public void ConstructorStoreNameTrustedPublisher () 
300                 {
301                         X509Store xs = new X509Store (StoreName.TrustedPublisher);
302                         // default properties
303                         Assert.AreEqual (StoreLocation.CurrentUser, xs.Location, "Location");
304                         Assert.AreEqual ("TrustedPublisher", xs.Name, "Name");
305                         Assert.IsNotNull (xs.Certificates, "Certificates");
306                 }
307
308                 [Test]
309                 [ExpectedException (typeof (ArgumentNullException))]
310                 public void Add_Null ()
311                 {
312                         new X509Store ().Add (null);
313                 }
314
315                 [Test]
316                 [ExpectedException (typeof (CryptographicException))]
317                 public void Add_NotOpened ()
318                 {
319                         // Open wasn't called
320                         new X509Store ().Add (cert1);
321                 }
322
323                 [Test]
324                 [ExpectedException (typeof (CryptographicException))]
325                 public void Add_OpenReadOnly ()
326                 {
327                         X509Store xs = new X509Store ("ReadOnlyStore");
328                         xs.Open (OpenFlags.ReadOnly);
329                         xs.Add (cert1);
330                 }
331
332                 [Test]
333                 public void Add_SameCertificate ()
334                 {
335                         X509Store xs = new X509Store ("ReadWriteStore");
336                         xs.Open (OpenFlags.ReadWrite);
337                         int n = xs.Certificates.Count;
338                         xs.Add (cert1);
339                         xs.Add (cert1);
340                         Assert.AreEqual (n + 1, xs.Certificates.Count, "Count");
341                         xs.Close ();
342                 }
343
344                 [Test]
345                 [ExpectedException (typeof (CryptographicException))]
346                 public void Add_Empty_Certificate ()
347                 {
348                         X509Store xs = new X509Store ("ReadWriteStore");
349                         xs.Open (OpenFlags.ReadWrite);
350                         xs.Add (cert_empty);
351                 }
352
353                 [Test]
354                 [ExpectedException (typeof (CryptographicException))]
355                 public void Add_ExistingCertificateReadOnly ()
356                 {
357                         X509Store xs = new X509Store ("ReadWriteStore");
358                         xs.Open (OpenFlags.ReadWrite);
359                         xs.Add (cert1);
360                         xs.Close ();
361                         xs.Open (OpenFlags.ReadOnly);
362                         xs.Add (cert1);
363                         xs.Close ();
364                 }
365
366                 [Test]
367                 [ExpectedException (typeof (ArgumentNullException))]
368                 public void AddRange_Null ()
369                 {
370                         new X509Store ().AddRange (null);
371                 }
372
373                 [Test]
374                 public void AddRange_Empty_Closed ()
375                 {
376                         X509Store xs = new X509Store ("ReadWriteStore");
377                         xs.AddRange (coll_empty);
378                         Assert.AreEqual (coll_empty.Count, xs.Certificates.Count, "Count");
379                 }
380
381                 [Test]
382                 public void AddRange_Empty_ReadOnly ()
383                 {
384                         X509Store xs = new X509Store ("ReadWriteStore");
385                         xs.Open (OpenFlags.ReadOnly);
386                         xs.AddRange (coll_empty);
387                         Assert.AreEqual (coll_empty.Count, xs.Certificates.Count, "Count");
388                 }
389
390                 [Test]
391                 public void AddRange_Empty_ReadWrite ()
392                 {
393                         X509Store xs = new X509Store ("ReadWriteStore");
394                         xs.Open (OpenFlags.ReadWrite);
395                         xs.AddRange (coll_empty);
396                         Assert.AreEqual (coll_empty.Count, xs.Certificates.Count, "Count");
397                 }
398
399                 [Test]
400                 [ExpectedException (typeof (CryptographicException))]
401                 public void AddRange_Empty_Certificate ()
402                 {
403                         X509Store xs = new X509Store ("ReadWriteStore");
404                         xs.Open (OpenFlags.ReadWrite);
405                         xs.AddRange (new X509Certificate2Collection (cert_empty));
406                 }
407
408                 [Test]
409                 public void AddRange ()
410                 {
411                         X509Store xs = new X509Store ("ReadWriteStore");
412                         xs.Open (OpenFlags.ReadWrite);
413                         xs.AddRange (coll);
414                         Assert.AreEqual (coll.Count, xs.Certificates.Count, "Count");
415                 }
416
417                 [Test]
418                 [ExpectedException (typeof (CryptographicException))]
419                 public void AddRange_NotOpened ()
420                 {
421                         // Open wasn't called
422                         new X509Store ().AddRange (coll);
423                 }
424
425                 [Test]
426                 [ExpectedException (typeof (CryptographicException))]
427                 public void AddRange_OpenReadOnly ()
428                 {
429                         X509Store xs = new X509Store ("ReadOnlyStore");
430                         xs.Open (OpenFlags.ReadOnly);
431                         xs.AddRange (coll);
432                 }
433
434                 [Test]
435                 public void Close_NotOpen ()
436                 {
437                         new X509Store ().Close ();
438                 }
439
440                 [Test]
441                 public void Close_Collection ()
442                 {
443                         X509Store xs = new X509Store ("ReadWriteStore");
444                         xs.Open (OpenFlags.ReadWrite);
445                         xs.Add (cert1);
446                         Assert.AreEqual (1, xs.Certificates.Count, "Open");
447                         xs.Close ();
448                         Assert.AreEqual (0, xs.Certificates.Count, "Close");
449                 }
450
451                 [Test]
452                 public void Open_Invalid ()
453                 {
454                         X509Store xs = new X509Store ("ReadWriteStore");
455                         xs.Open ((OpenFlags) Int32.MinValue);
456                 }
457
458                 [Test]
459                 [ExpectedException (typeof (CryptographicException))]
460                 public void Open_OpenExistingOnly ()
461                 {
462                         new X509Store ("doesn't-exists").Open (OpenFlags.OpenExistingOnly);
463                 }
464
465                 [Test]
466                 [ExpectedException (typeof (CryptographicException))]
467                 public void Open_Store_Null ()
468                 {
469                         // ctor is valid (see test) but can't be opened
470                         new X509Store (null).Open (OpenFlags.ReadOnly);
471                 }
472
473                 [Test]
474                 [ExpectedException (typeof (CryptographicException))]
475                 public void Open_Store_Empty ()
476                 {
477                         // ctor is valid (see test) but can't be opened
478                         new X509Store (String.Empty).Open (OpenFlags.ReadOnly);
479                 }
480
481                 [Test]
482                 [ExpectedException (typeof (ArgumentNullException))]
483                 public void Remove_Null ()
484                 {
485                         new X509Store ().Remove (null);
486                 }
487
488                 [Test]
489                 [ExpectedException (typeof (CryptographicException))]
490                 public void Remove_NotOpened ()
491                 {
492                         // Open wasn't called
493                         new X509Store ().Remove (cert1);
494                 }
495
496                 [Test]
497                 public void Remove_OpenReadOnly_Unexisting ()
498                 {
499                         X509Store xs = new X509Store ("ReadOnlyStore");
500                         xs.Open (OpenFlags.ReadOnly);
501                         // note: cert1 wasn't present, remove "succeed"
502                         xs.Remove (cert1);
503                 }
504
505                 [Test]
506                 [ExpectedException (typeof (CryptographicException))]
507                 public void Remove_OpenReadOnly_Existing ()
508                 {
509                         X509Store xs = new X509Store ("ReadWriteStore");
510                         xs.Open (OpenFlags.ReadWrite);
511                         xs.Add (cert1);
512                         xs.Close ();
513                         xs.Open (OpenFlags.ReadOnly);
514                         xs.Remove (cert1);
515                 }
516
517                 [Test]
518                 public void Remove_Empty_Certificate ()
519                 {
520                         X509Store xs = new X509Store ("ReadWriteStore");
521                         xs.Open (OpenFlags.ReadWrite);
522                         // note: impossible to add cert_empty, so we add something else
523                         // to be sure we'll follow the complete code path (loop) of removal
524                         xs.Add (cert1);
525                         xs.Remove (cert_empty);
526                 }
527
528                 [Test]
529                 [ExpectedException (typeof (ArgumentNullException))]
530                 public void RemoveRange_Null ()
531                 {
532                         new X509Store ().RemoveRange (null);
533                 }
534
535                 [Test]
536                 public void RemoveRange_Empty ()
537                 {
538                         X509Store xs = new X509Store ();
539                         xs.RemoveRange (coll_empty);
540                 }
541
542                 [Test]
543                 [ExpectedException (typeof (CryptographicException))]
544                 public void RemoveRange_NotOpened ()
545                 {
546                         // Open wasn't called
547                         new X509Store ().RemoveRange (coll);
548                 }
549
550                 [Test]
551                 public void RemoveRange_OpenReadOnly_Unexisting ()
552                 {
553                         X509Store xs = new X509Store ("ReadOnlyStore");
554                         xs.Open (OpenFlags.ReadOnly);
555                         // note: cert1 wasn't present, RemoveRange "succeed"
556                         xs.RemoveRange (coll);
557                 }
558
559                 [Test]
560                 [ExpectedException (typeof (CryptographicException))]
561                 public void RemoveRange_OpenReadOnly_Existing ()
562                 {
563                         X509Store xs = new X509Store ("ReadWriteStore");
564                         xs.Open (OpenFlags.ReadWrite);
565                         xs.AddRange (coll);
566                         xs.Close ();
567                         xs.Open (OpenFlags.ReadOnly);
568                         xs.RemoveRange (coll);
569                 }
570
571                 [Test]
572                 public void RemoveRange_Empty_Certificate ()
573                 {
574                         X509Store xs = new X509Store ("ReadWriteStore");
575                         xs.Open (OpenFlags.ReadWrite);
576                         // note: impossible to add cert_empty, so we add something else
577                         // to be sure we'll follow the complete code path (loop) of removal
578                         xs.AddRange (coll);
579                         xs.RemoveRange (new X509Certificate2Collection (cert_empty));
580                 }
581
582                 [Test]
583                 public void Collection_Add ()
584                 {
585                         X509Store xs = new X509Store ("ReadWriteStore");
586                         xs.Certificates.Add (cert1);
587                         Assert.AreEqual (0, xs.Certificates.Count, "Not Open");
588                         xs.Close ();
589                         Assert.AreEqual (0, xs.Certificates.Count, "Close");
590                 }
591         }
592 }
593
594 #endif