2 // X509CertificateCollection2Test.cs
3 // - NUnit tests for X509CertificateCollection2
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
35 using System.Security.Cryptography;
36 using System.Security.Cryptography.X509Certificates;
39 namespace MonoTests.System.Security.Cryptography.X509Certificates {
42 public class X509Certificate2CollectionTest {
44 private X509Certificate2Collection empty;
45 private X509Certificate2Collection single;
46 private X509Certificate2Collection collection;
48 private X509Certificate2 cert_empty;
49 private X509Certificate2 cert1;
50 private X509Certificate2 cert2;
53 public void FixtureSetUp ()
55 cert_empty = new X509Certificate2 ();
56 cert1 = new X509Certificate2 (X509Certificate2Test.farscape_pfx, "farscape", X509KeyStorageFlags.Exportable);
57 cert2 = new X509Certificate2 (Encoding.ASCII.GetBytes (X509Certificate2Test.base64_cert));
59 empty = new X509Certificate2Collection ();
60 single = new X509Certificate2Collection ();
62 collection = new X509Certificate2Collection (single);
63 collection.Add (cert2);
69 X509Certificate2Collection c = new X509Certificate2Collection ();
70 Assert.AreEqual (0, c.Count, "Count");
74 [ExpectedException (typeof (ArgumentNullException))]
75 public void Ctor_X509CertificateCollection2_Null ()
77 new X509Certificate2Collection ((X509Certificate2Collection) null);
81 public void Ctor_X509CertificateCollection2_Empty ()
83 X509Certificate2Collection c = new X509Certificate2Collection (empty);
84 Assert.AreEqual (0, c.Count, "Count");
88 public void Ctor_X509CertificateCollection2 ()
90 X509Certificate2Collection c = new X509Certificate2Collection (collection);
91 Assert.AreEqual (2, c.Count, "Count");
95 [ExpectedException (typeof (ArgumentNullException))]
96 public void Ctor_X509Certificate2_Null ()
98 new X509Certificate2Collection ((X509Certificate2) null);
102 [ExpectedException (typeof (ArgumentNullException))]
103 public void Ctor_X509Certificate2Array_Null ()
105 new X509Certificate2Collection ((X509Certificate2[]) null);
109 public void Ctor_X509Certificate2Array_Empty ()
111 X509Certificate2[] array = new X509Certificate2 [0];
112 X509Certificate2Collection c = new X509Certificate2Collection (array);
113 Assert.AreEqual (0, c.Count, "Count");
117 public void Ctor_X509Certificate2Array ()
119 X509Certificate2[] array = new X509Certificate2[3] { cert1, cert2, cert_empty };
120 X509Certificate2Collection c = new X509Certificate2Collection (array);
121 Assert.AreEqual (3, c.Count, "Count");
125 [ExpectedException (typeof (ArgumentNullException))]
126 public void Add_X509Certificate2_Null ()
128 collection.Add ((X509Certificate2) null);
132 public void Add_X509Certificate2 ()
134 X509Certificate2Collection c = new X509Certificate2Collection ();
135 Assert.AreEqual (0, c.Count, "0");
137 Assert.AreEqual (1, c.Count, "1");
138 // adding invalid certificate
140 Assert.AreEqual (2, c.Count, "2");
141 // re-adding same certificate
143 Assert.AreEqual (3, c.Count, "3");
147 [ExpectedException (typeof (ArgumentNullException))]
148 public void AddRange_X509Certificate2Collection_Null ()
150 collection.AddRange ((X509Certificate2Collection) null);
154 public void AddRange_X509Certificate2Collection ()
156 X509Certificate2Collection c = new X509Certificate2Collection ();
158 Assert.AreEqual (0, c.Count, "0");
160 Assert.AreEqual (1, c.Count, "1");
161 c.AddRange (collection);
162 Assert.AreEqual (3, c.Count, "3");
163 // re-adding same collection
165 Assert.AreEqual (4, c.Count, "4");
169 [ExpectedException (typeof (ArgumentNullException))]
170 public void AddRange_X509Certificate2Array_Null ()
172 collection.AddRange ((X509Certificate2[]) null);
176 public void AddRange_X509Certificate2Array ()
178 X509Certificate2Collection c = new X509Certificate2Collection ();
179 c.AddRange (new X509Certificate2 [0]);
180 Assert.AreEqual (0, c.Count, "0");
181 c.AddRange (new X509Certificate2[3] { cert1, cert2, cert_empty });
182 Assert.AreEqual (3, c.Count, "3");
186 [ExpectedException (typeof (ArgumentNullException))]
187 public void Contains_X509Certificate2_Null ()
189 empty.Contains ((X509Certificate2) null);
193 public void Contains_Empty ()
195 Assert.IsFalse (empty.Contains (cert_empty), "empty|cert_empty");
196 Assert.IsFalse (empty.Contains (cert1), "empty|cert1");
197 Assert.IsFalse (empty.Contains (cert2), "empty|cert2");
201 public void Contains ()
203 Assert.IsTrue (single.Contains (cert1), "single|cert1");
204 Assert.IsFalse (single.Contains (cert2), "single|cert2");
206 Assert.IsTrue (collection.Contains (cert1), "multi|cert1");
207 Assert.IsTrue (collection.Contains (cert2), "multi|cert2");
211 [ExpectedException (typeof (CryptographicException))]
212 public void Contains_EmptyCert ()
214 single.Contains (cert_empty);
215 // note: Equals fails, but it works for an empty collection (not called)
219 [ExpectedException (typeof (CryptographicException))]
220 public void Export_Empty_Authenticode ()
222 empty.Export (X509ContentType.Authenticode);
226 public void Export_Empty ()
228 Assert.IsNull (empty.Export (X509ContentType.Cert), "Cert");
229 Assert.IsNull (empty.Export (X509ContentType.Cert, null), "Cert,null");
230 Assert.IsNull (empty.Export (X509ContentType.Cert, String.Empty), "Cert,Empty");
231 Assert.IsNull (empty.Export (X509ContentType.SerializedCert), "SerializedCert");
235 [ExpectedException (typeof (CryptographicException))]
236 [Category ("NotWorking")]
237 public void Export_Empty_Pfx ()
239 byte[] data = empty.Export (X509ContentType.Pfx);
240 Assert.IsNotNull (data, "data");
241 Assert.AreEqual (X509ContentType.Pfx, X509Certificate2.GetCertContentType (data), "GetCertContentType");
243 new X509Certificate2 (data);
247 [ExpectedException (typeof (CryptographicException))]
248 [Category ("NotWorking")]
249 public void Export_Empty_Pkcs12 ()
251 byte[] data = empty.Export (X509ContentType.Pkcs12);
252 Assert.IsNotNull (data, "data");
253 Assert.AreEqual (X509ContentType.Pkcs12, X509Certificate2.GetCertContentType (data), "GetCertContentType");
255 new X509Certificate2 (data);
259 [ExpectedException (typeof (CryptographicException))]
260 [Category ("NotWorking")]
261 public void Export_Empty_Pkcs7 ()
263 byte[] data = empty.Export (X509ContentType.Pkcs7);
264 Assert.IsNotNull (data, "data");
265 Assert.AreEqual (X509ContentType.Pkcs7, X509Certificate2.GetCertContentType (data), "GetCertContentType");
267 new X509Certificate2 (data);
271 [ExpectedException (typeof (CryptographicException))]
272 [Category ("NotWorking")]
273 public void Export_Empty_SerializedStore ()
275 byte[] data = empty.Export (X509ContentType.SerializedStore);
276 Assert.IsNotNull (data, "data");
277 Assert.AreEqual (X509ContentType.SerializedStore, X509Certificate2.GetCertContentType (data), "GetCertContentType");
279 new X509Certificate2 (data);
283 [ExpectedException (typeof (CryptographicException))]
284 public void Export_Empty_Unknown ()
286 empty.Export (X509ContentType.Unknown);
290 [ExpectedException (typeof (CryptographicException))]
291 public void Export_Empty_Bad ()
293 empty.Export ((X509ContentType)Int32.MinValue);
297 [ExpectedException (typeof (CryptographicException))]
298 public void Export_Single_Authenticode ()
300 single.Export (X509ContentType.Authenticode);
304 public void Export_Single_Cert ()
306 byte[] data = single.Export (X509ContentType.Cert);
307 Assert.AreEqual (X509ContentType.Cert, X509Certificate2.GetCertContentType (data), "GetCertContentType");
308 Assert.AreEqual (data, cert1.RawData, "RawData");
310 X509Certificate2 c = new X509Certificate2 (data);
311 Assert.AreEqual (cert1, c, "Equals");
315 [Category ("NotWorking")]
316 public void Export_Single_Pfx ()
318 byte[] data = single.Export (X509ContentType.Pfx);
319 Assert.AreEqual (X509ContentType.Pfx, X509Certificate2.GetCertContentType (data), "GetCertContentType");
321 X509Certificate2 c = new X509Certificate2 (data);
322 Assert.AreEqual (cert1, c, "Equals");
326 [Category ("NotWorking")]
327 public void Export_Single_Pkcs12 ()
329 byte[] data = single.Export (X509ContentType.Pkcs12);
330 Assert.AreEqual (X509ContentType.Pkcs12, X509Certificate2.GetCertContentType (data), "GetCertContentType");
332 X509Certificate2 c = new X509Certificate2 (data);
333 Assert.AreEqual (cert1, c, "Equals");
337 [ExpectedException (typeof (CryptographicException))]
338 [Category ("NotWorking")]
339 public void Export_Single_Pkcs7 ()
341 byte[] data = single.Export (X509ContentType.Pkcs7);
342 Assert.AreEqual (X509ContentType.Pkcs7, X509Certificate2.GetCertContentType (data), "GetCertContentType");
344 new X509Certificate2 (data);
348 [Category ("NotWorking")]
349 public void Export_Single_SerializedCert ()
351 byte[] data = single.Export (X509ContentType.SerializedCert);
352 Assert.AreEqual (X509ContentType.SerializedCert, X509Certificate2.GetCertContentType (data), "GetCertContentType");
354 X509Certificate2 c = new X509Certificate2 (data);
355 Assert.AreEqual (cert1, c, "Equals");
359 [ExpectedException (typeof (CryptographicException))]
360 [Category ("NotWorking")]
361 public void Export_Single_SerializedStore ()
363 byte[] data = single.Export (X509ContentType.SerializedStore);
364 Assert.AreEqual (X509ContentType.SerializedStore, X509Certificate2.GetCertContentType (data), "GetCertContentType");
366 new X509Certificate2 (data);
370 [ExpectedException (typeof (CryptographicException))]
371 public void Export_Single_Unknown ()
373 single.Export (X509ContentType.Unknown);
377 [ExpectedException (typeof (CryptographicException))]
378 public void Export_Single_Bad ()
380 single.Export ((X509ContentType) Int32.MinValue);
384 [ExpectedException (typeof (CryptographicException))]
385 public void Export_Multiple_Authenticode ()
387 collection.Export (X509ContentType.Authenticode);
391 public void Export_Multiple_Cert ()
393 byte[] data = collection.Export (X509ContentType.Cert);
394 Assert.AreEqual (X509ContentType.Cert, X509Certificate2.GetCertContentType (data), "GetCertContentType");
395 // last certificate was exported
396 Assert.AreEqual (data, cert2.RawData, "RawData");
398 X509Certificate2 c = new X509Certificate2 (data);
399 Assert.AreEqual (cert2, c, "Equals");
403 [Category ("NotWorking")]
404 public void Export_Multiple_Pfx ()
406 byte[] data = collection.Export (X509ContentType.Pfx);
407 Assert.AreEqual (X509ContentType.Pfx, X509Certificate2.GetCertContentType (data), "GetCertContentType");
409 X509Certificate2 c = new X509Certificate2 (data);
410 Assert.AreEqual (cert1, c, "Equals");
414 [Category ("NotWorking")]
415 public void Export_Multiple_Pkcs12 ()
417 byte[] data = collection.Export (X509ContentType.Pkcs12);
418 Assert.AreEqual (X509ContentType.Pkcs12, X509Certificate2.GetCertContentType (data), "GetCertContentType");
420 X509Certificate2 c = new X509Certificate2 (data);
421 Assert.AreEqual (cert1, c, "Equals");
425 [ExpectedException (typeof (CryptographicException))]
426 [Category ("NotWorking")]
427 public void Export_Multiple_Pkcs7 ()
429 byte[] data = collection.Export (X509ContentType.Pkcs7);
430 Assert.AreEqual (X509ContentType.Pkcs7, X509Certificate2.GetCertContentType (data), "GetCertContentType");
432 new X509Certificate2 (data);
436 [Category ("NotWorking")]
437 public void Export_Multiple_SerializedCert ()
439 byte[] data = collection.Export (X509ContentType.SerializedCert);
440 Assert.AreEqual (X509ContentType.SerializedCert, X509Certificate2.GetCertContentType (data), "GetCertContentType");
442 X509Certificate2 c = new X509Certificate2 (data);
443 // last certificate was exported
444 Assert.AreEqual (cert2, c, "Equals");
448 [ExpectedException (typeof (CryptographicException))]
449 [Category ("NotWorking")]
450 public void Export_Multiple_SerializedStore ()
452 byte[] data = collection.Export (X509ContentType.SerializedStore);
453 Assert.AreEqual (X509ContentType.SerializedStore, X509Certificate2.GetCertContentType (data), "GetCertContentType");
455 new X509Certificate2 (data);
459 [ExpectedException (typeof (CryptographicException))]
460 public void Export_Multiple_Unknown ()
462 collection.Export (X509ContentType.Unknown);
466 [ExpectedException (typeof (CryptographicException))]
467 public void Export_Multiple_Bad ()
469 collection.Export ((X509ContentType) Int32.MinValue);
473 [ExpectedException (typeof (ArgumentNullException))]
474 public void Find_FindValue_Null ()
476 empty.Find (X509FindType.FindByApplicationPolicy, null, true);
480 [ExpectedException (typeof (CryptographicException))]
481 public void Find_FindType_Bad ()
483 empty.Find ((X509FindType)Int32.MinValue, new object(), true);
487 public void Find_Empty ()
489 string oid = "1.2.3.4";
490 Assert.AreEqual (0, empty.Find (X509FindType.FindByApplicationPolicy, oid, false).Count, "Empty|FindByApplicationPolicy");
491 Assert.AreEqual (0, empty.Find (X509FindType.FindByCertificatePolicy, oid, false).Count, "Empty|FindByCertificatePolicy");
492 Assert.AreEqual (0, empty.Find (X509FindType.FindByExtension, oid, false).Count, "Empty|FindByExtension");
493 Assert.AreEqual (0, empty.Find (X509FindType.FindByIssuerDistinguishedName, String.Empty, false).Count, "Empty|FindByIssuerDistinguishedName");
494 Assert.AreEqual (0, empty.Find (X509FindType.FindByIssuerName, String.Empty, false).Count, "Empty|FindByIssuerName");
495 Assert.AreEqual (0, empty.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.CrlSign, false).Count, "Empty|FindByKeyUsage");
496 Assert.AreEqual (0, empty.Find (X509FindType.FindBySerialNumber, String.Empty, false).Count, "Empty|FindBySerialNumber");
497 Assert.AreEqual (0, empty.Find (X509FindType.FindBySubjectDistinguishedName, String.Empty, false).Count, "Empty|FindBySubjectDistinguishedName");
498 Assert.AreEqual (0, empty.Find (X509FindType.FindBySubjectKeyIdentifier, String.Empty, false).Count, "Empty|FindBySubjectKeyIdentifier");
499 Assert.AreEqual (0, empty.Find (X509FindType.FindBySubjectName, String.Empty, false).Count, "Empty|FindByTemplateName");
500 Assert.AreEqual (0, empty.Find (X509FindType.FindByTemplateName, String.Empty, false).Count, "Empty|FindByTemplateName");
501 Assert.AreEqual (0, empty.Find (X509FindType.FindByThumbprint, String.Empty, false).Count, "Empty|FindByThumbprint");
502 Assert.AreEqual (0, empty.Find (X509FindType.FindByTimeExpired, DateTime.Now, false).Count, "Empty|FindByTimeExpired");
503 Assert.AreEqual (0, empty.Find (X509FindType.FindByTimeNotYetValid, DateTime.Now, false).Count, "Empty|FindByTimeNotYetValid");
504 Assert.AreEqual (0, empty.Find (X509FindType.FindByTimeValid, DateTime.Now, false).Count, "Empty|FindByTimeValid");
508 [ExpectedException (typeof (CryptographicException))]
509 public void Find_CollectionWithEmptyCert ()
511 new X509Certificate2Collection (cert_empty).Find (X509FindType.FindByIssuerName, String.Empty, false);
515 public void Find_FindByApplicationPolicy ()
517 X509Certificate2Collection result = collection.Find (X509FindType.FindByApplicationPolicy, "1.2.3.4", false);
518 Assert.AreEqual (1, result.Count, "FindByApplicationPolicy/Empty/false");
519 Assert.AreEqual (0, result[0].Extensions.Count, "no extension");
520 // FIXME - need a negative test case (with extensions)
524 [ExpectedException (typeof (ArgumentException))]
525 public void Find_FindByApplicationPolicy_NotOid ()
527 collection.Find (X509FindType.FindByApplicationPolicy, "policy", false);
531 public void Find_FindByCertificatePolicy ()
533 Assert.AreEqual (0, collection.Find (X509FindType.FindByCertificatePolicy, "1.2.3.4", false).Count, "FindByApplicationPolicy/Empty/false");
534 // FIXME - need a positive test case
538 [ExpectedException (typeof (ArgumentException))]
539 public void Find_FindByCertificatePolicy_NotOid ()
541 collection.Find (X509FindType.FindByCertificatePolicy, "policy", false);
545 public void Find_FindByExtension ()
548 Assert.AreEqual (0, collection.Find (X509FindType.FindByExtension, "2.5.29", false).Count, "FindByExtension/2.5.29/false");
550 Assert.AreEqual (1, collection.Find (X509FindType.FindByExtension, "2.5.29.1", false).Count, "FindByExtension/2.5.29.1/false");
551 Assert.AreEqual (1, collection.Find (X509FindType.FindByExtension, "2.5.29.37", false).Count, "FindByExtension/2.5.29.37/false");
555 [ExpectedException (typeof (ArgumentException))]
556 public void Find_FindByExtension_NotOid ()
558 collection.Find (X509FindType.FindByExtension, "KeyUsage", false);
562 public void Find_FindByIssuerDistinguishedName ()
565 Assert.AreEqual (0, collection.Find (X509FindType.FindByIssuerDistinguishedName, String.Empty, false).Count, "FindByIssuerDistinguishedName/Empty/false");
567 Assert.AreEqual (0, collection.Find (X509FindType.FindByIssuerDistinguishedName, "Mono", false).Count, "FindByIssuerDistinguishedName/Mono/false");
568 Assert.AreEqual (0, collection.Find (X509FindType.FindByIssuerDistinguishedName, "CASTOR\\poupou", false).Count, "FindByIssuerDistinguishedName/castor/false");
569 // full match (requires CN= parts)
570 Assert.AreEqual (1, collection.Find (X509FindType.FindByIssuerDistinguishedName, cert1.Issuer, false).Count, "FindByIssuerDistinguishedName/cert1/false");
571 Assert.AreEqual (1, collection.Find (X509FindType.FindByIssuerDistinguishedName, cert2.IssuerName.Name, false).Count, "FindByIssuerDistinguishedName/cert2/false");
575 [ExpectedException (typeof (CryptographicException))]
576 public void Find_FindByIssuerDistinguishedName_NotString ()
578 collection.Find (X509FindType.FindByIssuerDistinguishedName, 1, false);
582 public void Find_FindByIssuerName ()
585 Assert.AreEqual (collection.Count, collection.Find (X509FindType.FindByIssuerName, String.Empty, false).Count, "FindByIssuerName/Empty/false");
586 Assert.AreEqual (0, collection.Find (X509FindType.FindByIssuerName, String.Empty, true).Count, "FindByIssuerName/Empty/true");
588 Assert.AreEqual (1, collection.Find (X509FindType.FindByIssuerName, "Mono", false).Count, "FindByIssuerName/Mono/false");
589 Assert.AreEqual (1, collection.Find (X509FindType.FindByIssuerName, "CASTOR\\poupou", false).Count, "FindByIssuerName/castor/false");
590 // full match (doesn't like CN= parts)
591 Assert.AreEqual (0, collection.Find (X509FindType.FindByIssuerName, cert1.Issuer, false).Count, "FindByIssuerName/cert1/false");
592 Assert.AreEqual (0, collection.Find (X509FindType.FindByIssuerName, cert2.IssuerName.Name, false).Count, "FindByIssuerName/cert2/false");
596 [ExpectedException (typeof (CryptographicException))]
597 public void Find_FindByIssuerName_NotString ()
599 collection.Find (X509FindType.FindByIssuerName, DateTime.Now, false);
603 public void Find_FindByKeyUsage ()
606 Assert.AreEqual (2, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.None, false).Count, "FindByKeyUsage/None/false");
607 Assert.AreEqual (0, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.None, true).Count, "FindByKeyUsage/None/true");
608 // always match if no KeyUsageExtension is present in certificate, EnhancedKeyUsageExtension not considered
609 Assert.AreEqual (2, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.CrlSign, false).Count, "FindByKeyUsage/CrlSign/false");
610 Assert.AreEqual (2, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.DataEncipherment, false).Count, "FindByKeyUsage/DataEncipherment/false");
611 Assert.AreEqual (2, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.DecipherOnly, false).Count, "FindByKeyUsage/DecipherOnly/false");
612 Assert.AreEqual (2, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.DigitalSignature, false).Count, "FindByKeyUsage/DigitalSignature/false");
613 Assert.AreEqual (2, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.EncipherOnly, false).Count, "FindByKeyUsage/EncipherOnly/false");
614 Assert.AreEqual (2, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.KeyAgreement, false).Count, "FindByKeyUsage/KeyAgreement/false");
615 Assert.AreEqual (2, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.KeyCertSign, false).Count, "FindByKeyUsage/KeyCertSign/false");
616 Assert.AreEqual (2, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.KeyEncipherment, false).Count, "FindByKeyUsage/KeyEncipherment/false");
617 Assert.AreEqual (2, collection.Find (X509FindType.FindByKeyUsage, X509KeyUsageFlags.NonRepudiation, false).Count, "FindByKeyUsage/NonRepudiation/false");
621 [ExpectedException (typeof (CryptographicException))]
622 public void Find_FindByIssuerName_NotX509KeyUsageFlags ()
624 collection.Find (X509FindType.FindByKeyUsage, String.Empty, false);
628 public void Find_FindBySerialNumber ()
631 Assert.AreEqual (0, collection.Find (X509FindType.FindBySerialNumber, String.Empty, false).Count, "FindBySerialNumber/Empty/false");
632 // partial match (start, end)
633 Assert.AreEqual (0, collection.Find (X509FindType.FindBySerialNumber, "748B", false).Count, "FindBySerialNumber/748B/false");
634 Assert.AreEqual (0, collection.Find (X509FindType.FindBySerialNumber, "4769", false).Count, "FindBySerialNumber/4769/false");
636 Assert.AreEqual (1, collection.Find (X509FindType.FindBySerialNumber, cert1.SerialNumber, false).Count, "FindBySerialNumber/cert1/false");
637 Assert.AreEqual (1, collection.Find (X509FindType.FindBySerialNumber, cert2.SerialNumber, false).Count, "FindBySerialNumber/cert2/false");
638 Assert.AreEqual (1, collection.Find (X509FindType.FindBySerialNumber, cert1.SerialNumber.ToLowerInvariant (), false).Count, "FindBySerialNumber/cert1b/false");
639 Assert.AreEqual (1, collection.Find (X509FindType.FindBySerialNumber, cert2.SerialNumber.ToLowerInvariant (), false).Count, "FindBySerialNumber/cert2b/false");
640 // full match inverted
641 Assert.AreEqual (1, collection.Find (X509FindType.FindBySerialNumber, cert1.GetSerialNumberString (), false).Count, "FindBySerialNumber/cert1c/false");
642 Assert.AreEqual (1, collection.Find (X509FindType.FindBySerialNumber, cert2.GetSerialNumberString (), false).Count, "FindBySerialNumber/cert2c/false");
643 Assert.AreEqual (1, collection.Find (X509FindType.FindBySerialNumber, cert1.GetSerialNumberString ().ToLowerInvariant (), false).Count, "FindBySerialNumber/cert1d/false");
644 Assert.AreEqual (1, collection.Find (X509FindType.FindBySerialNumber, cert2.GetSerialNumberString ().ToLowerInvariant (), false).Count, "FindBySerialNumber/cert2d/false");
648 [ExpectedException (typeof (CryptographicException))]
649 public void Find_FindBySerialNumber_NotString ()
651 collection.Find (X509FindType.FindBySerialNumber, DateTime.Now, false);
655 public void Find_FindBySubjectDistinguishedName ()
658 Assert.AreEqual (0, collection.Find (X509FindType.FindBySubjectDistinguishedName, String.Empty, false).Count, "FindBySubjectDistinguishedName/Empty/false");
660 Assert.AreEqual (0, collection.Find (X509FindType.FindBySubjectDistinguishedName, "Mono", false).Count, "FindBySubjectDistinguishedName/Mono/false");
661 Assert.AreEqual (0, collection.Find (X509FindType.FindBySubjectDistinguishedName, "CASTOR\\poupou", false).Count, "FindBySubjectDistinguishedName/castor/false");
662 // full match (requires CN= parts) using all lowercase
663 Assert.AreEqual (1, collection.Find (X509FindType.FindBySubjectDistinguishedName, cert1.Subject.ToLowerInvariant (), false).Count, "FindBySubjectDistinguishedName/cert1/false");
664 Assert.AreEqual (1, collection.Find (X509FindType.FindBySubjectDistinguishedName, cert2.SubjectName.Name.ToLowerInvariant (), false).Count, "FindBySubjectDistinguishedName/cert2/false");
668 [ExpectedException (typeof (CryptographicException))]
669 public void Find_FindBySubjectDistinguishedName_NotString ()
671 collection.Find (X509FindType.FindBySubjectDistinguishedName, new object (), false);
675 public void Find_FindBySubjectKeyIdentifier ()
678 Assert.AreEqual (0, collection.Find (X509FindType.FindBySubjectKeyIdentifier, String.Empty, false).Count, "FindBySubjectKeyIdentifier/Empty/false");
680 X509Certificate2Collection c = new X509Certificate2Collection (collection);
681 c.Add (new X509Certificate2 (X509Certificate2Test.cert_8));
682 Assert.AreEqual (0, c.Find (X509FindType.FindBySubjectKeyIdentifier, "9D2D73C3B8E34D2928C3", false).Count, "FindBySubjectKeyIdentifier/half/false");
683 Assert.AreEqual (1, c.Find (X509FindType.FindBySubjectKeyIdentifier, "9D2D73C3B8E34D2928C365BEA998CBD68A06689C", false).Count, "FindBySubjectKeyIdentifier/full/false");
684 Assert.AreEqual (1, c.Find (X509FindType.FindBySubjectKeyIdentifier, "9d2d73c3b8e34d2928c365bea998cbd68a06689c", false).Count, "FindBySubjectKeyIdentifier/full/false");
688 [ExpectedException (typeof (CryptographicException))]
689 public void Find_FindBySubjectKeyIdentifier_NotString ()
691 collection.Find (X509FindType.FindBySubjectKeyIdentifier, 1, false);
695 public void Find_FindBySubjectName ()
698 Assert.AreEqual (collection.Count, collection.Find (X509FindType.FindBySubjectName, String.Empty, false).Count, "FindBySubjectName/Empty/false");
699 Assert.AreEqual (0, collection.Find (X509FindType.FindBySubjectName, String.Empty, true).Count, "FindBySubjectName/Empty/true");
700 // partial match (using inverted case)
701 Assert.AreEqual (1, collection.Find (X509FindType.FindBySubjectName, "farscap", false).Count, "FindBySubjectName/Mono/false");
702 Assert.AreEqual (1, collection.Find (X509FindType.FindBySubjectName, "castor\\POUPOU", false).Count, "FindBySubjectName/castor/false");
703 // full match (doesn't like CN= parts)
704 Assert.AreEqual (0, collection.Find (X509FindType.FindBySubjectName, cert1.Subject, false).Count, "FindBySubjectName/cert1/false");
705 Assert.AreEqual (0, collection.Find (X509FindType.FindBySubjectName, cert2.SubjectName.Name, false).Count, "FindBySubjectName/cert2/false");
709 [ExpectedException (typeof (CryptographicException))]
710 public void Find_FindBySubjectName_NotString ()
712 collection.Find (X509FindType.FindBySubjectName, 'c', false);
716 public void Find_FindByTemplateName ()
719 Assert.AreEqual (0, collection.Find (X509FindType.FindByTemplateName, String.Empty, false).Count, "FindByTemplateName/Empty/false");
721 Assert.AreEqual (0, collection.Find (X509FindType.FindByTemplateName, "*", false).Count, "FindByTemplateName/Mono/false");
722 // FIXME - need a positive test case
726 [ExpectedException (typeof (CryptographicException))]
727 public void Find_FindByTemplateName_NotString ()
729 collection.Find (X509FindType.FindByTemplateName, 0, false);
733 public void Find_FindByThumbprint ()
736 Assert.AreEqual (0, collection.Find (X509FindType.FindByThumbprint, String.Empty, false).Count, "FindByThumbprint/Empty/false");
737 // partial match (start, end)
738 Assert.AreEqual (0, collection.Find (X509FindType.FindByThumbprint, "3029", false).Count, "FindByThumbprint/3029/false");
739 Assert.AreEqual (0, collection.Find (X509FindType.FindByThumbprint, "8529", false).Count, "FindByThumbprint/8529/false");
741 Assert.AreEqual (1, collection.Find (X509FindType.FindByThumbprint, cert1.Thumbprint, false).Count, "FindByThumbprint/cert1/false");
742 Assert.AreEqual (1, collection.Find (X509FindType.FindByThumbprint, cert2.Thumbprint, false).Count, "FindByThumbprint/cert2/false");
743 Assert.AreEqual (1, collection.Find (X509FindType.FindByThumbprint, cert1.Thumbprint.ToLowerInvariant (), false).Count, "FindByThumbprint/cert1b/false");
744 Assert.AreEqual (1, collection.Find (X509FindType.FindByThumbprint, cert2.Thumbprint.ToLowerInvariant (), false).Count, "FindByThumbprint/cert2b/false");
745 // full match inverted
746 Assert.AreEqual (1, collection.Find (X509FindType.FindByThumbprint, cert1.GetCertHashString (), false).Count, "FindByThumbprint/cert1c/false");
747 Assert.AreEqual (1, collection.Find (X509FindType.FindByThumbprint, cert2.GetCertHashString (), false).Count, "FindByThumbprint/cert2c/false");
748 Assert.AreEqual (1, collection.Find (X509FindType.FindByThumbprint, cert1.GetCertHashString ().ToLowerInvariant (), false).Count, "FindByThumbprint/cert1d/false");
749 Assert.AreEqual (1, collection.Find (X509FindType.FindByThumbprint, cert2.GetCertHashString ().ToLowerInvariant (), false).Count, "FindByThumbprint/cert2d/false");
753 [ExpectedException (typeof (CryptographicException))]
754 public void Find_FindByThumbprint_NotString ()
756 collection.Find (X509FindType.FindByThumbprint, 0, false);
760 public void Find_FindByTimeExpired ()
762 // now (valid from today until 2039)
763 Assert.AreEqual (1, collection.Find (X509FindType.FindByTimeExpired, DateTime.Now, false).Count, "FindByTimeExpired/Now/false");
764 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeExpired, new DateTime (631108726620000000), false).Count, "FindByTimeExpired/2000/false");
765 Assert.AreEqual (2, collection.Find (X509FindType.FindByTimeExpired, new DateTime (644392619990000000), false).Count, "FindByTimeExpired/2042/false");
767 Assert.AreEqual (1, collection.Find (X509FindType.FindByTimeExpired, cert1.NotAfter, false).Count, "FindByTimeExpired/cert1.NotAfter/false");
768 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeExpired, cert1.NotBefore, false).Count, "FindByTimeExpired/cert1.NotBefore/false");
769 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeExpired, cert2.NotAfter, false).Count, "FindByTimeExpired/cert2.NotAfter/false");
770 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeExpired, cert2.NotBefore, false).Count, "FindByTimeExpired/cert2.NotBefore/false");
774 [ExpectedException (typeof (CryptographicException))]
775 public void Find_FindByTimeExpired_NotDateTime ()
777 collection.Find (X509FindType.FindByTimeExpired, String.Empty, false);
781 public void Find_FindByTimeNotYetValid ()
783 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeNotYetValid, DateTime.Now, false).Count, "FindByTimeNotYetValid/Now/false");
784 Assert.AreEqual (2, collection.Find (X509FindType.FindByTimeNotYetValid, new DateTime (631108726620000000), false).Count, "FindByTimeNotYetValid/2000/false");
785 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeNotYetValid, new DateTime (644392619990000000), false).Count, "FindByTimeNotYetValid/2042/false");
787 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeNotYetValid, cert1.NotAfter, false).Count, "FindByTimeNotYetValid/cert1.NotAfter/false");
788 Assert.AreEqual (1, collection.Find (X509FindType.FindByTimeNotYetValid, cert1.NotBefore, false).Count, "FindByTimeNotYetValid/cert1.NotBefore/false");
789 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeNotYetValid, cert2.NotAfter, false).Count, "FindByTimeNotYetValid/cert2.NotAfter/false");
790 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeNotYetValid, cert2.NotBefore, false).Count, "FindByTimeNotYetValid/cert2.NotBefore/false");
794 [ExpectedException (typeof (CryptographicException))]
795 public void Find_FindByTimeNotYetValid_NotDateTime ()
797 collection.Find (X509FindType.FindByTimeNotYetValid, DateTime.Now.ToString (), false);
801 public void Find_FindByTimeValid ()
803 Assert.AreEqual (1, collection.Find (X509FindType.FindByTimeValid, DateTime.Now, false).Count, "FindByTimeValid/Now/false");
804 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeValid, new DateTime (631108726620000000), false).Count, "FindByTimeValid/2000/false");
805 Assert.AreEqual (0, collection.Find (X509FindType.FindByTimeValid, new DateTime (644392619990000000), false).Count, "FindByTimeValid/2042/false");
807 Assert.AreEqual (1, collection.Find (X509FindType.FindByTimeValid, cert1.NotAfter, false).Count, "FindByTimeValid/cert1.NotAfter/false");
808 Assert.AreEqual (1, collection.Find (X509FindType.FindByTimeValid, cert1.NotBefore, false).Count, "FindByTimeValid/cert1.NotBefore/false");
809 Assert.AreEqual (2, collection.Find (X509FindType.FindByTimeValid, cert2.NotAfter, false).Count, "FindByTimeValid/cert2.NotAfter/false");
810 Assert.AreEqual (2, collection.Find (X509FindType.FindByTimeValid, cert2.NotBefore, false).Count, "FindByTimeValid/cert2.NotBefore/false");
814 [ExpectedException (typeof (CryptographicException))]
815 public void Find_FindByTimeValid_NotDateTime ()
817 collection.Find (X509FindType.FindByTimeValid, String.Empty, false);
821 [ExpectedException (typeof (ArgumentNullException))]
822 public void Remove_X509Certificate2_Null ()
824 collection.Remove ((X509Certificate2) null);
828 [ExpectedException (typeof (CryptographicException))]
829 public void Remove_X509Certificate2_Empty ()
831 single.Remove (cert_empty);
835 [ExpectedException (typeof (ArgumentNullException))]
836 public void RemoveRange_X509Certificate2Collection_Null ()
838 collection.RemoveRange ((X509Certificate2Collection) null);
842 [ExpectedException (typeof (CryptographicException))]
843 public void RemoveRange_X509Certificate2Collection_EmptyCert ()
845 collection.RemoveRange (new X509Certificate2Collection (cert_empty));
849 public void RemoveRange_X509Certificate2Collection_Empty ()
851 Assert.AreEqual (2, collection.Count, "Count/before");
852 collection.RemoveRange (empty);
853 Assert.AreEqual (2, collection.Count, "Count/after");
857 [ExpectedException (typeof (ArgumentNullException))]
858 public void RemoveRange_X509Certificate2Array_Null ()
860 collection.RemoveRange ((X509Certificate2[]) null);
864 [ExpectedException (typeof (CryptographicException))]
865 public void RemoveRange_X509Certificate2Array_EmptyCert ()
867 collection.RemoveRange (new X509Certificate2[1] { cert_empty });
871 public void RemoveRange_X509Certificate2Array_Empty ()
873 Assert.AreEqual (2, collection.Count, "Count/before");
874 collection.RemoveRange (new X509Certificate2[0]);
875 Assert.AreEqual (2, collection.Count, "Count/after");
879 [ExpectedException (typeof (InvalidCastException))]
880 public void MixedCollection_Indexer ()
882 X509Certificate2Collection c = new X509Certificate2Collection ();
883 c.Add (new X509Certificate (X509Certificate2Test.farscape_pfx, "farscape"));
884 Assert.IsTrue ((c[0] is X509Certificate), "X509Certificate/0");
885 // it's impossible to use the this[int] indexer to get the object in the collection
889 [ExpectedException (typeof (InvalidCastException))]
890 public void MixedCollection_Enumerator ()
892 X509Certificate2Collection c = new X509Certificate2Collection ();
893 c.Add (new X509Certificate (X509Certificate2Test.farscape_pfx, "farscape"));
894 foreach (object o in c) {
895 Assert.IsTrue ((o is X509Certificate), "X509Certificate");