Skip to content

[release/8.0-staging] Harden Ping_TimedOut_* tests #116631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: release/8.0-staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 46 additions & 35 deletions src/libraries/System.Net.Ping/tests/FunctionalTests/PingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -745,53 +745,64 @@ public async Task SendPingToExternalHostWithLowTtlTest()
Assert.NotEqual(IPAddress.Any, pingReply.Address);
}

[Fact]
[OuterLoop]
public void Ping_TimedOut_Sync_Success()
private async Task Ping_TimedOut_Core(Func<Ping, string, Task<PingReply>> sendPing)
{
var sender = new Ping();
PingReply reply = sender.Send(TestSettings.UnreachableAddress);
Ping sender = new Ping();
PingReply reply = await sendPing(sender, TestSettings.UnreachableAddress);
if (reply.Status == IPStatus.DestinationNetworkUnreachable)
{
// A network middleware has dropped the packed and replied with DestinationNetworkUnreachable. Repeat the PING attempt on another address.
reply = await sendPing(sender, TestSettings.UnreachableAddress2);
}

if (reply.Status == IPStatus.DestinationNetworkUnreachable)
{
// Do yet another attempt.
reply = await sendPing(sender, TestSettings.UnreachableAddress3);
}

Assert.Equal(IPStatus.TimedOut, reply.Status);
}

[Fact]
[OuterLoop]
public async Task Ping_TimedOut_EAP_Success()
{
var sender = new Ping();
sender.PingCompleted += (s, e) =>
{
var tcs = (TaskCompletionSource<PingReply>)e.UserState;
public Task Ping_TimedOut_Sync_Success()
=> Ping_TimedOut_Core((sender, address) => Task.Run(() => sender.Send(address)));

if (e.Cancelled)
{
tcs.TrySetCanceled();
}
else if (e.Error != null)
{
tcs.TrySetException(e.Error);
}
else
[Fact]
[OuterLoop]
public Task Ping_TimedOut_EAP_Success()
=> Ping_TimedOut_Core(async (sender, address) =>
{
static void PingCompleted(object sender, PingCompletedEventArgs e)
{
tcs.TrySetResult(e.Reply);
}
};
var tcs = (TaskCompletionSource<PingReply>)e.UserState;

var tcs = new TaskCompletionSource<PingReply>();
sender.SendAsync(TestSettings.UnreachableAddress, tcs);

PingReply reply = await tcs.Task;
Assert.Equal(IPStatus.TimedOut, reply.Status);
}
if (e.Cancelled)
{
tcs.TrySetCanceled();
}
else if (e.Error != null)
{
tcs.TrySetException(e.Error);
}
else
{
tcs.TrySetResult(e.Reply);
}
}
sender.PingCompleted += PingCompleted;
var tcs = new TaskCompletionSource<PingReply>();
sender.SendAsync(address, tcs);
PingReply reply = await tcs.Task;
sender.PingCompleted -= PingCompleted;
return reply;
});

[Fact]
[OuterLoop]
public async Task Ping_TimedOut_TAP_Success()
{
var sender = new Ping();
PingReply reply = await sender.SendPingAsync(TestSettings.UnreachableAddress);
Assert.Equal(IPStatus.TimedOut, reply.Status);
}
public Task Ping_TimedOut_TAP_Success()
=> Ping_TimedOut_Core((sender, address) => sender.SendPingAsync(address));

private static bool IsRemoteExecutorSupportedAndPrivilegedProcess => RemoteExecutor.IsSupported && PlatformDetection.IsPrivilegedProcess;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ internal static class TestSettings
{
public static readonly string LocalHost = "localhost";
public static readonly string UnreachableAddress = "192.0.2.0"; // TEST-NET-1
public static readonly string UnreachableAddress2 = "100.64.0.1"; // CGNAT block
public static readonly string UnreachableAddress3 = "10.255.255.1"; // High address in the private 10.0.0.0/8 range. Likely unused and unrouted.

public const int PingTimeout = 10 * 1000;

public const string PayloadAsString = "'Post hoc ergo propter hoc'. 'After it, therefore because of it'. It means one thing follows the other, therefore it was caused by the other. But it's not always true. In fact it's hardly ever true.";
Expand Down
Loading