@@ -72,12 +72,16 @@ private static void PingResultValidator(PingReply pingReply, IPAddress[] localIp
72
72
Assert . Contains ( pingReply . Address , localIpAddresses ) ; ///, "Reply address {pingReply.Address} is not expected local address.");
73
73
}
74
74
75
- private static byte [ ] GetPingPayloadForUnix ( AddressFamily addressFamily )
75
+ private static byte [ ] GetPingPayload ( AddressFamily addressFamily )
76
76
// On Unix, Non-root processes cannot send arbitrary data in the ping packet payload
77
77
=> Capability . CanUseRawSockets ( addressFamily ) || PlatformDetection . IsOSXLike
78
78
? TestSettings . PayloadAsBytes
79
79
: Array . Empty < byte > ( ) ;
80
80
81
+ public static bool DoesNotUsePingUtility => ! UsesPingUtility ;
82
+
83
+ public static bool UsesPingUtility => OperatingSystem . IsLinux ( ) && ! ( Capability . CanUseRawSockets ( TestSettings . GetLocalIPAddress ( ) . AddressFamily ) || PlatformDetection . IsOSXLike ) ;
84
+
81
85
[ ConditionalFact ( typeof ( PlatformDetection ) , nameof ( PlatformDetection . IsThreadingSupported ) ) ]
82
86
public async Task SendPingAsync_InvalidArgs ( )
83
87
{
@@ -224,12 +228,11 @@ await SendBatchPingAsync(
224
228
} ) ;
225
229
}
226
230
227
- [ PlatformSpecific ( TestPlatforms . Windows ) ]
228
231
[ Fact ]
229
232
public void SendPingWithIPAddressAndTimeoutAndBuffer ( )
230
233
{
231
- byte [ ] buffer = TestSettings . PayloadAsBytes ;
232
234
IPAddress localIpAddress = TestSettings . GetLocalIPAddress ( ) ;
235
+ byte [ ] buffer = GetPingPayload ( localIpAddress . AddressFamily ) ;
233
236
234
237
SendBatchPing (
235
238
( ping ) => ping . Send ( localIpAddress , TestSettings . PingTimeout , buffer ) ,
@@ -240,46 +243,11 @@ public void SendPingWithIPAddressAndTimeoutAndBuffer()
240
243
} ) ;
241
244
}
242
245
243
- [ PlatformSpecific ( TestPlatforms . Windows ) ]
244
246
[ ConditionalFact ( typeof ( PlatformDetection ) , nameof ( PlatformDetection . IsThreadingSupported ) ) ]
245
247
public async Task SendPingAsyncWithIPAddressAndTimeoutAndBuffer ( )
246
248
{
247
- byte [ ] buffer = TestSettings . PayloadAsBytes ;
248
249
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 ) ;
283
251
284
252
await SendBatchPingAsync (
285
253
( ping ) => ping . SendPingAsync ( localIpAddress , TestSettings . PingTimeout , buffer ) ,
@@ -339,7 +307,7 @@ public void SendPingWithIPAddressAndTimeoutAndBufferAndPingOptions_Unix(AddressF
339
307
return ;
340
308
}
341
309
342
- byte [ ] buffer = GetPingPayloadForUnix ( localIpAddress . AddressFamily ) ;
310
+ byte [ ] buffer = GetPingPayload ( localIpAddress . AddressFamily ) ;
343
311
344
312
SendBatchPing (
345
313
( ping ) => ping . Send ( localIpAddress , TestSettings . PingTimeout , buffer , new PingOptions ( ) ) ,
@@ -363,7 +331,7 @@ public async Task SendPingAsyncWithIPAddressAndTimeoutAndBufferAndPingOptions_Un
363
331
return ;
364
332
}
365
333
366
- byte [ ] buffer = GetPingPayloadForUnix ( localIpAddress . AddressFamily ) ;
334
+ byte [ ] buffer = GetPingPayload ( localIpAddress . AddressFamily ) ;
367
335
368
336
await SendBatchPingAsync (
369
337
( ping ) => ping . SendPingAsync ( localIpAddress , TestSettings . PingTimeout , buffer , new PingOptions ( ) ) ,
@@ -426,13 +394,12 @@ await SendBatchPingAsync(
426
394
} ) ;
427
395
}
428
396
429
- [ PlatformSpecific ( TestPlatforms . Windows ) ]
430
397
[ Fact ]
431
398
public void SendPingWithHostAndTimeoutAndBuffer ( )
432
399
{
433
400
IPAddress localIpAddress = TestSettings . GetLocalIPAddress ( ) ;
401
+ byte [ ] buffer = GetPingPayload ( localIpAddress . AddressFamily ) ;
434
402
435
- byte [ ] buffer = TestSettings . PayloadAsBytes ;
436
403
SendBatchPing (
437
404
( ping ) => ping . Send ( TestSettings . LocalHost , TestSettings . PingTimeout , buffer ) ,
438
405
( pingReply ) =>
@@ -442,13 +409,12 @@ public void SendPingWithHostAndTimeoutAndBuffer()
442
409
} ) ;
443
410
}
444
411
445
- [ PlatformSpecific ( TestPlatforms . Windows ) ]
446
412
[ ConditionalFact ( typeof ( PlatformDetection ) , nameof ( PlatformDetection . IsThreadingSupported ) ) ]
447
413
public async Task SendPingAsyncWithHostAndTimeoutAndBuffer ( )
448
414
{
449
415
IPAddress localIpAddress = await TestSettings . GetLocalIPAddressAsync ( ) ;
416
+ byte [ ] buffer = GetPingPayload ( localIpAddress . AddressFamily ) ;
450
417
451
- byte [ ] buffer = TestSettings . PayloadAsBytes ;
452
418
await SendBatchPingAsync (
453
419
( ping ) => ping . SendPingAsync ( TestSettings . LocalHost , TestSettings . PingTimeout , buffer ) ,
454
420
( pingReply ) =>
@@ -458,64 +424,27 @@ await SendBatchPingAsync(
458
424
} ) ;
459
425
}
460
426
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 ) ]
496
427
[ Fact ]
497
428
public void SendPingWithHostAndTimeoutAndBufferAndPingOptions ( )
498
429
{
499
430
IPAddress localIpAddress = TestSettings . GetLocalIPAddress ( ) ;
431
+ byte [ ] buffer = GetPingPayload ( localIpAddress . AddressFamily ) ;
500
432
501
- byte [ ] buffer = TestSettings . PayloadAsBytes ;
502
433
SendBatchPing (
503
434
( ping ) => ping . Send ( TestSettings . LocalHost , TestSettings . PingTimeout , buffer , new PingOptions ( ) ) ,
504
435
( pingReply ) =>
505
436
{
506
437
PingResultValidator ( pingReply , localIpAddress ) ;
507
-
508
438
Assert . Equal ( buffer , pingReply . Buffer ) ;
509
439
} ) ;
510
440
}
511
441
512
- [ PlatformSpecific ( TestPlatforms . Windows ) ]
513
442
[ ConditionalFact ( typeof ( PlatformDetection ) , nameof ( PlatformDetection . IsThreadingSupported ) ) ]
514
443
public async Task SendPingAsyncWithHostAndTimeoutAndBufferAndPingOptions ( )
515
444
{
516
445
IPAddress localIpAddress = await TestSettings . GetLocalIPAddressAsync ( ) ;
446
+ byte [ ] buffer = GetPingPayload ( localIpAddress . AddressFamily ) ;
517
447
518
- byte [ ] buffer = TestSettings . PayloadAsBytes ;
519
448
await SendBatchPingAsync (
520
449
( ping ) => ping . SendPingAsync ( TestSettings . LocalHost , TestSettings . PingTimeout , buffer , new PingOptions ( ) ) ,
521
450
( pingReply ) =>
@@ -526,50 +455,11 @@ await SendBatchPingAsync(
526
455
} ) ;
527
456
}
528
457
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 ) ) ]
564
459
public async Task SendPingWithIPAddressAndBigSize ( )
565
460
{
566
461
IPAddress localIpAddress = TestSettings . GetLocalIPAddress ( ) ;
567
462
568
- if ( ! ( Capability . CanUseRawSockets ( localIpAddress . AddressFamily ) || PlatformDetection . IsOSXLike ) )
569
- {
570
- throw new SkipTestException ( "Underprivileged process cannot specify ping payload." ) ;
571
- }
572
-
573
463
using ( Ping p = new Ping ( ) )
574
464
{
575
465
// Assert.DoesNotThrow
@@ -757,7 +647,7 @@ public async Task SendPingAsyncWithHostAndTtlAndFragmentPingOptions(bool fragmen
757
647
{
758
648
IPAddress [ ] localIpAddresses = await TestSettings . GetLocalIPAddressesAsync ( ) ;
759
649
760
- byte [ ] buffer = GetPingPayloadForUnix ( localIpAddresses [ 0 ] . AddressFamily ) ;
650
+ byte [ ] buffer = GetPingPayload ( localIpAddresses [ 0 ] . AddressFamily ) ;
761
651
762
652
PingOptions options = new PingOptions ( ) ;
763
653
options . Ttl = 32 ;
@@ -948,35 +838,21 @@ await SendBatchPingAsync(
948
838
} , localIpAddress . ToString ( ) , new RemoteInvokeOptions { StartInfo = remoteInvokeStartInfo } ) . Dispose ( ) ;
949
839
}
950
840
951
- [ PlatformSpecific ( TestPlatforms . AnyUnix ) ]
952
- [ Fact ]
841
+ [ ConditionalFact ( nameof ( UsesPingUtility ) ) ]
953
842
public void SendPing_CustomPayload_InsufficientPrivileges_Throws ( )
954
843
{
955
844
IPAddress [ ] localIpAddresses = TestSettings . GetLocalIPAddresses ( ) ;
956
845
957
- if ( ! ( Capability . CanUseRawSockets ( localIpAddresses [ 0 ] . AddressFamily ) || PlatformDetection . IsOSXLike ) )
958
- {
959
- // running on sufficiently privileged process
960
- return ;
961
- }
962
-
963
846
byte [ ] buffer = TestSettings . PayloadAsBytes ;
964
847
Ping ping = new Ping ( ) ;
965
848
Assert . Throws < PlatformNotSupportedException > ( ( ) => ping . Send ( TestSettings . LocalHost , TestSettings . PingTimeout , buffer ) ) ;
966
849
}
967
850
968
- [ PlatformSpecific ( TestPlatforms . AnyUnix ) ]
969
- [ Fact ]
851
+ [ ConditionalFact ( nameof ( UsesPingUtility ) ) ]
970
852
public async Task SendPingAsync_CustomPayload_InsufficientPrivileges_Throws ( )
971
853
{
972
854
IPAddress [ ] localIpAddresses = TestSettings . GetLocalIPAddresses ( ) ;
973
855
974
- if ( ! ( Capability . CanUseRawSockets ( localIpAddresses [ 0 ] . AddressFamily ) || PlatformDetection . IsOSXLike ) )
975
- {
976
- // running on sufficiently privileged process
977
- return ;
978
- }
979
-
980
856
byte [ ] buffer = TestSettings . PayloadAsBytes ;
981
857
Ping ping = new Ping ( ) ;
982
858
await Assert . ThrowsAsync < PlatformNotSupportedException > ( ( ) => ping . SendPingAsync ( TestSettings . LocalHost , TestSettings . PingTimeout , buffer ) ) ;
0 commit comments