Fix Test-Connection to correctly report BufferSize of 0#27657
Conversation
Use the requested BufferSize parameter value rather than the sent buffer length when constructing PingStatus objects. This prevents an empty buffer (sent when using -BufferSize 0 or the default) from always being reported as 32.
|
@raman118 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
Pull request overview
Fixes Test-Connection output reporting so that an explicitly requested -BufferSize 0 is reported as 0 (instead of being coerced to the default 32), aligning the PingStatus.BufferSize property and the default formatting table with the user’s requested value.
Changes:
- Update
TestConnectionCommandto pass the cmdlet’sBufferSizeparameter value intoPingStatus(instead of inferring from the underlying send buffer length). - Add a Pester regression test validating that
-BufferSize 0returnsBufferSize = 0.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 | Adds regression coverage for -BufferSize 0 reporting. |
| src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs | Changes PingStatus construction to report the requested BufferSize value. |
| // If we use the empty buffer for default size, we want to show | ||
| // the requested BufferSize (32) instead of the actual buffer length (0). |
Summary
This PR fixes a bug in
Test-Connectionwhere specifying-BufferSize 0incorrectly reports a buffer size of32in the output object and the CLIformatting tables, even though a 0-byte payload was actually sent.
What was broken & Root Cause
Previously, when creating the output
PingStatusobjects (both for standard pings and traceroutes), the cmdlet calculated the reported buffer size using this expression:```csharp
buffer.Length == 0 ? DefaultSendBufferSize : buffer.Length
This logic was added in the past to support non-Windows platforms (like Linux/macOS), where Test-Connection sends a 0-length buffer under the hood
when using the default size of 32 (to run without requiring administrative/root privileges).
However, checking buffer.Length == 0 meant the code couldn't distinguish between:
Because of this, both scenarios ended up reporting a buffer size of 32.
What changed
Instead of checking the underlying buffer.Length on the fly, we now pass the cmdlet's BufferSize property directly to the PingStatus constructor.
This works perfectly because:
• When using the default settings, BufferSize is 32 , so the output reports 32 .
• When the user specifies -BufferSize 0 , BufferSize is 0 , so the output correctly reports 0 .
• Any other custom buffer size is also reported exactly as requested.
How it was tested
Fixes #27656