Skip to content

Commit

Permalink
QUIC ALPN list narrowing down scenario test (#86659)
Browse files Browse the repository at this point in the history
* Added necessary test for ALPN Narrowing down on QUIC

Co-authored-by: Natalia Kondratyeva <knatalia@microsoft.com>
  • Loading branch information
liveans and CarnaViire authored Jun 22, 2023
1 parent 6af91cb commit 5bc65b0
Showing 1 changed file with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,103 @@ await Task.WhenAll(
await connection.DisposeAsync();
}).WaitAsync(TimeSpan.FromSeconds(5)));
}

[Fact]
public async Task Listener_AlpnNarrowingDown_Success()
{
using CancellationTokenSource testTimeoutCts = new CancellationTokenSource(PassingTestTimeout);
CancellationToken timeoutToken = testTimeoutCts.Token;

var listenerOptions = new QuicListenerOptions()
{
ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0),
ApplicationProtocols = new List<SslApplicationProtocol>()
{
new SslApplicationProtocol("foo"),
new SslApplicationProtocol("bar"),
new SslApplicationProtocol("test"),
},
ConnectionOptionsCallback = (_, _, _) =>
{
// Narrowing down alpn list to more specific.
var options = CreateQuicServerOptions();
options.ServerAuthenticationOptions.ApplicationProtocols = new()
{
new SslApplicationProtocol("bar"),
new SslApplicationProtocol("test"),
};
return ValueTask.FromResult(options);
}
};
await using QuicListener listener = await CreateQuicListener(listenerOptions);

// Successful connection with bar ALPN
QuicClientConnectionOptions clientOptions1 = CreateQuicClientOptions(listener.LocalEndPoint);
clientOptions1.ClientAuthenticationOptions.ApplicationProtocols = new()
{
new SslApplicationProtocol("foo"),
new SslApplicationProtocol("bar"),
};
ValueTask<QuicConnection> connectTask1 = CreateQuicConnection(clientOptions1);
await using QuicConnection serverConnection1 = await listener.AcceptConnectionAsync().AsTask().WaitAsync(timeoutToken);
await using QuicConnection clientConnection1 = await connectTask1.AsTask().WaitAsync(timeoutToken);

Assert.Equal(new SslApplicationProtocol("bar"), clientConnection1.NegotiatedApplicationProtocol);

// Successful connection with test ALPN
QuicClientConnectionOptions clientOptions2 = CreateQuicClientOptions(listener.LocalEndPoint);
clientOptions2.ClientAuthenticationOptions.ApplicationProtocols = new()
{
new SslApplicationProtocol("foo"),
new SslApplicationProtocol("test"),
};
ValueTask<QuicConnection> connectTask2 = CreateQuicConnection(clientOptions2);
await using QuicConnection serverConnection2 = await listener.AcceptConnectionAsync().AsTask().WaitAsync(timeoutToken);
await using QuicConnection clientConnection2 = await connectTask2.AsTask().WaitAsync(timeoutToken);

Assert.Equal(new SslApplicationProtocol("test"), clientConnection2.NegotiatedApplicationProtocol);
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/86701")]
[Theory]
[InlineData("foo")]
[InlineData("not_existing")]
public async Task Listener_AlpnNarrowingDown_Failure(string alpn)
{
using CancellationTokenSource testTimeoutCts = new CancellationTokenSource(PassingTestTimeout);
CancellationToken timeoutToken = testTimeoutCts.Token;

var listenerOptions = new QuicListenerOptions()
{
ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0),
ApplicationProtocols = new List<SslApplicationProtocol>()
{
new SslApplicationProtocol("foo"),
new SslApplicationProtocol("bar"),
new SslApplicationProtocol("test"),
},
ConnectionOptionsCallback = (_, _, _) =>
{
// Narrowing down alpn list to more specific.
var options = CreateQuicServerOptions();
options.ServerAuthenticationOptions.ApplicationProtocols = new()
{
new SslApplicationProtocol("bar"),
new SslApplicationProtocol("test"),
};
return ValueTask.FromResult(options);
}
};
await using QuicListener listener = await CreateQuicListener(listenerOptions);

QuicClientConnectionOptions clientOptions = CreateQuicClientOptions(listener.LocalEndPoint);
clientOptions.ClientAuthenticationOptions.ApplicationProtocols = new()
{
new SslApplicationProtocol(alpn),
};
ValueTask<QuicConnection> connectTask = CreateQuicConnection(clientOptions);
await Assert.ThrowsAsync<AuthenticationException>(() => listener.AcceptConnectionAsync().AsTask().WaitAsync(timeoutToken));
await Assert.ThrowsAsync<AuthenticationException>(() => connectTask.AsTask().WaitAsync(timeoutToken));
}
}
}

0 comments on commit 5bc65b0

Please sign in to comment.