Fix compilation of System.Configuration test suite.
[mono.git] / mcs / class / System / Test / System.Security.Cryptography.X509Certificates / X509Certificate2CollectionTest.cs
1 //
2 // X509CertificateCollection2Test.cs 
3 //      - NUnit tests for X509CertificateCollection2
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 #if NET_2_0
31
32 using NUnit.Framework;
33
34 using System;
35 using System.Security.Cryptography;
36 using System.Security.Cryptography.X509Certificates;
37 using System.Text;
38
39 namespace MonoTests.System.Security.Cryptography.X509Certificates {
40
41         [TestFixture]
42         public class X509Certificate2CollectionTest {
43
44                 private X509Certificate2Collection empty;
45                 private X509Certificate2Collection single;
46                 private X509Certificate2Collection collection;
47
48                 private X509Certificate2 cert_empty;
49                 private X509Certificate2 cert1;
50                 private X509Certificate2 cert2;
51
52                 [TestFixtureSetUp]
53                 public void FixtureSetUp ()
54                 {
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));
58
59                         empty = new X509Certificate2Collection ();
60                         single = new X509Certificate2Collection ();
61                         single.Add (cert1);
62                         collection = new X509Certificate2Collection (single);
63                         collection.Add (cert2);
64                 }
65
66                 [Test]
67                 public void Ctor ()
68                 {
69                         X509Certificate2Collection c = new X509Certificate2Collection ();
70                         Assert.AreEqual (0, c.Count, "Count");
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (ArgumentNullException))]
75                 public void Ctor_X509CertificateCollection2_Null ()
76                 {
77                         new X509Certificate2Collection ((X509Certificate2Collection) null);
78                 }
79
80                 [Test]
81                 public void Ctor_X509CertificateCollection2_Empty ()
82                 {
83                         X509Certificate2Collection c = new X509Certificate2Collection (empty);
84                         Assert.AreEqual (0, c.Count, "Count");
85                 }
86
87                 [Test]
88                 public void Ctor_X509CertificateCollection2 ()
89                 {
90                         X509Certificate2Collection c = new X509Certificate2Collection (collection);
91                         Assert.AreEqual (2, c.Count, "Count");
92                 }
93
94                 [Test]
95                 [ExpectedException (typeof (ArgumentNullException))]
96                 public void Ctor_X509Certificate2_Null ()
97                 {
98                         new X509Certificate2Collection ((X509Certificate2) null);
99                 }
100
101                 [Test]
102                 [ExpectedException (typeof (ArgumentNullException))]
103                 public void Ctor_X509Certificate2Array_Null ()
104                 {
105                         new X509Certificate2Collection ((X509Certificate2[]) null);
106                 }
107
108                 [Test]
109                 public void Ctor_X509Certificate2Array_Empty ()
110                 {
111                         X509Certificate2[] array = new X509Certificate2 [0];
112                         X509Certificate2Collection c = new X509Certificate2Collection (array);
113                         Assert.AreEqual (0, c.Count, "Count");
114                 }
115
116                 [Test]
117                 public void Ctor_X509Certificate2Array ()
118                 {
119                         X509Certificate2[] array = new X509Certificate2[3] { cert1, cert2, cert_empty };
120                         X509Certificate2Collection c = new X509Certificate2Collection (array);
121                         Assert.AreEqual (3, c.Count, "Count");
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (ArgumentNullException))]
126                 public void Add_X509Certificate2_Null ()
127                 {
128                         collection.Add ((X509Certificate2) null);
129                 }
130
131                 [Test]
132                 public void Add_X509Certificate2 ()
133                 {
134                         X509Certificate2Collection c = new X509Certificate2Collection ();
135                         Assert.AreEqual (0, c.Count, "0");
136                         c.Add (cert1);
137                         Assert.AreEqual (1, c.Count, "1");
138                         // adding invalid certificate
139                         c.Add (cert_empty);
140                         Assert.AreEqual (2, c.Count, "2");
141                         // re-adding same certificate
142                         c.Add (cert1);
143                         Assert.AreEqual (3, c.Count, "3");
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (ArgumentNullException))]
148                 public void AddRange_X509Certificate2Collection_Null ()
149                 {
150                         collection.AddRange ((X509Certificate2Collection) null);
151                 }
152
153                 [Test]
154                 public void AddRange_X509Certificate2Collection ()
155                 {
156                         X509Certificate2Collection c = new X509Certificate2Collection ();
157                         c.AddRange (empty);
158                         Assert.AreEqual (0, c.Count, "0");
159                         c.AddRange (single);
160                         Assert.AreEqual (1, c.Count, "1");
161                         c.AddRange (collection);
162                         Assert.AreEqual (3, c.Count, "3");
163                         // re-adding same collection
164                         c.AddRange (single);
165                         Assert.AreEqual (4, c.Count, "4");
166                 }
167
168                 [Test]
169                 [ExpectedException (typeof (ArgumentNullException))]
170                 public void AddRange_X509Certificate2Array_Null ()
171                 {
172                         collection.AddRange ((X509Certificate2[]) null);
173                 }
174
175                 [Test]
176                 public void AddRange_X509Certificate2Array ()
177                 {
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");
183                 }
184
185                 [Test]
186                 [ExpectedException (typeof (ArgumentNullException))]
187                 public void Contains_X509Certificate2_Null ()
188                 {
189                         empty.Contains ((X509Certificate2) null);
190                 }
191
192                 [Test]
193                 public void Contains_Empty ()
194                 {
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");
198                 }
199
200                 [Test]
201                 public void Contains ()
202                 {
203                         Assert.IsTrue (single.Contains (cert1), "single|cert1");
204                         Assert.IsFalse (single.Contains (cert2), "single|cert2");
205
206                         Assert.IsTrue (collection.Contains (cert1), "multi|cert1");
207                         Assert.IsTrue (collection.Contains (cert2), "multi|cert2");
208                 }
209
210                 [Test]
211                 [ExpectedException (typeof (CryptographicException))]
212                 public void Contains_EmptyCert ()
213                 {
214                         single.Contains (cert_empty);
215                         // note: Equals fails, but it works for an empty collection (not called)
216                 }
217
218                 [Test]
219                 [ExpectedException (typeof (CryptographicException))]
220                 public void Export_Empty_Authenticode ()
221                 {
222                         empty.Export (X509ContentType.Authenticode);
223                 }
224
225                 [Test]
226                 public void Export_Empty ()
227                 {
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");
232                 }
233
234                 [Test]
235                 [ExpectedException (typeof (CryptographicException))]
236                 [Category ("NotWorking")]
237                 public void Export_Empty_Pfx ()
238                 {
239                         byte[] data = empty.Export (X509ContentType.Pfx);
240                         Assert.IsNotNull (data, "data");
241                         Assert.AreEqual (X509ContentType.Pfx, X509Certificate2.GetCertContentType (data), "GetCertContentType");
242                         // not usable
243                         new X509Certificate2 (data);
244                 }
245
246                 [Test]
247                 [ExpectedException (typeof (CryptographicException))]
248                 [Category ("NotWorking")]
249                 public void Export_Empty_Pkcs12 ()
250                 {
251                         byte[] data = empty.Export (X509ContentType.Pkcs12);
252                         Assert.IsNotNull (data, "data");
253                         Assert.AreEqual (X509ContentType.Pkcs12, X509Certificate2.GetCertContentType (data), "GetCertContentType");
254                         // not usable
255                         new X509Certificate2 (data);
256                 }
257
258                 [Test]
259                 [ExpectedException (typeof (CryptographicException))]
260                 [Category ("NotWorking")]
261                 public void Export_Empty_Pkcs7 ()
262                 {
263                         byte[] data = empty.Export (X509ContentType.Pkcs7);
264                         Assert.IsNotNull (data, "data");
265                         Assert.AreEqual (X509ContentType.Pkcs7, X509Certificate2.GetCertContentType (data), "GetCertContentType");
266                         // not usable
267                         new X509Certificate2 (data);
268                 }
269
270                 [Test]
271                 [ExpectedException (typeof (CryptographicException))]
272                 [Category ("NotWorking")]
273                 public void Export_Empty_SerializedStore ()
274                 {
275                         byte[] data = empty.Export (X509ContentType.SerializedStore);
276                         Assert.IsNotNull (data, "data");
277                         Assert.AreEqual (X509ContentType.SerializedStore, X509Certificate2.GetCertContentType (data), "GetCertContentType");
278                         // not usable
279                         new X509Certificate2 (data);
280                 }
281
282                 [Test]
283                 [ExpectedException (typeof (CryptographicException))]
284                 public void Export_Empty_Unknown ()
285                 {
286                         empty.Export (X509ContentType.Unknown);
287                 }
288
289                 [Test]
290                 [ExpectedException (typeof (CryptographicException))]
291                 public void Export_Empty_Bad ()
292                 {
293                         empty.Export ((X509ContentType)Int32.MinValue);
294                 }
295
296                 [Test]
297                 [ExpectedException (typeof (CryptographicException))]
298                 public void Export_Single_Authenticode ()
299                 {
300                         single.Export (X509ContentType.Authenticode);
301                 }
302
303                 [Test]
304                 public void Export_Single_Cert ()
305                 {
306                         byte[] data = single.Export (X509ContentType.Cert);
307                         Assert.AreEqual (X509ContentType.Cert, X509Certificate2.GetCertContentType (data), "GetCertContentType");
308                         Assert.AreEqual (data, cert1.RawData, "RawData");
309                         // usable
310                         X509Certificate2 c = new X509Certificate2 (data);
311                         Assert.AreEqual (cert1, c, "Equals");
312                 }
313
314                 [Test]
315                 [Category ("NotWorking")]
316                 public void Export_Single_Pfx ()
317                 {
318                         byte[] data = single.Export (X509ContentType.Pfx);
319                         Assert.AreEqual (X509ContentType.Pfx, X509Certificate2.GetCertContentType (data), "GetCertContentType");
320                         // usable
321                         X509Certificate2 c = new X509Certificate2 (data);
322                         Assert.AreEqual (cert1, c, "Equals");
323                 }
324
325                 [Test]
326                 [Category ("NotWorking")]
327                 public void Export_Single_Pkcs12 ()
328                 {
329                         byte[] data = single.Export (X509ContentType.Pkcs12);
330                         Assert.AreEqual (X509ContentType.Pkcs12, X509Certificate2.GetCertContentType (data), "GetCertContentType");
331                         // usable
332                         X509Certificate2 c = new X509Certificate2 (data);
333                         Assert.AreEqual (cert1, c, "Equals");
334                 }
335
336                 [Test]
337                 [ExpectedException (typeof (CryptographicException))]
338                 [Category ("NotWorking")]
339                 public void Export_Single_Pkcs7 ()
340                 {
341                         byte[] data = single.Export (X509ContentType.Pkcs7);
342                         Assert.AreEqual (X509ContentType.Pkcs7, X509Certificate2.GetCertContentType (data), "GetCertContentType");
343                         // not usable
344                         new X509Certificate2 (data);
345                 }
346
347                 [Test]
348                 [Category ("NotWorking")]
349                 public void Export_Single_SerializedCert ()
350                 {
351                         byte[] data = single.Export (X509ContentType.SerializedCert);
352                         Assert.AreEqual (X509ContentType.SerializedCert, X509Certificate2.GetCertContentType (data), "GetCertContentType");
353                         // usable
354                         X509Certificate2 c = new X509Certificate2 (data);
355                         Assert.AreEqual (cert1, c, "Equals");
356                 }
357
358                 [Test]
359                 [ExpectedException (typeof (CryptographicException))]
360                 [Category ("NotWorking")]
361                 public void Export_Single_SerializedStore ()
362                 {
363                         byte[] data = single.Export (X509ContentType.SerializedStore);
364                         Assert.AreEqual (X509ContentType.SerializedStore, X509Certificate2.GetCertContentType (data), "GetCertContentType");
365                         // not usable
366                         new X509Certificate2 (data);
367                 }
368
369                 [Test]
370                 [ExpectedException (typeof (CryptographicException))]
371                 public void Export_Single_Unknown ()
372                 {
373                         single.Export (X509ContentType.Unknown);
374                 }
375
376                 [Test]
377                 [ExpectedException (typeof (CryptographicException))]
378                 public void Export_Single_Bad ()
379                 {
380                         single.Export ((X509ContentType) Int32.MinValue);
381                 }
382
383                 [Test]
384                 [ExpectedException (typeof (CryptographicException))]
385                 public void Export_Multiple_Authenticode ()
386                 {
387                         collection.Export (X509ContentType.Authenticode);
388                 }
389
390                 [Test]
391                 public void Export_Multiple_Cert ()
392                 {
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");
397                         // usable
398                         X509Certificate2 c = new X509Certificate2 (data);
399                         Assert.AreEqual (cert2, c, "Equals");
400                 }
401
402                 [Test]
403                 [Category ("NotWorking")]
404                 public void Export_Multiple_Pfx ()
405                 {
406                         byte[] data = collection.Export (X509ContentType.Pfx);
407                         Assert.AreEqual (X509ContentType.Pfx, X509Certificate2.GetCertContentType (data), "GetCertContentType");
408                         // usable
409                         X509Certificate2 c = new X509Certificate2 (data);
410                         Assert.AreEqual (cert1, c, "Equals");
411                 }
412
413                 [Test]
414                 [Category ("NotWorking")]
415                 public void Export_Multiple_Pkcs12 ()
416                 {
417                         byte[] data = collection.Export (X509ContentType.Pkcs12);
418                         Assert.AreEqual (X509ContentType.Pkcs12, X509Certificate2.GetCertContentType (data), "GetCertContentType");
419                         // usable
420                         X509Certificate2 c = new X509Certificate2 (data);
421                         Assert.AreEqual (cert1, c, "Equals");
422                 }
423
424                 [Test]
425                 [ExpectedException (typeof (CryptographicException))]
426                 [Category ("NotWorking")]
427                 public void Export_Multiple_Pkcs7 ()
428                 {
429                         byte[] data = collection.Export (X509ContentType.Pkcs7);
430                         Assert.AreEqual (X509ContentType.Pkcs7, X509Certificate2.GetCertContentType (data), "GetCertContentType");
431                         // not usable
432                         new X509Certificate2 (data);
433                 }
434
435                 [Test]
436                 [Category ("NotWorking")]
437                 public void Export_Multiple_SerializedCert ()
438                 {
439                         byte[] data = collection.Export (X509ContentType.SerializedCert);
440                         Assert.AreEqual (X509ContentType.SerializedCert, X509Certificate2.GetCertContentType (data), "GetCertContentType");
441                         // usable
442                         X509Certificate2 c = new X509Certificate2 (data);
443                         // last certificate was exported
444                         Assert.AreEqual (cert2, c, "Equals");
445                 }
446
447                 [Test]
448                 [ExpectedException (typeof (CryptographicException))]
449                 [Category ("NotWorking")]
450                 public void Export_Multiple_SerializedStore ()
451                 {
452                         byte[] data = collection.Export (X509ContentType.SerializedStore);
453                         Assert.AreEqual (X509ContentType.SerializedStore, X509Certificate2.GetCertContentType (data), "GetCertContentType");
454                         // not usable
455                         new X509Certificate2 (data);
456                 }
457
458                 [Test]
459                 [ExpectedException (typeof (CryptographicException))]
460                 public void Export_Multiple_Unknown ()
461                 {
462                         collection.Export (X509ContentType.Unknown);
463                 }
464
465                 [Test]
466                 [ExpectedException (typeof (CryptographicException))]
467                 public void Export_Multiple_Bad ()
468                 {
469                         collection.Export ((X509ContentType) Int32.MinValue);
470                 }
471
472                 [Test]
473                 [ExpectedException (typeof (ArgumentNullException))]
474                 public void Find_FindValue_Null ()
475                 {
476                         empty.Find (X509FindType.FindByApplicationPolicy, null, true);
477                 }
478
479                 [Test]
480                 [ExpectedException (typeof (CryptographicException))]
481                 public void Find_FindType_Bad ()
482                 {
483                         empty.Find ((X509FindType)Int32.MinValue, new object(), true);
484                 }
485
486                 [Test]
487                 public void Find_Empty ()
488                 {
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");
505                 }
506
507                 [Test]
508                 [ExpectedException (typeof (CryptographicException))]
509                 public void Find_CollectionWithEmptyCert ()
510                 {
511                         new X509Certificate2Collection (cert_empty).Find (X509FindType.FindByIssuerName, String.Empty, false);
512                 }
513
514                 [Test]
515                 public void Find_FindByApplicationPolicy ()
516                 {
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)
521                 }
522
523                 [Test]
524                 [ExpectedException (typeof (ArgumentException))]
525                 public void Find_FindByApplicationPolicy_NotOid ()
526                 {
527                         collection.Find (X509FindType.FindByApplicationPolicy, "policy", false);
528                 }
529
530                 [Test]
531                 public void Find_FindByCertificatePolicy ()
532                 {
533                         Assert.AreEqual (0, collection.Find (X509FindType.FindByCertificatePolicy, "1.2.3.4", false).Count, "FindByApplicationPolicy/Empty/false");
534                         // FIXME - need a positive test case
535                 }
536
537                 [Test]
538                 [ExpectedException (typeof (ArgumentException))]
539                 public void Find_FindByCertificatePolicy_NotOid ()
540                 {
541                         collection.Find (X509FindType.FindByCertificatePolicy, "policy", false);
542                 }
543
544                 [Test]
545                 public void Find_FindByExtension ()
546                 {
547                         // partial match
548                         Assert.AreEqual (0, collection.Find (X509FindType.FindByExtension, "2.5.29", false).Count, "FindByExtension/2.5.29/false");
549                         // full match
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");
552                 }
553
554                 [Test]
555                 [ExpectedException (typeof (ArgumentException))]
556                 public void Find_FindByExtension_NotOid ()
557                 {
558                         collection.Find (X509FindType.FindByExtension, "KeyUsage", false);
559                 }
560
561                 [Test]
562                 public void Find_FindByIssuerDistinguishedName ()
563                 {
564                         // empty
565                         Assert.AreEqual (0, collection.Find (X509FindType.FindByIssuerDistinguishedName, String.Empty, false).Count, "FindByIssuerDistinguishedName/Empty/false");
566                         // partial match
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");
572                 }
573
574                 [Test]
575                 [ExpectedException (typeof (CryptographicException))]
576                 public void Find_FindByIssuerDistinguishedName_NotString ()
577                 {
578                         collection.Find (X509FindType.FindByIssuerDistinguishedName, 1, false);
579                 }
580
581                 [Test]
582                 public void Find_FindByIssuerName ()
583                 {
584                         // empty
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");
587                         // partial match
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");
593                 }
594
595                 [Test]
596                 [ExpectedException (typeof (CryptographicException))]
597                 public void Find_FindByIssuerName_NotString ()
598                 {
599                         collection.Find (X509FindType.FindByIssuerName, DateTime.Now, false);
600                 }
601
602                 [Test]
603                 public void Find_FindByKeyUsage ()
604                 {
605                         // empty
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");
618                 }
619
620                 [Test]
621                 [ExpectedException (typeof (CryptographicException))]
622                 public void Find_FindByIssuerName_NotX509KeyUsageFlags ()
623                 {
624                         collection.Find (X509FindType.FindByKeyUsage, String.Empty, false);
625                 }
626
627                 [Test]
628                 public void Find_FindBySerialNumber ()
629                 {
630                         // empty
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");
635                         // full match
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");
645                 }
646
647                 [Test]
648                 [ExpectedException (typeof (CryptographicException))]
649                 public void Find_FindBySerialNumber_NotString ()
650                 {
651                         collection.Find (X509FindType.FindBySerialNumber, DateTime.Now, false);
652                 }
653
654                 [Test]
655                 public void Find_FindBySubjectDistinguishedName ()
656                 {
657                         // empty
658                         Assert.AreEqual (0, collection.Find (X509FindType.FindBySubjectDistinguishedName, String.Empty, false).Count, "FindBySubjectDistinguishedName/Empty/false");
659                         // partial match
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");
665                 }
666
667                 [Test]
668                 [ExpectedException (typeof (CryptographicException))]
669                 public void Find_FindBySubjectDistinguishedName_NotString ()
670                 {
671                         collection.Find (X509FindType.FindBySubjectDistinguishedName, new object (), false);
672                 }
673
674                 [Test]
675                 public void Find_FindBySubjectKeyIdentifier ()
676                 {
677                         // empty
678                         Assert.AreEqual (0, collection.Find (X509FindType.FindBySubjectKeyIdentifier, String.Empty, false).Count, "FindBySubjectKeyIdentifier/Empty/false");
679
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");
685                 }
686
687                 [Test]
688                 [ExpectedException (typeof (CryptographicException))]
689                 public void Find_FindBySubjectKeyIdentifier_NotString ()
690                 {
691                         collection.Find (X509FindType.FindBySubjectKeyIdentifier, 1, false);
692                 }
693
694                 [Test]
695                 public void Find_FindBySubjectName ()
696                 {
697                         // empty
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");
706                 }
707
708                 [Test]
709                 [ExpectedException (typeof (CryptographicException))]
710                 public void Find_FindBySubjectName_NotString ()
711                 {
712                         collection.Find (X509FindType.FindBySubjectName, 'c', false);
713                 }
714
715                 [Test]
716                 public void Find_FindByTemplateName ()
717                 {
718                         // empty
719                         Assert.AreEqual (0, collection.Find (X509FindType.FindByTemplateName, String.Empty, false).Count, "FindByTemplateName/Empty/false");
720                         // wilcard match
721                         Assert.AreEqual (0, collection.Find (X509FindType.FindByTemplateName, "*", false).Count, "FindByTemplateName/Mono/false");
722                         // FIXME - need a positive test case
723                 }
724
725                 [Test]
726                 [ExpectedException (typeof (CryptographicException))]
727                 public void Find_FindByTemplateName_NotString ()
728                 {
729                         collection.Find (X509FindType.FindByTemplateName, 0, false);
730                 }
731
732                 [Test]
733                 public void Find_FindByThumbprint ()
734                 {
735                         // empty
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");
740                         // full match
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");
750                 }
751
752                 [Test]
753                 [ExpectedException (typeof (CryptographicException))]
754                 public void Find_FindByThumbprint_NotString ()
755                 {
756                         collection.Find (X509FindType.FindByThumbprint, 0, false);
757                 }
758
759                 [Test]
760                 public void Find_FindByTimeExpired ()
761                 {
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");
766
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");
771                 }
772
773                 [Test]
774                 [ExpectedException (typeof (CryptographicException))]
775                 public void Find_FindByTimeExpired_NotDateTime ()
776                 {
777                         collection.Find (X509FindType.FindByTimeExpired, String.Empty, false);
778                 }
779
780                 [Test]
781                 public void Find_FindByTimeNotYetValid ()
782                 {
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");
786
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");
791                 }
792
793                 [Test]
794                 [ExpectedException (typeof (CryptographicException))]
795                 public void Find_FindByTimeNotYetValid_NotDateTime ()
796                 {
797                         collection.Find (X509FindType.FindByTimeNotYetValid, DateTime.Now.ToString (), false);
798                 }
799
800                 [Test]
801                 public void Find_FindByTimeValid ()
802                 {
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");
806
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");
811                 }
812
813                 [Test]
814                 [ExpectedException (typeof (CryptographicException))]
815                 public void Find_FindByTimeValid_NotDateTime ()
816                 {
817                         collection.Find (X509FindType.FindByTimeValid, String.Empty, false);
818                 }
819
820                 [Test]
821                 [ExpectedException (typeof (ArgumentNullException))]
822                 public void Remove_X509Certificate2_Null ()
823                 {
824                         collection.Remove ((X509Certificate2) null);
825                 }
826
827                 [Test]
828                 [ExpectedException (typeof (CryptographicException))]
829                 public void Remove_X509Certificate2_Empty ()
830                 {
831                         single.Remove (cert_empty);
832                 }
833
834                 [Test]
835                 [ExpectedException (typeof (ArgumentNullException))]
836                 public void RemoveRange_X509Certificate2Collection_Null ()
837                 {
838                         collection.RemoveRange ((X509Certificate2Collection) null);
839                 }
840
841                 [Test]
842                 [ExpectedException (typeof (CryptographicException))]
843                 public void RemoveRange_X509Certificate2Collection_EmptyCert ()
844                 {
845                         collection.RemoveRange (new X509Certificate2Collection (cert_empty));
846                 }
847
848                 [Test]
849                 public void RemoveRange_X509Certificate2Collection_Empty ()
850                 {
851                         Assert.AreEqual (2, collection.Count, "Count/before");
852                         collection.RemoveRange (empty);
853                         Assert.AreEqual (2, collection.Count, "Count/after");
854                 }
855
856                 [Test]
857                 [ExpectedException (typeof (ArgumentNullException))]
858                 public void RemoveRange_X509Certificate2Array_Null ()
859                 {
860                         collection.RemoveRange ((X509Certificate2[]) null);
861                 }
862
863                 [Test]
864                 [ExpectedException (typeof (CryptographicException))]
865                 public void RemoveRange_X509Certificate2Array_EmptyCert ()
866                 {
867                         collection.RemoveRange (new X509Certificate2[1] { cert_empty });
868                 }
869
870                 [Test]
871                 public void RemoveRange_X509Certificate2Array_Empty ()
872                 {
873                         Assert.AreEqual (2, collection.Count, "Count/before");
874                         collection.RemoveRange (new X509Certificate2[0]);
875                         Assert.AreEqual (2, collection.Count, "Count/after");
876                 }
877
878                 [Test]
879                 [ExpectedException (typeof (InvalidCastException))]
880                 public void MixedCollection_Indexer ()
881                 {
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
886                 }
887
888                 [Test]
889                 [ExpectedException (typeof (InvalidCastException))]
890                 public void MixedCollection_Enumerator ()
891                 {
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");
896                         }
897                 }
898         }
899 }
900
901 #endif