Skip to content

Commit 2ad4d51

Browse files
committed
Consolidate tests
1 parent 68d3699 commit 2ad4d51

File tree

3 files changed

+23
-147
lines changed

3 files changed

+23
-147
lines changed

src/libraries/System.Net.Ping/src/Resources/Strings.resx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@
8888
<value>System.Net.Ping is not supported on this platform.</value>
8989
</data>
9090
<data name="net_ping_utility_custom_payload" xml:space="preserve">
91-
<value>Unable to send custom ping payload. Run program with privileged user or grant cap_net_raw capability using setcap(8).</value>
91+
<value>Unable to send custom ping payload. Run program under privileged user account or grant cap_net_raw capability using setcap(8).</value>
9292
</data>
93-
</root>
93+
</root>

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public PingReply Send(IPAddress address, int timeout, byte[] buffer, PingOptions
227227
{
228228
return SendPingCore(addressSnapshot, buffer, timeout, options);
229229
}
230-
catch (Exception e)
230+
catch (Exception e) when (e is not PlatformNotSupportedException)
231231
{
232232
throw new PingException(SR.net_ping, e);
233233
}
@@ -336,7 +336,7 @@ private async Task<PingReply> SendPingAsyncInternal(IPAddress address, int timeo
336336
Task<PingReply> pingReplyTask = SendPingAsyncCore(addressSnapshot, buffer, timeout, options);
337337
return await pingReplyTask.ConfigureAwait(false);
338338
}
339-
catch (Exception e)
339+
catch (Exception e) when (e is not PlatformNotSupportedException)
340340
{
341341
throw new PingException(SR.net_ping, e);
342342
}
@@ -388,7 +388,7 @@ private PingReply GetAddressAndSend(string hostNameOrAddress, int timeout, byte[
388388
IPAddress[] addresses = Dns.GetHostAddresses(hostNameOrAddress);
389389
return SendPingCore(addresses[0], buffer, timeout, options);
390390
}
391-
catch (Exception e)
391+
catch (Exception e) when (e is not PlatformNotSupportedException)
392392
{
393393
throw new PingException(SR.net_ping, e);
394394
}
@@ -407,7 +407,7 @@ private async Task<PingReply> GetAddressAndSendAsync(string hostNameOrAddress, i
407407
Task<PingReply> pingReplyTask = SendPingAsyncCore(addresses[0], buffer, timeout, options);
408408
return await pingReplyTask.ConfigureAwait(false);
409409
}
410-
catch (Exception e)
410+
catch (Exception e) when (e is not PlatformNotSupportedException)
411411
{
412412
throw new PingException(SR.net_ping, e);
413413
}

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

Lines changed: 17 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ private static void PingResultValidator(PingReply pingReply, IPAddress[] localIp
7272
Assert.Contains(pingReply.Address, localIpAddresses); ///, "Reply address {pingReply.Address} is not expected local address.");
7373
}
7474

75-
private static byte[] GetPingPayloadForUnix(AddressFamily addressFamily)
75+
private static byte[] GetPingPayload(AddressFamily addressFamily)
7676
// On Unix, Non-root processes cannot send arbitrary data in the ping packet payload
7777
=> Capability.CanUseRawSockets(addressFamily) || PlatformDetection.IsOSXLike
7878
? TestSettings.PayloadAsBytes
7979
: Array.Empty<byte>();
8080

81+
public static bool DoesNotUsePingUtility => !UsesPingUtility;
82+
83+
public static bool UsesPingUtility => OperatingSystem.IsLinux() && !(Capability.CanUseRawSockets(TestSettings.GetLocalIPAddress().AddressFamily) || PlatformDetection.IsOSXLike);
84+
8185
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
8286
public async Task SendPingAsync_InvalidArgs()
8387
{
@@ -224,12 +228,11 @@ await SendBatchPingAsync(
224228
});
225229
}
226230

227-
[PlatformSpecific(TestPlatforms.Windows)]
228231
[Fact]
229232
public void SendPingWithIPAddressAndTimeoutAndBuffer()
230233
{
231-
byte[] buffer = TestSettings.PayloadAsBytes;
232234
IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
235+
byte[] buffer = GetPingPayload(localIpAddress.AddressFamily);
233236

234237
SendBatchPing(
235238
(ping) => ping.Send(localIpAddress, TestSettings.PingTimeout, buffer),
@@ -240,46 +243,11 @@ public void SendPingWithIPAddressAndTimeoutAndBuffer()
240243
});
241244
}
242245

