Skip to content

Commit

Permalink
do not use 1.1.1.1 and similar
Browse files Browse the repository at this point in the history
  • Loading branch information
antonfirsov committed Oct 12, 2020
1 parent 512edf5 commit 80501f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public static (Socket, Socket) CreateConnectedSocketPair(IPAddress serverAddress
return (client, server);
}

public static int GetUnusedPort(AddressFamily addressFamily, ProtocolType protocolType)
{
using Socket temp = new Socket(addressFamily, protocolType == ProtocolType.Tcp ? SocketType.Stream : SocketType.Dgram, protocolType);
IPAddress address = addressFamily == AddressFamily.InterNetwork ? IPAddress.Loopback : IPAddress.IPv6Loopback;
return temp.BindToAnonymousPort(address);
}

// Tries to connect within the provided timeout interval
// Useful to speed up "can not connect" assertions on Windows
public static bool TryConnect(this Socket socket, EndPoint remoteEndpoint, int millisecondsTimeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ public async Task Connect_AfterDisconnect_Fails()

public static readonly TheoryData<IPAddress> ConnectGetsCanceledByDispose_Data = new TheoryData<IPAddress>
{
{ IPAddress.Parse("1.1.1.1") },
{ IPAddress.Parse("fd11:1:1:1:1:1:1:1") },
{ IPAddress.Parse("1.1.1.1").MapToIPv6() },
{ IPAddress.Loopback },
{ IPAddress.IPv6Loopback },
{ IPAddress.Loopback.MapToIPv6() },
};

[Theory]
[Theory(Timeout = 30000)]
[MemberData(nameof(ConnectGetsCanceledByDispose_Data))]
[PlatformSpecific(~(TestPlatforms.OSX | TestPlatforms.FreeBSD))] // Not supported on BSD like OSes.
public async Task ConnectGetsCanceledByDispose(IPAddress address)
Expand All @@ -140,8 +140,8 @@ public async Task ConnectGetsCanceledByDispose(IPAddress address)
{
var client = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
if (address.IsIPv4MappedToIPv6) client.DualMode = true;

Task connectTask = ConnectAsync(client, new IPEndPoint(address, 23));
int port = SocketTestExtensions.GetUnusedPort(address.AddressFamily, ProtocolType.Tcp);
Task connectTask = ConnectAsync(client, new IPEndPoint(address, port));

// Wait a little so the operation is started.
await Task.Delay(msDelay);
Expand Down

0 comments on commit 80501f9

Please sign in to comment.