Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -477,17 +477,17 @@ private void ProcessTraceroute(string targetNameOrAddress)

if (!Quiet.IsPresent)
{
// The default send buffer is intentionally empty (see s_DefaultSendBuffer),
// so buffer.Length cannot distinguish the default from an explicit
// BufferSize of 0. Report the requested size instead.
var status = new PingStatus(
Source,
routerName,
reply,
reply.Status == IPStatus.Success
? reply.RoundtripTime
: timer.ElapsedMilliseconds,

// If we use the empty buffer, then .NET actually uses a 32 byte buffer so we want to show
// as the result object the actual buffer size used instead of 0.
buffer.Length == 0 ? DefaultSendBufferSize : buffer.Length,
BufferSize,
pingNum: i);
WriteObject(new TraceStatus(
currentHop,
Expand Down Expand Up @@ -727,12 +727,15 @@ private void ProcessPing(string targetNameOrAddress)
}
else
{
// The default send buffer is intentionally empty (see s_DefaultSendBuffer),
// so buffer.Length cannot distinguish the default from an explicit
// BufferSize of 0. Report the requested size instead.
WriteObject(new PingStatus(
Source,
resolvedTargetName,
reply,
reply.RoundtripTime,
buffer.Length == 0 ? DefaultSendBufferSize : buffer.Length,
BufferSize,
pingNum: (uint)i));
}

Expand Down Expand Up @@ -885,9 +888,11 @@ private IPHostEntry GetCancellableHostEntry(string targetNameOrAddress)

// Users most often use the default buffer size so we cache the buffer.
// Creates and fills a send buffer. This follows the ping.exe and CoreFX model.
// An explicit size of 0 also returns the shared empty buffer: when .NET falls back
// to the ping utility (unelevated Unix), it rejects any other buffer instance.
private static byte[] GetSendBuffer(int bufferSize)
{
if (bufferSize == DefaultSendBufferSize)
if (bufferSize == DefaultSendBufferSize || bufferSize == 0)
{
return s_DefaultSendBuffer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,26 @@ Describe "Test-Connection" -tags "CI", "RequireSudoOnUnix" {
}
}

# Separate Describe without RequireSudoOnUnix: the default and the explicit-0 buffer are both the
# shared empty buffer, which .NET accepts on its ping-utility fallback, so these need no privileges.
Describe "Test-Connection BufferSize reporting" -Tag "CI" {
BeforeAll {
$targetName = "localhost"
}

It "BufferSize 0 is reported as 0" {
$result = Test-Connection $targetName -Count 1 -BufferSize 0

$result.BufferSize | Should -Be 0
}

It "Default BufferSize is reported as 32" {
$result = Test-Connection $targetName -Count 1

$result.BufferSize | Should -Be 32
}
}

Describe "Connection" -Tag "CI", "RequireAdminOnWindows" {
BeforeAll {
# Ensure the local host listen on port 80
Expand Down