Skip to content

Commit c05e9d1

Browse files
vcsjoneswfurtrzikmbartonjs
authored
Update System.Net.* unit tests to react to SYSLIB0057 obsoletion (#104487)
* Update System.Net.* to react to SYSLIB0057 obsoletion * react to changes in System.Net.WebSockets.Client.Wasm.Tests.csproj --------- Co-authored-by: Tomas Weinfurt <[email protected]> Co-authored-by: Radek Zikmund <[email protected]> Co-authored-by: Jeremy Barton <[email protected]> Co-authored-by: Radek Zikmund <[email protected]>
1 parent d756692 commit c05e9d1

22 files changed

+28
-30
lines changed

src/libraries/Common/tests/System/Net/Configuration.Certificates.Dynamic.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static (X509Certificate2 certificate, X509Certificate2Collection) Generat
163163
if (!ephemeralKey && PlatformDetection.IsWindows)
164164
{
165165
X509Certificate2 ephemeral = endEntity;
166-
endEntity = new X509Certificate2(endEntity.Export(X509ContentType.Pfx), (string?)null, X509KeyStorageFlags.Exportable);
166+
endEntity = X509CertificateLoader.LoadPkcs12(endEntity.Export(X509ContentType.Pfx), (string?)null, X509KeyStorageFlags.Exportable);
167167
ephemeral.Dispose();
168168
}
169169

src/libraries/Common/tests/System/Net/Configuration.Certificates.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ static Certificates()
4747
Assert.True(mutex?.WaitOne(MutexTimeoutMs) ?? true, "Could not acquire the global certificate mutex.");
4848
try
4949
{
50-
s_serverCertificate = new X509Certificate2(serverCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
51-
s_clientCertificate = new X509Certificate2(clientCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
52-
s_noEKUCertificate = new X509Certificate2(noEKUCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
53-
s_selfSignedServerCertificate = new X509Certificate2(selfSignedServerCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
54-
s_selfSignedClientCertificate = new X509Certificate2(selfSignedClientCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
50+
s_serverCertificate = X509CertificateLoader.LoadPkcs12(serverCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
51+
s_clientCertificate = X509CertificateLoader.LoadPkcs12(clientCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
52+
s_noEKUCertificate = X509CertificateLoader.LoadPkcs12(noEKUCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
53+
s_selfSignedServerCertificate = X509CertificateLoader.LoadPkcs12(selfSignedServerCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
54+
s_selfSignedClientCertificate = X509CertificateLoader.LoadPkcs12(selfSignedClientCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
5555
}
5656
finally { mutex?.ReleaseMutex(); }
5757
}
@@ -91,7 +91,7 @@ public static X509Certificate2 GetSelfSigned13ServerCertificate()
9191
{
9292
using (innerCert)
9393
{
94-
cert = new X509Certificate2(innerCert.Export(X509ContentType.Pfx));
94+
cert = X509CertificateLoader.LoadPkcs12(innerCert.Export(X509ContentType.Pfx), (string?)null);
9595
}
9696
}
9797
else

src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.ClientCertificates.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ await TestHelper.WhenAllCompletedOrAnyFailed(
115115
{
116116
_output.WriteLine(
117117
"Client cert: {0}",
118-
new X509Certificate2(sslStream.RemoteCertificate.Export(X509ContentType.Cert)).GetNameInfo(X509NameType.SimpleName, false));
118+
X509CertificateLoader.LoadCertificate(sslStream.RemoteCertificate.Export(X509ContentType.Cert)).GetNameInfo(X509NameType.SimpleName, false));
119119
Assert.Equal(cert, sslStream.RemoteCertificate);
120120
}
121121
else
@@ -233,7 +233,7 @@ await TestHelper.WhenAllCompletedOrAnyFailed(
233233

234234
_output.WriteLine(
235235
"Client cert: {0}",
236-
new X509Certificate2(sslStream.RemoteCertificate.Export(X509ContentType.Cert)).GetNameInfo(X509NameType.SimpleName, false));
236+
X509CertificateLoader.LoadCertificate(sslStream.RemoteCertificate.Export(X509ContentType.Cert)).GetNameInfo(X509NameType.SimpleName, false));
237237

238238
Assert.Equal(clientCertificate.GetCertHashString(), sslStream.RemoteCertificate.GetCertHashString());
239239

src/libraries/Common/tests/System/Net/Http/TestHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public static X509Certificate2 CreateServerSelfSignedCertificate(string name = "
142142
X509Certificate2 cert = req.CreateSelfSigned(start, end);
143143
if (PlatformDetection.IsWindows)
144144
{
145-
cert = new X509Certificate2(cert.Export(X509ContentType.Pfx), (string?)null);
145+
cert = X509CertificateLoader.LoadPkcs12(cert.Export(X509ContentType.Pfx), (string?)null);
146146
}
147147

148148
return cert;

src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/CertificateAuthority.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void Dispose()
126126

127127
internal X509Certificate2 CloneIssuerCert()
128128
{
129-
return new X509Certificate2(_cert.RawData);
129+
return X509CertificateLoader.LoadCertificate(_cert.RawData);
130130
}
131131

132132
internal void Revoke(X509Certificate2 certificate, DateTimeOffset revocationTime)

src/libraries/Common/tests/System/Security/Cryptography/X509Certificates/RevocationResponder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ private static byte[] GetCertDataForAiaResponseKind(AiaResponseKind kind, Certif
340340
}
341341
else if (kind == AiaResponseKind.Pkcs12)
342342
{
343-
using X509Certificate2 cert = new X509Certificate2(authority.GetCertData());
343+
using X509Certificate2 cert = X509CertificateLoader.LoadCertificate(authority.GetCertData());
344344
return cert.Export(X509ContentType.Pkcs12);
345345
}
346346
else

src/libraries/System.Net.Http.Json/tests/FunctionalTests/System.Net.Http.Json.Functional.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<PropertyGroup>
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
55
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
6-
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
76
</PropertyGroup>
87

98
<ItemGroup>
@@ -33,6 +32,7 @@
3332
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
3433
<Reference Include="System.Net.Http" />
3534
<ProjectReference Include="..\..\src\System.Net.Http.Json.csproj" />
35+
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.Cryptography\src\Microsoft.Bcl.Cryptography.csproj" />
3636
</ItemGroup>
3737

3838
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">

src/libraries/System.Net.Http.WinHttpHandler/tests/FunctionalTests/System.Net.Http.WinHttpHandler.Functional.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
55
<DefineConstants>$(DefineConstants);WINHTTPHANDLER_TEST</DefineConstants>
66
<EnablePreviewFeatures>true</EnablePreviewFeatures>
7-
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
87
</PropertyGroup>
98
<ItemGroup>
109
<Compile Include="$(CommonTestPath)System\Net\Configuration.cs"
@@ -155,5 +154,6 @@
155154
<Reference Include="System.DirectoryServices.Protocols" />
156155
<Reference Include="System.Net.Http" />
157156
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.Http.Json\src\System.Net.Http.Json.csproj" />
157+
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.Cryptography\src\Microsoft.Bcl.Cryptography.csproj" />
158158
</ItemGroup>
159159
</Project>

src/libraries/System.Net.Http.WinHttpHandler/tests/UnitTests/ClientCertificateHelper.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace System.Net.Http.WinHttpHandlerUnitTests
1111
public class ClientCertificateHelper
1212
{
1313
private readonly X509Certificate2 _cert_KeyUsageIncludesDigitalSignature_EKUIncludesClientAuth_PrivateKey =
14-
new X509Certificate2(
14+
X509CertificateLoader.LoadPkcs12(
1515
Convert.FromBase64String(
1616
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Suppression approved. Unit test dummy certificate.")]
1717
@"MIIKTgIBAzCCCgoGCSqGSIb3DQEHAaCCCfsEggn3MIIJ8zCCBgwGCSqGSIb3DQEHAaCCBf0EggX5
@@ -64,7 +64,7 @@ public class ClientCertificateHelper
6464
"password");
6565

6666
private readonly X509Certificate2 _cert_KeyUsageMissingDigitalSignature_EKUIncludesClientAuth_PrivateKey =
67-
new X509Certificate2(
67+
X509CertificateLoader.LoadPkcs12(
6868
Convert.FromBase64String(
6969
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Suppression approved. Unit test dummy certificate.")]
7070
@"MIIKTgIBAzCCCgoGCSqGSIb3DQEHAaCCCfsEggn3MIIJ8zCCBgwGCSqGSIb3DQEHAaCCBf0EggX5
@@ -117,7 +117,7 @@ public class ClientCertificateHelper
117117
"password");
118118

119119
private readonly X509Certificate2 _cert_KeyUsageIncludesDigitalSignature_EKUMissingClientAuth_PrivateKey =
120-
new X509Certificate2(
120+
X509CertificateLoader.LoadPkcs12(
121121
Convert.FromBase64String(
122122
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Suppression approved. Dummy certificate for testing.")]
123123
@"MIIKRgIBAzCCCgIGCSqGSIb3DQEHAaCCCfMEggnvMIIJ6zCCBgQGCSqGSIb3DQEHAaCCBfUEggXx
@@ -170,7 +170,7 @@ public class ClientCertificateHelper
170170
"password");
171171

172172
private readonly X509Certificate2 _cert_KeyUsageIncludesDigitalSignature_NoEKU_PrivateKey =
173-
new X509Certificate2(
173+
X509CertificateLoader.LoadPkcs12(
174174
Convert.FromBase64String(
175175
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Suppression approved. Dummy certificate for testing.")]
176176
@"MIIKPgIBAzCCCfoGCSqGSIb3DQEHAaCCCesEggnnMIIJ4zCCBgwGCSqGSIb3DQEHAaCCBf0EggX5
@@ -223,7 +223,7 @@ public class ClientCertificateHelper
223223
"password");
224224

225225
private readonly X509Certificate2 _cert_KeyUsageIncludesDigitalSignature_EKUIncludesClientAuth_NoPrivateKey =
226-
new X509Certificate2(
226+
X509CertificateLoader.LoadCertificate(
227227
Convert.FromBase64String(
228228
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Suppression approved. Dummy certificate for testing.")]
229229
@"MIIDFjCCAf6gAwIBAgIQTm8+EF94L4FJ0nBFl5LICzANBgkqhkiG9w0BAQsFADAb

src/libraries/System.Net.Http.WinHttpHandler/tests/UnitTests/System.Net.Http.WinHttpHandler.Unit.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<NoWarn>$(NoWarn);0436;SYSLIB0057</NoWarn>
3+
<NoWarn>$(NoWarn);0436</NoWarn>
44
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
55
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
66
<TargetFramework>$(NetCoreAppCurrent)-windows</TargetFramework>

src/libraries/System.Net.Http/tests/FunctionalTests/System.Net.Http.Functional.Tests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-android;$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-wasi;$(NetCoreAppCurrent)-osx</TargetFrameworks>
1010
<EnablePreviewFeatures>true</EnablePreviewFeatures>
1111
<EventSourceSupport Condition="'$(TestNativeAot)' == 'true'">true</EventSourceSupport>
12-
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
1312
</PropertyGroup>
1413

1514
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->

src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/System.Net.NetworkInformation.Functional.Tests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
77
<DefineConstants>$(DefineConstants);NETWORKINFORMATION_TEST</DefineConstants>
88
<EventSourceSupport Condition="'$(TestNativeAot)' == 'true'">true</EventSourceSupport>
9-
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
109
</PropertyGroup>
1110
<ItemGroup>
1211
<Compile Include="AssemblyInfo.cs" />

src/libraries/System.Net.Quic/tests/FunctionalTests/System.Net.Quic.Functional.Tests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-osx</TargetFrameworks>
66
<EnablePreviewFeatures>true</EnablePreviewFeatures>
77
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
8-
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
98
</PropertyGroup>
109
<ItemGroup>
1110
<RdXmlFile Include="default.rd.xml" />

src/libraries/System.Net.Requests/tests/System.Net.Requests.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
77
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
88
<!-- SYSLIB0014: WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead. -->
9-
<NoWarn>$(NoWarn);SYSLIB0014;SYSLIB0057</NoWarn>
9+
<NoWarn>$(NoWarn);SYSLIB0014</NoWarn>
1010
<EnablePreviewFeatures>true</EnablePreviewFeatures>
1111
<EventSourceSupport Condition="'$(TestNativeAot)' == 'true'">true</EventSourceSupport>
1212
</PropertyGroup>

src/libraries/System.Net.Security/tests/FunctionalTests/CertificateValidationRemoteServer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private async Task ConnectWithRevocation_WithCallback_Core(
242242

243243
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
244244
{
245-
X509Certificate2 temp = new X509Certificate2(serverCert.Export(X509ContentType.Pkcs12));
245+
X509Certificate2 temp = X509CertificateLoader.LoadPkcs12(serverCert.Export(X509ContentType.Pkcs12), (string?)null);
246246
serverCert.Dispose();
247247
serverCert = temp;
248248
}

src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamStreamToStreamTest.cs

+2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ public static IEnumerable<object[]> SslStream_StreamToStream_Authentication_Succ
6565
using (X509Certificate2 clientCert = Configuration.Certificates.GetClientCertificate())
6666
{
6767
yield return new object[] { new X509Certificate2(serverCert), new X509Certificate2(clientCert) };
68+
#pragma warning disable SYSLIB0057 // Test case is explicitly testing X509Certificate instances.
6869
yield return new object[] { new X509Certificate(serverCert.Export(X509ContentType.Pfx), (string)null), new X509Certificate(clientCert.Export(X509ContentType.Pfx), (string)null) };
70+
#pragma warning restore SYSLIB0057
6971
}
7072
}
7173

src/libraries/System.Net.Security/tests/FunctionalTests/System.Net.Security.Tests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
77
<EnablePreviewFeatures>true</EnablePreviewFeatures>
88
<EventSourceSupport Condition="'$(TestNativeAot)' == 'true'">true</EventSourceSupport>
9-
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
109
</PropertyGroup>
1110
<ItemGroup>
1211
<Compile Include="AssemblyInfo.cs" />

src/libraries/System.Net.Security/tests/UnitTests/SslAuthenticationOptionsTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ public void ServerCertificate_Get_Set_Succeeds()
109109
_serverOptions.ServerCertificate = null;
110110

111111
Assert.Null(_serverOptions.ServerCertificate);
112+
#pragma warning disable SYSLIB0057
112113
X509Certificate cert = new X509Certificate2(stackalloc byte[0]);
114+
#pragma warning restore SYSLIB0057
113115
_serverOptions.ServerCertificate = cert;
114116

115117
Assert.Equal(cert, _serverOptions.ServerCertificate);

src/libraries/System.Net.Security/tests/UnitTests/System.Net.Security.Unit.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
-->
1010
<NoWarn>$(NoWarn);436</NoWarn>
1111
<!-- Disable: CLSCompliant attribute is not needed -->
12-
<NoWarn>$(NoWarn);3021;SYSLIB0057</NoWarn>
12+
<NoWarn>$(NoWarn);3021</NoWarn>
1313
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-android</TargetFrameworks>
1414
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
1515
</PropertyGroup>

src/libraries/System.Net.WebClient/tests/System.Net.WebClient.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
44
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
55
<!-- SYSLIB0014: WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead. -->
6-
<NoWarn>$(NoWarn);SYSLIB0014;SYSLIB0057</NoWarn>
6+
<NoWarn>$(NoWarn);SYSLIB0014</NoWarn>
77
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
88
</PropertyGroup>
99
<ItemGroup>

src/libraries/System.Net.WebSockets.Client/tests/System.Net.WebSockets.Client.Tests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<StringResourcesPath>../src/Resources/Strings.resx</StringResourcesPath>
66
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppCurrent)-browser</TargetFrameworks>
77
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
8-
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
98
</PropertyGroup>
109

1110
<PropertyGroup Condition="'$(TargetOS)' == 'browser'">

src/libraries/System.Net.WebSockets.Client/tests/wasm/System.Net.WebSockets.Client.Wasm.Tests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
66
<TargetFramework>$(NetCoreAppCurrent)-browser</TargetFramework>
77
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
8-
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
98
</PropertyGroup>
109

1110
<PropertyGroup Condition="'$(TargetOS)' == 'browser'">

0 commit comments

Comments
 (0)