Skip to content

Fix Test-Connection to report BufferSize 0 correctly - #27905

Open
Malin Fossum (malinfossum) wants to merge 5 commits into
PowerShell:masterfrom
malinfossum:fix/27656-buffersize-zero
Open

Malin Fossum (malinfossum) wants to merge 5 commits into
PowerShell:masterfrom
malinfossum:fix/27656-buffersize-zero

Conversation

@malinfossum

@malinfossum Malin Fossum (malinfossum) commented Aug 31, 2026 •

Copy link
Copy Markdown

PR Summary

Test-Connection -BufferSize 0 reported BufferSize as 32 even though a 0-byte payload is sent.

Since #20369 the default send buffer is intentionally empty (sending a custom buffer requires privileges on Unix), so the reporting fallback buffer.Length == 0 ? DefaultSendBufferSize : buffer.Length could not distinguish an explicit -BufferSize 0 from the default. Both report sites (ping and traceroute) now report the requested BufferSize parameter instead:

  • -BufferSize 0 → 0 (fixed)
  • default → 32 (unchanged)
  • -BufferSize n → n (unchanged)

Tests added for the explicit-0 and default cases; the full Test-Connection Pester suite passes locally on Windows (28 passed, 0 failed, 5 pending).

GetSendBuffer(0) now returns the shared empty buffer as well. .NET's ping-utility fallback (unelevated Unix) rejects any buffer instance other than its default or Array.Empty, so -BufferSize 0 used to throw PlatformNotSupportedException there; it now behaves like the default ping. That also lets the two new tests live in a Describe without RequireSudoOnUnix, so they run in the unelevated Linux/macOS passes too.

Note for reviewers: on Windows a default ping actually sends a 0-byte payload (the empty default buffer is passed through to Ping.Send; Reply.Buffer.Length is 0) while reporting 32. That is pre-existing behavior from #20369 and unchanged here — happy to file a separate issue if the working group wants to revisit it.

Supersedes #27657, which appears stalled on the CLA since July — credit to Doremon (@raman118) for the earlier attempt.

PR Context

Fixes #27656

PR Checklist

When -BufferSize 0 is specified, PingStatus.BufferSize reported the
default of 32 because the empty-buffer fallback added in PowerShell#20369 cannot
distinguish an explicit 0 from the default empty send buffer. Report the
requested BufferSize parameter instead, which also stays correct for the
default case where an empty buffer is sent for Unix compatibility.

Fixes PowerShell#27656
@malinfossum
Malin Fossum (malinfossum) requested a review from a team as a code owner August 31, 2026 09:26
Copilot AI lite review requested due to automatic review settings August 31, 2026 09:26
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Test-Connection output so that an explicit -BufferSize 0 is reported as 0 (instead of being conflated with the default send-buffer behavior introduced around the .NET 8 change), and adds regression tests for the explicit-0 and default cases.

Changes:

  • Report the requested BufferSize parameter value in both ping and traceroute output objects.
  • Add Pester coverage for -BufferSize 0 and the default BufferSize reporting behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs Report BufferSize based on the cmdlet parameter rather than the underlying send buffer length (ping + traceroute).
test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 Add regression tests validating reported BufferSize for explicit 0 and default behavior.
Suppressed comments (1)

src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs:740

  • Same as the traceroute path: this comment suggests the empty-buffer case is driven by custom buffers needing privileges, but the default BufferSize path also deliberately uses an empty buffer. Tweaking wording would better match how s_DefaultSendBuffer is used.
                        // The buffer may be empty even when BufferSize is not 0 as sending
                        // a custom buffer needs privileges on Unix (see GetSendBuffer),
                        // so report the requested size.
                        BufferSize,

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +221 to +231
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
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping these in the RequireSudoOnUnix Describe on purpose. Unelevated on Linux/macOS, .NET uses the ping utility path, which throws PlatformNotSupportedException for any buffer that is not the exact Array.Empty<byte>() reference (Ping.PingUtility.cs). GetSendBuffer(0) returns new byte[0], so -BufferSize 0 errors in the no-sudo pass and the test would fail there. The existing -BufferSize 2 test lives in the same Describe for the same reason. The tests still run in the elevated Linux/macOS jobs, which are green.

Comment on lines +488 to +490
// The buffer may be empty even when BufferSize is not 0 as sending
// a custom buffer needs privileges on Unix (see GetSendBuffer),
// so report the requested size.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - the default buffer is empty by design (s_DefaultSendBuffer = Array.Empty<byte>()), regardless of privileges. Reworded both comments in 1040f60 to say that buffer.Length cannot distinguish the default from an explicit -BufferSize 0, which is the actual reason for reporting the parameter.

GetSendBuffer(0) returned a fresh zero-length array. .NET's ping-utility
fallback (unelevated Unix) rejects any buffer that is not its default or
Array.Empty, so `-BufferSize 0` threw PlatformNotSupportedException there.
Returning the shared empty buffer makes it behave like the default ping.

With that, the two BufferSize-reporting tests no longer need privileges,
so they move to their own Describe that runs in the no-sudo pass too.
@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test-Connection -BufferSize 0 reports buffer size of 32

2 participants