|
| 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 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +using Xunit; |
| 8 | +using Xunit.Abstractions; |
| 9 | + |
| 10 | +namespace System.Net.Http.Functional.Tests |
| 11 | +{ |
| 12 | + public class NtAuthServer : IDisposable |
| 13 | + { |
| 14 | + // When tests projects are run in parallel, overlapping port ranges can cause a race condition |
| 15 | + // when looking for free ports during dynamic port allocation. |
| 16 | + private const int PortMin = 5001; |
| 17 | + private const int PortMax = 8000; |
| 18 | + |
| 19 | + private HttpListener _listener; |
| 20 | + private Task _serverTask; |
| 21 | + |
| 22 | + public NtAuthServer(AuthenticationSchemes authSchemes) |
| 23 | + { |
| 24 | + // Create server. |
| 25 | + CreateServer(authSchemes); |
| 26 | + |
| 27 | + // Start listening for requests. |
| 28 | + _serverTask = Task.Run(async () => |
| 29 | + { |
| 30 | + try |
| 31 | + { |
| 32 | + while (true) |
| 33 | + { |
| 34 | + HttpListenerContext context = await _listener.GetContextAsync(); |
| 35 | + |
| 36 | + bool noAccess = (context.Request.RawUrl == NoAccessUrl); |
| 37 | + if (noAccess) |
| 38 | + { |
| 39 | + context.Response.AddHeader("Www-Authenticate", "Negotiate"); |
| 40 | + } |
| 41 | + |
| 42 | + context.Response.StatusCode = noAccess ? 401 : 200; |
| 43 | + context.Response.ContentLength64 = 0; |
| 44 | + context.Response.OutputStream.Close(); |
| 45 | + } |
| 46 | + } |
| 47 | + catch (HttpListenerException) |
| 48 | + { |
| 49 | + // Ignore. |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + public void Dispose() |
| 55 | + { |
| 56 | + _listener.Stop(); |
| 57 | + _serverTask.Wait(); |
| 58 | + } |
| 59 | + |
| 60 | + public string BaseUrl { get; private set; } |
| 61 | + public string NoAccessUrl => "/noaccess"; |
| 62 | + |
| 63 | + private void CreateServer(AuthenticationSchemes authSchemes) |
| 64 | + { |
| 65 | + for (int port = PortMin; port < PortMax; port++) |
| 66 | + { |
| 67 | + string url = $"http://localhost:{port}/"; |
| 68 | + |
| 69 | + _listener = new HttpListener(); |
| 70 | + _listener.Prefixes.Add(url); |
| 71 | + _listener.AuthenticationSchemes = authSchemes; |
| 72 | + |
| 73 | + try |
| 74 | + { |
| 75 | + _listener.Start(); |
| 76 | + BaseUrl = url; |
| 77 | + return; |
| 78 | + } |
| 79 | + catch (HttpListenerException) |
| 80 | + { |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + throw new Exception("Failed to locate a free port."); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public class NtAuthServers : IDisposable |
| 89 | + { |
| 90 | + public readonly NtAuthServer NtlmServer = new NtAuthServer(AuthenticationSchemes.Ntlm); |
| 91 | + public readonly NtAuthServer NegotiateServer = new NtAuthServer(AuthenticationSchemes.Negotiate); |
| 92 | + |
| 93 | + public void Dispose() |
| 94 | + { |
| 95 | + NtlmServer.Dispose(); |
| 96 | + NegotiateServer.Dispose(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public class NtAuthTests : IClassFixture<NtAuthServers> |
| 101 | + { |
| 102 | + private readonly NtAuthServers _servers; |
| 103 | + private readonly ITestOutputHelper _output; |
| 104 | + |
| 105 | + public NtAuthTests(NtAuthServers servers, ITestOutputHelper output) |
| 106 | + { |
| 107 | + _servers = servers; |
| 108 | + _output = output; |
| 109 | + } |
| 110 | + |
| 111 | + [OuterLoop] |
| 112 | + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsWindows), nameof(PlatformDetection.IsNotWindowsNanoServer))] // HttpListener doesn't support nt auth on non-Windows platforms |
| 113 | + [InlineData(true, HttpStatusCode.OK)] |
| 114 | + [InlineData(true, HttpStatusCode.Unauthorized)] |
| 115 | + [InlineData(false, HttpStatusCode.OK)] |
| 116 | + [InlineData(false, HttpStatusCode.Unauthorized)] |
| 117 | + public async Task GetAsync_NtAuthServer_ExpectedStatusCode(bool ntlm, HttpStatusCode expectedStatusCode) |
| 118 | + { |
| 119 | + NtAuthServer server = ntlm ? _servers.NtlmServer : _servers.NegotiateServer; |
| 120 | + |
| 121 | + var handler = new HttpClientHandler(); |
| 122 | + handler.UseDefaultCredentials = true; |
| 123 | + using (var client = new HttpClient(handler)) |
| 124 | + { |
| 125 | + client.BaseAddress = new Uri(server.BaseUrl); |
| 126 | + string path = expectedStatusCode == HttpStatusCode.Unauthorized ? server.NoAccessUrl : ""; |
| 127 | + HttpResponseMessage response = await client.GetAsync(path); |
| 128 | + |
| 129 | + Assert.Equal(expectedStatusCode, response.StatusCode); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments