Files
dotnet-runtime/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ClientCertificates.cs
T
David Shulman 935520c1dc Disable most networking tests for default CI runs
Lately, the CI system and test server infrastructure has been overloaded with many PRs. This is causing many networking tests to randomly fail due to resource exhaustion caused by so many simultaneous PR runs on the same CI machines. This occurrs not only when using an external test server endpoint (corefx-net.cloudapp.net) but also loopback servers.

To improve engineering efficiency for the team, we are moving most of the tests in the networking libraries from the default CI runs (innerloop). For developers working on networking libraries, the networking tests can be run by invoking the "outerloop" tests.

We will not be allowing any merges of PRs in the networking libraries unless the outerloop tests are also run by the developer.

This affects tests in the following libraries right now:

* System.Net.Http
* System.Net.Http.WinHttpHandler
* System.Net.Sockets
* System.Net.Security
* System.Net.WebSockets.Client

I will be working on fine tuning these exclusions (issue dotnet/corefx#11345) to try to bring back tests into the innerloop runs when we have begun to address the infrastructure issues.


Commit migrated from https://github.com/dotnet/corefx/commit/defbb8afd11b317f6b5cc2f4422a03d701b6dba3
2016-09-01 11:23:40 -07:00

145 lines
6.2 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.Net.Security;
using System.Net.Sockets;
using System.Net.Test.Common;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Xunit;
namespace System.Net.Http.Functional.Tests
{
public class HttpClientHandler_ClientCertificates_Test
{
[Fact]
public void ClientCertificateOptions_Default()
{
using (var handler = new HttpClientHandler())
{
Assert.Equal(ClientCertificateOption.Manual, handler.ClientCertificateOptions);
}
}
[Theory]
[InlineData((ClientCertificateOption)2)]
[InlineData((ClientCertificateOption)(-1))]
public void ClientCertificateOptions_InvalidArg_ThrowsException(ClientCertificateOption option)
{
using (var handler = new HttpClientHandler())
{
Assert.Throws<ArgumentOutOfRangeException>("value", () => handler.ClientCertificateOptions = option);
}
}
[Theory]
[InlineData(ClientCertificateOption.Automatic)]
[InlineData(ClientCertificateOption.Manual)]
public void ClientCertificateOptions_ValueArg_Roundtrips(ClientCertificateOption option)
{
using (var handler = new HttpClientHandler())
{
handler.ClientCertificateOptions = option;
Assert.Equal(option, handler.ClientCertificateOptions);
}
}
[OuterLoop] // TODO: Issue #11345
[ConditionalFact(nameof(BackendDoesNotSupportCustomCertificateHandling))]
public async Task Automatic_SSLBackendNotSupported_ThrowsPlatformNotSupportedException()
{
using (var client = new HttpClient(new HttpClientHandler() { ClientCertificateOptions = ClientCertificateOption.Automatic }))
{
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => client.GetAsync(Configuration.Http.SecureRemoteEchoServer));
}
}
[OuterLoop] // TODO: Issue #11345
[ConditionalFact(nameof(BackendDoesNotSupportCustomCertificateHandling))]
public async Task Manual_SSLBackendNotSupported_ThrowsPlatformNotSupportedException()
{
var handler = new HttpClientHandler();
handler.ClientCertificates.Add(Configuration.Certificates.GetClientCertificate());
using (var client = new HttpClient(handler))
{
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => client.GetAsync(Configuration.Http.SecureRemoteEchoServer));
}
}
[OuterLoop] // TODO: Issue #11345
[ActiveIssue(9543, PlatformID.Windows)] // reuseClient==false fails in debug/release, reuseClient==true fails sporadically in release
[ConditionalTheory(nameof(BackendSupportsCustomCertificateHandling))]
[InlineData(6, false)]
[InlineData(3, true)]
public async Task Manual_CertificateSentMatchesCertificateReceived_Success(
int numberOfRequests,
bool reuseClient) // validate behavior with and without connection pooling, which impacts client cert usage
{
var options = new LoopbackServer.Options { UseSsl = true };
using (X509Certificate2 cert = Configuration.Certificates.GetClientCertificate())
{
Func<HttpClient> createClient = () =>
{
var handler = new HttpClientHandler() { ServerCertificateCustomValidationCallback = delegate { return true; } };
handler.ClientCertificates.Add(cert);
return new HttpClient(handler);
};
Func<HttpClient, Socket, Uri, Task> makeAndValidateRequest = async (client, server, url) =>
{
await TestHelper.WhenAllCompletedOrAnyFailed(
client.GetStringAsync(url),
LoopbackServer.AcceptSocketAsync(server, async (socket, stream, reader, writer) =>
{
SslStream sslStream = Assert.IsType<SslStream>(stream);
Assert.Equal(cert, sslStream.RemoteCertificate);
await LoopbackServer.ReadWriteAcceptedAsync(socket, reader, writer);
return null;
}, options));
};
await LoopbackServer.CreateServerAsync(async (server, url) =>
{
if (reuseClient)
{
using (HttpClient client = createClient())
{
for (int i = 0; i < numberOfRequests; i++)
{
await makeAndValidateRequest(client, server, url);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
else
{
for (int i = 0; i < numberOfRequests; i++)
{
using (HttpClient client = createClient())
{
await makeAndValidateRequest(client, server, url);
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}, options);
}
}
private static bool BackendSupportsCustomCertificateHandling =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
(CurlSslVersionDescription()?.StartsWith("OpenSSL") ?? false);
private static bool BackendDoesNotSupportCustomCertificateHandling => !BackendSupportsCustomCertificateHandling;
[DllImport("System.Net.Http.Native", EntryPoint = "HttpNative_GetSslVersionDescription")]
private static extern string CurlSslVersionDescription();
}
}