2 // X509StoreTest.cs - NUnit tests for X509Store
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
12 using NUnit.Framework;
15 using System.Collections;
16 using System.Security.Cryptography;
17 using System.Security.Cryptography.X509Certificates;
20 namespace MonoTests.System.Security.Cryptography.X509Certificates {
23 public class X509StoreTest {
25 private X509Certificate2 cert_empty;
26 private X509Certificate2 cert1;
27 private X509Certificate2 cert2;
28 private X509Certificate2Collection coll_empty;
29 private X509Certificate2Collection coll;
32 public void FixtureSetUp ()
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 ();
42 CleanUpStore ("ReadOnlyStore");
48 CleanUpStore ("ReadWriteStore");
51 private void CleanUpStore (string s)
53 X509Store xs = new X509Store (s);
54 xs.Open (OpenFlags.ReadWrite);
55 int n = xs.Certificates.Count;
57 X509Certificate2[] array = new X509Certificate2[n];
58 xs.Certificates.CopyTo (array, 0);
59 foreach (X509Certificate2 x in array)
65 private void CheckDefaults (X509Store xs)
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");
75 public void ConstructorEmpty ()
77 X509Store xs = new X509Store ();
82 [ExpectedException (typeof (ArgumentNullException))]
83 public void ConstructorIntPtr ()
85 new X509Store (IntPtr.Zero);
89 [ExpectedException (typeof (ArgumentException))]
90 public void ConstructorStoreLocation_Invalid ()
92 new X509Store ((StoreLocation) Int32.MinValue);
96 public void ConstructorStoreLocationCurrentUser ()
98 X509Store xs = new X509Store (StoreLocation.CurrentUser);
103 public void ConstructorStoreLocationLocalMachine ()
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");
113 public void ConstructorStoreString_Null ()
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");
122 public void ConstructorStoreString_Empty ()
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");
131 public void ConstructorStoreStringAddressBook ()
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");
141 public void ConstructorStoreStringAuthRoot ()
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");
151 public void ConstructorStoreStringCertificateAuthority ()
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");
161 public void ConstructorStoreStringDisallowed ()
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");
171 public void ConstructorStoreStringMy ()
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");
181 public void ConstructorStoreStringRoot ()
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");
191 public void ConstructorStoreStringTrustedPeople ()
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");
201 public void ConstructorStoreStringTrustedPublisher ()
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");
211 public void ConstructorStoreStringMono ()
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");
222 [ExpectedException (typeof (ArgumentException))]
223 public void ConstructorStoreName_Invalid ()
225 new X509Store ((StoreName) Int32.MinValue);
229 public void ConstructorStoreNameAddressBook ()
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");
239 public void ConstructorStoreNameAuthRoot ()
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");
249 public void ConstructorStoreNameCertificateAuthority ()
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");
259 public void ConstructorStoreNameDisallowed ()
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");
269 public void ConstructorStoreNameMy ()
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");
279 public void ConstructorStoreNameRoot ()
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");
289 public void ConstructorStoreNameTrustedPeople ()
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");
299 public void ConstructorStoreNameTrustedPublisher ()
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");
309 [ExpectedException (typeof (ArgumentNullException))]
310 public void Add_Null ()
312 new X509Store ().Add (null);
316 [ExpectedException (typeof (CryptographicException))]
317 public void Add_NotOpened ()
319 // Open wasn't called
320 new X509Store ().Add (cert1);
324 [ExpectedException (typeof (CryptographicException))]
325 public void Add_OpenReadOnly ()
327 X509Store xs = new X509Store ("ReadOnlyStore");
328 xs.Open (OpenFlags.ReadOnly);
333 public void Add_SameCertificate ()
335 X509Store xs = new X509Store ("ReadWriteStore");
336 xs.Open (OpenFlags.ReadWrite);
337 int n = xs.Certificates.Count;
340 Assert.AreEqual (n + 1, xs.Certificates.Count, "Count");
345 [ExpectedException (typeof (CryptographicException))]
346 public void Add_Empty_Certificate ()
348 X509Store xs = new X509Store ("ReadWriteStore");
349 xs.Open (OpenFlags.ReadWrite);
354 [ExpectedException (typeof (CryptographicException))]
355 public void Add_ExistingCertificateReadOnly ()
357 X509Store xs = new X509Store ("ReadWriteStore");
358 xs.Open (OpenFlags.ReadWrite);
361 xs.Open (OpenFlags.ReadOnly);
367 [ExpectedException (typeof (ArgumentNullException))]
368 public void AddRange_Null ()
370 new X509Store ().AddRange (null);
374 public void AddRange_Empty_Closed ()
376 X509Store xs = new X509Store ("ReadWriteStore");
377 xs.AddRange (coll_empty);
378 Assert.AreEqual (coll_empty.Count, xs.Certificates.Count, "Count");
382 public void AddRange_Empty_ReadOnly ()
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");
391 public void AddRange_Empty_ReadWrite ()
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");
400 [ExpectedException (typeof (CryptographicException))]
401 public void AddRange_Empty_Certificate ()
403 X509Store xs = new X509Store ("ReadWriteStore");
404 xs.Open (OpenFlags.ReadWrite);
405 xs.AddRange (new X509Certificate2Collection (cert_empty));
409 public void AddRange ()
411 X509Store xs = new X509Store ("ReadWriteStore");
412 xs.Open (OpenFlags.ReadWrite);
414 Assert.AreEqual (coll.Count, xs.Certificates.Count, "Count");
418 [ExpectedException (typeof (CryptographicException))]
419 public void AddRange_NotOpened ()
421 // Open wasn't called
422 new X509Store ().AddRange (coll);
426 [ExpectedException (typeof (CryptographicException))]
427 public void AddRange_OpenReadOnly ()
429 X509Store xs = new X509Store ("ReadOnlyStore");
430 xs.Open (OpenFlags.ReadOnly);
435 public void Close_NotOpen ()
437 new X509Store ().Close ();
441 public void Close_Collection ()
443 X509Store xs = new X509Store ("ReadWriteStore");
444 xs.Open (OpenFlags.ReadWrite);
446 Assert.AreEqual (1, xs.Certificates.Count, "Open");
448 Assert.AreEqual (0, xs.Certificates.Count, "Close");
452 public void Open_Invalid ()
454 X509Store xs = new X509Store ("ReadWriteStore");
455 xs.Open ((OpenFlags) Int32.MinValue);
459 [ExpectedException (typeof (CryptographicException))]
460 public void Open_OpenExistingOnly ()
462 new X509Store ("doesn't-exists").Open (OpenFlags.OpenExistingOnly);
466 [ExpectedException (typeof (CryptographicException))]
467 public void Open_Store_Null ()
469 // ctor is valid (see test) but can't be opened
470 new X509Store (null).Open (OpenFlags.ReadOnly);
474 [ExpectedException (typeof (CryptographicException))]
475 public void Open_Store_Empty ()
477 // ctor is valid (see test) but can't be opened
478 new X509Store (String.Empty).Open (OpenFlags.ReadOnly);
482 [ExpectedException (typeof (ArgumentNullException))]
483 public void Remove_Null ()
485 new X509Store ().Remove (null);
489 [ExpectedException (typeof (CryptographicException))]
490 public void Remove_NotOpened ()
492 // Open wasn't called
493 new X509Store ().Remove (cert1);
497 public void Remove_OpenReadOnly_Unexisting ()
499 X509Store xs = new X509Store ("ReadOnlyStore");
500 xs.Open (OpenFlags.ReadOnly);
501 // note: cert1 wasn't present, remove "succeed"
506 [ExpectedException (typeof (CryptographicException))]
507 public void Remove_OpenReadOnly_Existing ()
509 X509Store xs = new X509Store ("ReadWriteStore");
510 xs.Open (OpenFlags.ReadWrite);
513 xs.Open (OpenFlags.ReadOnly);
518 public void Remove_Empty_Certificate ()
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
525 xs.Remove (cert_empty);
529 [ExpectedException (typeof (ArgumentNullException))]
530 public void RemoveRange_Null ()
532 new X509Store ().RemoveRange (null);
536 public void RemoveRange_Empty ()
538 X509Store xs = new X509Store ();
539 xs.RemoveRange (coll_empty);
543 [ExpectedException (typeof (CryptographicException))]
544 public void RemoveRange_NotOpened ()
546 // Open wasn't called
547 new X509Store ().RemoveRange (coll);
551 public void RemoveRange_OpenReadOnly_Unexisting ()
553 X509Store xs = new X509Store ("ReadOnlyStore");
554 xs.Open (OpenFlags.ReadOnly);
555 // note: cert1 wasn't present, RemoveRange "succeed"
556 xs.RemoveRange (coll);
560 [ExpectedException (typeof (CryptographicException))]
561 public void RemoveRange_OpenReadOnly_Existing ()
563 X509Store xs = new X509Store ("ReadWriteStore");
564 xs.Open (OpenFlags.ReadWrite);
567 xs.Open (OpenFlags.ReadOnly);
568 xs.RemoveRange (coll);
572 public void RemoveRange_Empty_Certificate ()
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
579 xs.RemoveRange (new X509Certificate2Collection (cert_empty));
583 public void Collection_Add ()
585 X509Store xs = new X509Store ("ReadWriteStore");
586 xs.Certificates.Add (cert1);
587 Assert.AreEqual (0, xs.Certificates.Count, "Not Open");
589 Assert.AreEqual (0, xs.Certificates.Count, "Close");