243-
[PlatformSpecific(TestPlatforms.Windows)]
244246
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
245247
public async Task SendPingAsyncWithIPAddressAndTimeoutAndBuffer()
246248
{
247-
byte[] buffer = TestSettings.PayloadAsBytes;
248249
IPAddress localIpAddress = await TestSettings.GetLocalIPAddressAsync();
249-
250-
await SendBatchPingAsync(
251-
(ping) => ping.SendPingAsync(localIpAddress, TestSettings.PingTimeout, buffer),
252-
(pingReply) =>
253-
{
254-
PingResultValidator(pingReply, localIpAddress);
255-
Assert.Equal(buffer, pingReply.Buffer);
256-
});
257-
}
258-
259-
[PlatformSpecific(TestPlatforms.AnyUnix)]
260-
[Fact]
261-
public void SendPingWithIPAddressAndTimeoutAndBuffer_Unix()
262-
{
263-
IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
264-
265-
byte[] buffer = GetPingPayloadForUnix(localIpAddress.AddressFamily);
266-
267-
SendBatchPing(
268-
(ping) => ping.Send(localIpAddress, TestSettings.PingTimeout, buffer),
269-
(pingReply) =>
270-
{
271-
PingResultValidator(pingReply, localIpAddress);
272-
Assert.Equal(buffer, pingReply.Buffer);
273-
});
274-
}
275-
276-
[PlatformSpecific(TestPlatforms.AnyUnix)]
277-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
278-
public async Task SendPingAsyncWithIPAddressAndTimeoutAndBuffer_Unix()
279-
{
280-
IPAddress localIpAddress = await TestSettings.GetLocalIPAddressAsync();
281-
282-
byte[] buffer = GetPingPayloadForUnix(localIpAddress.AddressFamily);
250+
byte[] buffer = GetPingPayload(localIpAddress.AddressFamily);
283251

284252
await SendBatchPingAsync(
285253
(ping) => ping.SendPingAsync(localIpAddress, TestSettings.PingTimeout, buffer),
@@ -339,7 +307,7 @@ public void SendPingWithIPAddressAndTimeoutAndBufferAndPingOptions_Unix(AddressF
339307
return;
340308
}
341309

342-
byte[] buffer = GetPingPayloadForUnix(localIpAddress.AddressFamily);
310+
byte[] buffer = GetPingPayload(localIpAddress.AddressFamily);
343311

344312
SendBatchPing(
345313
(ping) => ping.Send(localIpAddress, TestSettings.PingTimeout, buffer, new PingOptions()),
@@ -363,7 +331,7 @@ public async Task SendPingAsyncWithIPAddressAndTimeoutAndBufferAndPingOptions_Un
363331
return;
364332
}
365333

366-
byte[] buffer = GetPingPayloadForUnix(localIpAddress.AddressFamily);
334+
byte[] buffer = GetPingPayload(localIpAddress.AddressFamily);
367335

368336
await SendBatchPingAsync(
369337
(ping) => ping.SendPingAsync(localIpAddress, TestSettings.PingTimeout, buffer, new PingOptions()),
@@ -426,13 +394,12 @@ await SendBatchPingAsync(
426394
});
427395
}
428396

429-
[PlatformSpecific(TestPlatforms.Windows)]
430397
[Fact]
431398
public void SendPingWithHostAndTimeoutAndBuffer()
432399
{
433400
IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
401+
byte[] buffer = GetPingPayload(localIpAddress.AddressFamily);
434402

435-
byte[] buffer = TestSettings.PayloadAsBytes;
436403
SendBatchPing(
437404
(ping) => ping.Send(TestSettings.LocalHost, TestSettings.PingTimeout, buffer),
438405
(pingReply) =>
@@ -442,13 +409,12 @@ public void SendPingWithHostAndTimeoutAndBuffer()
442409
});
443410
}
444411

