forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiveFrom.cs
554 lines (458 loc) · 24.3 KB
/
ReceiveFrom.cs
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// 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;
using Xunit.Sdk;
namespace System.Net.Sockets.Tests
{
public abstract class ReceiveFrom<T> : SocketTestHelperBase<T> where T : SocketHelperBase, new()
{
protected static Socket CreateSocket(AddressFamily addressFamily = AddressFamily.InterNetwork) => new Socket(addressFamily, SocketType.Dgram, ProtocolType.Udp);
protected static IPEndPoint GetGetDummyTestEndpoint(AddressFamily addressFamily = AddressFamily.InterNetwork) =>
addressFamily == AddressFamily.InterNetwork ? new IPEndPoint(IPAddress.Parse("1.2.3.4"), 1234) : new IPEndPoint(IPAddress.Parse("1:2:3::4"), 1234);
protected static readonly TimeSpan CancellationTestTimeout = TimeSpan.FromSeconds(30);
protected ReceiveFrom(ITestOutputHelper output) : base(output) { }
[Theory]
[InlineData(1, -1, 0)] // offset low
[InlineData(1, 2, 0)] // offset high
[InlineData(1, 0, -1)] // count low
[InlineData(1, 0, 2)] // count high
[InlineData(1, 1, 1)] // count high
public async Task OutOfRange_Throws_ArgumentOutOfRangeException(int length, int offset, int count)
{
using Socket socket = CreateSocket();
ArraySegment<byte> buffer = new FakeArraySegment
{
Array = new byte[length],
Count = count,
Offset = offset
}.ToActual();
await AssertThrowsSynchronously<ArgumentOutOfRangeException>(() => ReceiveFromAsync(socket, buffer, GetGetDummyTestEndpoint()));
}
[Fact]
public async Task NullBuffer_Throws_ArgumentNullException()
{
if (!ValidatesArrayArguments) return;
using Socket socket = CreateSocket();
await AssertThrowsSynchronously<ArgumentNullException>(() => ReceiveFromAsync(socket, null, GetGetDummyTestEndpoint()));
}
[Fact]
public async Task NullEndpoint_Throws_ArgumentException()
{
using Socket socket = CreateSocket();
if (UsesEap)
{
await AssertThrowsSynchronously<ArgumentException>(() => ReceiveFromAsync(socket, new byte[1], null));
}
else
{
await AssertThrowsSynchronously<ArgumentNullException>(() => ReceiveFromAsync(socket, new byte[1], null));
}
}
[Fact]
public async Task NullSocketAddress_Throws_ArgumentException()
{
using Socket socket = CreateSocket();
SocketAddress socketAddress = null;
Assert.Throws<ArgumentNullException>(() => socket.ReceiveFrom(new byte[1], SocketFlags.None, socketAddress));
await Assert.ThrowsAsync<ArgumentNullException>(() => socket.ReceiveFromAsync(new Memory<byte>(new byte[1]), SocketFlags.None, socketAddress).AsTask());
}
[Fact]
public async Task AddressFamilyDoesNotMatch_Throws_ArgumentException()
{
using var ipv4Socket = CreateSocket();
EndPoint ipV6Endpoint = GetGetDummyTestEndpoint(AddressFamily.InterNetworkV6);
await AssertThrowsSynchronously<ArgumentException>(() => ReceiveFromAsync(ipv4Socket, new byte[1], ipV6Endpoint));
}
[Fact]
public async Task NotBound_Throws_InvalidOperationException()
{
// ReceiveFromAsync(saea) does not throw.
// [ActiveIssue("https://github.com/dotnet/runtime/issues/47714")]
if (UsesEap) return;
using Socket socket = CreateSocket();
await AssertThrowsSynchronously<InvalidOperationException>(() => ReceiveFromAsync(socket, new byte[1], GetGetDummyTestEndpoint()));
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ReceiveSent_TCP_Success(bool ipv6)
{
if (ipv6 && PlatformDetection.IsApplePlatform)
{
// [ActiveIssue("https://github.com/dotnet/runtime/issues/47335")]
// accept() will create a (seemingly) DualMode socket on Mac,
// but since recvmsg() does not work with DualMode on that OS, we throw PNSE CheckDualModeReceiveSupport().
// Weirdly, the flag is readable, but an attempt to write it leads to EINVAL.
// The best option seems to be to skip this test for the Mac+IPV6 case
return;
}
(Socket sender, Socket receiver) = SocketTestExtensions.CreateConnectedSocketPair(ipv6);
using (sender)
using (receiver)
{
byte[] sendBuffer = { 1, 2, 3 };
sender.Send(sendBuffer);
byte[] receiveBuffer = new byte[3];
var r = await ReceiveFromAsync(receiver, receiveBuffer, sender.LocalEndPoint);
Assert.Equal(3, r.ReceivedBytes);
AssertExtensions.SequenceEqual(sendBuffer, receiveBuffer);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ReceiveSent_UDP_Success(bool ipv4)
{
const int Offset = 10;
const int DatagramSize = 256;
const int DatagramsToSend = 16;
IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;
using Socket receiver = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
using Socket sender = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
ConfigureNonBlocking(sender);
ConfigureNonBlocking(receiver);
receiver.BindToAnonymousPort(address);
sender.BindToAnonymousPort(address);
byte[] sendBuffer = new byte[DatagramSize];
var receiveInternalBuffer = new byte[DatagramSize + Offset];
var emptyBuffer = new byte[Offset];
ArraySegment<byte> receiveBuffer = new ArraySegment<byte>(receiveInternalBuffer, Offset, DatagramSize);
Random rnd = new Random(0);
IPEndPoint remoteEp = new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0);
for (int i = 0; i < DatagramsToSend; i++)
{
rnd.NextBytes(sendBuffer);
sender.SendTo(sendBuffer, receiver.LocalEndPoint);
SocketReceiveFromResult result = await ReceiveFromAsync(receiver, receiveBuffer, remoteEp);
Assert.Equal(DatagramSize, result.ReceivedBytes);
AssertExtensions.SequenceEqual(emptyBuffer, new ReadOnlySpan<byte>(receiveInternalBuffer, 0, Offset));
AssertExtensions.SequenceEqual(sendBuffer, new ReadOnlySpan<byte>(receiveInternalBuffer, Offset, DatagramSize));
Assert.Equal(sender.LocalEndPoint, result.RemoteEndPoint);
remoteEp = (IPEndPoint)result.RemoteEndPoint;
if (i > 0)
{
// reference should be same after first round
Assert.True(remoteEp == result.RemoteEndPoint);
}
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ReceiveSent_DualMode_Success(bool ipv4)
{
const int Offset = 10;
const int DatagramSize = 256;
const int DatagramsToSend = 16;
IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;
using Socket receiver = new Socket(SocketType.Dgram, ProtocolType.Udp);
using Socket sender = new Socket(SocketType.Dgram, ProtocolType.Udp);
if (receiver.DualMode != true || sender.DualMode != true)
{
throw SkipException.ForSkip("DualMode not available");
}
ConfigureNonBlocking(sender);
ConfigureNonBlocking(receiver);
receiver.BindToAnonymousPort(address);
sender.BindToAnonymousPort(address);
byte[] sendBuffer = new byte[DatagramSize];
var receiveInternalBuffer = new byte[DatagramSize + Offset];
var emptyBuffer = new byte[Offset];
ArraySegment<byte> receiveBuffer = new ArraySegment<byte>(receiveInternalBuffer, Offset, DatagramSize);
Random rnd = new Random(0);
for (int i = 0; i < DatagramsToSend; i++)
{
rnd.NextBytes(sendBuffer);
sender.SendTo(sendBuffer, receiver.LocalEndPoint);
IPEndPoint remoteEp = new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0);
SocketReceiveFromResult result = await ReceiveFromAsync(receiver, receiveBuffer, remoteEp);
Assert.Equal(DatagramSize, result.ReceivedBytes);
AssertExtensions.SequenceEqual(emptyBuffer, new ReadOnlySpan<byte>(receiveInternalBuffer, 0, Offset));
AssertExtensions.SequenceEqual(sendBuffer, new ReadOnlySpan<byte>(receiveInternalBuffer, Offset, DatagramSize));
Assert.Equal(sender.LocalEndPoint, result.RemoteEndPoint);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void ReceiveSent_SocketAddress_Success(bool ipv4)
{
const int DatagramSize = 256;
const int DatagramsToSend = 16;
IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;
using Socket server = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
using Socket client = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
client.BindToAnonymousPort(address);
server.BindToAnonymousPort(address);
byte[] sendBuffer = new byte[DatagramSize];
byte[] receiveBuffer = new byte[DatagramSize];
SocketAddress serverSA = server.LocalEndPoint.Serialize();
SocketAddress clientSA = client.LocalEndPoint.Serialize();
SocketAddress sa = new SocketAddress(address.AddressFamily);
Random rnd = new Random(0);
for (int i = 0; i < DatagramsToSend; i++)
{
rnd.NextBytes(sendBuffer);
client.SendTo(sendBuffer.AsSpan(), SocketFlags.None, serverSA);
int readBytes = server.ReceiveFrom(receiveBuffer, SocketFlags.None, sa);
Assert.Equal(sa, clientSA);
Assert.Equal(client.LocalEndPoint, client.LocalEndPoint.Create(sa));
Assert.True(new Span<byte>(receiveBuffer, 0, readBytes).SequenceEqual(sendBuffer));
// and send it back to make sure it works.
rnd.NextBytes(sendBuffer);
server.SendTo(sendBuffer, SocketFlags.None, sa);
readBytes = client.ReceiveFrom(receiveBuffer, SocketFlags.None, sa);
Assert.Equal(sa, serverSA);
Assert.Equal(server.LocalEndPoint, server.LocalEndPoint.Create(sa));
Assert.True(new Span<byte>(receiveBuffer, 0, readBytes).SequenceEqual(sendBuffer));
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ReceiveSent_SocketAddressAsync_Success(bool ipv4)
{
const int DatagramSize = 256;
const int DatagramsToSend = 16;
IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;
using Socket server = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
using Socket client = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
client.BindToAnonymousPort(address);
server.BindToAnonymousPort(address);
byte[] sendBuffer = new byte[DatagramSize];
byte[] receiveBuffer = new byte[DatagramSize];
SocketAddress serverSA = server.LocalEndPoint.Serialize();
SocketAddress clientSA = client.LocalEndPoint.Serialize();
SocketAddress sa = new SocketAddress(address.AddressFamily);
Random rnd = new Random(0);
for (int i = 0; i < DatagramsToSend; i++)
{
rnd.NextBytes(sendBuffer);
await client.SendToAsync(sendBuffer, SocketFlags.None, serverSA);
int readBytes = await server.ReceiveFromAsync(receiveBuffer, SocketFlags.None, sa);
Assert.Equal(sa, clientSA);
Assert.Equal(client.LocalEndPoint, client.LocalEndPoint.Create(sa));
Assert.True(new Span<byte>(receiveBuffer, 0, readBytes).SequenceEqual(sendBuffer));
// and send it back to make sure it works.
rnd.NextBytes(sendBuffer);
await server.SendToAsync(sendBuffer, SocketFlags.None, sa);
readBytes = await client.ReceiveFromAsync(receiveBuffer, SocketFlags.None, sa);
Assert.Equal(sa, serverSA);
Assert.Equal(server.LocalEndPoint, server.LocalEndPoint.Create(sa));
Assert.True(new Span<byte>(receiveBuffer, 0, readBytes).SequenceEqual(sendBuffer));
}
}
[Fact]
public void ReceiveSent_SmallSocketAddress_Throws()
{
using Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
server.BindToAnonymousPort(IPAddress.Loopback);
byte[] receiveBuffer = new byte[1];
SocketAddress serverSA = server.LocalEndPoint.Serialize();
SocketAddress sa = new SocketAddress(AddressFamily.InterNetwork, 2);
Assert.Throws<ArgumentOutOfRangeException>(() => server.ReceiveFrom(receiveBuffer, SocketFlags.None, sa));
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task ClosedBeforeOperation_Throws_ObjectDisposedException(bool closeOrDispose)
{
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.BindToAnonymousPort(IPAddress.Any);
if (closeOrDispose) socket.Close();
else socket.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(() => ReceiveFromAsync(socket, new byte[1], GetGetDummyTestEndpoint()));
}
[Theory]
[InlineData(true)]
[InlineData(false)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/52124", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
public async Task ClosedDuringOperation_Throws_ObjectDisposedExceptionOrSocketException(bool closeOrDispose)
{
if (UsesSync && PlatformDetection.IsOSX)
{
// [ActiveIssue("https://github.com/dotnet/runtime/issues/47342")]
// On Mac, Close/Dispose hangs when invoked concurrently with a blocking UDP receive.
return;
}
int msDelay = 100;
if (UsesSync)
{
// In sync case Dispose may happen before the operation is started,
// in that case we would see an ObjectDisposedException instead of a SocketException.
// We may need to try the run a couple of times to deal with the timing race.
await RetryHelper.ExecuteAsync(() => RunTestAsync(), maxAttempts: 10, retryWhen: e => e is XunitException);
}
else
{
await RunTestAsync();
}
async Task RunTestAsync()
{
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.BindToAnonymousPort(IPAddress.Any);
Task receiveTask = ReceiveFromAsync(socket, new byte[1], GetGetDummyTestEndpoint());
await Task.Delay(msDelay);
msDelay *= 2;
if (closeOrDispose) socket.Close();
else socket.Dispose();
SocketException ex = await Assert.ThrowsAsync<SocketException>(() => receiveTask)
.WaitAsync(CancellationTestTimeout);
SocketError expectedError = UsesSync ? SocketError.Interrupted : SocketError.OperationAborted;
Assert.Equal(expectedError, ex.SocketErrorCode);
}
}
[PlatformSpecific(TestPlatforms.Windows)] // It's allowed to shutdown() UDP sockets on Windows, however on Unix this will lead to ENOTCONN
[Theory]
[InlineData(SocketShutdown.Both)]
[InlineData(SocketShutdown.Receive)]
public async Task ShutdownReceiveBeforeOperation_ThrowsSocketException(SocketShutdown shutdown)
{
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.BindToAnonymousPort(IPAddress.Any);
socket.Shutdown(shutdown);
// [ActiveIssue("https://github.com/dotnet/runtime/issues/47469")]
// Shutdown(Both) does not seem to take immediate effect for Receive(Message)From in a consistent manner, trying to workaround with a delay:
if (shutdown == SocketShutdown.Both) await Task.Delay(50);
SocketException exception = await Assert.ThrowsAnyAsync<SocketException>(() => ReceiveFromAsync(socket, new byte[1], GetGetDummyTestEndpoint()))
.WaitAsync(CancellationTestTimeout);
Assert.Equal(SocketError.Shutdown, exception.SocketErrorCode);
}
[PlatformSpecific(TestPlatforms.Windows)] // It's allowed to shutdown() UDP sockets on Windows, however on Unix this will lead to ENOTCONN
[Fact]
public async Task ShutdownSend_ReceiveFromShouldSucceed()
{
using var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
receiver.BindToAnonymousPort(IPAddress.Loopback);
receiver.Shutdown(SocketShutdown.Send);
using var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sender.BindToAnonymousPort(IPAddress.Loopback);
sender.SendTo(new byte[1], receiver.LocalEndPoint);
var r = await ReceiveFromAsync(receiver, new byte[1], sender.LocalEndPoint);
Assert.Equal(1, r.ReceivedBytes);
}
}
public sealed class ReceiveFrom_Sync : ReceiveFrom<SocketHelperArraySync>
{
public ReceiveFrom_Sync(ITestOutputHelper output) : base(output) { }
}
public sealed class ReceiveFrom_SyncForceNonBlocking : ReceiveFrom<SocketHelperSyncForceNonBlocking>
{
public ReceiveFrom_SyncForceNonBlocking(ITestOutputHelper output) : base(output) { }
}
public sealed class ReceiveFrom_Apm : ReceiveFrom<SocketHelperApm>
{
public ReceiveFrom_Apm(ITestOutputHelper output) : base(output) { }
[Fact]
public void EndReceiveFrom_NullAsyncResult_Throws_ArgumentNullException()
{
EndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1);
using Socket socket = CreateSocket();
Assert.Throws<ArgumentNullException>(() => socket.EndReceiveFrom(null, ref endpoint));
}
[Fact]
public void EndReceiveFrom_UnrelatedAsyncResult_Throws_ArgumentException()
{
EndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1);
using Socket socket = CreateSocket();
Assert.Throws<ArgumentException>(() => socket.EndReceiveFrom(Task.CompletedTask, ref endpoint));
}
[Fact]
public void EndReceiveFrom_NullEndPoint_Throws_ArgumentNullException()
{
EndPoint validEndPoint = new IPEndPoint(IPAddress.Loopback, 1);
EndPoint invalidEndPoint = null;
using Socket socket = CreateSocket();
socket.BindToAnonymousPort(IPAddress.Loopback);
IAsyncResult iar = socket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref validEndPoint, null, null);
Assert.Throws<ArgumentNullException>("endPoint", () => socket.EndReceiveFrom(iar, ref invalidEndPoint));
}
[Fact]
public void EndReceiveFrom_AddressFamilyDoesNotMatch_Throws_ArgumentException()
{
EndPoint validEndPoint = new IPEndPoint(IPAddress.Loopback, 1);
EndPoint invalidEndPoint = new IPEndPoint(IPAddress.IPv6Loopback, 1);
using Socket socket = CreateSocket();
socket.BindToAnonymousPort(IPAddress.Loopback);
IAsyncResult iar = socket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref validEndPoint, null, null);
Assert.Throws<ArgumentException>("endPoint", () => socket.EndReceiveFrom(iar, ref invalidEndPoint));
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54418", TestPlatforms.MacCatalyst)]
public void BeginReceiveFrom_RemoteEpIsReturnedWhenCompletedSynchronously()
{
EndPoint anyEp = new IPEndPoint(IPAddress.Any, 0);
EndPoint remoteEp = anyEp;
using Socket receiver = CreateSocket();
receiver.BindToAnonymousPort(IPAddress.Loopback);
using Socket sender = CreateSocket();
sender.BindToAnonymousPort(IPAddress.Loopback);
sender.SendTo(new byte[1], receiver.LocalEndPoint);
IAsyncResult iar = receiver.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref remoteEp, null, null);
if (iar.CompletedSynchronously)
{
_output.WriteLine("Completed synchronously, updated endpoint.");
Assert.Equal(sender.LocalEndPoint, remoteEp);
}
else
{
_output.WriteLine("Completed asynchronously, did not update endPoint");
Assert.Equal(anyEp, remoteEp);
}
}
}
public sealed class ReceiveFrom_Task : ReceiveFrom<SocketHelperTask>
{
public ReceiveFrom_Task(ITestOutputHelper output) : base(output) { }
}
public sealed class ReceiveFrom_CancellableTask : ReceiveFrom<SocketHelperCancellableTask>
{
public ReceiveFrom_CancellableTask(ITestOutputHelper output) : base(output) { }
[Theory]
[MemberData(nameof(LoopbacksAndBuffers))]
public async Task WhenCanceled_Throws(IPAddress loopback, bool precanceled)
{
using Socket socket = new Socket(loopback.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
using var dummy = new Socket(loopback.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
socket.BindToAnonymousPort(loopback);
dummy.BindToAnonymousPort(loopback);
Memory<byte> buffer = new byte[1];
CancellationTokenSource cts = new CancellationTokenSource();
if (precanceled) cts.Cancel();
else cts.CancelAfter(100);
OperationCanceledException ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => socket.ReceiveFromAsync(buffer, SocketFlags.None, dummy.LocalEndPoint, cts.Token).AsTask())
.WaitAsync(CancellationTestTimeout);
Assert.Equal(cts.Token, ex.CancellationToken);
}
}
public sealed class ReceiveFrom_Eap : ReceiveFrom<SocketHelperEap>
{
public ReceiveFrom_Eap(ITestOutputHelper output) : base(output) { }
[Fact]
public void ReceiveFromAsync_NullAsyncEventArgs_Throws_ArgumentNullException()
{
using Socket socket = CreateSocket();
Assert.Throws<ArgumentNullException>(() => socket.ReceiveFromAsync(null));
}
}
public sealed class ReceiveFrom_SpanSync : ReceiveFrom<SocketHelperSpanSync>
{
public ReceiveFrom_SpanSync(ITestOutputHelper output) : base(output) { }
}
public sealed class ReceiveFrom_SpanSyncForceNonBlocking : ReceiveFrom<SocketHelperSpanSyncForceNonBlocking>
{
public ReceiveFrom_SpanSyncForceNonBlocking(ITestOutputHelper output) : base(output) { }
}
public sealed class ReceiveFrom_MemoryArrayTask : ReceiveFrom<SocketHelperMemoryArrayTask>
{
public ReceiveFrom_MemoryArrayTask(ITestOutputHelper output) : base(output) { }
}
public sealed class ReceiveFrom_MemoryNativeTask : ReceiveFrom<SocketHelperMemoryNativeTask>
{
public ReceiveFrom_MemoryNativeTask(ITestOutputHelper output) : base(output) { }
}
}