Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b35850a

Browse files
committed
X509Certificate2Collection.Contains: Allow null certificates
X509Certificate2Collection.Contains throws ArgumentNullException when a null value is passed in, but the IList.Contains implementation and X509CertificateCollection.Contains do not. This commit changes X509Certificate2Collection.Contains to not throw for a null value to be consistent with the IList.Contains implementation and X509CertificateCollection.Contains.
1 parent 1acb8f4 commit b35850a

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/System.Security.Cryptography.X509Certificates/src/System/Security/Cryptography/X509Certificates/X509Certificate2Collection.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ public void AddRange(X509Certificate2Collection certificates)
9494

9595
public bool Contains(X509Certificate2 certificate)
9696
{
97-
if (certificate == null)
98-
throw new ArgumentNullException("certificate");
97+
// This method used to throw ArgumentNullException, but it has been deliberately changed
98+
// to no longer throw to match the behavior of X509CertificateCollection.Contains and the
99+
// IList.Contains implementation, which do not throw.
99100

100101
return base.Contains(certificate);
101102
}

src/System.Security.Cryptography.X509Certificates/tests/CollectionTests.cs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,6 @@ public static void X509Certificate2CollectionThrowsArgumentNullException()
129129
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509CertificateCollection)null));
130130
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509Certificate2[])null));
131131
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509Certificate2Collection)null));
132-
133-
// Note: X509CertificateCollection.Contains does not throw, but X509Certificate2Collection.Contains does throw.
134-
Assert.Throws<ArgumentNullException>(() => collection.Contains((X509Certificate2)null));
135-
136132
Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null, 0));
137133
Assert.Throws<ArgumentNullException>(() => collection.Insert(0, (X509Certificate)null));
138134
Assert.Throws<ArgumentNullException>(() => collection.Insert(0, (X509Certificate2)null));
@@ -209,6 +205,57 @@ public static void X509Certificate2CollectionThrowsArgumentOutOfRangeException()
209205
}
210206
}
211207

208+
[Fact]
209+
public static void X509CertificateCollectionContains()
210+
{
211+
using (X509Certificate c1 = new X509Certificate())
212+
using (X509Certificate c2 = new X509Certificate())
213+
using (X509Certificate c3 = new X509Certificate())
214+
{
215+
X509CertificateCollection collection = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
216+
217+
Assert.True(collection.Contains(c1));
218+
Assert.True(collection.Contains(c2));
219+
Assert.True(collection.Contains(c3));
220+
Assert.False(collection.Contains(null));
221+
222+
IList ilist = (IList)collection;
223+
Assert.True(ilist.Contains(c1));
224+
Assert.True(ilist.Contains(c2));
225+
Assert.True(ilist.Contains(c3));
226+
Assert.False(ilist.Contains(null));
227+
Assert.False(ilist.Contains("Bogus"));
228+
}
229+
}
230+
231+
[Fact]
232+
public static void X509Certificate2CollectionContains()
233+
{
234+
using (X509Certificate2 c1 = new X509Certificate2())
235+
using (X509Certificate2 c2 = new X509Certificate2())
236+
using (X509Certificate2 c3 = new X509Certificate2())
237+
{
238+
X509Certificate2Collection collection = new X509Certificate2Collection(new X509Certificate2[] { c1, c2, c3 });
239+
240+
Assert.True(collection.Contains(c1));
241+
Assert.True(collection.Contains(c2));
242+
Assert.True(collection.Contains(c3));
243+
244+
// Note: X509Certificate2Collection.Contains used to throw ArgumentNullException, but it
245+
// has been deliberately changed to no longer throw to match the behavior of
246+
// X509CertificateCollection.Contains and the IList.Contains implementation, which do not
247+
// throw.
248+
Assert.False(collection.Contains(null));
249+
250+
IList ilist = (IList)collection;
251+
Assert.True(ilist.Contains(c1));
252+
Assert.True(ilist.Contains(c2));
253+
Assert.True(ilist.Contains(c3));
254+
Assert.False(ilist.Contains(null));
255+
Assert.False(ilist.Contains("Bogus"));
256+
}
257+
}
258+
212259
[Fact]
213260
public static void X509CertificateCollectionEnumeratorModification()
214261
{

0 commit comments

Comments
 (0)