Files
dotnet-runtime/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ServerCertificates.Unix.cs
T
Tom Deseyn 6c1f181e7c CurlHandler: set ssl options to match X509 machine store certificates (dotnet/corefx#28312)
* CurlHandler: set ssl options to match X509 machine store certificates

* Rework HttpClientUsesSslCertEnvironmentVariables test

* PR feedback


Commit migrated from https://github.com/dotnet/corefx/commit/1b588ca420d881d8c20db69022b1c64bc5223588
2018-03-22 22:51:51 -04:00

101 lines
3.7 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Security;
using System.Net.Test.Common;
using System.Runtime.InteropServices;
using System.Security.Authentication.ExtendedProtection;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Xunit;
namespace System.Net.Http.Functional.Tests
{
public abstract partial class HttpClientHandler_ServerCertificates_Test
{
private static bool ShouldSuppressRevocationException
{
get
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return false;
}
// If a run on a clean macOS ever fails we need to consider that "false"
// for CheckCertificateRevocationList is actually "use a system default" now,
// and may require changing how this option is exposed. Considering the variety of
// systems this should probably be complex like
// enum RevocationCheckingOption {
// // Use it if able
// BestPlatformSecurity = 0,
// // Don't use it, if that's an option.
// BestPlatformPerformance,
// // Required
// MustCheck,
// // Prohibited
// MustNotCheck,
// }
if (Interop.Http.GetSslVersionDescription() == "SecureTransport")
{
return true;
}
return false;
}
}
internal bool BackendSupportsCustomCertificateHandling
{
get
{
if (UseSocketsHttpHandler)
{
return true;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return false;
}
// For other Unix-based systems it's true if (and only if) the openssl backend
// is used with libcurl.
return (Interop.Http.GetSslVersionDescription()?.StartsWith(Interop.Http.OpenSsl10Description, StringComparison.OrdinalIgnoreCase) ?? false);
}
}
[Fact]
[PlatformSpecific(~TestPlatforms.OSX)] // Not implemented
public void HttpClientUsesSslCertEnvironmentVariables()
{
// We set SSL_CERT_DIR and SSL_CERT_FILE to empty locations.
// The HttpClient should fail to validate the server certificate.
var psi = new ProcessStartInfo();
string sslCertDir = GetTestFilePath();
Directory.CreateDirectory(sslCertDir);
psi.Environment.Add("SSL_CERT_DIR", sslCertDir);
string sslCertFile = GetTestFilePath();
File.WriteAllText(sslCertFile, "");
psi.Environment.Add("SSL_CERT_FILE", sslCertFile);
RemoteInvoke(async useSocketsHttpHandlerString =>
{
const string Url = "https://www.microsoft.com";
using (var client = CreateHttpClient(useSocketsHttpHandlerString))
{
await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(Url));
}
return SuccessExitCode;
}, UseSocketsHttpHandler.ToString(), new RemoteInvokeOptions { StartInfo = psi }).Dispose();
}
}
}