Skip to content

Commit

Permalink
Update System.Net.* unit tests to react to SYSLIB0057 obsoletion (dot…
Browse files Browse the repository at this point in the history
…net#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 <tweinfurt@yahoo.com>
Co-authored-by: Radek Zikmund <r.zikmund.rz@gmail.com>
Co-authored-by: Jeremy Barton <jbarton@microsoft.com>
Co-authored-by: Radek Zikmund <32671551+rzikm@users.noreply.github.com>
  • Loading branch information
5 people authored and jtschuster committed Sep 17, 2024
1 parent 9b8fec6 commit e5a8609
Show file tree
Hide file tree
Showing 22 changed files with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static (X509Certificate2 certificate, X509Certificate2Collection) Generat
if (!ephemeralKey && PlatformDetection.IsWindows)
{
X509Certificate2 ephemeral = endEntity;
endEntity = new X509Certificate2(endEntity.Export(X509ContentType.Pfx), (string?)null, X509KeyStorageFlags.Exportable);
endEntity = X509CertificateLoader.LoadPkcs12(endEntity.Export(X509ContentType.Pfx), (string?)null, X509KeyStorageFlags.Exportable);
ephemeral.Dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ static Certificates()
Assert.True(mutex?.WaitOne(MutexTimeoutMs) ?? true, "Could not acquire the global certificate mutex.");
try
{
s_serverCertificate = new X509Certificate2(serverCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
s_clientCertificate = new X509Certificate2(clientCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
s_noEKUCertificate = new X509Certificate2(noEKUCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
s_selfSignedServerCertificate = new X509Certificate2(selfSignedServerCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
s_selfSignedClientCertificate = new X509Certificate2(selfSignedClientCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
s_serverCertificate = X509CertificateLoader.LoadPkcs12(serverCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
s_clientCertificate = X509CertificateLoader.LoadPkcs12(clientCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
s_noEKUCertificate = X509CertificateLoader.LoadPkcs12(noEKUCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
s_selfSignedServerCertificate = X509CertificateLoader.LoadPkcs12(selfSignedServerCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
s_selfSignedClientCertificate = X509CertificateLoader.LoadPkcs12(selfSignedClientCertificateBytes, CertificatePassword, X509KeyStorageFlags.Exportable);
}
finally { mutex?.ReleaseMutex(); }
}
Expand Down Expand Up @@ -91,7 +91,7 @@ public static X509Certificate2 GetSelfSigned13ServerCertificate()
{
using (innerCert)
{
cert = new X509Certificate2(innerCert.Export(X509ContentType.Pfx));
cert = X509CertificateLoader.LoadPkcs12(innerCert.Export(X509ContentType.Pfx), (string?)null);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ await TestHelper.WhenAllCompletedOrAnyFailed(
{
_output.WriteLine(
"Client cert: {0}",
new X509Certificate2(sslStream.RemoteCertificate.Export(X509ContentType.Cert)).GetNameInfo(X509NameType.SimpleName, false));
X509CertificateLoader.LoadCertificate(sslStream.RemoteCertificate.Export(X509ContentType.Cert)).GetNameInfo(X509NameType.SimpleName, false));
Assert.Equal(cert, sslStream.RemoteCertificate);
}
else
Expand Down Expand Up @@ -233,7 +233,7 @@ await TestHelper.WhenAllCompletedOrAnyFailed(
_output.WriteLine(
"Client cert: {0}",
new X509Certificate2(sslStream.RemoteCertificate.Export(X509ContentType.Cert)).GetNameInfo(X509NameType.SimpleName, false));
X509CertificateLoader.LoadCertificate(sslStream.RemoteCertificate.Export(X509ContentType.Cert)).GetNameInfo(X509NameType.SimpleName, false));
Assert.Equal(clientCertificate.GetCertHashString(), sslStream.RemoteCertificate.GetCertHashString());
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Common/tests/System/Net/Http/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static X509Certificate2 CreateServerSelfSignedCertificate(string name = "
X509Certificate2 cert = req.CreateSelfSigned(start, end);
if (PlatformDetection.IsWindows)
{
cert = new X509Certificate2(cert.Export(X509ContentType.Pfx), (string?)null);
cert = X509CertificateLoader.LoadPkcs12(cert.Export(X509ContentType.Pfx), (string?)null);
}

return cert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void Dispose()

internal X509Certificate2 CloneIssuerCert()
{
return new X509Certificate2(_cert.RawData);
return X509CertificateLoader.LoadCertificate(_cert.RawData);
}

internal void Revoke(X509Certificate2 certificate, DateTimeOffset revocationTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ private static byte[] GetCertDataForAiaResponseKind(AiaResponseKind kind, Certif
}
else if (kind == AiaResponseKind.Pkcs12)
{
using X509Certificate2 cert = new X509Certificate2(authority.GetCertData());
using X509Certificate2 cert = X509CertificateLoader.LoadCertificate(authority.GetCertData());
return cert.Export(X509ContentType.Pkcs12);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -33,6 +32,7 @@
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<Reference Include="System.Net.Http" />
<ProjectReference Include="..\..\src\System.Net.Http.Json.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.Cryptography\src\Microsoft.Bcl.Cryptography.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<DefineConstants>$(DefineConstants);WINHTTPHANDLER_TEST</DefineConstants>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonTestPath)System\Net\Configuration.cs"
Expand Down Expand Up @@ -155,5 +154,6 @@
<Reference Include="System.DirectoryServices.Protocols" />
<Reference Include="System.Net.Http" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.Http.Json\src\System.Net.Http.Json.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.Cryptography\src\Microsoft.Bcl.Cryptography.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.Net.Http.WinHttpHandlerUnitTests
public class ClientCertificateHelper
{
private readonly X509Certificate2 _cert_KeyUsageIncludesDigitalSignature_EKUIncludesClientAuth_PrivateKey =
new X509Certificate2(
X509CertificateLoader.LoadPkcs12(
Convert.FromBase64String(
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Suppression approved. Unit test dummy certificate.")]
@"MIIKTgIBAzCCCgoGCSqGSIb3DQEHAaCCCfsEggn3MIIJ8zCCBgwGCSqGSIb3DQEHAaCCBf0EggX5
Expand Down Expand Up @@ -64,7 +64,7 @@ public class ClientCertificateHelper
"password");

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

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

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

private readonly X509Certificate2 _cert_KeyUsageIncludesDigitalSignature_EKUIncludesClientAuth_NoPrivateKey =
new X509Certificate2(
X509CertificateLoader.LoadCertificate(
Convert.FromBase64String(
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Suppression approved. Dummy certificate for testing.")]
@"MIIDFjCCAf6gAwIBAgIQTm8+EF94L4FJ0nBFl5LICzANBgkqhkiG9w0BAQsFADAb
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);0436;SYSLIB0057</NoWarn>
<NoWarn>$(NoWarn);0436</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
<TargetFramework>$(NetCoreAppCurrent)-windows</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-android;$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-wasi;$(NetCoreAppCurrent)-osx</TargetFrameworks>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<EventSourceSupport Condition="'$(TestNativeAot)' == 'true'">true</EventSourceSupport>
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
</PropertyGroup>

<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
<DefineConstants>$(DefineConstants);NETWORKINFORMATION_TEST</DefineConstants>
<EventSourceSupport Condition="'$(TestNativeAot)' == 'true'">true</EventSourceSupport>
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-osx</TargetFrameworks>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
</PropertyGroup>
<ItemGroup>
<RdXmlFile Include="default.rd.xml" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
<!-- SYSLIB0014: WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead. -->
<NoWarn>$(NoWarn);SYSLIB0014;SYSLIB0057</NoWarn>
<NoWarn>$(NoWarn);SYSLIB0014</NoWarn>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<EventSourceSupport Condition="'$(TestNativeAot)' == 'true'">true</EventSourceSupport>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private async Task ConnectWithRevocation_WithCallback_Core(

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
X509Certificate2 temp = new X509Certificate2(serverCert.Export(X509ContentType.Pkcs12));
X509Certificate2 temp = X509CertificateLoader.LoadPkcs12(serverCert.Export(X509ContentType.Pkcs12), (string?)null);
serverCert.Dispose();
serverCert = temp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public static IEnumerable<object[]> SslStream_StreamToStream_Authentication_Succ
using (X509Certificate2 clientCert = Configuration.Certificates.GetClientCertificate())
{
yield return new object[] { new X509Certificate2(serverCert), new X509Certificate2(clientCert) };
#pragma warning disable SYSLIB0057 // Test case is explicitly testing X509Certificate instances.
yield return new object[] { new X509Certificate(serverCert.Export(X509ContentType.Pfx), (string)null), new X509Certificate(clientCert.Export(X509ContentType.Pfx), (string)null) };
#pragma warning restore SYSLIB0057
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<EventSourceSupport Condition="'$(TestNativeAot)' == 'true'">true</EventSourceSupport>
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public void ServerCertificate_Get_Set_Succeeds()
_serverOptions.ServerCertificate = null;

Assert.Null(_serverOptions.ServerCertificate);
#pragma warning disable SYSLIB0057
X509Certificate cert = new X509Certificate2(stackalloc byte[0]);
#pragma warning restore SYSLIB0057
_serverOptions.ServerCertificate = cert;

Assert.Equal(cert, _serverOptions.ServerCertificate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-->
<NoWarn>$(NoWarn);436</NoWarn>
<!-- Disable: CLSCompliant attribute is not needed -->
<NoWarn>$(NoWarn);3021;SYSLIB0057</NoWarn>
<NoWarn>$(NoWarn);3021</NoWarn>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-android</TargetFrameworks>
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
<!-- SYSLIB0014: WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead. -->
<NoWarn>$(NoWarn);SYSLIB0014;SYSLIB0057</NoWarn>
<NoWarn>$(NoWarn);SYSLIB0014</NoWarn>
<IgnoreForCI Condition="'$(TargetOS)' == 'browser'">true</IgnoreForCI>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<StringResourcesPath>../src/Resources/Strings.resx</StringResourcesPath>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppCurrent)-browser</TargetFrameworks>
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
<TargetFramework>$(NetCoreAppCurrent)-browser</TargetFramework>
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
<NoWarn>$(NoWarn);SYSLIB0057</NoWarn>
</PropertyGroup>

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

0 comments on commit e5a8609

Please sign in to comment.