Skip to content

Commit 3ae0664

Browse files
[release/8.0-staging] Harden Ping_TimedOut_* tests (#116631)
* harden Ping_TimedOut_* tests * improve comments --------- Co-authored-by: antonfirsov <[email protected]> Co-authored-by: Anton Firszov <[email protected]>
1 parent d39c236 commit 3ae0664

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

src/libraries/System.Net.Ping/tests/FunctionalTests/PingTest.cs

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -745,53 +745,64 @@ public async Task SendPingToExternalHostWithLowTtlTest()
745745
Assert.NotEqual(IPAddress.Any, pingReply.Address);
746746
}
747747

748-
[Fact]
749-
[OuterLoop]
750-
public void Ping_TimedOut_Sync_Success()
748+
private async Task Ping_TimedOut_Core(Func<Ping, string, Task<PingReply>> sendPing)
751749
{
752-
var sender = new Ping();
753-
PingReply reply = sender.Send(TestSettings.UnreachableAddress);
750+
Ping sender = new Ping();
751+
PingReply reply = await sendPing(sender, TestSettings.UnreachableAddress);
752+
if (reply.Status == IPStatus.DestinationNetworkUnreachable)
753+
{
754+
// A network middleware has dropped the packed and replied with DestinationNetworkUnreachable. Repeat the PING attempt on another address.
755+
reply = await sendPing(sender, TestSettings.UnreachableAddress2);
756+
}
757+
758+
if (reply.Status == IPStatus.DestinationNetworkUnreachable)
759+
{
760+
// Do yet another attempt.
761+
reply = await sendPing(sender, TestSettings.UnreachableAddress3);
762+
}
763+
754764
Assert.Equal(IPStatus.TimedOut, reply.Status);
755765
}
756766

757767
[Fact]
758768
[OuterLoop]
759-
public async Task Ping_TimedOut_EAP_Success()
760-
{
761-
var sender = new Ping();
762-
sender.PingCompleted += (s, e) =>
763-
{
764-
var tcs = (TaskCompletionSource<PingReply>)e.UserState;
769+
public Task Ping_TimedOut_Sync_Success()
770+
=> Ping_TimedOut_Core((sender, address) => Task.Run(() => sender.Send(address)));
765771

766-
if (e.Cancelled)
767-
{
768-
tcs.TrySetCanceled();
769-
}
770-
else if (e.Error != null)
771-
{
772-
tcs.TrySetException(e.Error);
773-
}
774-
else
772+
[Fact]
773+
[OuterLoop]
774+
public Task Ping_TimedOut_EAP_Success()
775+
=> Ping_TimedOut_Core(async (sender, address) =>
776+
{
777+
static void PingCompleted(object sender, PingCompletedEventArgs e)
775778
{
776-
tcs.TrySetResult(e.Reply);
777-
}
778-
};
779+
var tcs = (TaskCompletionSource<PingReply>)e.UserState;
779780

780-
var tcs = new TaskCompletionSource<PingReply>();
781-
sender.SendAsync(TestSettings.UnreachableAddress, tcs);
782-
783-
PingReply reply = await tcs.Task;
784-
Assert.Equal(IPStatus.TimedOut, reply.Status);
785-
}
781+
if (e.Cancelled)
782+
{
783+
tcs.TrySetCanceled();
784+
}
785+
else if (e.Error != null)
786+
{
787+
tcs.TrySetException(e.Error);
788+
}
789+
else
790+
{
791+
tcs.TrySetResult(e.Reply);
792+
}
793+
}
794+
sender.PingCompleted += PingCompleted;
795+
var tcs = new TaskCompletionSource<PingReply>();
796+
sender.SendAsync(address, tcs);
797+
PingReply reply = await tcs.Task;
798+
sender.PingCompleted -= PingCompleted;
799+
return reply;
800+
});
786801

787802
[Fact]
788803
[OuterLoop]
789-
public async Task Ping_TimedOut_TAP_Success()
790-
{
791-
var sender = new Ping();
792-
PingReply reply = await sender.SendPingAsync(TestSettings.UnreachableAddress);
793-
Assert.Equal(IPStatus.TimedOut, reply.Status);
794-
}
804+
public Task Ping_TimedOut_TAP_Success()
805+
=> Ping_TimedOut_Core((sender, address) => sender.SendPingAsync(address));
795806

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

src/libraries/System.Net.Ping/tests/FunctionalTests/TestSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ internal static class TestSettings
1111
{
1212
public static readonly string LocalHost = "localhost";
1313
public static readonly string UnreachableAddress = "192.0.2.0"; // TEST-NET-1
14+
public static readonly string UnreachableAddress2 = "100.64.0.1"; // CGNAT block
15+
public static readonly string UnreachableAddress3 = "10.255.255.1"; // High address in the private 10.0.0.0/8 range. Likely unused and unrouted.
16+
1417
public const int PingTimeout = 10 * 1000;
1518

1619
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.";

0 commit comments

Comments
 (0)