Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore LoopbackServer exceptions in MaxHeadersLength test #73937

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public override Task InitializeConnectionAsync()
throw new NotImplementedException();
}

private Task EnsureControlStreamAcceptedAsync()
public Task EnsureControlStreamAcceptedAsync()
{
if (_inboundControlStream != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,50 +49,101 @@ public void ValidValue_SetGet_Roundtrips(int validValue)
[Fact]
public async Task SetAfterUse_Throws()
{
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
for (int repeat = 0; repeat <= (UseVersion.Major == 3 ? 50 : 1); repeat++)
{
using HttpClientHandler handler = CreateHttpClientHandler();
using HttpClient client = CreateHttpClient(handler);
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpClientHandler handler = CreateHttpClientHandler();
using HttpClient client = CreateHttpClient(handler);

handler.MaxResponseHeadersLength = 1;
(await client.GetStreamAsync(uri)).Dispose();
Assert.Throws<InvalidOperationException>(() => handler.MaxResponseHeadersLength = 1);
},
server => server.AcceptConnectionSendResponseAndCloseAsync());
}
}

handler.MaxResponseHeadersLength = 1;
(await client.GetStreamAsync(uri)).Dispose();
Assert.Throws<InvalidOperationException>(() => handler.MaxResponseHeadersLength = 1);
},
server => server.AcceptConnectionSendResponseAndCloseAsync());
public static IEnumerable<object[]> LargeSingleHeader_ThrowsException_MemberData()
{
object[][] options = new[]
{
new object[] { 1 },
new object[] { 2 },
new object[] { 15 },
};

var rng = new Random();
int count = options.Length * 50;

for (int i = 0; i < count; i++)
{
yield return options[rng.Next(options.Length)];
}
}

[Theory]
[InlineData(1)]
[InlineData(15)]
[MemberData(nameof(LargeSingleHeader_ThrowsException_MemberData))]
public async Task LargeSingleHeader_ThrowsException(int maxResponseHeadersLength)
{
if (UseVersion.Major != 3) return;

using HttpClientHandler handler = CreateHttpClientHandler();
handler.MaxResponseHeadersLength = maxResponseHeadersLength;

await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpClient client = CreateHttpClient(handler);

Exception e = await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(uri));
Exception e = await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(uri)).WaitAsync(TestHelper.PassingTestTimeout);
if (!IsWinHttpHandler)
{
Assert.Contains((handler.MaxResponseHeadersLength * 1024).ToString(), e.ToString());
}
},
async server =>
{
await server.HandleRequestAsync(headers: new[] { new HttpHeaderData("Foo", new string('a', handler.MaxResponseHeadersLength * 1024)) });
HttpHeaderData[] headers = new[] { new HttpHeaderData("Foo", new string('a', handler.MaxResponseHeadersLength * 1024)) };

try
{
await server.HandleRequestAsync(headers: headers).WaitAsync(TestHelper.PassingTestTimeout);
}
catch (Exception ex)
{
_output.WriteLine($"Ignored exception:{Environment.NewLine}{ex}");
}
});
}

public static IEnumerable<object[]> ThresholdExceeded_ThrowsException_MemberData()
{
object[][] options = new[]
{
new object[] { null, 63 * 1024 },
new object[] { null, 65 * 1024 },
new object[] { 1, 100 },
new object[] { 1, 1024 },
new object[] { 2, 1100 },
new object[] { 2, 2048 },
new object[] { int.MaxValue / 800, 100 * 1024 },
};

var rng = new Random();
int count = options.Length * 50;

for (int i = 0; i < count; i++)
{
yield return options[rng.Next(options.Length)];
}
}

[Theory]
[InlineData(null, 63 * 1024)]
[InlineData(null, 65 * 1024)]
[InlineData(1, 100)]
[InlineData(1, 1024)]
[InlineData(int.MaxValue / 800, 100 * 1024)] // Capped at int.MaxValue
[MemberData(nameof(ThresholdExceeded_ThrowsException_MemberData))]
public async Task ThresholdExceeded_ThrowsException(int? maxResponseHeadersLength, int headersLengthEstimate)
{
if (UseVersion.Major != 3) return;

await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpClientHandler handler = CreateHttpClientHandler();
Expand All @@ -106,11 +157,11 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>

if (headersLengthEstimate < handler.MaxResponseHeadersLength * 1024L)
{
await client.GetAsync(uri);
await client.GetAsync(uri).WaitAsync(TestHelper.PassingTestTimeout);
}
else
{
Exception e = await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(uri));
Exception e = await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(uri)).WaitAsync(TestHelper.PassingTestTimeout);
if (!IsWinHttpHandler)
{
Assert.Contains((handler.MaxResponseHeadersLength * 1024).ToString(), e.ToString());
Expand All @@ -125,7 +176,14 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
headers.Add(new HttpHeaderData($"Custom-{i}", new string('a', 480)));
}

await server.HandleRequestAsync(headers: headers);
try
{
await server.HandleRequestAsync(headers: headers).WaitAsync(TestHelper.PassingTestTimeout);
}
catch (Exception ex)
{
_output.WriteLine($"Ignored exception:{Environment.NewLine}{ex}");
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1240,12 +1240,70 @@ public SocketsHttpHandler_HttpClientHandler_MaxResponseHeadersLength_Http2(ITest
protected override Version UseVersion => HttpVersion.Version20;
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/73930")]
[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsQuicSupported))]
public sealed class SocketsHttpHandler_HttpClientHandler_MaxResponseHeadersLength_Http3 : HttpClientHandler_MaxResponseHeadersLength_Test
{
public SocketsHttpHandler_HttpClientHandler_MaxResponseHeadersLength_Http3(ITestOutputHelper output) : base(output) { }
protected override Version UseVersion => HttpVersion.Version30;

public static IEnumerable<object[]> Http3Test_MemberData()
{
object[][] options = new[]
{
new object[] { null },
new object[] { 1 },
new object[] { 2 },
new object[] { 16 },
new object[] { 64 },
new object[] { 256 },
new object[] { 1024 },
new object[] { 10240 },
};

var rng = new Random();
int count = options.Length * 50;

for (int i = 0; i < count; i++)
{
yield return options[rng.Next(options.Length)];
}
}

[Theory]
[MemberData(nameof(Http3Test_MemberData))]
public async Task Http3Test(int? maxResponseHeadersLength)
{
var requestCts = new CancellationTokenSource();
var controlStreamEstablishedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using HttpClientHandler handler = CreateHttpClientHandler();
using HttpClient client = CreateHttpClient(handler);

if (maxResponseHeadersLength.HasValue)
{
handler.MaxResponseHeadersLength = maxResponseHeadersLength.Value;
}

await Assert.ThrowsAnyAsync<Exception>(() => client.GetAsync(uri, requestCts.Token));

await controlStreamEstablishedTcs.Task.WaitAsync(TestHelper.PassingTestTimeout);
},
async server =>
{
await server.AcceptConnectionAsync(async connection =>
{
requestCts.Cancel();

var http3Connection = (Http3LoopbackConnection)connection;

await http3Connection.EnsureControlStreamAcceptedAsync().WaitAsync(TestHelper.PassingTestTimeout);

controlStreamEstablishedTcs.SetResult();
});
});
}
}

[SkipOnPlatform(TestPlatforms.Browser, "Socket is not supported on Browser")]
Expand Down