This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
DualModeSocketTest.cs
2734 lines (2364 loc) · 111 KB
/
DualModeSocketTest.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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Linq;
using System.Net.Test.Common;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.Sockets.Tests
{
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeConstructorAndProperty : DualModeBase
{
[Fact]
public void DualModeConstructor_InterNetworkV6Default()
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
Assert.Equal(AddressFamily.InterNetworkV6, socket.AddressFamily);
Assert.True(socket.DualMode);
}
}
[Fact]
public void DualModeUdpConstructor_DualModeConfgiured()
{
using (Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
Assert.Equal(AddressFamily.InterNetworkV6, socket.AddressFamily);
Assert.True(socket.DualMode);
}
}
[Fact]
public void NormalConstructor_DualModeConfgiureable()
{
using (Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
{
Assert.False(socket.DualMode);
socket.DualMode = true;
Assert.True(socket.DualMode);
socket.DualMode = false;
Assert.False(socket.DualMode);
}
}
[Fact]
public void IPv4Constructor_DualModeThrows()
{
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Assert.Throws<NotSupportedException>(() =>
{
Assert.False(socket.DualMode);
});
Assert.Throws<NotSupportedException>(() =>
{
socket.DualMode = true;
});
}
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeConnectToIPAddress : DualModeBase
{
[Fact] // Base case
public void Socket_ConnectV4IPAddressToV4Host_Throws()
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
socket.DualMode = false;
Assert.Throws<NotSupportedException>(() =>
{
socket.Connect(IPAddress.Loopback, UnusedPort);
});
}
}
[Fact] // Base Case
public void ConnectV4MappedIPAddressToV4Host_Success()
{
DualModeConnect_IPAddressToHost_Helper(IPAddress.Loopback.MapToIPv6(), IPAddress.Loopback, false);
}
[Fact] // Base Case
public void ConnectV4MappedIPAddressToDualHost_Success()
{
DualModeConnect_IPAddressToHost_Helper(IPAddress.Loopback.MapToIPv6(), IPAddress.IPv6Any, true);
}
[Fact]
public void ConnectV4IPAddressToV4Host_Success()
{
DualModeConnect_IPAddressToHost_Helper(IPAddress.Loopback, IPAddress.Loopback, false);
}
[Fact]
public void ConnectV6IPAddressToV6Host_Success()
{
DualModeConnect_IPAddressToHost_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Loopback, false);
}
[Fact]
public void ConnectV4IPAddressToV6Host_Fails()
{
DualModeConnect_IPAddressToHost_Fails_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback);
}
[Fact]
public void ConnectV6IPAddressToV4Host_Fails()
{
DualModeConnect_IPAddressToHost_Fails_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback);
}
[Fact]
public void ConnectV4IPAddressToDualHost_Success()
{
DualModeConnect_IPAddressToHost_Helper(IPAddress.Loopback, IPAddress.IPv6Any, true);
}
[Fact]
public void ConnectV6IPAddressToDualHost_Success()
{
DualModeConnect_IPAddressToHost_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Any, true);
}
private void DualModeConnect_IPAddressToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
socket.Connect(connectTo, port);
Assert.True(socket.Connected);
}
}
private void DualModeConnect_IPAddressToHost_Fails_Helper(IPAddress connectTo, IPAddress listenOn)
{
Assert.ThrowsAny<SocketException>(() =>
{
DualModeConnect_IPAddressToHost_Helper(connectTo, listenOn, false);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// On Unix, socket assignment is random (not incremental) and there is a small chance the
// listening socket was created in another test currently running. Try the test one more time.
DualModeConnect_IPAddressToHost_Helper(connectTo, listenOn, false);
}
});
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeConnectToIPEndPoint : DualModeBase
{
[Fact] // Base case
public void Socket_ConnectV4IPEndPointToV4Host_Throws()
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
socket.DualMode = false;
Assert.ThrowsAny<SocketException>(() =>
{
socket.Connect(new IPEndPoint(IPAddress.Loopback, UnusedPort));
});
}
}
[Fact] // Base case
public void ConnectV4MappedIPEndPointToV4Host_Success()
{
DualModeConnect_IPEndPointToHost_Helper(IPAddress.Loopback.MapToIPv6(), IPAddress.Loopback, false);
}
[Fact] // Base case
public void ConnectV4MappedIPEndPointToDualHost_Success()
{
DualModeConnect_IPEndPointToHost_Helper(IPAddress.Loopback.MapToIPv6(), IPAddress.IPv6Any, true);
}
[Fact]
public void ConnectV4IPEndPointToV4Host_Success()
{
DualModeConnect_IPEndPointToHost_Helper(IPAddress.Loopback, IPAddress.Loopback, false);
}
[Fact]
public void ConnectV6IPEndPointToV6Host_Success()
{
DualModeConnect_IPEndPointToHost_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Loopback, false);
}
[Fact]
public void ConnectV4IPEndPointToV6Host_Fails()
{
DualModeConnect_IPEndPointToHost_Fails_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback);
}
[Fact]
public void ConnectV6IPEndPointToV4Host_Fails()
{
DualModeConnect_IPEndPointToHost_Fails_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback);
}
[Fact]
public void ConnectV4IPEndPointToDualHost_Success()
{
DualModeConnect_IPEndPointToHost_Helper(IPAddress.Loopback, IPAddress.IPv6Any, true);
}
[Fact]
public void ConnectV6IPEndPointToDualHost_Success()
{
DualModeConnect_IPEndPointToHost_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Any, true);
}
private void DualModeConnect_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
socket.Connect(new IPEndPoint(connectTo, port));
Assert.True(socket.Connected);
}
}
private void DualModeConnect_IPEndPointToHost_Fails_Helper(IPAddress connectTo, IPAddress listenOn)
{
Assert.ThrowsAny<SocketException>(() =>
{
DualModeConnect_IPEndPointToHost_Helper(connectTo, listenOn, false);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// On Unix, socket assignment is random (not incremental) and there is a small chance the
// listening socket was created in another test currently running. Try the test one more time.
DualModeConnect_IPEndPointToHost_Helper(connectTo, listenOn, false);
}
});
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeConnectToIPAddressArray : DualModeBase
{
[Fact] // Base Case
// "None of the discovered or specified addresses match the socket address family."
public void Socket_ConnectV4IPAddressListToV4Host_Throws()
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
socket.DualMode = false;
using (SocketServer server = new SocketServer(_log, IPAddress.Loopback, false, out int port))
{
AssertExtensions.Throws<ArgumentException>("addresses", () =>
{
socket.Connect(new IPAddress[] { IPAddress.Loopback }, port);
});
}
}
}
[Theory]
[PlatformSpecific(TestPlatforms.Windows)] // Binds to a specific port on 'connectTo' which on Unix may already be in use
[MemberData(nameof(DualMode_IPAddresses_ListenOn_DualMode_Throws_Data))]
public void DualModeConnect_IPAddressListToHost_Throws(IPAddress[] connectTo, IPAddress listenOn, bool dualModeServer)
{
Assert.ThrowsAny<SocketException>(() => DualModeConnect_IPAddressListToHost_Success(connectTo, listenOn, dualModeServer));
}
[Theory]
[MemberData(nameof(DualMode_IPAddresses_ListenOn_DualMode_Success_Data))]
public void DualModeConnect_IPAddressListToHost_Success(IPAddress[] connectTo, IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
socket.Connect(connectTo, port);
Assert.True(socket.Connected);
}
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeConnectToHostString : DualModeBase
{
[ConditionalTheory(nameof(LocalhostIsBothIPv4AndIPv6))]
[MemberData(nameof(DualMode_Connect_IPAddress_DualMode_Data))]
public void DualModeConnect_LoopbackDnsToHost_Helper(IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
socket.Connect("localhost", port);
Assert.True(socket.Connected);
}
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeConnectToDnsEndPoint : DualModeBase
{
[ConditionalTheory(nameof(LocalhostIsBothIPv4AndIPv6))]
[MemberData(nameof(DualMode_Connect_IPAddress_DualMode_Data))]
public void DualModeConnect_DnsEndPointToHost_Helper(IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
socket.Connect(new DnsEndPoint("localhost", port, AddressFamily.Unspecified));
Assert.True(socket.Connected);
}
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeBeginConnectToIPAddress : DualModeBase
{
[Fact] // Base case
public void Socket_BeginConnectV4IPAddressToV4Host_Throws()
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
socket.DualMode = false;
Assert.Throws<NotSupportedException>(() =>
{
socket.BeginConnect(IPAddress.Loopback, UnusedPort, null, null);
});
}
}
[Fact]
public Task BeginConnectV4IPAddressToV4Host_Success() => DualModeBeginConnect_IPAddressToHost_Helper(IPAddress.Loopback, IPAddress.Loopback, false);
[Fact]
public Task BeginConnectV6IPAddressToV6Host_Success() => DualModeBeginConnect_IPAddressToHost_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Loopback, false);
[Fact]
public Task BeginConnectV4IPAddressToV6Host_Fails() => DualModeBeginConnect_IPAddressToHost_Fails_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback);
[Fact]
public Task BeginConnectV6IPAddressToV4Host_Fails() => DualModeBeginConnect_IPAddressToHost_Fails_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback);
[Fact]
public Task BeginConnectV4IPAddressToDualHost_Success() => DualModeBeginConnect_IPAddressToHost_Helper(IPAddress.Loopback, IPAddress.IPv6Any, true);
[Fact]
public Task BeginConnectV6IPAddressToDualHost_Success() => DualModeBeginConnect_IPAddressToHost_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Any, true);
private async Task DualModeBeginConnect_IPAddressToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
await Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, connectTo, port, null);
Assert.True(socket.Connected);
}
}
private async Task DualModeBeginConnect_IPAddressToHost_Fails_Helper(IPAddress connectTo, IPAddress listenOn)
{
SocketException e = await Assert.ThrowsAnyAsync<SocketException>(async () =>
{
await DualModeBeginConnect_IPAddressToHost_Helper(connectTo, listenOn, false);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// On Unix, socket assignment is random (not incremental) and there is a small chance the
// listening socket was created in another test currently running. Try the test one more time.
await DualModeBeginConnect_IPAddressToHost_Helper(connectTo, listenOn, false);
}
});
Assert.NotEmpty(e.Message);
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeBeginConnectToIPEndPoint : DualModeBase
{
[Fact] // Base case
// "The system detected an invalid pointer address in attempting to use a pointer argument in a call"
public void Socket_BeginConnectV4IPEndPointToV4Host_Throws()
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
socket.DualMode = false;
Assert.Throws<SocketException>(() =>
{
socket.BeginConnect(new IPEndPoint(IPAddress.Loopback, UnusedPort), null, null);
});
}
}
[Fact]
public Task BeginConnectV4IPEndPointToV4Host_Success() => DualModeBeginConnect_IPEndPointToHost_Helper(IPAddress.Loopback, IPAddress.Loopback, false);
[Fact]
public Task BeginConnectV6IPEndPointToV6Host_Success() => DualModeBeginConnect_IPEndPointToHost_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Loopback, false);
[Fact]
public Task BeginConnectV4IPEndPointToDualHost_Success() => DualModeBeginConnect_IPEndPointToHost_Helper(IPAddress.Loopback, IPAddress.IPv6Any, true);
[Fact]
public Task BeginConnectV6IPEndPointToDualHost_Success() => DualModeBeginConnect_IPEndPointToHost_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Any, true);
private async Task DualModeBeginConnect_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
await Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, new IPEndPoint(connectTo, port), null);
Assert.True(socket.Connected);
}
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeBeginConnect : DualModeBase
{
[Theory]
[MemberData(nameof(DualMode_IPAddresses_ListenOn_DualMode_Data))]
[PlatformSpecific(TestPlatforms.Windows)] // Connecting sockets to DNS endpoints via the instance Connect and ConnectAsync methods not supported on Unix
private async Task DualModeBeginConnect_IPAddressListToHost_Helper(IPAddress[] connectTo, IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
await Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, connectTo, port, null);
Assert.True(socket.Connected);
}
}
[Theory]
[MemberData(nameof(DualMode_Connect_IPAddress_DualMode_Data))]
[PlatformSpecific(TestPlatforms.Windows)] // Connecting sockets to DNS endpoints via the instance Connect and ConnectAsync methods not supported on Unix
public async Task DualModeBeginConnect_LoopbackDnsToHost_Helper(IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
await Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, "localhost", port, null);
Assert.True(socket.Connected);
}
}
[Theory]
[MemberData(nameof(DualMode_Connect_IPAddress_DualMode_Data))]
[PlatformSpecific(TestPlatforms.Windows)] // Connecting sockets to DNS endpoints via the instance Connect and ConnectAsync methods not supported on Unix
public async Task DualModeBeginConnect_DnsEndPointToHost_Helper(IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
await Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, new DnsEndPoint("localhost", port), null);
Assert.True(socket.Connected);
}
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeConnectAsync : DualModeBase
{
[Fact] // Base case
public void Socket_ConnectAsyncV4IPEndPointToV4Host_Throws()
{
using (Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, UnusedPort);
Assert.Throws<NotSupportedException>(() =>
{
socket.ConnectAsync(args);
});
}
}
[Fact]
public void ConnectAsyncV4IPEndPointToV4Host_Success()
{
DualModeConnectAsync_IPEndPointToHost_Helper(IPAddress.Loopback, IPAddress.Loopback, false);
}
[Fact]
public void ConnectAsyncV6IPEndPointToV6Host_Success()
{
DualModeConnectAsync_IPEndPointToHost_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Loopback, false);
}
[Fact]
public void ConnectAsyncV4IPEndPointToV6Host_Fails()
{
DualModeConnectAsync_IPEndPointToHost_Fails_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback);
}
[Fact]
public void ConnectAsyncV6IPEndPointToV4Host_Fails()
{
DualModeConnectAsync_IPEndPointToHost_Fails_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback);
}
[Fact]
public void ConnectAsyncV4IPEndPointToDualHost_Success()
{
DualModeConnectAsync_IPEndPointToHost_Helper(IPAddress.Loopback, IPAddress.IPv6Any, true);
}
[Fact]
public void ConnectAsyncV6IPEndPointToDualHost_Success()
{
DualModeConnectAsync_IPEndPointToHost_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Any, true);
}
private void DualModeConnectAsync_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
ManualResetEvent waitHandle = new ManualResetEvent(false);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += new EventHandler<SocketAsyncEventArgs>(AsyncCompleted);
args.RemoteEndPoint = new IPEndPoint(connectTo, port);
args.UserToken = waitHandle;
bool pending = socket.ConnectAsync(args);
if (!pending)
waitHandle.Set();
Assert.True(waitHandle.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
if (args.SocketError != SocketError.Success)
{
throw new SocketException((int)args.SocketError);
}
Assert.True(socket.Connected);
}
}
private void DualModeConnectAsync_IPEndPointToHost_Fails_Helper(IPAddress connectTo, IPAddress listenOn)
{
Assert.ThrowsAny<SocketException>(() =>
{
DualModeConnectAsync_IPEndPointToHost_Helper(connectTo, listenOn, false);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// On Unix, socket assignment is random (not incremental) and there is a small chance the
// listening socket was created in another test currently running. Try the test one more time.
DualModeConnectAsync_IPEndPointToHost_Helper(connectTo, listenOn, false);
}
});
}
[ConditionalTheory(nameof(LocalhostIsBothIPv4AndIPv6))]
[MemberData(nameof(DualMode_Connect_IPAddress_DualMode_Data))]
public void DualModeConnectAsync_DnsEndPointToHost_Helper(IPAddress listenOn, bool dualModeServer)
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
ManualResetEvent waitHandle = new ManualResetEvent(false);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += new EventHandler<SocketAsyncEventArgs>(AsyncCompleted);
args.RemoteEndPoint = new DnsEndPoint("localhost", port);
args.UserToken = waitHandle;
bool pending = socket.ConnectAsync(args);
if (!pending)
waitHandle.Set();
Assert.True(waitHandle.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
if (args.SocketError != SocketError.Success)
{
throw new SocketException((int)args.SocketError);
}
Assert.True(socket.Connected);
}
}
[ConditionalTheory(nameof(LocalhostIsBothIPv4AndIPv6))]
[ActiveIssue(20893)]
[MemberData(nameof(DualMode_Connect_IPAddress_DualMode_Data))]
public void DualModeConnectAsync_Static_DnsEndPointToHost_Helper(IPAddress listenOn, bool dualModeServer)
{
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out int port))
{
ManualResetEvent waitHandle = new ManualResetEvent(false);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += new EventHandler<SocketAsyncEventArgs>(AsyncCompleted);
args.RemoteEndPoint = new DnsEndPoint("localhost", port);
args.UserToken = waitHandle;
bool pending = Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, args);
if (!pending)
waitHandle.Set();
Assert.True(waitHandle.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
if (args.SocketError != SocketError.Success)
{
throw new SocketException((int)args.SocketError);
}
Assert.True(args.ConnectSocket.Connected);
args.ConnectSocket.Dispose();
}
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeBind : DualModeBase
{
[Fact]
public void Socket_BindV4IPEndPoint_Throws()
{
Assert.Throws<SocketException>(() =>
{
using (Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
{
socket.Bind(new IPEndPoint(IPAddress.Loopback, UnusedBindablePort));
}
});
}
[Fact] // Base Case; BSoD on Win7, Win8 with IPv4 uninstalled
public void BindMappedV4IPEndPoint_Success()
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
socket.BindToAnonymousPort(IPAddress.Loopback.MapToIPv6());
}
}
[Fact] // BSoD on Win7, Win8 with IPv4 uninstalled
public void BindV4IPEndPoint_Success()
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
socket.BindToAnonymousPort(IPAddress.Loopback);
}
}
[Fact]
public void BindV6IPEndPoint_Success()
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
socket.BindToAnonymousPort(IPAddress.IPv6Loopback);
}
}
[Fact]
public void Socket_BindDnsEndPoint_Throws()
{
AssertExtensions.Throws<ArgumentException>("remoteEP", () =>
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
socket.Bind(new DnsEndPoint("localhost", UnusedBindablePort));
}
});
}
[Fact]
public void Socket_EnableDualModeAfterV4Bind_Throws()
{
using (Socket serverSocket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
serverSocket.DualMode = false;
serverSocket.BindToAnonymousPort(IPAddress.IPv6Any);
Assert.Throws<SocketException>(() =>
{
serverSocket.DualMode = true;
});
}
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeAccept : DualModeBase
{
[Fact]
public void AcceptV4BoundToSpecificV4_Success()
{
Accept_Helper(IPAddress.Loopback, IPAddress.Loopback);
}
[Fact]
public void AcceptV4BoundToAnyV4_Success()
{
Accept_Helper(IPAddress.Any, IPAddress.Loopback);
}
[Fact]
public void AcceptV6BoundToSpecificV6_Success()
{
Accept_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Loopback);
}
[Fact]
public void AcceptV6BoundToAnyV6_Success()
{
Accept_Helper(IPAddress.IPv6Any, IPAddress.IPv6Loopback);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // https://github.com/dotnet/corefx/issues/5832
public void AcceptV6BoundToSpecificV4_CantConnect()
{
Assert.Throws<SocketException>(() =>
{
Accept_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback);
});
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // https://github.com/dotnet/corefx/issues/5832
public void AcceptV4BoundToSpecificV6_CantConnect()
{
Assert.Throws<SocketException>(() =>
{
Accept_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback);
});
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // https://github.com/dotnet/corefx/issues/5832
public void AcceptV6BoundToAnyV4_CantConnect()
{
Assert.Throws<SocketException>(() =>
{
Accept_Helper(IPAddress.Any, IPAddress.IPv6Loopback);
});
}
[Fact]
public void AcceptV4BoundToAnyV6_Success()
{
Accept_Helper(IPAddress.IPv6Any, IPAddress.Loopback);
}
private void Accept_Helper(IPAddress listenOn, IPAddress connectTo)
{
using (Socket serverSocket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
int port = serverSocket.BindToAnonymousPort(listenOn);
serverSocket.Listen(1);
SocketClient client = new SocketClient(_log, serverSocket, connectTo, port);
Socket clientSocket = serverSocket.Accept();
Assert.True(clientSocket.Connected);
AssertDualModeEnabled(clientSocket, listenOn);
Assert.Equal(AddressFamily.InterNetworkV6, clientSocket.AddressFamily);
}
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeBeginAccept : DualModeBase
{
[Fact]
public void BeginAcceptV4BoundToSpecificV4_Success()
{
DualModeConnect_BeginAccept_Helper(IPAddress.Loopback, IPAddress.Loopback);
}
[Fact]
public void BeginAcceptV4BoundToAnyV4_Success()
{
DualModeConnect_BeginAccept_Helper(IPAddress.Any, IPAddress.Loopback);
}
[Fact]
public void BeginAcceptV6BoundToSpecificV6_Success()
{
DualModeConnect_BeginAccept_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Loopback);
}
[Fact]
public void BeginAcceptV6BoundToAnyV6_Success()
{
DualModeConnect_BeginAccept_Helper(IPAddress.IPv6Any, IPAddress.IPv6Loopback);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Binds to a specific port on 'connectTo' which on Unix may already be in use
public void BeginAcceptV6BoundToSpecificV4_CantConnect()
{
Assert.Throws<SocketException>(() =>
{
DualModeConnect_BeginAccept_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback);
});
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Binds to a specific port on 'connectTo' which on Unix may already be in use
public void BeginAcceptV4BoundToSpecificV6_CantConnect()
{
Assert.Throws<SocketException>(() =>
{
DualModeConnect_BeginAccept_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback);
});
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Binds to a specific port on 'connectTo' which on Unix may already be in use
public void BeginAcceptV6BoundToAnyV4_CantConnect()
{
Assert.Throws<SocketException>(() =>
{
DualModeConnect_BeginAccept_Helper(IPAddress.Any, IPAddress.IPv6Loopback);
});
}
[Fact]
public void BeginAcceptV4BoundToAnyV6_Success()
{
DualModeConnect_BeginAccept_Helper(IPAddress.IPv6Any, IPAddress.Loopback);
}
private void DualModeConnect_BeginAccept_Helper(IPAddress listenOn, IPAddress connectTo)
{
using (Socket serverSocket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
int port = serverSocket.BindToAnonymousPort(listenOn);
serverSocket.Listen(1);
IAsyncResult async = serverSocket.BeginAccept(null, null);
SocketClient client = new SocketClient(_log, serverSocket, connectTo, port);
// Due to the nondeterministic nature of calling dispose on a Socket that is doing
// an EndAccept operation, we expect two types of exceptions to happen.
Socket clientSocket;
try
{
clientSocket = serverSocket.EndAccept(async);
Assert.True(clientSocket.Connected);
AssertDualModeEnabled(clientSocket, listenOn);
Assert.Equal(AddressFamily.InterNetworkV6, clientSocket.AddressFamily);
if (connectTo == IPAddress.Loopback)
{
Assert.Contains(((IPEndPoint)clientSocket.LocalEndPoint).Address, ValidIPv6Loopbacks);
}
else
{
Assert.Equal(connectTo.MapToIPv6(), ((IPEndPoint)clientSocket.LocalEndPoint).Address);
}
}
catch (ObjectDisposedException) { }
catch (SocketException) { }
Assert.True(
client.WaitHandle.WaitOne(TestSettings.PassingTestTimeout),
"Timed out while waiting for connection");
if (client.Error != SocketError.Success)
{
throw new SocketException((int)client.Error);
}
}
}
}
[Trait("IPv4", "true")]
[Trait("IPv6", "true")]
public class DualModeAcceptAsync : DualModeBase
{
[Fact]
public void AcceptAsyncV4BoundToSpecificV4_Success()
{
DualModeConnect_AcceptAsync_Helper(IPAddress.Loopback, IPAddress.Loopback);
}
[Fact]
public void AcceptAsyncV4BoundToAnyV4_Success()
{
DualModeConnect_AcceptAsync_Helper(IPAddress.Any, IPAddress.Loopback);
}
[Fact]
public void AcceptAsyncV6BoundToSpecificV6_Success()
{
DualModeConnect_AcceptAsync_Helper(IPAddress.IPv6Loopback, IPAddress.IPv6Loopback);
}
[Fact]
public void AcceptAsyncV6BoundToAnyV6_Success()
{
DualModeConnect_AcceptAsync_Helper(IPAddress.IPv6Any, IPAddress.IPv6Loopback);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Binds to a specific port on 'connectTo' which on Unix may already be in use
public void AcceptAsyncV6BoundToSpecificV4_CantConnect()
{
Assert.Throws<SocketException>(() =>
{
DualModeConnect_AcceptAsync_Helper(IPAddress.Loopback, IPAddress.IPv6Loopback);
});
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Binds to a specific port on 'connectTo' which on Unix may already be in use
public void AcceptAsyncV4BoundToSpecificV6_CantConnect()
{
Assert.Throws<SocketException>(() =>
{
DualModeConnect_AcceptAsync_Helper(IPAddress.IPv6Loopback, IPAddress.Loopback);
});
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Binds to a specific port on 'connectTo' which on Unix may already be in use
public void AcceptAsyncV6BoundToAnyV4_CantConnect()
{
Assert.Throws<SocketException>(() =>
{
DualModeConnect_AcceptAsync_Helper(IPAddress.Any, IPAddress.IPv6Loopback);
});
}
[Fact]
public void AcceptAsyncV4BoundToAnyV6_Success()
{
DualModeConnect_AcceptAsync_Helper(IPAddress.IPv6Any, IPAddress.Loopback);
}
private void DualModeConnect_AcceptAsync_Helper(IPAddress listenOn, IPAddress connectTo)
{
using (Socket serverSocket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
int port = serverSocket.BindToAnonymousPort(listenOn);
serverSocket.Listen(1);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += AsyncCompleted;
ManualResetEvent waitHandle = new ManualResetEvent(false);
args.UserToken = waitHandle;
args.SocketError = SocketError.SocketError;
_log.WriteLine(args.GetHashCode() + " SocketAsyncEventArgs with manual event " + waitHandle.GetHashCode());
if (!serverSocket.AcceptAsync(args))
{
throw new SocketException((int)args.SocketError);
}
SocketClient client = new SocketClient(_log, serverSocket, connectTo, port);
var waitHandles = new WaitHandle[2];
waitHandles[0] = waitHandle;
waitHandles[1] = client.WaitHandle;
int completedHandle = WaitHandle.WaitAny(waitHandles, TestSettings.PassingTestTimeout);
if (completedHandle == WaitHandle.WaitTimeout)
{
throw new TimeoutException("Timed out while waiting for either of client and server connections...");
}
if (completedHandle == 1) // Client finished
{
if (client.Error != SocketError.Success)
{
// Client SocketException
throw new SocketException((int)client.Error);
}
if (!waitHandle.WaitOne(5000)) // Now wait for the server.
{
throw new TimeoutException("Timed out while waiting for the server accept...");
}
}
_log.WriteLine(args.SocketError.ToString());
if (args.SocketError != SocketError.Success)
{
throw new SocketException((int)args.SocketError);
}
Socket clientSocket = args.AcceptSocket;
Assert.NotNull(clientSocket);
Assert.True(clientSocket.Connected);