Don't write DNS resolution errors on Test-Connection -Quiet (#12204)

This commit is contained in:
Joel Sallow (/u/ta11ow)
2020-03-28 15:50:32 +05:00
committed by GitHub
parent 1ec7c826f0
commit dfe9955346
2 changed files with 66 additions and 20 deletions
@@ -58,6 +58,8 @@ namespace Microsoft.PowerShell.Commands
private static byte[]? s_DefaultSendBuffer; private static byte[]? s_DefaultSendBuffer;
private readonly CancellationTokenSource _dnsLookupCancel = new CancellationTokenSource();
private bool _disposed; private bool _disposed;
private Ping? _sender; private Ping? _sender;
@@ -275,6 +277,7 @@ namespace Microsoft.PowerShell.Commands
protected override void StopProcessing() protected override void StopProcessing()
{ {
_sender?.SendAsyncCancel(); _sender?.SendAsyncCancel();
_dnsLookupCancel.Cancel();
} }
#region ConnectionTest #region ConnectionTest
@@ -283,6 +286,11 @@ namespace Microsoft.PowerShell.Commands
{ {
if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress)) if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress))
{ {
if (Quiet.IsPresent)
{
WriteObject(false);
}
return; return;
} }
@@ -334,6 +342,11 @@ namespace Microsoft.PowerShell.Commands
if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress)) if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
{ {
if (!Quiet.IsPresent)
{
WriteObject(false);
}
return; return;
} }
@@ -471,6 +484,11 @@ namespace Microsoft.PowerShell.Commands
PingReply? reply, replyResult = null; PingReply? reply, replyResult = null;
if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress)) if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
{ {
if (Quiet.IsPresent)
{
WriteObject(-1);
}
return; return;
} }
@@ -574,6 +592,11 @@ namespace Microsoft.PowerShell.Commands
{ {
if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress)) if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
{ {
if (Quiet.IsPresent)
{
WriteObject(false);
}
return; return;
} }
@@ -667,7 +690,7 @@ namespace Microsoft.PowerShell.Commands
if (ResolveDestination) if (ResolveDestination)
{ {
hostEntry = Dns.GetHostEntry(targetNameOrAddress); hostEntry = GetCancellableHostEntry(targetNameOrAddress);
resolvedTargetName = hostEntry.HostName; resolvedTargetName = hostEntry.HostName;
} }
else else
@@ -679,15 +702,21 @@ namespace Microsoft.PowerShell.Commands
{ {
try try
{ {
hostEntry = Dns.GetHostEntry(targetNameOrAddress); hostEntry = GetCancellableHostEntry(targetNameOrAddress);
if (ResolveDestination) if (ResolveDestination)
{ {
resolvedTargetName = hostEntry.HostName; resolvedTargetName = hostEntry.HostName;
hostEntry = Dns.GetHostEntry(hostEntry.HostName); hostEntry = GetCancellableHostEntry(hostEntry.HostName);
} }
} }
catch (PipelineStoppedException)
{
throw;
}
catch (Exception ex) catch (Exception ex)
{
if (!Quiet.IsPresent)
{ {
string message = StringUtil.Format( string message = StringUtil.Format(
TestConnectionResources.NoPingResult, TestConnectionResources.NoPingResult,
@@ -700,6 +729,8 @@ namespace Microsoft.PowerShell.Commands
ErrorCategory.ResourceUnavailable, ErrorCategory.ResourceUnavailable,
resolvedTargetName); resolvedTargetName);
WriteError(errorRecord); WriteError(errorRecord);
}
return false; return false;
} }
@@ -732,6 +763,20 @@ namespace Microsoft.PowerShell.Commands
return true; return true;
} }
private IPHostEntry GetCancellableHostEntry(string targetNameOrAddress)
{
var task = Dns.GetHostEntryAsync(targetNameOrAddress);
var waitHandles = new[] { ((IAsyncResult)task).AsyncWaitHandle, _dnsLookupCancel.Token.WaitHandle };
// WaitAny() returns the index of the first signal it gets; 1 is our cancellation token.
if (WaitHandle.WaitAny(waitHandles) == 1)
{
throw new PipelineStoppedException();
}
return task.GetAwaiter().GetResult();
}
private IPAddress? GetHostAddress(IPHostEntry hostEntry) private IPAddress? GetHostAddress(IPHostEntry hostEntry)
{ {
AddressFamily addressFamily = IPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; AddressFamily addressFamily = IPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
@@ -59,18 +59,19 @@ Describe "Test-Connection" -tags "CI" {
$result2 | Should -BeFalse $result2 | Should -BeFalse
} }
It "Ping fake host" { It 'returns false without errors for an unresolvable address when using -Quiet' {
Test-Connection -Quiet -ErrorAction Stop -Count 1 -TargetName "fakeHost" | Should -BeFalse
}
{ $result = Test-Connection "fakeHost" -Count 1 -Quiet -ErrorAction Stop } | It "Ping fake host" {
{ Test-Connection "fakeHost" -Count 1 -ErrorAction Stop } |
Should -Throw -ErrorId "TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand" Should -Throw -ErrorId "TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand"
# Error code = 11001 - Host not found. # Error code = 11001 - Host not found.
if ((Get-PlatformInfo).Platform -match "raspbian") { if ((Get-PlatformInfo).Platform -match "raspbian") {
$code = 11 $code = 11
} } elseif (!$IsWindows) {
elseif (!$IsWindows) {
$code = -131073 $code = -131073
} } else {
else {
$code = 11001 $code = 11001
} }
$error[0].Exception.InnerException.ErrorCode | Should -Be $code $error[0].Exception.InnerException.ErrorCode | Should -Be $code