|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http; |
| 7 | +using Microsoft.AspNetCore.Builder; |
| 8 | +using Microsoft.AspNetCore.Connections; |
| 9 | +using Microsoft.AspNetCore.Hosting; |
| 10 | +using Microsoft.AspNetCore.Http; |
| 11 | +using Microsoft.AspNetCore.Server.Kestrel.Core; |
| 12 | +using Microsoft.AspNetCore.Server.Kestrel.FunctionalTests; |
| 13 | +using Microsoft.AspNetCore.Testing; |
| 14 | +using Microsoft.Extensions.DependencyInjection; |
| 15 | +using Microsoft.Extensions.Hosting; |
| 16 | +using Microsoft.Extensions.Logging; |
| 17 | +using Xunit; |
| 18 | + |
| 19 | +namespace Interop.FunctionalTests.Http3 |
| 20 | +{ |
| 21 | + [QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/35070")] |
| 22 | + public class Http3TlsTests : LoggedTest |
| 23 | + { |
| 24 | + [ConditionalFact] |
| 25 | + [MsQuicSupported] |
| 26 | + public async Task ServerCertificateSelector_Invoked() |
| 27 | + { |
| 28 | + var builder = CreateHostBuilder(async context => |
| 29 | + { |
| 30 | + await context.Response.WriteAsync("Hello World"); |
| 31 | + }, configureKestrel: kestrelOptions => |
| 32 | + { |
| 33 | + kestrelOptions.ListenAnyIP(0, listenOptions => |
| 34 | + { |
| 35 | + listenOptions.Protocols = HttpProtocols.Http3; |
| 36 | + listenOptions.UseHttps(httpsOptions => |
| 37 | + { |
| 38 | + httpsOptions.ServerCertificateSelector = (context, host) => |
| 39 | + { |
| 40 | + Assert.Null(context); // The context isn't available durring the quic handshake. |
| 41 | + Assert.Equal("localhost", host); |
| 42 | + return TestResources.GetTestCertificate(); |
| 43 | + }; |
| 44 | + }); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + using var host = builder.Build(); |
| 49 | + using var client = CreateClient(); |
| 50 | + |
| 51 | + await host.StartAsync().DefaultTimeout(); |
| 52 | + |
| 53 | + // Using localhost instead of 127.0.0.1 because IPs don't set SNI and the Host header isn't currently used as an override. |
| 54 | + var request = new HttpRequestMessage(HttpMethod.Get, $"https://localhost:{host.GetPort()}/"); |
| 55 | + request.Version = HttpVersion.Version30; |
| 56 | + request.VersionPolicy = HttpVersionPolicy.RequestVersionExact; |
| 57 | + // https://github.com/dotnet/runtime/issues/57169 Host isn't used for SNI |
| 58 | + request.Headers.Host = "testhost"; |
| 59 | + |
| 60 | + var response = await client.SendAsync(request, CancellationToken.None); |
| 61 | + response.EnsureSuccessStatusCode(); |
| 62 | + var result = await response.Content.ReadAsStringAsync(); |
| 63 | + Assert.Equal(HttpVersion.Version30, response.Version); |
| 64 | + Assert.Equal("Hello World", result); |
| 65 | + |
| 66 | + await host.StopAsync().DefaultTimeout(); |
| 67 | + } |
| 68 | + |
| 69 | + private static HttpMessageInvoker CreateClient(TimeSpan? idleTimeout = null) |
| 70 | + { |
| 71 | + var handler = new SocketsHttpHandler(); |
| 72 | + handler.SslOptions = new System.Net.Security.SslClientAuthenticationOptions |
| 73 | + { |
| 74 | + RemoteCertificateValidationCallback = (_, __, ___, ____) => true, |
| 75 | + TargetHost = "targethost" |
| 76 | + }; |
| 77 | + if (idleTimeout != null) |
| 78 | + { |
| 79 | + handler.PooledConnectionIdleTimeout = idleTimeout.Value; |
| 80 | + } |
| 81 | + |
| 82 | + return new HttpMessageInvoker(handler); |
| 83 | + } |
| 84 | + |
| 85 | + private IHostBuilder CreateHostBuilder(RequestDelegate requestDelegate, HttpProtocols? protocol = null, Action<KestrelServerOptions> configureKestrel = null) |
| 86 | + { |
| 87 | + return new HostBuilder() |
| 88 | + .ConfigureWebHost(webHostBuilder => |
| 89 | + { |
| 90 | + webHostBuilder |
| 91 | + .UseKestrel(o => |
| 92 | + { |
| 93 | + if (configureKestrel == null) |
| 94 | + { |
| 95 | + o.Listen(IPAddress.Parse("127.0.0.1"), 0, listenOptions => |
| 96 | + { |
| 97 | + listenOptions.Protocols = protocol ?? HttpProtocols.Http3; |
| 98 | + listenOptions.UseHttps(); |
| 99 | + }); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + configureKestrel(o); |
| 104 | + } |
| 105 | + }) |
| 106 | + .Configure(app => |
| 107 | + { |
| 108 | + app.Run(requestDelegate); |
| 109 | + }); |
| 110 | + }) |
| 111 | + .ConfigureServices(AddTestLogging) |
| 112 | + .ConfigureHostOptions(o => |
| 113 | + { |
| 114 | + if (Debugger.IsAttached) |
| 115 | + { |
| 116 | + // Avoid timeout while debugging. |
| 117 | + o.ShutdownTimeout = TimeSpan.FromHours(1); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + o.ShutdownTimeout = TimeSpan.FromSeconds(1); |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments