Skip to content

Commit

Permalink
Add acceptance test for failed SSL handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Dec 23, 2019
1 parent 9d53c4c commit 48e59e5
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions Testing/GenHTTP.Testing.Acceptance/Core/SecurityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
using GenHTTP.Testing.Acceptance.Domain;
using GenHTTP.Modules.Core;
using GenHTTP.Api.Infrastructure;

using System.Security.Authentication;

namespace GenHTTP.Testing.Acceptance.Core
{

Expand Down Expand Up @@ -168,6 +169,25 @@ public void TestSecurityError()
});
}

/// <summary>
/// As a web developer, I can decide not to return a certificate which will
/// abort the server SSL handshake.
/// </summary>
[Fact]
public void TestNoCertificate()
{
RunSecure((insec, sec) =>
{
Assert.Throws<WebException>(() =>
{
var failedRequest = WebRequest.CreateHttp($"https://localhost:{sec}");
failedRequest.IgnoreSecurityErrors();

failedRequest.GetSafeResponse();
});
}, host: "myserver");
}

private static void RunSecure(Action<ushort, ushort> logic, Action<IServerBuilder>? adjustments = null, SecureUpgrade? mode = null, string host = "localhost")
{
var content = Layout.Create().Add("index", Content.From("Hello Alice!"), true);
Expand All @@ -176,13 +196,13 @@ private static void RunSecure(Action<ushort, ushort> logic, Action<IServerBuilde

var port = TestRunner.NextPort();

using var cert = GetCertificate();

using var cert = GetCertificate();

var builder = runner.Builder
.Router(content)
.Bind(IPAddress.Any, runner.Port)
.Bind(IPAddress.Any, port, host, cert);

.Bind(IPAddress.Any, port, new PickyCertificateProvider(host, cert), SslProtocols.Tls12);
if (mode != null)
{
builder.SecureUpgrade(mode.Value);
Expand All @@ -207,6 +227,26 @@ private static X509Certificate2 GetCertificate()
}
}

private class PickyCertificateProvider : ICertificateProvider
{

private string Host { get; }

private X509Certificate2 Certificate { get; }

public PickyCertificateProvider(string host, X509Certificate2 certificate)
{
Host = host;
Certificate = certificate;
}

public X509Certificate2? Provide(string? host)
{
return (host == Host) ? Certificate : null;
}

}

}

}
Expand Down

0 comments on commit 48e59e5

Please sign in to comment.