|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Net.Sockets; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.AspNetCore.Builder; |
| 9 | +using Microsoft.AspNetCore.Hosting; |
| 10 | +using Microsoft.AspNetCore.Http; |
| 11 | +using Microsoft.AspNetCore.Testing; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests |
| 15 | +{ |
| 16 | + public class HandleInheritanceTest : TestApplicationErrorLoggerLoggedTest |
| 17 | + { |
| 18 | + [Fact] |
| 19 | + public async Task SpawnChildProcess_DoesNotInheritListenHandle() |
| 20 | + { |
| 21 | + var hostBuilder = TransportSelector.GetWebHostBuilder() |
| 22 | + .UseKestrel() |
| 23 | + .ConfigureServices(AddTestLogging) |
| 24 | + .UseUrls("http://127.0.0.1:0") |
| 25 | + .Configure(app => |
| 26 | + { |
| 27 | + app.Run(context => |
| 28 | + { |
| 29 | + return context.Response.WriteAsync("Hello World"); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + using (var host = hostBuilder.Build()) |
| 34 | + { |
| 35 | + await host.StartAsync(); |
| 36 | + |
| 37 | + var processInfo = new ProcessStartInfo |
| 38 | + { |
| 39 | + FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "bash", |
| 40 | + CreateNoWindow = true, |
| 41 | + }; |
| 42 | + using (var process = Process.Start(processInfo)) |
| 43 | + { |
| 44 | + var port = host.GetPort(); |
| 45 | + await host.StopAsync(); |
| 46 | + |
| 47 | + using (var client = new TcpClient()) |
| 48 | + { |
| 49 | + await Assert.ThrowsAnyAsync<SocketException>(() => client.ConnectAsync("127.0.0.1", port)); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task SpawnChildProcess_DoesNotInheritConnectionHandle() |
| 57 | + { |
| 58 | + var hostBuilder = TransportSelector.GetWebHostBuilder() |
| 59 | + .UseKestrel() |
| 60 | + .ConfigureServices(AddTestLogging) |
| 61 | + .UseUrls("http://127.0.0.1:0") |
| 62 | + .Configure(app => |
| 63 | + { |
| 64 | + app.Run(context => |
| 65 | + { |
| 66 | + return context.Response.WriteAsync("Hello World"); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + using (var host = hostBuilder.Build()) |
| 71 | + { |
| 72 | + await host.StartAsync(); |
| 73 | + |
| 74 | + var processInfo = new ProcessStartInfo |
| 75 | + { |
| 76 | + FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "bash", |
| 77 | + CreateNoWindow = true, |
| 78 | + }; |
| 79 | + using (var process = Process.Start(processInfo)) |
| 80 | + { |
| 81 | + using (var client = new TcpClient()) |
| 82 | + { |
| 83 | + await client.ConnectAsync("127.0.0.1", host.GetPort()); |
| 84 | + |
| 85 | + await host.StopAsync(); |
| 86 | + |
| 87 | + var read = await client.GetStream().ReadAsync(new byte[100], 0, 100).DefaultTimeout(); |
| 88 | + Assert.Equal(0, read); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments