Repository navigation
Fix Test-Connection to report BufferSize 0 correctly - #27905
Malin Fossum (malinfossum) wants to merge 5 commits into
Conversation
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
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
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
BufferSizeparameter value in both ping and traceroute output objects. - Add Pester coverage for
-BufferSize 0and 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_DefaultSendBufferis 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.
| 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 | ||
| } |
There was a problem hiding this comment.
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.
| // 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. |
There was a problem hiding this comment.
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.
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
PR Summary
Test-Connection -BufferSize 0reportedBufferSizeas 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.Lengthcould not distinguish an explicit-BufferSize 0from the default. Both report sites (ping and traceroute) now report the requestedBufferSizeparameter instead:-BufferSize 0→ 0 (fixed)-BufferSize n→ n (unchanged)Tests added for the explicit-0 and default cases; the full
Test-ConnectionPester 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 orArray.Empty, so-BufferSize 0used to throwPlatformNotSupportedExceptionthere; it now behaves like the default ping. That also lets the two new tests live in a Describe withoutRequireSudoOnUnix, 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.Lengthis 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
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header