-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
CollectionTests.cs
1789 lines (1500 loc) · 74.6 KB
/
CollectionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Formats.Asn1;
using System.IO;
using System.Text;
using Test.Cryptography;
using Xunit;
namespace System.Security.Cryptography.X509Certificates.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Browser doesn't support X.509 certificates")]
public static class CollectionTests
{
[Fact]
public static void X509CertificateCollectionsProperties()
{
IList ilist = new X509CertificateCollection();
Assert.False(ilist.IsSynchronized);
Assert.False(ilist.IsFixedSize);
Assert.False(ilist.IsReadOnly);
ilist = new X509Certificate2Collection();
Assert.False(ilist.IsSynchronized);
Assert.False(ilist.IsFixedSize);
Assert.False(ilist.IsReadOnly);
}
[Fact]
public static void X509CertificateCollectionConstructors()
{
using (X509Certificate c1 = new X509Certificate())
using (X509Certificate c2 = new X509Certificate())
using (X509Certificate c3 = new X509Certificate())
{
X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
Assert.Equal(3, cc.Count);
Assert.Same(c1, cc[0]);
Assert.Same(c2, cc[1]);
Assert.Same(c3, cc[2]);
X509CertificateCollection cc2 = new X509CertificateCollection(cc);
Assert.Equal(3, cc2.Count);
Assert.Same(c1, cc2[0]);
Assert.Same(c2, cc2[1]);
Assert.Same(c3, cc2[2]);
Assert.Throws<ArgumentNullException>(() => new X509CertificateCollection(new X509Certificate[] { c1, c2, null, c3 }));
}
}
[Fact]
public static void X509Certificate2CollectionConstructors()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
using (X509Certificate2 c3 = new X509Certificate2())
{
X509Certificate2Collection cc = new X509Certificate2Collection(new X509Certificate2[] { c1, c2, c3 });
Assert.Equal(3, cc.Count);
Assert.Same(c1, cc[0]);
Assert.Same(c2, cc[1]);
Assert.Same(c3, cc[2]);
X509Certificate2Collection cc2 = new X509Certificate2Collection(cc);
Assert.Equal(3, cc2.Count);
Assert.Same(c1, cc2[0]);
Assert.Same(c2, cc2[1]);
Assert.Same(c3, cc2[2]);
Assert.Throws<ArgumentNullException>(() => new X509Certificate2Collection(new X509Certificate2[] { c1, c2, null, c3 }));
using (X509Certificate c4 = new X509Certificate())
{
X509Certificate2Collection collection = new X509Certificate2Collection { c1, c2, c3 };
((IList)collection).Add(c4); // Add non-X509Certificate2 object
Assert.Throws<InvalidCastException>(() => new X509Certificate2Collection(collection));
}
}
}
[Fact]
public static void X509Certificate2CollectionEnumerator()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
using (X509Certificate2 c3 = new X509Certificate2())
{
X509Certificate2Collection cc = new X509Certificate2Collection(new X509Certificate2[] { c1, c2, c3 });
object ignored;
X509Certificate2Enumerator e = cc.GetEnumerator();
for (int i = 0; i < 2; i++)
{
// Not started
Assert.Throws<InvalidOperationException>(() => ignored = e.Current);
Assert.True(e.MoveNext());
Assert.Same(c1, e.Current);
Assert.True(e.MoveNext());
Assert.Same(c2, e.Current);
Assert.True(e.MoveNext());
Assert.Same(c3, e.Current);
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
// Ended
Assert.Throws<InvalidOperationException>(() => ignored = e.Current);
e.Reset();
}
IEnumerator e2 = cc.GetEnumerator();
TestNonGenericEnumerator(e2, c1, c2, c3);
IEnumerator e3 = ((IEnumerable)cc).GetEnumerator();
TestNonGenericEnumerator(e3, c1, c2, c3);
}
}
[Fact]
public static void X509CertificateCollectionEnumerator()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
using (X509Certificate2 c3 = new X509Certificate2())
{
X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
object ignored;
X509CertificateCollection.X509CertificateEnumerator e = cc.GetEnumerator();
for (int i = 0; i < 2; i++)
{
// Not started
Assert.Throws<InvalidOperationException>(() => ignored = e.Current);
Assert.True(e.MoveNext());
Assert.Same(c1, e.Current);
Assert.True(e.MoveNext());
Assert.Same(c2, e.Current);
Assert.True(e.MoveNext());
Assert.Same(c3, e.Current);
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
// Ended
Assert.Throws<InvalidOperationException>(() => ignored = e.Current);
e.Reset();
}
IEnumerator e2 = cc.GetEnumerator();
TestNonGenericEnumerator(e2, c1, c2, c3);
IEnumerator e3 = ((IEnumerable)cc).GetEnumerator();
TestNonGenericEnumerator(e3, c1, c2, c3);
}
}
private static void TestNonGenericEnumerator(IEnumerator e, object c1, object c2, object c3)
{
object ignored;
for (int i = 0; i < 2; i++)
{
// Not started
Assert.Throws<InvalidOperationException>(() => ignored = e.Current);
Assert.True(e.MoveNext());
Assert.Same(c1, e.Current);
Assert.True(e.MoveNext());
Assert.Same(c2, e.Current);
Assert.True(e.MoveNext());
Assert.Same(c3, e.Current);
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
// Ended
Assert.Throws<InvalidOperationException>(() => ignored = e.Current);
e.Reset();
}
}
[Fact]
public static void X509CertificateCollectionThrowsArgumentNullException()
{
using (X509Certificate certificate = new X509Certificate())
{
Assert.Throws<ArgumentNullException>(() => new X509CertificateCollection((X509Certificate[])null));
Assert.Throws<ArgumentNullException>(() => new X509CertificateCollection((X509CertificateCollection)null));
X509CertificateCollection collection = new X509CertificateCollection { certificate };
Assert.Throws<ArgumentNullException>(() => collection[0] = null);
Assert.Throws<ArgumentNullException>(() => collection.Add(null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509Certificate[])null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509CertificateCollection)null));
Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => collection.Insert(0, null));
Assert.Throws<ArgumentNullException>(() => collection.Remove(null));
IList ilist = (IList)collection;
Assert.Throws<ArgumentNullException>(() => ilist[0] = null);
Assert.Throws<ArgumentNullException>(() => ilist.Add(null));
Assert.Throws<ArgumentNullException>(() => ilist.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => ilist.Insert(0, null));
Assert.Throws<ArgumentNullException>(() => ilist.Remove(null));
}
AssertExtensions.Throws<ArgumentNullException, NullReferenceException>(
() => new X509CertificateCollection.X509CertificateEnumerator(null));
}
[Fact]
public static void X509Certificate2CollectionThrowsArgumentNullException()
{
using (X509Certificate2 certificate = new X509Certificate2())
{
Assert.Throws<ArgumentNullException>(() => new X509Certificate2Collection((X509Certificate2[])null));
Assert.Throws<ArgumentNullException>(() => new X509Certificate2Collection((X509Certificate2Collection)null));
X509Certificate2Collection collection = new X509Certificate2Collection { certificate };
Assert.Throws<ArgumentNullException>(() => collection[0] = null);
Assert.Throws<ArgumentNullException>(() => collection.Add((X509Certificate)null));
Assert.Throws<ArgumentNullException>(() => collection.Add((X509Certificate2)null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509Certificate[])null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509CertificateCollection)null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509Certificate2[])null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509Certificate2Collection)null));
Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => collection.Insert(0, (X509Certificate)null));
Assert.Throws<ArgumentNullException>(() => collection.Insert(0, (X509Certificate2)null));
Assert.Throws<ArgumentNullException>(() => collection.Remove((X509Certificate)null));
Assert.Throws<ArgumentNullException>(() => collection.Remove((X509Certificate2)null));
Assert.Throws<ArgumentNullException>(() => collection.RemoveRange((X509Certificate2[])null));
Assert.Throws<ArgumentNullException>(() => collection.RemoveRange((X509Certificate2Collection)null));
Assert.Throws<ArgumentNullException>(() => collection.Import((byte[])null));
Assert.Throws<ArgumentNullException>(() => collection.Import((string)null));
IList ilist = (IList)collection;
Assert.Throws<ArgumentNullException>(() => ilist[0] = null);
Assert.Throws<ArgumentNullException>(() => ilist.Add(null));
Assert.Throws<ArgumentNullException>(() => ilist.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => ilist.Insert(0, null));
Assert.Throws<ArgumentNullException>(() => ilist.Remove(null));
}
}
[Fact]
public static void X509CertificateCollectionThrowsArgumentOutOfRangeException()
{
using (X509Certificate certificate = new X509Certificate())
{
X509CertificateCollection collection = new X509CertificateCollection { certificate };
Assert.Throws<ArgumentOutOfRangeException>(() => collection[-1]);
Assert.Throws<ArgumentOutOfRangeException>(() => collection[collection.Count]);
Assert.Throws<ArgumentOutOfRangeException>(() => collection[-1] = certificate);
Assert.Throws<ArgumentOutOfRangeException>(() => collection[collection.Count] = certificate);
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Insert(-1, certificate));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Insert(collection.Count + 1, certificate));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.RemoveAt(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.RemoveAt(collection.Count));
IList ilist = (IList)collection;
Assert.Throws<ArgumentOutOfRangeException>(() => ilist[-1]);
Assert.Throws<ArgumentOutOfRangeException>(() => ilist[collection.Count]);
Assert.Throws<ArgumentOutOfRangeException>(() => ilist[-1] = certificate);
Assert.Throws<ArgumentOutOfRangeException>(() => ilist[collection.Count] = certificate);
Assert.Throws<ArgumentOutOfRangeException>(() => ilist.Insert(-1, certificate));
Assert.Throws<ArgumentOutOfRangeException>(() => ilist.Insert(collection.Count + 1, certificate));
Assert.Throws<ArgumentOutOfRangeException>(() => ilist.RemoveAt(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => ilist.RemoveAt(collection.Count));
}
}
[Fact]
public static void X509Certificate2CollectionThrowsArgumentOutOfRangeException()
{
using (X509Certificate2 certificate = new X509Certificate2())
{
X509Certificate2Collection collection = new X509Certificate2Collection { certificate };
Assert.Throws<ArgumentOutOfRangeException>(() => collection[-1]);
Assert.Throws<ArgumentOutOfRangeException>(() => collection[collection.Count]);
Assert.Throws<ArgumentOutOfRangeException>(() => collection[-1] = certificate);
Assert.Throws<ArgumentOutOfRangeException>(() => collection[collection.Count] = certificate);
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Insert(-1, certificate));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Insert(collection.Count + 1, certificate));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.RemoveAt(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.RemoveAt(collection.Count));
IList ilist = (IList)collection;
Assert.Throws<ArgumentOutOfRangeException>(() => ilist[-1]);
Assert.Throws<ArgumentOutOfRangeException>(() => ilist[collection.Count]);
Assert.Throws<ArgumentOutOfRangeException>(() => ilist[-1] = certificate);
Assert.Throws<ArgumentOutOfRangeException>(() => ilist[collection.Count] = certificate);
Assert.Throws<ArgumentOutOfRangeException>(() => ilist.Insert(-1, certificate));
Assert.Throws<ArgumentOutOfRangeException>(() => ilist.Insert(collection.Count + 1, certificate));
Assert.Throws<ArgumentOutOfRangeException>(() => ilist.RemoveAt(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => ilist.RemoveAt(collection.Count));
}
}
[Fact]
public static void X509CertificateCollectionContains()
{
using (X509Certificate c1 = new X509Certificate())
using (X509Certificate c2 = new X509Certificate())
using (X509Certificate c3 = new X509Certificate())
{
X509CertificateCollection collection = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
Assert.True(collection.Contains(c1));
Assert.True(collection.Contains(c2));
Assert.True(collection.Contains(c3));
Assert.False(collection.Contains(null));
IList ilist = (IList)collection;
Assert.True(ilist.Contains(c1));
Assert.True(ilist.Contains(c2));
Assert.True(ilist.Contains(c3));
Assert.False(ilist.Contains(null));
Assert.False(ilist.Contains("Bogus"));
}
}
[Fact]
public static void X509Certificate2CollectionContains()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
using (X509Certificate2 c3 = new X509Certificate2())
{
X509Certificate2Collection collection = new X509Certificate2Collection(new X509Certificate2[] { c1, c2, c3 });
Assert.True(collection.Contains(c1));
Assert.True(collection.Contains(c2));
Assert.True(collection.Contains(c3));
// Note: X509Certificate2Collection.Contains used to throw ArgumentNullException, but it
// has been deliberately changed to no longer throw to match the behavior of
// X509CertificateCollection.Contains and the IList.Contains implementation, which do not
// throw.
Assert.False(collection.Contains(null));
IList ilist = (IList)collection;
Assert.True(ilist.Contains(c1));
Assert.True(ilist.Contains(c2));
Assert.True(ilist.Contains(c3));
Assert.False(ilist.Contains(null));
Assert.False(ilist.Contains("Bogus"));
}
}
[Fact]
public static void X509CertificateCollectionEnumeratorModification()
{
using (X509Certificate c1 = new X509Certificate())
using (X509Certificate c2 = new X509Certificate())
using (X509Certificate c3 = new X509Certificate())
{
X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
X509CertificateCollection.X509CertificateEnumerator e = cc.GetEnumerator();
cc.Add(c1);
// Collection changed.
Assert.Throws<InvalidOperationException>(() => e.MoveNext());
Assert.Throws<InvalidOperationException>(() => e.Reset());
}
}
[Fact]
public static void X509Certificate2CollectionEnumeratorModification()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
using (X509Certificate2 c3 = new X509Certificate2())
{
X509Certificate2Collection cc = new X509Certificate2Collection(new X509Certificate2[] { c1, c2, c3 });
X509Certificate2Enumerator e = cc.GetEnumerator();
cc.Add(c1);
// Collection changed.
Assert.Throws<InvalidOperationException>(() => e.MoveNext());
Assert.Throws<InvalidOperationException>(() => e.Reset());
}
}
[Fact]
public static void X509CertificateCollectionAdd()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
{
X509CertificateCollection cc = new X509CertificateCollection();
int idx = cc.Add(c1);
Assert.Equal(0, idx);
Assert.Same(c1, cc[0]);
idx = cc.Add(c2);
Assert.Equal(1, idx);
Assert.Same(c2, cc[1]);
Assert.Throws<ArgumentNullException>(() => cc.Add(null));
IList il = new X509CertificateCollection();
idx = il.Add(c1);
Assert.Equal(0, idx);
Assert.Same(c1, il[0]);
idx = il.Add(c2);
Assert.Equal(1, idx);
Assert.Same(c2, il[1]);
Assert.Throws<ArgumentNullException>(() => il.Add(null));
}
}
[Fact]
public static void X509CertificateCollectionAsIList()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
{
IList il = new X509CertificateCollection();
il.Add(c1);
il.Add(c2);
Assert.Throws<ArgumentNullException>(() => il[0] = null);
}
}
[Fact]
// On .NET Framework, list is untyped so it allows arbitrary types in it
public static void X509CertificateCollectionAsIListBogusEntry()
{
using (X509Certificate2 c = new X509Certificate2())
{
IList il = new X509CertificateCollection();
il.Add(c);
string bogus = "Bogus";
AssertExtensions.Throws<ArgumentException>("value", () => il[0] = bogus);
AssertExtensions.Throws<ArgumentException>("value", () => il.Add(bogus));
AssertExtensions.Throws<ArgumentException>("value", () => il.Remove(bogus));
AssertExtensions.Throws<ArgumentException>("value", () => il.Insert(0, bogus));
}
}
[Fact]
public static void AddDoesNotClone()
{
using (X509Certificate2 c1 = new X509Certificate2())
{
X509Certificate2Collection coll = new X509Certificate2Collection();
coll.Add(c1);
Assert.Same(c1, coll[0]);
}
}
[Fact]
public static void ImportStoreSavedAsCerData()
{
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
{
using (ImportedCollection ic = Cert.Import(TestData.StoreSavedAsCerData))
{
X509Certificate2Collection cc2 = ic.Collection;
int count = cc2.Count;
Assert.Equal(1, count);
using (X509Certificate2 c = cc2[0])
{
// pfxCer was loaded directly, cc2[0] was Imported, two distinct copies.
Assert.NotSame(pfxCer, c);
Assert.Equal(pfxCer, c);
Assert.Equal(pfxCer.Thumbprint, c.Thumbprint);
}
}
}
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // StoreSavedAsSerializedCerData not supported on Unix
public static void ImportStoreSavedAsSerializedCerData_Windows()
{
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword, Cert.EphemeralIfPossible))
{
using (ImportedCollection ic = Cert.Import(TestData.StoreSavedAsSerializedCerData))
{
X509Certificate2Collection cc2 = ic.Collection;
int count = cc2.Count;
Assert.Equal(1, count);
using (X509Certificate2 c = cc2[0])
{
// pfxCer was loaded directly, cc2[0] was Imported, two distinct copies.
Assert.NotSame(pfxCer, c);
Assert.Equal(pfxCer, c);
Assert.Equal(pfxCer.Thumbprint, c.Thumbprint);
}
}
}
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // StoreSavedAsSerializedCerData not supported on Unix
public static void ImportStoreSavedAsSerializedCerData_Unix()
{
X509Certificate2Collection cc2 = new X509Certificate2Collection();
Assert.ThrowsAny<CryptographicException>(() => cc2.Import(TestData.StoreSavedAsSerializedCerData));
Assert.Equal(0, cc2.Count);
}
[Theory]
[MemberData(nameof(StorageFlags))]
[PlatformSpecific(TestPlatforms.Windows)] // StoreSavedAsSerializedStoreData not supported on Unix
public static void ImportStoreSavedAsSerializedStoreData_Windows(X509KeyStorageFlags keyStorageFlags)
{
using (var msCer = new X509Certificate2(TestData.MsCertificate))
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword, keyStorageFlags))
using (ImportedCollection ic = Cert.Import(TestData.StoreSavedAsSerializedStoreData))
{
X509Certificate2Collection cc2 = ic.Collection;
int count = cc2.Count;
Assert.Equal(2, count);
X509Certificate2[] cs = cc2.ToArray().OrderBy(c => c.Subject).ToArray();
Assert.NotSame(msCer, cs[0]);
Assert.Equal(msCer, cs[0]);
Assert.Equal(msCer.Thumbprint, cs[0].Thumbprint);
Assert.NotSame(pfxCer, cs[1]);
Assert.Equal(pfxCer, cs[1]);
Assert.Equal(pfxCer.Thumbprint, cs[1].Thumbprint);
}
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // StoreSavedAsSerializedStoreData not supported on Unix
public static void ImportStoreSavedAsSerializedStoreData_Unix()
{
X509Certificate2Collection cc2 = new X509Certificate2Collection();
Assert.ThrowsAny<CryptographicException>(() => cc2.Import(TestData.StoreSavedAsSerializedStoreData));
Assert.Equal(0, cc2.Count);
}
[Fact]
[SkipOnPlatform(PlatformSupport.MobileAppleCrypto, "PKCS#7 import is not available")]
public static void ImportStoreSavedAsPfxData()
{
using (var msCer = new X509Certificate2(TestData.MsCertificate))
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword, Cert.EphemeralIfPossible))
using (ImportedCollection ic = Cert.Import(TestData.StoreSavedAsPfxData))
{
X509Certificate2Collection cc2 = ic.Collection;
int count = cc2.Count;
Assert.Equal(2, count);
X509Certificate2[] cs = cc2.ToArray().OrderBy(c => c.Subject).ToArray();
Assert.NotSame(msCer, cs[0]);
Assert.Equal(msCer, cs[0]);
Assert.Equal(msCer.Thumbprint, cs[0].Thumbprint);
Assert.NotSame(pfxCer, cs[1]);
Assert.Equal(pfxCer, cs[1]);
Assert.Equal(pfxCer.Thumbprint, cs[1].Thumbprint);
}
}
[Fact]
public static void ImportInvalidData()
{
X509Certificate2Collection cc2 = new X509Certificate2Collection();
Assert.ThrowsAny<CryptographicException>(() => cc2.Import(new byte[] { 0, 1, 1, 2, 3, 5, 8, 13, 21 }));
}
[Theory]
[MemberData(nameof(StorageFlags))]
public static void ImportFromFileTests(X509KeyStorageFlags storageFlags)
{
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword, storageFlags))
{
using (ImportedCollection ic = Cert.Import(TestFiles.PfxFile, TestData.PfxDataPassword, storageFlags))
{
X509Certificate2Collection cc2 = ic.Collection;
int count = cc2.Count;
Assert.Equal(1, count);
using (X509Certificate2 c = cc2[0])
{
// pfxCer was loaded directly, cc2[0] was Imported, two distinct copies.
Assert.NotSame(pfxCer, c);
Assert.Equal(pfxCer, c);
Assert.Equal(pfxCer.Thumbprint, c.Thumbprint);
}
}
}
}
[Fact]
public static void ImportMultiplePrivateKeysPfx()
{
using (ImportedCollection ic = Cert.Import(TestData.MultiPrivateKeyPfx, (string?)null, X509KeyStorageFlags.DefaultKeySet))
{
X509Certificate2Collection collection = ic.Collection;
Assert.Equal(2, collection.Count);
foreach (X509Certificate2 cert in collection)
{
Assert.True(cert.HasPrivateKey, "cert.HasPrivateKey");
}
}
}
[Fact]
public static void ExportCert()
{
TestExportSingleCert(X509ContentType.Cert);
}
[Fact]
public static void ExportCert_SecureString()
{
TestExportSingleCert_SecureStringPassword(X509ContentType.Cert);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // SerializedCert not supported on Unix
public static void ExportSerializedCert_Windows()
{
TestExportSingleCert(X509ContentType.SerializedCert);
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // SerializedCert not supported on Unix
public static void ExportSerializedCert_Unix()
{
using (var msCer = new X509Certificate2(TestData.MsCertificate))
using (var ecdsa256Cer = new X509Certificate2(TestData.ECDsa256Certificate))
{
X509Certificate2Collection cc = new X509Certificate2Collection(new[] { msCer, ecdsa256Cer });
Assert.Throws<PlatformNotSupportedException>(() => cc.Export(X509ContentType.SerializedCert));
}
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // SerializedStore not supported on Unix
public static void ExportSerializedStore_Windows()
{
TestExportStore(X509ContentType.SerializedStore);
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // SerializedStore not supported on Unix
public static void ExportSerializedStore_Unix()
{
using (var msCer = new X509Certificate2(TestData.MsCertificate))
using (var ecdsa256Cer = new X509Certificate2(TestData.ECDsa256Certificate))
{
X509Certificate2Collection cc = new X509Certificate2Collection(new[] { msCer, ecdsa256Cer });
Assert.Throws<PlatformNotSupportedException>(() => cc.Export(X509ContentType.SerializedStore));
}
}
[Fact]
[SkipOnPlatform(PlatformSupport.MobileAppleCrypto, "PKCS#7 export is not available")]
public static void ExportPkcs7()
{
TestExportStore(X509ContentType.Pkcs7);
}
[Fact]
public static void X509CertificateCollectionSyncRoot()
{
var cc = new X509CertificateCollection();
Assert.NotNull(((ICollection)cc).SyncRoot);
Assert.Same(((ICollection)cc).SyncRoot, ((ICollection)cc).SyncRoot);
}
[Fact]
public static void ExportEmpty_Cert()
{
var collection = new X509Certificate2Collection();
byte[] exported = collection.Export(X509ContentType.Cert);
Assert.Null(exported);
}
[Fact]
public static void ExportEmpty_Pkcs12()
{
var collection = new X509Certificate2Collection();
byte[] exported = collection.Export(X509ContentType.Pkcs12);
// The empty PFX is legal, the answer won't be null.
Assert.NotNull(exported);
}
[Fact]
public static void ExportUnrelatedPfx()
{
// Export multiple certificates which are not part of any kind of certificate chain.
// Nothing in the PKCS12 structure requires they're related, but it might be an underlying
// assumption of the provider.
using (var cert1 = new X509Certificate2(TestData.MsCertificate))
using (var cert2 = new X509Certificate2(TestData.ComplexNameInfoCert))
using (var cert3 = new X509Certificate2(TestData.CertWithPolicies))
{
var collection = new X509Certificate2Collection
{
cert1,
cert2,
cert3,
};
byte[] exported = collection.Export(X509ContentType.Pkcs12);
using (ImportedCollection ic = Cert.Import(exported, (string?)null, X509KeyStorageFlags.DefaultKeySet))
{
X509Certificate2Collection importedCollection = ic.Collection;
// Verify that the two collections contain the same certificates,
// but the order isn't really a factor.
Assert.Equal(collection.Count, importedCollection.Count);
// Compare just the subject names first, because it's the easiest thing to read out of the failure message.
string[] subjects = new string[collection.Count];
string[] importedSubjects = new string[collection.Count];
for (int i = 0; i < collection.Count; i++)
{
subjects[i] = collection[i].GetNameInfo(X509NameType.SimpleName, false);
importedSubjects[i] = importedCollection[i].GetNameInfo(X509NameType.SimpleName, false);
}
Assert.Equal(subjects, importedSubjects);
// But, really, the collections should be equivalent
// (after being coerced to IEnumerable<X509Certificate2>)
Assert.Equal(collection, importedCollection);
}
}
}
[Fact]
public static void MultipleImport()
{
var collection = new X509Certificate2Collection();
try
{
collection.Import(TestFiles.DummyTcpServerPfxFile, (string)null, Cert.EphemeralIfPossible);
collection.Import(TestData.PfxData, TestData.PfxDataPassword, Cert.EphemeralIfPossible);
Assert.Equal(3, collection.Count);
}
finally
{
foreach (X509Certificate2 cert in collection)
{
cert.Dispose();
}
}
}
[Fact]
public static void ExportMultiplePrivateKeys()
{
var collection = new X509Certificate2Collection();
try
{
collection.Import(TestFiles.DummyTcpServerPfxFile, (string)null, X509KeyStorageFlags.Exportable | Cert.EphemeralIfPossible);
collection.Import(TestData.PfxData, TestData.PfxDataPassword, X509KeyStorageFlags.Exportable | Cert.EphemeralIfPossible);
// Pre-condition, we have multiple private keys
int originalPrivateKeyCount = collection.Count(c => c.HasPrivateKey);
Assert.Equal(2, originalPrivateKeyCount);
byte[] exported = collection.Export(X509ContentType.Pkcs12);
using (ImportedCollection ic = Cert.Import(exported, (string?)null, X509KeyStorageFlags.DefaultKeySet))
{
X509Certificate2Collection importedCollection = ic.Collection;
Assert.Equal(collection.Count, importedCollection.Count);
int importedPrivateKeyCount = importedCollection.Count(c => c.HasPrivateKey);
Assert.Equal(originalPrivateKeyCount, importedPrivateKeyCount);
}
}
finally
{
foreach (X509Certificate2 cert in collection)
{
cert.Dispose();
}
}
}
[Fact]
[SkipOnPlatform(TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "The PKCS#12 Exportable flag is not supported on iOS/MacCatalyst/tvOS")]
public static void CanAddMultipleCertsWithSinglePrivateKey()
{
using (var oneWithKey = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword, X509KeyStorageFlags.Exportable | Cert.EphemeralIfPossible))
using (var twoWithoutKey = new X509Certificate2(TestData.ComplexNameInfoCert))
{
Assert.True(oneWithKey.HasPrivateKey);
var col = new X509Certificate2Collection
{
oneWithKey,
twoWithoutKey,
};
Assert.Equal(1, col.Count(x => x.HasPrivateKey));
Assert.Equal(2, col.Count);
byte[] buffer = col.Export(X509ContentType.Pfx);
using (ImportedCollection newCollection = Cert.Import(buffer, (string?)null, X509KeyStorageFlags.DefaultKeySet))
{
Assert.Equal(2, newCollection.Collection.Count);
}
}
}
[Fact]
public static void X509CertificateCollectionCopyTo()
{
using (X509Certificate c1 = new X509Certificate())
using (X509Certificate c2 = new X509Certificate())
using (X509Certificate c3 = new X509Certificate())
{
X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
X509Certificate[] array1 = new X509Certificate[cc.Count];
cc.CopyTo(array1, 0);
Assert.Same(c1, array1[0]);
Assert.Same(c2, array1[1]);
Assert.Same(c3, array1[2]);
X509Certificate[] array2 = new X509Certificate[cc.Count];
((ICollection)cc).CopyTo(array2, 0);
Assert.Same(c1, array2[0]);
Assert.Same(c2, array2[1]);
Assert.Same(c3, array2[2]);
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNonZeroLowerBoundArraySupported))]
public static void X509ChainElementCollection_CopyTo_NonZeroLowerBound_ThrowsIndexOutOfRangeException()
{
using (var microsoftDotCom = new X509Certificate2(TestData.MicrosoftDotComSslCertBytes))
using (var microsoftDotComIssuer = new X509Certificate2(TestData.MicrosoftDotComIssuerBytes))
using (var microsoftDotComRoot = new X509Certificate2(TestData.MicrosoftDotComRootBytes))
using (var chainHolder = new ChainHolder())
{
X509Chain chain = chainHolder.Chain;
chain.ChainPolicy.ExtraStore.Add(microsoftDotComRoot);
chain.ChainPolicy.ExtraStore.Add(microsoftDotComIssuer);
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
// Halfway between microsoftDotCom's NotBefore and NotAfter
// This isn't a boundary condition test.
chain.ChainPolicy.VerificationTime = new DateTime(2021, 02, 26, 12, 01, 01, DateTimeKind.Local);
bool valid = chain.Build(microsoftDotCom);
Assert.True(valid, "Precondition: Chain built validly");
ICollection collection = chain.ChainElements;
Array array = Array.CreateInstance(typeof(object), new int[] { 10 }, new int[] { 10 });
Assert.Throws<IndexOutOfRangeException>(() => collection.CopyTo(array, 0));
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNonZeroLowerBoundArraySupported))]
public static void X509ExtensionCollection_CopyTo_NonZeroLowerBound_ThrowsIndexOutOfRangeException()
{
using (X509Certificate2 cert = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword, Cert.EphemeralIfPossible))
{
ICollection collection = cert.Extensions;
Array array = Array.CreateInstance(typeof(object), new int[] { 10 }, new int[] { 10 });
Assert.Throws<IndexOutOfRangeException>(() => collection.CopyTo(array, 0));
}
}
[Fact]
public static void X509CertificateCollectionIndexOf()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword, Cert.EphemeralIfPossible))
{
X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2 });
Assert.Equal(0, cc.IndexOf(c1));
Assert.Equal(1, cc.IndexOf(c2));
IList il = cc;
Assert.Equal(0, il.IndexOf(c1));
Assert.Equal(1, il.IndexOf(c2));
}
}
[Fact]
public static void X509CertificateCollectionRemove()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword, Cert.EphemeralIfPossible))
{
X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2 });
cc.Remove(c1);
Assert.Equal(1, cc.Count);
Assert.Same(c2, cc[0]);
cc.Remove(c2);
Assert.Equal(0, cc.Count);
AssertExtensions.Throws<ArgumentException>(null, () => cc.Remove(c2));
IList il = new X509CertificateCollection(new X509Certificate[] { c1, c2 });
il.Remove(c1);
Assert.Equal(1, il.Count);
Assert.Same(c2, il[0]);
il.Remove(c2);
Assert.Equal(0, il.Count);
AssertExtensions.Throws<ArgumentException>(null, () => il.Remove(c2));
}
}
[Fact]
public static void X509CertificateCollectionRemoveAt()
{
using (X509Certificate c1 = new X509Certificate())
using (X509Certificate c2 = new X509Certificate())
using (X509Certificate c3 = new X509Certificate())
{
X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
cc.RemoveAt(0);
Assert.Equal(2, cc.Count);
Assert.Same(c2, cc[0]);
Assert.Same(c3, cc[1]);
cc.RemoveAt(1);
Assert.Equal(1, cc.Count);
Assert.Same(c2, cc[0]);
cc.RemoveAt(0);
Assert.Equal(0, cc.Count);
IList il = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
il.RemoveAt(0);
Assert.Equal(2, il.Count);
Assert.Same(c2, il[0]);