forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuicConnectionTests.cs
More file actions
298 lines (244 loc) · 13.3 KB
/
QuicConnectionTests.cs
File metadata and controls
298 lines (244 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.Quic.Tests
{
public abstract class QuicConnectionTests<T> : QuicTestBase<T>
where T : IQuicImplProviderFactory, new()
{
const int ExpectedErrorCode = 1234;
public QuicConnectionTests(ITestOutputHelper output) : base(output) { }
[Fact]
public async Task TestConnect()
{
using QuicListener listener = CreateQuicListener();
using QuicConnection clientConnection = CreateQuicConnection(listener.ListenEndPoint);
Assert.False(clientConnection.Connected);
Assert.Equal(listener.ListenEndPoint, clientConnection.RemoteEndPoint);
ValueTask connectTask = clientConnection.ConnectAsync();
ValueTask<QuicConnection> acceptTask = listener.AcceptConnectionAsync();
await new Task[] { connectTask.AsTask(), acceptTask.AsTask()}.WhenAllOrAnyFailed(PassingTestTimeoutMilliseconds);
QuicConnection serverConnection = acceptTask.Result;
Assert.True(clientConnection.Connected);
Assert.True(serverConnection.Connected);
Assert.Equal(listener.ListenEndPoint, serverConnection.LocalEndPoint);
Assert.Equal(listener.ListenEndPoint, clientConnection.RemoteEndPoint);
Assert.Equal(clientConnection.LocalEndPoint, serverConnection.RemoteEndPoint);
Assert.Equal(ApplicationProtocol.ToString(), clientConnection.NegotiatedApplicationProtocol.ToString());
Assert.Equal(ApplicationProtocol.ToString(), serverConnection.NegotiatedApplicationProtocol.ToString());
}
private static async Task<QuicStream> OpenAndUseStreamAsync(QuicConnection c)
{
QuicStream s = c.OpenBidirectionalStream();
// This will pend
await s.ReadAsync(new byte[1]);
return s;
}
[Fact]
public async Task CloseAsync_WithPendingAcceptAndConnect_PendingAndSubsequentThrowOperationAbortedException()
{
using var sync = new SemaphoreSlim(0);
await RunClientServer(
async clientConnection =>
{
await sync.WaitAsync();
},
async serverConnection =>
{
// Pend operations before the client closes.
Task<QuicStream> acceptTask = serverConnection.AcceptStreamAsync().AsTask();
Assert.False(acceptTask.IsCompleted);
Task<QuicStream> connectTask = OpenAndUseStreamAsync(serverConnection);
Assert.False(connectTask.IsCompleted);
await serverConnection.CloseAsync(ExpectedErrorCode);
sync.Release();
// Pending ops should fail
await Assert.ThrowsAsync<QuicOperationAbortedException>(() => acceptTask);
await Assert.ThrowsAsync<QuicOperationAbortedException>(() => connectTask);
// Subsequent attempts should fail
// TODO: Which exception is correct?
if (IsMockProvider)
{
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await serverConnection.AcceptStreamAsync());
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await OpenAndUseStreamAsync(serverConnection));
}
else
{
await Assert.ThrowsAsync<QuicOperationAbortedException>(async () => await serverConnection.AcceptStreamAsync());
// TODO: ActiveIssue https://github.com/dotnet/runtime/issues/56133
// MsQuic fails with System.Net.Quic.QuicException: Failed to open stream to peer. Error Code: INVALID_STATE
//await Assert.ThrowsAsync<QuicOperationAbortedException>(async () => await OpenAndUseStreamAsync(serverConnection));
await Assert.ThrowsAsync<QuicException>(() => OpenAndUseStreamAsync(serverConnection));
}
});
}
[Fact]
public async Task Dispose_WithPendingAcceptAndConnect_PendingAndSubsequentThrowOperationAbortedException()
{
using var sync = new SemaphoreSlim(0);
await RunClientServer(
async clientConnection =>
{
await sync.WaitAsync();
},
async serverConnection =>
{
// Pend operations before the client closes.
Task<QuicStream> acceptTask = serverConnection.AcceptStreamAsync().AsTask();
Assert.False(acceptTask.IsCompleted);
Task<QuicStream> connectTask = OpenAndUseStreamAsync(serverConnection);
Assert.False(connectTask.IsCompleted);
serverConnection.Dispose();
sync.Release();
// Pending ops should fail
await Assert.ThrowsAsync<QuicOperationAbortedException>(() => acceptTask);
await Assert.ThrowsAsync<QuicOperationAbortedException>(() => connectTask);
// Subsequent attempts should fail
// TODO: Should these be QuicOperationAbortedException, to match above? Or vice-versa?
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await serverConnection.AcceptStreamAsync());
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await OpenAndUseStreamAsync(serverConnection));
});
}
[Fact]
public async Task ConnectionClosedByPeer_WithPendingAcceptAndConnect_PendingAndSubsequentThrowConnectionAbortedException()
{
if (IsMockProvider)
{
return;
}
using var sync = new SemaphoreSlim(0);
await RunClientServer(
async clientConnection =>
{
await sync.WaitAsync();
await clientConnection.CloseAsync(ExpectedErrorCode);
},
async serverConnection =>
{
// Pend operations before the client closes.
Task<QuicStream> acceptTask = serverConnection.AcceptStreamAsync().AsTask();
Assert.False(acceptTask.IsCompleted);
Task<QuicStream> connectTask = OpenAndUseStreamAsync(serverConnection);
Assert.False(connectTask.IsCompleted);
sync.Release();
// Pending ops should fail
QuicConnectionAbortedException ex;
ex = await Assert.ThrowsAsync<QuicConnectionAbortedException>(() => acceptTask);
Assert.Equal(ExpectedErrorCode, ex.ErrorCode);
ex = await Assert.ThrowsAsync<QuicConnectionAbortedException>(() => connectTask);
Assert.Equal(ExpectedErrorCode, ex.ErrorCode);
// Subsequent attempts should fail
ex = await Assert.ThrowsAsync<QuicConnectionAbortedException>(() => serverConnection.AcceptStreamAsync().AsTask());
Assert.Equal(ExpectedErrorCode, ex.ErrorCode);
// TODO: ActiveIssue https://github.com/dotnet/runtime/issues/56133
// MsQuic fails with System.Net.Quic.QuicException: Failed to open stream to peer. Error Code: INVALID_STATE
if (IsMsQuicProvider)
{
await Assert.ThrowsAsync<QuicException>(() => OpenAndUseStreamAsync(serverConnection));
}
else
{
ex = await Assert.ThrowsAsync<QuicConnectionAbortedException>(() => OpenAndUseStreamAsync(serverConnection));
Assert.Equal(ExpectedErrorCode, ex.ErrorCode);
}
});
}
private static async Task DoWrites(QuicStream writer, int writeCount)
{
for (int i = 0; i < writeCount; i++)
{
await writer.WriteAsync(new byte[1]);
}
}
private static async Task DoReads(QuicStream reader, int readCount)
{
for (int i = 0; i < readCount; i++)
{
int bytesRead = await reader.ReadAsync(new byte[1]);
Assert.Equal(1, bytesRead);
}
}
[Theory]
[InlineData(1)]
[InlineData(10)]
public async Task CloseAsync_WithOpenStream_LocalAndPeerStreamsFailWithQuicOperationAbortedException(int writesBeforeClose)
{
if (IsMockProvider)
{
return;
}
using var sync = new SemaphoreSlim(0);
await RunClientServer(
async clientConnection =>
{
using QuicStream clientStream = clientConnection.OpenBidirectionalStream();
await DoWrites(clientStream, writesBeforeClose);
// Wait for peer to receive data
await sync.WaitAsync();
await clientConnection.CloseAsync(ExpectedErrorCode);
await Assert.ThrowsAsync<QuicOperationAbortedException>(async () => await clientStream.ReadAsync(new byte[1]));
await Assert.ThrowsAsync<QuicOperationAbortedException>(async () => await clientStream.WriteAsync(new byte[1]));
},
async serverConnection =>
{
using QuicStream serverStream = await serverConnection.AcceptStreamAsync();
await DoReads(serverStream, writesBeforeClose);
sync.Release();
// Since the peer did the abort, we should receive the abort error code in the exception.
QuicConnectionAbortedException ex;
ex = await Assert.ThrowsAsync<QuicConnectionAbortedException>(async () => await serverStream.ReadAsync(new byte[1]));
Assert.Equal(ExpectedErrorCode, ex.ErrorCode);
ex = await Assert.ThrowsAsync<QuicConnectionAbortedException>(async () => await serverStream.WriteAsync(new byte[1]));
Assert.Equal(ExpectedErrorCode, ex.ErrorCode);
});
}
[OuterLoop("Depends on IdleTimeout")]
[Theory]
[InlineData(1)]
[InlineData(10)]
public async Task Dispose_WithOpenLocalStream_LocalStreamFailsWithQuicOperationAbortedException(int writesBeforeClose)
{
if (IsMockProvider)
{
return;
}
// Set a short idle timeout so that after we dispose the connection, the peer will discover the connection is dead before too long.
QuicListenerOptions listenerOptions = CreateQuicListenerOptions();
listenerOptions.IdleTimeout = TimeSpan.FromSeconds(1);
using var sync = new SemaphoreSlim(0);
await RunClientServer(
async clientConnection =>
{
using QuicStream clientStream = clientConnection.OpenBidirectionalStream();
await DoWrites(clientStream, writesBeforeClose);
// Wait for peer to receive data
await sync.WaitAsync();
clientConnection.Dispose();
await Assert.ThrowsAsync<QuicOperationAbortedException>(async () => await clientStream.ReadAsync(new byte[1]));
await Assert.ThrowsAsync<QuicOperationAbortedException>(async () => await clientStream.WriteAsync(new byte[1]));
},
async serverConnection =>
{
using QuicStream serverStream = await serverConnection.AcceptStreamAsync();
await DoReads(serverStream, writesBeforeClose);
sync.Release();
// The client has done an abortive shutdown of the connection, which means we are not notified that the connection has closed.
// But the connection idle timeout should kick in and eventually we will get exceptions.
await Assert.ThrowsAsync<QuicConnectionAbortedException>(async () => await serverStream.ReadAsync(new byte[1]));
await Assert.ThrowsAsync<QuicConnectionAbortedException>(async () => await serverStream.WriteAsync(new byte[1]));
}, listenerOptions: listenerOptions);
}
}
public sealed class QuicConnectionTests_MockProvider : QuicConnectionTests<MockProviderFactory>
{
public QuicConnectionTests_MockProvider(ITestOutputHelper output) : base(output) { }
}
[ConditionalClass(typeof(QuicTestBase<MsQuicProviderFactory>), nameof(QuicTestBase<MsQuicProviderFactory>.IsSupported))]
public sealed class QuicConnectionTests_MsQuicProvider : QuicConnectionTests<MsQuicProviderFactory>
{
public QuicConnectionTests_MsQuicProvider(ITestOutputHelper output) : base(output) { }
}
}