From 84fb871ff1da5aca65c0508520bde9162ce6f514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Thu, 9 Sep 2021 13:40:05 +0200 Subject: [PATCH] System.Net.Ping: Skip TimeoutIsRespected test if network is unreachable (#58745) The TestSettings.UnreachableAddress is using 192.0.2.0 which is documented in RFC5735 as a network to be used in docs/example code. On the Android devices in Helix the upstream network is configured to not route this address which results in a "Destination Net Unreachable" response which causes the timeout argument to ping to be ignored and ping returns immediately. Skip the test in these cases. Example output from ping on the device: ``` 1|sunfish:/ $ time ping -c 1 -W 2 -s 50 192.0.2.0 PING 192.0.2.0 (192.0.2.0) 50(78) bytes of data. From 131.107.5.118: icmp_seq=1 Destination Net Unreachable --- 192.0.2.0 ping statistics --- 1 packets transmitted, 0 received, +1 errors, 100% packet loss, time Oms 0m00.03s real 0m00.00s user 0m00.00s system ``` --- .../FunctionalTests/UnixPingUtilityTests.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Ping/tests/FunctionalTests/UnixPingUtilityTests.cs b/src/libraries/System.Net.Ping/tests/FunctionalTests/UnixPingUtilityTests.cs index cafc675f80722..b34f45bdd881f 100644 --- a/src/libraries/System.Net.Ping/tests/FunctionalTests/UnixPingUtilityTests.cs +++ b/src/libraries/System.Net.Ping/tests/FunctionalTests/UnixPingUtilityTests.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Xunit; +using Microsoft.DotNet.XUnitExtensions; namespace System.Net.NetworkInformation.Tests { @@ -19,7 +20,7 @@ public class UnixPingUtilityTests { private const int IcmpHeaderLengthInBytes = 8; - [Theory] + [ConditionalTheory] [InlineData(0)] [InlineData(100)] [InlineData(1000)] @@ -32,11 +33,24 @@ public static void TimeoutIsRespected(int timeout) p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardOutput = true; + bool destinationNetUnreachable = false; + p.OutputDataReceived += delegate (object sendingProcess, DataReceivedEventArgs outputLine) + { + if (outputLine.Data?.Contains("Destination Net Unreachable", StringComparison.OrdinalIgnoreCase) == true) + destinationNetUnreachable = true; + }; + Stopwatch stopWatch = Stopwatch.StartNew(); p.Start(); + p.BeginOutputReadLine(); p.WaitForExit(); + if (destinationNetUnreachable) + { + throw new SkipTestException($"Network doesn't route {TestSettings.UnreachableAddress}, skipping test."); + } + //ensure that the process takes longer than or within 10ms of 'timeout', with a 5s maximum Assert.InRange(stopWatch.ElapsedMilliseconds, timeout - 10, 5000); }