445-
[PlatformSpecific(TestPlatforms.Windows)]
446412
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
447413
public async Task SendPingAsyncWithHostAndTimeoutAndBuffer()
448414
{
449415
IPAddress localIpAddress = await TestSettings.GetLocalIPAddressAsync();
416+
byte[] buffer = GetPingPayload(localIpAddress.AddressFamily);
450417

451-
byte[] buffer = TestSettings.PayloadAsBytes;
452418
await SendBatchPingAsync(
453419
(ping) => ping.SendPingAsync(TestSettings.LocalHost, TestSettings.PingTimeout, buffer),
454420
(pingReply) =>
@@ -458,64 +424,27 @@ await SendBatchPingAsync(
458424
});
459425
}
460426

461-
[PlatformSpecific(TestPlatforms.AnyUnix)]
462-
[Fact]
463-
public void SendPingWithHostAndTimeoutAndBuffer_Unix()
464-
{
465-
IPAddress[] localIpAddresses = TestSettings.GetLocalIPAddresses();
466-
467-
byte[] buffer = GetPingPayloadForUnix(localIpAddresses[0].AddressFamily);
468-
469-
SendBatchPing(
470-
(ping) => ping.Send(TestSettings.LocalHost, TestSettings.PingTimeout, buffer),
471-
(pingReply) =>
472-
{
473-
PingResultValidator(pingReply, localIpAddresses);
474-
Assert.Equal(buffer, pingReply.Buffer);
475-
});
476-
}
477-
478-
[PlatformSpecific(TestPlatforms.AnyUnix)]
479-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
480-
public async Task SendPingAsyncWithHostAndTimeoutAndBuffer_Unix()
481-
{
482-
IPAddress[] localIpAddresses = await TestSettings.GetLocalIPAddressesAsync();
483-
484-
byte[] buffer = GetPingPayloadForUnix(localIpAddresses[0].AddressFamily);
485-
486-
await SendBatchPingAsync(
487-
(ping) => ping.SendPingAsync(TestSettings.LocalHost, TestSettings.PingTimeout, buffer),
488-
(pingReply) =>
489-
{
490-
PingResultValidator(pingReply, localIpAddresses);
491-
Assert.Equal(buffer, pingReply.Buffer);
492-
});
493-
}
494-
495-
[PlatformSpecific(TestPlatforms.Windows)]
496427
[Fact]
497428
public void SendPingWithHostAndTimeoutAndBufferAndPingOptions()
498429
{
499430
IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
431+
byte[] buffer = GetPingPayload(localIpAddress.AddressFamily);
500432

501-
byte[] buffer = TestSettings.PayloadAsBytes;
502433
SendBatchPing(
503434
(ping) => ping.Send(TestSettings.LocalHost, TestSettings.PingTimeout, buffer, new PingOptions()),
504435
(pingReply) =>
505436
{
506437
PingResultValidator(pingReply, localIpAddress);
507-
508438
Assert.Equal(buffer, pingReply.Buffer);
509439
});
510440
}
511441

512-
[PlatformSpecific(TestPlatforms.Windows)]
513442
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
514443
public async Task SendPingAsyncWithHostAndTimeoutAndBufferAndPingOptions()
515444
{
516445
IPAddress localIpAddress = await TestSettings.GetLocalIPAddressAsync();
446+
byte[] buffer = GetPingPayload(localIpAddress.AddressFamily);
517447

518-
byte[] buffer = TestSettings.PayloadAsBytes;
519448
await SendBatchPingAsync(
520449
(ping) => ping.SendPingAsync(TestSettings.LocalHost, TestSettings.PingTimeout, buffer, new PingOptions()),
521450
(pingReply) =>
@@ -526,50 +455,11 @@ await SendBatchPingAsync(
526455
});
527456
}
528457

529-
[PlatformSpecific(TestPlatforms.AnyUnix)]
530-
[Fact]
531-
public void SendPingWithHostAndTimeoutAndBufferAndPingOptions_Unix()
532-
{
533-
IPAddress[] localIpAddresses = TestSettings.GetLocalIPAddresses();
534-
535-
byte[] buffer = GetPingPayloadForUnix(localIpAddresses[0].AddressFamily);
536-
537-
SendBatchPing(
538-
(ping) => ping.Send(TestSettings.LocalHost, TestSettings.PingTimeout, buffer, new PingOptions()),
539-
(pingReply) =>
540-
{
541-
PingResultValidator(pingReply, localIpAddresses);
542-
Assert.Equal(buffer, pingReply.Buffer);
543-
});
544-
}
545-
546-
[PlatformSpecific(TestPlatforms.AnyUnix)]
547-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
548-
public async Task SendPingAsyncWithHostAndTimeoutAndBufferAndPingOptions_Unix()
549-
{
550-
IPAddress[] localIpAddresses = await TestSettings.GetLocalIPAddressesAsync();
551-
552-
byte[] buffer = GetPingPayloadForUnix(localIpAddresses[0].AddressFamily);
553-
554-
await SendBatchPingAsync(
555-
(ping) => ping.SendPingAsync(TestSettings.LocalHost, TestSettings.PingTimeout, buffer, new PingOptions()),
556-
(pingReply) =>
557-
{
558-
PingResultValidator(pingReply, localIpAddresses);
559-
Assert.Equal(buffer, pingReply.Buffer);
560-
});
561-
}
562-
563-
[Fact]
458+
[ConditionalFact(nameof(DoesNotUsePingUtility))]
564459
public async Task SendPingWithIPAddressAndBigSize()
565460
{
566461
IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
567462

568-
if (!(Capability.CanUseRawSockets(localIpAddress.AddressFamily) || PlatformDetection.IsOSXLike))
569-
{
570-
throw new SkipTestException("Underprivileged process cannot specify ping payload.");
571-
}
572-
573463
using (Ping p = new Ping())
574464
{
575465
// Assert.DoesNotThrow
@@ -757,7 +647,7 @@ public async Task SendPingAsyncWithHostAndTtlAndFragmentPingOptions(bool fragmen
757647
{
758648
IPAddress[] localIpAddresses = await TestSettings.GetLocalIPAddressesAsync();
759649

760-
byte[] buffer = GetPingPayloadForUnix(localIpAddresses[0].AddressFamily);
650+
byte[] buffer = GetPingPayload(localIpAddresses[0].AddressFamily);
761651

762652
PingOptions options = new PingOptions();
763653
options.Ttl = 32;
@@ -948,35 +838,21 @@ await SendBatchPingAsync(
948838
}, localIpAddress.ToString(), new RemoteInvokeOptions { StartInfo = remoteInvokeStartInfo }).Dispose();
949839
}
950840

951-
[PlatformSpecific(TestPlatforms.AnyUnix)]
952-
[Fact]
841+
[ConditionalFact(nameof(UsesPingUtility))]
953842
public void SendPing_CustomPayload_InsufficientPrivileges_Throws()
954843
{
955844
IPAddress[] localIpAddresses = TestSettings.GetLocalIPAddresses();
956845

957-
if (!(Capability.CanUseRawSockets(localIpAddresses[0].AddressFamily) || PlatformDetection.IsOSXLike))
958-
{
959-
// running on sufficiently privileged process
960-
return;
961-
}
962-
963846
byte[] buffer = TestSettings.PayloadAsBytes;
964847
Ping ping = new Ping();
965848
Assert.Throws<PlatformNotSupportedException>(() => ping.Send(TestSettings.LocalHost, TestSettings.PingTimeout, buffer));
966849
}
967850

968-
[PlatformSpecific(TestPlatforms.AnyUnix)]
969-
[Fact]
851+
[ConditionalFact(nameof(UsesPingUtility))]
970852
public async Task SendPingAsync_CustomPayload_InsufficientPrivileges_Throws()
971853
{
972854
IPAddress[] localIpAddresses = TestSettings.GetLocalIPAddresses();
973855

974-
if (!(Capability.CanUseRawSockets(localIpAddresses[0].AddressFamily) || PlatformDetection.IsOSXLike))
975-
{
976-
// running on sufficiently privileged process
977-
return;
978-
}
979-
980856
byte[] buffer = TestSettings.PayloadAsBytes;
981857
Ping ping = new Ping();
982858
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => ping.SendPingAsync(TestSettings.LocalHost, TestSettings.PingTimeout, buffer));

0 commit comments

Comments
 (0)