1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Threading.Tasks; 6 using Xunit; 7 8 namespace System.Net.Sockets.Tests 9 { 10 public abstract class Connect<T> : SocketTestHelperBase<T> where T : SocketHelperBase, new() 11 { 12 [OuterLoop] // TODO: Issue #11345 13 [Theory] 14 [MemberData(nameof(Loopbacks))] Connect_Success(IPAddress listenAt)15 public async Task Connect_Success(IPAddress listenAt) 16 { 17 int port; 18 using (SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, listenAt, out port)) 19 { 20 using (Socket client = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) 21 { 22 Task connectTask = ConnectAsync(client, new IPEndPoint(listenAt, port)); 23 await connectTask; 24 Assert.True(client.Connected); 25 } 26 } 27 } 28 29 [OuterLoop] // TODO: Issue #11345 30 [Theory] 31 [MemberData(nameof(Loopbacks))] Connect_MultipleIPAddresses_Success(IPAddress listenAt)32 public async Task Connect_MultipleIPAddresses_Success(IPAddress listenAt) 33 { 34 if (!SupportsMultiConnect) 35 return; 36 37 int port; 38 using (SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, listenAt, out port)) 39 using (Socket client = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) 40 { 41 Task connectTask = MultiConnectAsync(client, new IPAddress[] { IPAddress.Loopback, IPAddress.IPv6Loopback }, port); 42 await connectTask; 43 Assert.True(client.Connected); 44 } 45 } 46 47 [OuterLoop] // TODO: Issue #11345 48 [Fact] 49 [ActiveIssue(22765, TestPlatforms.AnyUnix)] Connect_OnConnectedSocket_Fails()50 public async Task Connect_OnConnectedSocket_Fails() 51 { 52 int port; 53 using (SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, IPAddress.Loopback, out port)) 54 using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) 55 { 56 await ConnectAsync(client, new IPEndPoint(IPAddress.Loopback, port)); 57 58 // In the sync case, we throw a derived exception here, so need to use ThrowsAnyAsync 59 SocketException se = await Assert.ThrowsAnyAsync<SocketException>(() => ConnectAsync(client, new IPEndPoint(IPAddress.Loopback, port))); 60 Assert.Equal(SocketError.IsConnected, se.SocketErrorCode); 61 } 62 } 63 64 [PlatformSpecific(TestPlatforms.Windows)] // Unix currently does not support Disconnect 65 [OuterLoop] // TODO: Issue #11345 66 [Fact] Connect_AfterDisconnect_Fails()67 public async Task Connect_AfterDisconnect_Fails() 68 { 69 int port; 70 using (SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, IPAddress.Loopback, out port)) 71 using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) 72 { 73 await ConnectAsync(client, new IPEndPoint(IPAddress.Loopback, port)); 74 client.Disconnect(reuseSocket: false); 75 76 if (ConnectAfterDisconnectResultsInInvalidOperationException) 77 { 78 await Assert.ThrowsAsync<InvalidOperationException>(() => ConnectAsync(client, new IPEndPoint(IPAddress.Loopback, port))); 79 } 80 else 81 { 82 SocketException se = await Assert.ThrowsAsync<SocketException>(() => ConnectAsync(client, new IPEndPoint(IPAddress.Loopback, port))); 83 Assert.Equal(SocketError.IsConnected, se.SocketErrorCode); 84 } 85 } 86 } 87 } 88 89 public sealed class ConnectSync : Connect<SocketHelperArraySync> { } 90 public sealed class ConnectSyncForceNonBlocking : Connect<SocketHelperSyncForceNonBlocking> { } 91 public sealed class ConnectApm : Connect<SocketHelperApm> { } 92 public sealed class ConnectTask : Connect<SocketHelperTask> { } 93 public sealed class ConnectEap : Connect<SocketHelperEap> { } 94 } 95