forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketOptionNameTest.cs
More file actions
724 lines (634 loc) · 34.3 KB
/
SocketOptionNameTest.cs
File metadata and controls
724 lines (634 loc) · 34.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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Net.Test.Common;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;
namespace System.Net.Sockets.Tests
{
public partial class SocketOptionNameTest
{
private static bool SocketsReuseUnicastPortSupport => Capability.SocketsReuseUnicastPortSupport().HasValue;
[ConditionalFact(nameof(SocketsReuseUnicastPortSupport))]
public void ReuseUnicastPort_CreateSocketGetOption()
{
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
if (Capability.SocketsReuseUnicastPortSupport().Value)
{
Assert.Equal(0, (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort));
}
else
{
Assert.Throws<SocketException>(() => socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort));
}
}
}
[ConditionalFact(nameof(SocketsReuseUnicastPortSupport))]
public void ReuseUnicastPort_CreateSocketSetOption()
{
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
if (Capability.SocketsReuseUnicastPortSupport().Value)
{
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 0);
int optionValue = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort);
Assert.Equal(0, optionValue);
}
else
{
Assert.Throws<SocketException>(() => socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 1));
}
}
}
[Fact]
public void MulticastOption_CreateSocketSetGetOption_GroupAndInterfaceIndex_SetSucceeds_GetThrows()
{
int interfaceIndex = 0;
IPAddress groupIp = IPAddress.Parse("239.1.2.3");
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(groupIp, interfaceIndex));
Assert.Throws<SocketException>(() => socket.GetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership));
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoNorServerCore))] // Skip on Nano: https://github.com/dotnet/runtime/issues/26286
public async Task MulticastInterface_Set_AnyInterface_Succeeds()
{
// On all platforms, index 0 means "any interface"
await MulticastInterface_Set_Helper(0);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoNorServerCore))] // Skip on Nano: https://github.com/dotnet/runtime/issues/26286
[PlatformSpecific(TestPlatforms.Windows)] // see comment below
public async Task MulticastInterface_Set_Loopback_Succeeds()
{
// On Windows, we can apparently assume interface 1 is "loopback." On other platforms, this is not a
// valid assumption. We could maybe use NetworkInterface.LoopbackInterfaceIndex to get the index, but
// this would introduce a dependency on System.Net.NetworkInformation, which depends on System.Net.Sockets,
// which is what we're testing here.... So for now, we'll just assume "loopback == 1" and run this on
// Windows only.
await MulticastInterface_Set_Helper(1);
}
private async Task MulticastInterface_Set_Helper(int interfaceIndex)
{
IPAddress multicastAddress = IPAddress.Parse("239.1.2.3");
string message = "hello";
int port;
using (Socket receiveSocket = CreateBoundUdpSocket(out port),
sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
receiveSocket.ReceiveTimeout = 1000;
receiveSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress, interfaceIndex));
sendSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(interfaceIndex));
var receiveBuffer = new byte[1024];
var receiveTask = receiveSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), SocketFlags.None);
sendSocket.SendTo(Encoding.UTF8.GetBytes(message), new IPEndPoint(multicastAddress, port));
int bytesReceived = await receiveTask.WaitAsync(TimeSpan.FromSeconds(30));
string receivedMessage = Encoding.UTF8.GetString(receiveBuffer, 0, bytesReceived);
Assert.Equal(receivedMessage, message);
}
}
[Fact]
public void MulticastInterface_Set_InvalidIndex_Throws()
{
int interfaceIndex = 31415;
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
Assert.Throws<SocketException>(() =>
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(interfaceIndex)));
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoNorServerCore))] // Skip on Nano: https://github.com/dotnet/runtime/issues/26286
[SkipOnPlatform(TestPlatforms.OSX | TestPlatforms.FreeBSD, "Expected behavior is different on OSX or FreeBSD")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/52124", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
public async Task MulticastInterface_Set_IPv6_AnyInterface_Succeeds()
{
if (PlatformDetection.IsRedHatFamily7)
{
// RH7 seems to have issues with multicast in Azure. Same code and setup can pass when executed outside of Azure.
throw new SkipTestException("IPv6 multicast environment not available");
}
// On all platforms, index 0 means "any interface"
await MulticastInterface_Set_IPv6_Helper(0);
}
[Fact]
public void MulticastTTL_Set_IPv4_Succeeds()
{
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
// This should not throw. We currently do not have good mechanism how to verify that the TTL/Hops is actually set.
int ttl = (int)socket.GetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive);
ttl += 1;
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, ttl);
Assert.Equal(ttl, (int)socket.GetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive));
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoNorServerCore))] // Skip on Nano: https://github.com/dotnet/runtime/issues/26286
public void MulticastTTL_Set_IPv6_Succeeds()
{
using (Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp))
{
// This should not throw. We currently do not have good mechanism how to verify that the TTL/Hops is actually set.
int ttl = (int)socket.GetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive);
ttl += 1;
socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive, ttl);
Assert.Equal(ttl, (int)socket.GetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive));
}
}
[Theory]
[InlineData(AddressFamily.InterNetwork)]
[InlineData(AddressFamily.InterNetworkV6)]
public void Ttl_Set_Succeeds(AddressFamily af)
{
using (Socket socket = new Socket(af, SocketType.Dgram, ProtocolType.Udp))
{
short newTtl = socket.Ttl;
// Change default ttl.
newTtl += (short)((newTtl < 255) ? 1 : -1);
socket.Ttl = newTtl;
Assert.Equal(newTtl, socket.Ttl);
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoNorServerCore))] // Skip on Nano: https://github.com/dotnet/runtime/issues/26286
[PlatformSpecific(TestPlatforms.Windows)]
public async Task MulticastInterface_Set_IPv6_Loopback_Succeeds()
{
// On Windows, we can apparently assume interface 1 is "loopback." On other platforms, this is not a
// valid assumption. We could maybe use NetworkInterface.LoopbackInterfaceIndex to get the index, but
// this would introduce a dependency on System.Net.NetworkInformation, which depends on System.Net.Sockets,
// which is what we're testing here.... So for now, we'll just assume "loopback == 1" and run this on
// Windows only.
await MulticastInterface_Set_IPv6_Helper(1);
}
private async Task MulticastInterface_Set_IPv6_Helper(int interfaceIndex)
{
IPAddress multicastAddress = IPAddress.Parse("ff11::1:1");
string message = "hello";
int port;
using (Socket receiveSocket = CreateBoundUdpIPv6Socket(out port),
sendSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp))
{
receiveSocket.ReceiveTimeout = 1000;
receiveSocket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(multicastAddress, interfaceIndex));
sendSocket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastInterface, interfaceIndex);
var receiveBuffer = new byte[1024];
var receiveTask = receiveSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), SocketFlags.None);
sendSocket.SendTo(Encoding.UTF8.GetBytes(message), new IPEndPoint(multicastAddress, port));
int bytesReceived = await receiveTask.WaitAsync(TimeSpan.FromSeconds(30));
string receivedMessage = Encoding.UTF8.GetString(receiveBuffer, 0, bytesReceived);
Assert.Equal(receivedMessage, message);
}
}
[Fact]
public void MulticastInterface_Set_IPv6_InvalidIndex_Throws()
{
int interfaceIndex = 31415;
using (Socket s = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp))
{
Assert.Throws<SocketException>(() =>
s.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastInterface, interfaceIndex));
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
[SkipOnPlatform(TestPlatforms.FreeBSD, "on FreeBSD Connect may or may not fail immediately based on timing.")]
public void FailedConnect_GetSocketOption_SocketOptionNameError(bool simpleGet)
{
using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { Blocking = false })
{
// Fail a Connect
using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
server.Bind(new IPEndPoint(IPAddress.Loopback, 0)); // bind but don't listen
Assert.ThrowsAny<Exception>(() => client.Connect(server.LocalEndPoint));
}
// Verify via Select that there's an error
const int FailedTimeout = 10 * 1000 * 1000; // 10 seconds
var errorList = new List<Socket> { client };
Socket.Select(null, null, errorList, FailedTimeout);
Assert.Equal(1, errorList.Count);
// Get the last error and validate it's what's expected
int errorCode;
if (simpleGet)
{
errorCode = (int)client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
}
else
{
byte[] optionValue = new byte[sizeof(int)];
client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error, optionValue);
errorCode = BitConverter.ToInt32(optionValue, 0);
}
Assert.Equal((int)SocketError.ConnectionRefused, errorCode);
// Then get it again
if (OperatingSystem.IsWindows())
{
// The Windows implementation doesn't clear the error code after retrieved.
// https://github.com/dotnet/runtime/issues/17260
Assert.Equal(errorCode, (int)client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error));
}
else
{
// The Unix implementation matches the getsockopt and MSDN docs and clears the error code as part of retrieval.
Assert.Equal((int)SocketError.Success, (int)client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error));
}
}
}
// Create an Udp Socket and binds it to an available port
private static Socket CreateBoundUdpSocket(out int localPort)
{
Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// sending a message will bind the socket to an available port
string sendMessage = "dummy message";
int port = 54320;
IPAddress multicastAddress = IPAddress.Parse("239.1.1.1");
receiveSocket.SendTo(Encoding.UTF8.GetBytes(sendMessage), new IPEndPoint(multicastAddress, port));
localPort = (receiveSocket.LocalEndPoint as IPEndPoint).Port;
return receiveSocket;
}
// Create an Udp Socket and binds it to an available port
private static Socket CreateBoundUdpIPv6Socket(out int localPort)
{
Socket receiveSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
// sending a message will bind the socket to an available port
string sendMessage = "dummy message";
int port = 54320;
IPAddress multicastAddress = IPAddress.Parse("ff11::1:1");
receiveSocket.SendTo(Encoding.UTF8.GetBytes(sendMessage), new IPEndPoint(multicastAddress, port));
localPort = (receiveSocket.LocalEndPoint as IPEndPoint).Port;
return receiveSocket;
}
[Theory]
[InlineData(null, null, null, true)]
[InlineData(null, null, false, true)]
[InlineData(null, false, false, true)]
[InlineData(null, true, false, true)]
[InlineData(null, true, true, false)]
[InlineData(true, null, null, true)]
[InlineData(true, null, false, true)]
[InlineData(true, null, true, true)]
[InlineData(true, false, null, true)]
[InlineData(true, false, false, true)]
[InlineData(true, false, true, true)]
public void ReuseAddress(bool? exclusiveAddressUse, bool? firstSocketReuseAddress, bool? secondSocketReuseAddress, bool expectFailure)
{
using (Socket a = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
if (exclusiveAddressUse.HasValue)
{
a.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, exclusiveAddressUse.Value);
}
if (firstSocketReuseAddress.HasValue)
{
a.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, firstSocketReuseAddress.Value);
}
a.Bind(new IPEndPoint(IPAddress.Loopback, 0));
using (Socket b = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
if (secondSocketReuseAddress.HasValue)
{
b.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, secondSocketReuseAddress.Value);
}
if (expectFailure)
{
Assert.ThrowsAny<SocketException>(() => b.Bind(a.LocalEndPoint));
}
else
{
b.Bind(a.LocalEndPoint);
}
}
}
}
[Theory]
[PlatformSpecific(TestPlatforms.Windows)] // ExclusiveAddressUse option is a Windows-specific option (when set to "true," tells Windows not to allow reuse of same local address)
[InlineData(false, null, null, true)]
[InlineData(false, null, false, true)]
[InlineData(false, false, null, true)]
[InlineData(false, false, false, true)]
[InlineData(false, true, null, true)]
[InlineData(false, true, false, true)]
[InlineData(false, true, true, false)]
public void ReuseAddress_Windows(bool? exclusiveAddressUse, bool? firstSocketReuseAddress, bool? secondSocketReuseAddress, bool expectFailure)
{
ReuseAddress(exclusiveAddressUse, firstSocketReuseAddress, secondSocketReuseAddress, expectFailure);
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Windows defaults are different
public void ExclusiveAddress_Default_Unix()
{
using (Socket a = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
Assert.Equal(1, (int)a.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse));
Assert.True(a.ExclusiveAddressUse);
Assert.Equal(0, (int)a.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress));
}
}
[Theory]
[InlineData(1)]
[InlineData(0)]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Unix does not have separate options for ExclusiveAddressUse and ReuseAddress.
public void SettingExclusiveAddress_SetsReuseAddress(int value)
{
using (Socket a = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
a.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, value);
Assert.Equal(value, (int)a.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse));
Assert.Equal(value == 1 ? 0 : 1, (int)a.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress));
}
// SettingReuseAddress_SetsExclusiveAddress
using (Socket a = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
a.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, value);
Assert.Equal(value, (int)a.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress));
Assert.Equal(value == 1 ? 0 : 1, (int)a.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse));
}
}
[Fact]
public void ExclusiveAddressUseTcp()
{
using (Socket a = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
// ExclusiveAddressUse defaults to true on Unix, on Windows it defaults to false.
a.ExclusiveAddressUse = true;
a.Bind(new IPEndPoint(IPAddress.Loopback, 0));
a.Listen();
int port = (a.LocalEndPoint as IPEndPoint).Port;
using (Socket b = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
SocketException ex = Assert.ThrowsAny<SocketException>(() => b.Bind(new IPEndPoint(IPAddress.Loopback, port)));
Assert.Equal(SocketError.AddressAlreadyInUse, ex.SocketErrorCode);
}
}
}
[Fact]
[PlatformSpecific(TestPlatforms.Linux | TestPlatforms.OSX)]
public unsafe void ReuseAddressUdp()
{
// Verify that .NET Core Sockets can bind to the UDP address from applications
// that allow binding the same address.
int SOL_SOCKET = -1;
int option = -1;
if (OperatingSystem.IsLinux())
{
// Linux: use SO_REUSEADDR to allow binding the same address.
SOL_SOCKET = 1;
const int SO_REUSEADDR = 2;
option = SO_REUSEADDR;
}
else if (OperatingSystem.IsMacOS())
{
// BSD: use SO_REUSEPORT to allow binding the same address.
SOL_SOCKET = 0xffff;
const int SO_REUSEPORT = 0x200;
option = SO_REUSEPORT;
}
using (Socket s1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
int value = 1;
s1.SetRawSocketOption(SOL_SOCKET, option, new Span<byte>(&value, sizeof(int)));
s1.Bind(new IPEndPoint(IPAddress.Any, 0));
using (Socket s2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
s2.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
s2.Bind(s1.LocalEndPoint);
}
}
}
[Theory]
[PlatformSpecific(TestPlatforms.Windows)] // SetIPProtectionLevel not supported on Unix
[InlineData(IPProtectionLevel.EdgeRestricted, AddressFamily.InterNetwork, SocketOptionLevel.IP)]
[InlineData(IPProtectionLevel.Restricted, AddressFamily.InterNetwork, SocketOptionLevel.IP)]
[InlineData(IPProtectionLevel.Unrestricted, AddressFamily.InterNetwork, SocketOptionLevel.IP)]
[InlineData(IPProtectionLevel.EdgeRestricted, AddressFamily.InterNetworkV6, SocketOptionLevel.IPv6)]
[InlineData(IPProtectionLevel.Restricted, AddressFamily.InterNetworkV6, SocketOptionLevel.IPv6)]
[InlineData(IPProtectionLevel.Unrestricted, AddressFamily.InterNetworkV6, SocketOptionLevel.IPv6)]
public void SetIPProtectionLevel_Windows(IPProtectionLevel level, AddressFamily family, SocketOptionLevel optionLevel)
{
using (var socket = new Socket(family, SocketType.Stream, ProtocolType.Tcp))
{
socket.SetIPProtectionLevel(level);
int result = (int)socket.GetSocketOption(optionLevel, SocketOptionName.IPProtectionLevel);
Assert.Equal(result, (int)level);
}
}
[Theory]
[PlatformSpecific(TestPlatforms.AnyUnix)] // SetIPProtectionLevel not supported on Unix
[InlineData(IPProtectionLevel.EdgeRestricted, AddressFamily.InterNetwork)]
[InlineData(IPProtectionLevel.Restricted, AddressFamily.InterNetwork)]
[InlineData(IPProtectionLevel.Unrestricted, AddressFamily.InterNetwork)]
[InlineData(IPProtectionLevel.EdgeRestricted, AddressFamily.InterNetworkV6)]
[InlineData(IPProtectionLevel.Restricted, AddressFamily.InterNetworkV6)]
[InlineData(IPProtectionLevel.Unrestricted, AddressFamily.InterNetworkV6)]
public void SetIPProtectionLevel_Unix(IPProtectionLevel level, AddressFamily family)
{
using (var socket = new Socket(family, SocketType.Stream, ProtocolType.Tcp))
{
Assert.Throws<PlatformNotSupportedException>(() => socket.SetIPProtectionLevel(level));
}
}
[Theory]
[InlineData(AddressFamily.InterNetwork)]
[InlineData(AddressFamily.InterNetworkV6)]
public void SetIPProtectionLevel_ArgumentException(AddressFamily family)
{
using (var socket = new Socket(family, SocketType.Stream, ProtocolType.Tcp))
{
AssertExtensions.Throws<ArgumentException>("level", () => socket.SetIPProtectionLevel(IPProtectionLevel.Unspecified));
}
}
[Theory]
[InlineData(AddressFamily.InterNetwork)]
[InlineData(AddressFamily.InterNetworkV6)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/50568", TestPlatforms.Android)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/52124", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
public void GetSetRawSocketOption_Roundtrips(AddressFamily family)
{
int SOL_SOCKET;
int SO_RCVBUF;
if (OperatingSystem.IsWindows() ||
OperatingSystem.IsMacOS())
{
SOL_SOCKET = 0xffff;
SO_RCVBUF = 0x1002;
}
else if (OperatingSystem.IsLinux())
{
SOL_SOCKET = 1;
SO_RCVBUF = 8;
}
else
{
throw new SkipTestException("Unknown platform");
}
using (var socket = new Socket(family, SocketType.Stream, ProtocolType.Tcp))
{
const int SetSize = 8192;
int ExpectedGetSize =
OperatingSystem.IsLinux() ? SetSize * 2 : // Linux kernel documented to double the size
SetSize;
socket.SetRawSocketOption(SOL_SOCKET, SO_RCVBUF, BitConverter.GetBytes(SetSize));
var buffer = new byte[sizeof(int)];
Assert.Equal(4, socket.GetRawSocketOption(SOL_SOCKET, SO_RCVBUF, buffer));
Assert.Equal(ExpectedGetSize, BitConverter.ToInt32(buffer));
Assert.Equal(ExpectedGetSize, socket.ReceiveBufferSize);
}
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/52124", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
public void Get_AcceptConnection_Succeeds()
{
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Assert.Equal(0, s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.AcceptConnection));
s.Bind(new IPEndPoint(IPAddress.Loopback, 0));
s.Listen();
Assert.Equal(1, s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.AcceptConnection));
}
}
[Fact]
public void GetUnsupportedSocketOption_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
SocketException se = Assert.Throws<SocketException>(() => socket1.GetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1)));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");
Assert.True(socket1.Connected, "Connected");
}
}
[Fact]
public void GetUnsupportedSocketOptionBytesArg_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
var optionValue = new byte[4];
SocketException se = Assert.Throws<SocketException>(() => socket1.GetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1), optionValue));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");
Assert.True(socket1.Connected, "Connected");
}
}
[Fact]
public void GetUnsupportedSocketOptionLengthArg_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
SocketException se = Assert.Throws<SocketException>(() => socket1.GetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1), optionLength: 4));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");
Assert.True(socket1.Connected, "Connected");
}
}
[Fact]
public void SetUnsupportedSocketOptionIntArg_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
SocketException se = Assert.Throws<SocketException>(() => socket1.SetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1), optionValue: 1));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");
Assert.True(socket1.Connected, "Connected");
}
}
[Fact]
public void SetUnsupportedSocketOptionBytesArg_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
var optionValue = new byte[4];
SocketException se = Assert.Throws<SocketException>(() => socket1.SetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1), optionValue));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");
Assert.True(socket1.Connected, "Connected");
}
}
[Fact]
public void SetUnsupportedSocketOptionBoolArg_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
bool optionValue = true;
SocketException se = Assert.Throws<SocketException>(() => socket1.SetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1), optionValue));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");
Assert.True(socket1.Connected, "Connected");
}
}
[Fact]
public void GetUnsupportedRawSocketOption_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
var optionValue = new byte[4];
SocketException se = Assert.Throws<SocketException>(() => socket1.GetRawSocketOption(SOL_SOCKET, -1, optionValue));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");
Assert.True(socket1.Connected, "Connected");
}
}
[Fact]
public void SetUnsupportedRawSocketOption_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
var optionValue = new byte[4];
SocketException se = Assert.Throws<SocketException>(() => socket1.SetRawSocketOption(SOL_SOCKET, -1, optionValue));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");
Assert.True(socket1.Connected, "Connected");
}
}
private static int SOL_SOCKET = OperatingSystem.IsLinux() ? 1 : (int)SocketOptionLevel.Socket;
}
[Collection("NoParallelTests")]
// Set of tests to not run together with any other tests.
public partial class NoParallelTests
{
[Fact]
public void BindDuringTcpWait_Succeeds()
{
int port = 0;
using (Socket a = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
a.Bind(new IPEndPoint(IPAddress.Loopback, 0));
port = (a.LocalEndPoint as IPEndPoint).Port;
a.Listen();
// Connect a client
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
client.Connect(new IPEndPoint(IPAddress.Loopback, port));
// accept socket and close it with zero linger time.
a.Accept().Close(0);
}
}
// Bind a socket to the same address we just used.
// To avoid conflict with other tests, this is part of the NoParallelTests test collection.
using (Socket b = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
b.Bind(new IPEndPoint(IPAddress.Loopback, port));
}
}
}
}