From 2cd4296ee738b52b77c70a07dfbe6f7c4a731a1b Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Tue, 24 Dec 2019 22:54:58 -0800 Subject: [PATCH 01/46] Added verbose TCP test logic --- .../management/TestConnectionCommand.cs | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index ab9c299b712..01617b2fc08 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -28,6 +28,7 @@ namespace Microsoft.PowerShell.Commands [OutputType(typeof(PingMtuStatus), ParameterSetName = new string[] { MtuSizeDetectParameterSet })] [OutputType(typeof(int), ParameterSetName = new string[] { MtuSizeDetectParameterSet })] [OutputType(typeof(TraceStatus), ParameterSetName = new string[] { TraceRouteParameterSet })] + [OutputType(typeof(TcpTestStatus), ParameterSetName = new string[] { TcpPortVerboseParameterSet })] public class TestConnectionCommand : PSCmdlet, IDisposable { #region Parameter Set Names @@ -35,6 +36,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable private const string RepeatPingParameterSet = "RepeatPing"; private const string TraceRouteParameterSet = "TraceRoute"; private const string TcpPortParameterSet = "TcpPort"; + private const string TcpPortVerboseParameterSet = "TcpPortVerbose"; private const string MtuSizeDetectParameterSet = "MtuSizeDetect"; #endregion @@ -86,6 +88,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [Parameter(ParameterSetName = TraceRouteParameterSet)] [Parameter(ParameterSetName = MtuSizeDetectParameterSet)] [Parameter(ParameterSetName = TcpPortParameterSet)] + [Parameter(ParameterSetName = TcpPortVerboseParameterSet)] public SwitchParameter IPv4 { get; set; } /// @@ -96,6 +99,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [Parameter(ParameterSetName = TraceRouteParameterSet)] [Parameter(ParameterSetName = MtuSizeDetectParameterSet)] [Parameter(ParameterSetName = TcpPortParameterSet)] + [Parameter(ParameterSetName = TcpPortVerboseParameterSet)] public SwitchParameter IPv6 { get; set; } /// @@ -106,6 +110,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [Parameter(ParameterSetName = TraceRouteParameterSet)] [Parameter(ParameterSetName = MtuSizeDetectParameterSet)] [Parameter(ParameterSetName = TcpPortParameterSet)] + [Parameter(ParameterSetName = TcpPortVerboseParameterSet)] public SwitchParameter ResolveDestination { get; set; } /// @@ -117,6 +122,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [Parameter(ParameterSetName = RepeatPingParameterSet)] [Parameter(ParameterSetName = TraceRouteParameterSet)] [Parameter(ParameterSetName = TcpPortParameterSet)] + [Parameter(ParameterSetName = TcpPortVerboseParameterSet)] public string Source { get; } = Dns.GetHostName(); /// @@ -226,6 +232,13 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [Parameter(Mandatory = true, ParameterSetName = TcpPortParameterSet)] public int TcpPort { get; set; } + /// + /// Gets or sets whether to perform a verbose TCP connection test. + /// + [ValidateRange(0, 65535)] + [Parameter(Mandatory = true, ParameterSetName = TcpPortVerboseParameterSet)] + public int TcpPortVerbose { get; set; } + #endregion Parameters /// @@ -268,6 +281,9 @@ protected override void ProcessRecord() case TcpPortParameterSet: ProcessConnectionByTCPPort(targetName); break; + case TcpPortVerboseParameterSet: + TcpPortConnectionTestVerbose(targetName); + break; } } } @@ -330,6 +346,74 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) #endregion ConnectionTest + #region VerboseTcpTest + + private void TcpPortConnectionTestVerbose(string targetNameOrAddress) + { + if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress)) + { + return; + } + + TcpClient client = new TcpClient(); + Stopwatch stopwatch = new Stopwatch(); + + try + { + stopwatch.Start(); + + Task connectionTask = client.ConnectAsync(targetAddress, TcpPortVerbose); + Task.WhenAny(connectionTask, Task.Delay(new TimeSpan(0, 0, TimeoutSeconds))).Wait(); + + if (connectionTask.Status == TaskStatus.RanToCompletion) + { + stopwatch.Stop(); + + WriteObject(new TcpTestStatus( + Source, + Source, + targetNameOrAddress, + targetAddress.ToString(), + TcpPortVerbose, + stopwatch.ElapsedMilliseconds, + TcpConnectionTestResult.Success + )); + } + else + { + WriteObject(new TcpTestStatus( + Source, + Source, + targetNameOrAddress, + targetAddress.ToString(), + TcpPortVerbose, + 0, + TcpConnectionTestResult.Timeout + )); + } + + return; + } + catch + { + WriteObject(new TcpTestStatus( + Source, + Source, + targetNameOrAddress, + targetAddress.ToString(), + TcpPortVerbose, + 0, + TcpConnectionTestResult.Failed + )); + } + finally + { + client.Close(); + } + } + + #endregion VerboseTcpTest + #region TracerouteTest private void ProcessTraceroute(string targetNameOrAddress) @@ -859,6 +943,92 @@ private static void OnPingComplete(object sender, PingCompletedEventArgs e) ((TestConnectionCommand)e.UserState)._pingComplete.Set(); } + /// + /// The class contains information about the TCP connection test + /// + public class TcpTestStatus + { + /// + /// Initializes a new instance of the class. + /// This constructor allows manually specifying the initial values for the cases where the PingReply + /// object may be missing some information, specifically in the instances where PingReply objects are + /// utilised to perform a traceroute. + /// + /// The source machine name or IP of the test. + /// The resolved IP from the source + /// The destination machine name or IP of the test. + /// The resolved IP from the destination + /// The port used for the connection. + /// The latency of the test. + /// The result of the test. + internal TcpTestStatus(string source, string sourceAddress, string destination, string destinationAddress, int port, long latency, TcpConnectionTestResult testResult) + { + Source = source; + SourceAddress = sourceAddress; + Destination = destination; + DestinationAddress = destinationAddress; + Port = port; + Latency = latency; + TestResult = testResult; + } + + /// + /// Gets the source from which the test was sent. + /// + public string Source { get; } + + /// + /// Gets the resolved address from which the test was sent. + /// + public string SourceAddress { get; } + + /// + /// Gets the destination name. + /// + public string Destination { get; } + + /// + /// Gets the resolved address for the destination. + /// + public string DestinationAddress { get; } + + /// + /// Gets the port used for the test. + /// + public int Port { get; } + + /// + /// The latancy seen during the test + /// + public long Latency { get; } + + /// + /// The result of the connection test + /// + public TcpConnectionTestResult TestResult { get; } + } + + /// + /// Results of the verbose TCP connection test + /// + public enum TcpConnectionTestResult + { + /// + /// Connection was successful. + /// + Success, + + /// + /// Test was not able to run. + /// + Failed, + + /// + /// Connection timed out. + /// + Timeout, + } + /// /// The class contains information about the source, the destination and ping results. /// From 13371933a6aa4d6527dcbc4d7c49ca30d3f41773 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Wed, 25 Dec 2019 00:04:38 -0800 Subject: [PATCH 02/46] Cleaned up output, removed source IP address --- .../commands/management/TestConnectionCommand.cs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 01617b2fc08..706723aec47 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -370,7 +370,6 @@ private void TcpPortConnectionTestVerbose(string targetNameOrAddress) stopwatch.Stop(); WriteObject(new TcpTestStatus( - Source, Source, targetNameOrAddress, targetAddress.ToString(), @@ -382,7 +381,6 @@ private void TcpPortConnectionTestVerbose(string targetNameOrAddress) else { WriteObject(new TcpTestStatus( - Source, Source, targetNameOrAddress, targetAddress.ToString(), @@ -390,14 +388,11 @@ private void TcpPortConnectionTestVerbose(string targetNameOrAddress) 0, TcpConnectionTestResult.Timeout )); - } - - return; + } } catch { WriteObject(new TcpTestStatus( - Source, Source, targetNameOrAddress, targetAddress.ToString(), @@ -955,16 +950,14 @@ public class TcpTestStatus /// utilised to perform a traceroute. /// /// The source machine name or IP of the test. - /// The resolved IP from the source /// The destination machine name or IP of the test. /// The resolved IP from the destination /// The port used for the connection. /// The latency of the test. /// The result of the test. - internal TcpTestStatus(string source, string sourceAddress, string destination, string destinationAddress, int port, long latency, TcpConnectionTestResult testResult) + internal TcpTestStatus(string source, string destination, string destinationAddress, int port, long latency, TcpConnectionTestResult testResult) { Source = source; - SourceAddress = sourceAddress; Destination = destination; DestinationAddress = destinationAddress; Port = port; @@ -977,11 +970,6 @@ internal TcpTestStatus(string source, string sourceAddress, string destination, /// public string Source { get; } - /// - /// Gets the resolved address from which the test was sent. - /// - public string SourceAddress { get; } - /// /// Gets the destination name. /// From 9325584c9af6ca7dd112f904a33b3651daf3c76c Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 26 Dec 2019 22:58:31 -0800 Subject: [PATCH 03/46] Consolidated detailed output logic into ProcessConnectionByTCPPort() --- .../management/TestConnectionCommand.cs | 143 ++++++------------ 1 file changed, 46 insertions(+), 97 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 706723aec47..f7215bb0306 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -28,7 +28,7 @@ namespace Microsoft.PowerShell.Commands [OutputType(typeof(PingMtuStatus), ParameterSetName = new string[] { MtuSizeDetectParameterSet })] [OutputType(typeof(int), ParameterSetName = new string[] { MtuSizeDetectParameterSet })] [OutputType(typeof(TraceStatus), ParameterSetName = new string[] { TraceRouteParameterSet })] - [OutputType(typeof(TcpTestStatus), ParameterSetName = new string[] { TcpPortVerboseParameterSet })] + [OutputType(typeof(TcpTestStatus), ParameterSetName = new string[] { TcpPortParameterSet })] public class TestConnectionCommand : PSCmdlet, IDisposable { #region Parameter Set Names @@ -36,7 +36,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable private const string RepeatPingParameterSet = "RepeatPing"; private const string TraceRouteParameterSet = "TraceRoute"; private const string TcpPortParameterSet = "TcpPort"; - private const string TcpPortVerboseParameterSet = "TcpPortVerbose"; private const string MtuSizeDetectParameterSet = "MtuSizeDetect"; #endregion @@ -88,7 +87,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [Parameter(ParameterSetName = TraceRouteParameterSet)] [Parameter(ParameterSetName = MtuSizeDetectParameterSet)] [Parameter(ParameterSetName = TcpPortParameterSet)] - [Parameter(ParameterSetName = TcpPortVerboseParameterSet)] public SwitchParameter IPv4 { get; set; } /// @@ -99,7 +97,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [Parameter(ParameterSetName = TraceRouteParameterSet)] [Parameter(ParameterSetName = MtuSizeDetectParameterSet)] [Parameter(ParameterSetName = TcpPortParameterSet)] - [Parameter(ParameterSetName = TcpPortVerboseParameterSet)] public SwitchParameter IPv6 { get; set; } /// @@ -110,7 +107,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [Parameter(ParameterSetName = TraceRouteParameterSet)] [Parameter(ParameterSetName = MtuSizeDetectParameterSet)] [Parameter(ParameterSetName = TcpPortParameterSet)] - [Parameter(ParameterSetName = TcpPortVerboseParameterSet)] public SwitchParameter ResolveDestination { get; set; } /// @@ -122,7 +118,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [Parameter(ParameterSetName = RepeatPingParameterSet)] [Parameter(ParameterSetName = TraceRouteParameterSet)] [Parameter(ParameterSetName = TcpPortParameterSet)] - [Parameter(ParameterSetName = TcpPortVerboseParameterSet)] public string Source { get; } = Dns.GetHostName(); /// @@ -231,13 +226,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [ValidateRange(0, 65535)] [Parameter(Mandatory = true, ParameterSetName = TcpPortParameterSet)] public int TcpPort { get; set; } - - /// - /// Gets or sets whether to perform a verbose TCP connection test. - /// - [ValidateRange(0, 65535)] - [Parameter(Mandatory = true, ParameterSetName = TcpPortVerboseParameterSet)] - public int TcpPortVerbose { get; set; } #endregion Parameters @@ -281,9 +269,6 @@ protected override void ProcessRecord() case TcpPortParameterSet: ProcessConnectionByTCPPort(targetName); break; - case TcpPortVerboseParameterSet: - TcpPortConnectionTestVerbose(targetName); - break; } } } @@ -306,12 +291,23 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) return; } - TcpClient client = new TcpClient(); + TcpTestStatus testResult = new TcpTestStatus( + Source, + targetNameOrAddress, + targetAddress.ToString(), + TcpPort, + 0, + TcpConnectionTestResult.Failed + ); + TcpClient client = new TcpClient(); + try { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); - string targetString = targetAddress.ToString(); for (var i = 1; i <= TimeoutSeconds; i++) { @@ -320,16 +316,23 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (timeoutTask.Status == TaskStatus.Faulted || timeoutTask.Status == TaskStatus.Canceled) { - // Waiting is interrupted by Ctrl-C. - WriteObject(false); - return; + testResult.TestResult = TcpConnectionTestResult.Cancelled; + break; } - - if (connectionTask.Status == TaskStatus.RanToCompletion) + + if (i == TimeoutSeconds && timeoutTask.Status == TaskStatus.RanToCompletion) { - WriteObject(true); - return; + testResult.TestResult = TcpConnectionTestResult.Timeout; + break; } + + if (connectionTask.Status == TaskStatus.RanToCompletion) + { + stopwatch.Stop(); + testResult.TestResult = TcpConnectionTestResult.Success; + testResult.Latency = stopwatch.ElapsedMilliseconds; + break; + } } } catch @@ -339,75 +342,19 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) finally { client.Close(); - } - - WriteObject(false); - } - - #endregion ConnectionTest - - #region VerboseTcpTest - private void TcpPortConnectionTestVerbose(string targetNameOrAddress) - { - if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress)) - { - return; - } - - TcpClient client = new TcpClient(); - Stopwatch stopwatch = new Stopwatch(); - - try - { - stopwatch.Start(); - - Task connectionTask = client.ConnectAsync(targetAddress, TcpPortVerbose); - Task.WhenAny(connectionTask, Task.Delay(new TimeSpan(0, 0, TimeoutSeconds))).Wait(); - - if (connectionTask.Status == TaskStatus.RanToCompletion) + if (Quiet.IsPresent) { - stopwatch.Stop(); - - WriteObject(new TcpTestStatus( - Source, - targetNameOrAddress, - targetAddress.ToString(), - TcpPortVerbose, - stopwatch.ElapsedMilliseconds, - TcpConnectionTestResult.Success - )); - } - else - { - WriteObject(new TcpTestStatus( - Source, - targetNameOrAddress, - targetAddress.ToString(), - TcpPortVerbose, - 0, - TcpConnectionTestResult.Timeout - )); - } - } - catch - { - WriteObject(new TcpTestStatus( - Source, - targetNameOrAddress, - targetAddress.ToString(), - TcpPortVerbose, - 0, - TcpConnectionTestResult.Failed - )); - } - finally - { - client.Close(); + WriteObject(testResult.TestResult == TcpConnectionTestResult.Success); + } + else + { + WriteObject(testResult); + } } } - #endregion VerboseTcpTest + #endregion ConnectionTest #region TracerouteTest @@ -945,9 +892,6 @@ public class TcpTestStatus { /// /// Initializes a new instance of the class. - /// This constructor allows manually specifying the initial values for the cases where the PingReply - /// object may be missing some information, specifically in the instances where PingReply objects are - /// utilised to perform a traceroute. /// /// The source machine name or IP of the test. /// The destination machine name or IP of the test. @@ -986,18 +930,18 @@ internal TcpTestStatus(string source, string destination, string destinationAddr public int Port { get; } /// - /// The latancy seen during the test + /// Gets or sets the latancy of the connection /// - public long Latency { get; } + public long Latency { get; set; } /// - /// The result of the connection test + /// Gets or sets the result of the test /// - public TcpConnectionTestResult TestResult { get; } + public TcpConnectionTestResult TestResult { get; set; } } /// - /// Results of the verbose TCP connection test + /// Results of the detailed TCP connection test /// public enum TcpConnectionTestResult { @@ -1015,6 +959,11 @@ public enum TcpConnectionTestResult /// Connection timed out. /// Timeout, + + /// + /// Test was cancelled + /// + Cancelled } /// From c005933a7b26459d7f2b2529264a4b3445695409 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 26 Dec 2019 23:34:24 -0800 Subject: [PATCH 04/46] Updated existing tests with -Quiet switch --- .../Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index b7e90b9383b..81bbf94e1bc 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -292,10 +292,10 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { } It "Test connection to local host port 80" { - Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort | Should -BeTrue + Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort -Quiet | Should -BeTrue } It "Test connection to unreachable host port 80" { - Test-Connection $UnreachableAddress -TcpPort 80 -TimeOut 1 | Should -BeFalse + Test-Connection $UnreachableAddress -TcpPort 80 -TimeOut 1 -Quiet | Should -BeFalse } } From 1698fc5a490df3f948f99e2f6baabcc73c8a46f3 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Fri, 27 Dec 2019 23:08:11 -0800 Subject: [PATCH 05/46] Improved output formatting, changed logic to match --- .../management/TestConnectionCommand.cs | 103 ++++++++++-------- .../PowerShellCore_format_ps1xml.cs | 29 +++++ 2 files changed, 89 insertions(+), 43 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index f7215bb0306..c622a87e1ff 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -138,6 +138,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// The default (from Windows) is 4 times. /// [Parameter(ParameterSetName = DefaultPingParameterSet)] + [Parameter(ParameterSetName = TcpPortParameterSet)] [ValidateRange(ValidateRangeKind.Positive)] public int Count { get; set; } = 4; @@ -291,67 +292,76 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) return; } - TcpTestStatus testResult = new TcpTestStatus( - Source, - targetNameOrAddress, - targetAddress.ToString(), - TcpPort, - 0, - TcpConnectionTestResult.Failed - ); - - TcpClient client = new TcpClient(); - try { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - - Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); + int successfulConnections = 0; - for (var i = 1; i <= TimeoutSeconds; i++) + for (var i = 1; i <= Count; i++) { - Task timeoutTask = Task.Delay(millisecondsDelay: 1000); + TcpTestStatus testResult = new TcpTestStatus( + i, + Source, + targetNameOrAddress, + targetAddress.ToString(), + TcpPort, + 0, + TcpConnectionTestResult.Failed + ); + + TcpClient client = new TcpClient(); + + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + + Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); + Task timeoutTask = Task.Delay(new TimeSpan(0, 0, TimeoutSeconds)); Task.WhenAny(connectionTask, timeoutTask).Result.Wait(); + stopwatch.Stop(); + + client.Close(); + if (timeoutTask.Status == TaskStatus.Faulted || timeoutTask.Status == TaskStatus.Canceled) { - testResult.TestResult = TcpConnectionTestResult.Cancelled; - break; + testResult.Result = TcpConnectionTestResult.Cancelled; + return; } - if (i == TimeoutSeconds && timeoutTask.Status == TaskStatus.RanToCompletion) + if (timeoutTask.Status == TaskStatus.RanToCompletion) { - testResult.TestResult = TcpConnectionTestResult.Timeout; - break; + testResult.Result = TcpConnectionTestResult.Timeout; } if (connectionTask.Status == TaskStatus.RanToCompletion) { - stopwatch.Stop(); - testResult.TestResult = TcpConnectionTestResult.Success; + successfulConnections++; + testResult.Result = TcpConnectionTestResult.Success; testResult.Latency = stopwatch.ElapsedMilliseconds; - break; - } + } + + if (Quiet.IsPresent) + { + if (i > successfulConnections) + { + WriteObject(false); + return; + } + else if(i == Count) + { + WriteObject(true); + return; + } + } + else + { + WriteObject(testResult); + } } } catch { // Silently ignore connection errors. } - finally - { - client.Close(); - - if (Quiet.IsPresent) - { - WriteObject(testResult.TestResult == TcpConnectionTestResult.Success); - } - else - { - WriteObject(testResult); - } - } } #endregion ConnectionTest @@ -893,22 +903,29 @@ public class TcpTestStatus /// /// Initializes a new instance of the class. /// + /// The number of this test. /// The source machine name or IP of the test. /// The destination machine name or IP of the test. /// The resolved IP from the destination /// The port used for the connection. /// The latency of the test. - /// The result of the test. - internal TcpTestStatus(string source, string destination, string destinationAddress, int port, long latency, TcpConnectionTestResult testResult) + /// The result of the test. + internal TcpTestStatus(int testNum, string source, string destination, string destinationAddress, int port, long latency, TcpConnectionTestResult result) { + TestNum = testNum; Source = source; Destination = destination; DestinationAddress = destinationAddress; Port = port; Latency = latency; - TestResult = testResult; + Result = result; } + /// + /// Gets and sets the count of the test. + /// + public int TestNum { get; set; } + /// /// Gets the source from which the test was sent. /// @@ -937,7 +954,7 @@ internal TcpTestStatus(string source, string destination, string destinationAddr /// /// Gets or sets the result of the test /// - public TcpConnectionTestResult TestResult { get; set; } + public TcpConnectionTestResult Result { get; set; } } /// diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 768ece83daf..08eba8a0f78 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -240,6 +240,10 @@ internal static IEnumerable GetFormatData() "Microsoft.PowerShell.MarkdownRender.PSMarkdownOptionInfo", ViewsOf_Microsoft_PowerShell_MarkdownRender_MarkdownOptionInfo()); + yield return new ExtendedTypeDefinition( + "Microsoft.PowerShell.Commands.TestConnectionCommand+TcpTestStatus", + ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TcpTestStatus()); + yield return new ExtendedTypeDefinition( "Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_PingStatus()); @@ -1837,6 +1841,31 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Ma .EndEntry() .EndList()); } + + private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TcpTestStatus() + { + yield return new FormatViewDefinition( + "Microsoft.PowerShell.Commands.TestConnectionCommand+TcpTestStatus", + TableControl.Create() + .AddHeader(Alignment.Right, label: "Test", width: 4) + .AddHeader(Alignment.Left, label: "Source", width: 16) + .AddHeader(Alignment.Left, label: "Destination", width: 16) + .AddHeader(Alignment.Left, label: "DestinationAddress", width: 18) + .AddHeader(Alignment.Right, label: "Port", width: 7) + .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) + .AddHeader(Alignment.Left, label: "Result", width: 16) + .StartRowDefinition() + .AddPropertyColumn("TestNum") + .AddPropertyColumn("Source") + .AddPropertyColumn("Destination") + .AddPropertyColumn("DestinationAddress") + .AddPropertyColumn("Port") + .AddPropertyColumn("Latency") + .AddPropertyColumn("Result") + .EndRowDefinition() + .GroupByProperty("Destination") + .EndTable()); + } private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_PingStatus() { From 34d56afd010ccb80b26b64ea465507356b0cb96e Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sat, 28 Dec 2019 00:49:30 -0800 Subject: [PATCH 06/46] Error handling and logic improvements --- .../management/TestConnectionCommand.cs | 77 ++++++++++--------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index c622a87e1ff..54decb0399d 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -292,31 +292,31 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) return; } - try - { - int successfulConnections = 0; + int successfulConnections = 0; - for (var i = 1; i <= Count; i++) + for (var i = 1; i <= Count; i++) + { + TcpTestStatus testResult = new TcpTestStatus( + i, + Source, + targetNameOrAddress, + targetAddress.ToString(), + TcpPort, + 0, + TcpConnectionTestResult.New + ); + + try { - TcpTestStatus testResult = new TcpTestStatus( - i, - Source, - targetNameOrAddress, - targetAddress.ToString(), - TcpPort, - 0, - TcpConnectionTestResult.Failed - ); - TcpClient client = new TcpClient(); - + Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); - - Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); + + Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); Task timeoutTask = Task.Delay(new TimeSpan(0, 0, TimeoutSeconds)); - Task.WhenAny(connectionTask, timeoutTask).Result.Wait(); + Task.WhenAny(connectionTask, timeoutTask).Result.Wait(); stopwatch.Stop(); client.Close(); @@ -338,29 +338,29 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) testResult.Result = TcpConnectionTestResult.Success; testResult.Latency = stopwatch.ElapsedMilliseconds; } + } + catch + { + testResult.Result = TcpConnectionTestResult.Failed; + } - if (Quiet.IsPresent) - { - if (i > successfulConnections) - { - WriteObject(false); - return; - } - else if(i == Count) - { - WriteObject(true); - return; - } + if (Quiet.IsPresent) + { + if (i > successfulConnections) + { + WriteObject(false); + return; } - else + else if(i == Count) { - WriteObject(testResult); + WriteObject(true); + return; } } - } - catch - { - // Silently ignore connection errors. + else + { + WriteObject(testResult); + } } } @@ -962,6 +962,11 @@ internal TcpTestStatus(int testNum, string source, string destination, string de /// public enum TcpConnectionTestResult { + /// + /// Connection test has not run + /// + New, + /// /// Connection was successful. /// From 3896c981426172957cbb20aca82c8f8aa54e6d7f Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sat, 28 Dec 2019 01:25:26 -0800 Subject: [PATCH 07/46] Updated tests to match new output --- .../Test-Connection.Tests.ps1 | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index 81bbf94e1bc..86355c15268 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -291,11 +291,47 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $UnreachableAddress = "10.11.12.13" } - It "Test connection to local host port 80" { + It "Test quiet connection to local host on working port" { Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort -Quiet | Should -BeTrue } - It "Test connection to unreachable host port 80" { + It "Test quiet connection to unreachable host port 80" { Test-Connection $UnreachableAddress -TcpPort 80 -TimeOut 1 -Quiet | Should -BeFalse } + + It "Test detailed connection to local host on working port" { + $result = Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort + + $result.Count | Should -Be 4 + $result[0].TestNum | Should -BeExactly 1 + $result[0].Destination | Should -BeExactly '127.0.0.1' + $result[0].DestinationAddress | Should -BeExactly '127.0.0.1' + $result[0].Port | Should -Be $WebListener.HttpPort + $result[0].Latency | Should -BeGreaterOrEqual 0 + $result[0].Result | Should -BeExactly 'Success' + } + + It "Test detailed connection to local host on working port with modified count" { + $result = Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort -Count 2 + + $result.Count | Should -Be 2 + $result[0].TestNum | Should -BeExactly 1 + $result[0].Destination | Should -BeExactly '127.0.0.1' + $result[0].DestinationAddress | Should -BeExactly '127.0.0.1' + $result[0].Port | Should -Be $WebListener.HttpPort + $result[0].Latency | Should -BeGreaterOrEqual 0 + $result[0].Result | Should -BeExactly 'Success' + } + + It "Test detailed connection to unreachable host port 80" { + $result = Test-Connection $UnreachableAddress -TcpPort 80 -TimeOut 1 + + $result.Count | Should -Be 4 + $result[0].TestNum | Should -BeExactly 1 + $result[0].Destination | Should -BeExactly $UnreachableAddress + $result[0].DestinationAddress | Should -BeExactly $UnreachableAddress + $result[0].Port | Should -Be 80 + $result[0].Latency | Should -BeExactly 0 + $result[0].Result | Should -Not -BeExactly 'Success' + } } From 1b6c06d45cd8b6a81aad8b24e14061e0c21bb421 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 29 Dec 2019 00:44:06 -0800 Subject: [PATCH 08/46] Updated logic, tcp tests now run once by default --- .../management/TestConnectionCommand.cs | 32 ++++++++++++++----- .../Test-Connection.Tests.ps1 | 4 +-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 54decb0399d..ebff26e1a0e 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -134,13 +134,14 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public int MaxHops { get; set; } = DefaultMaxHops; /// - /// Gets or sets the number of ping attempts. - /// The default (from Windows) is 4 times. + /// Gets or sets the number of test attempts. + /// The value is -1 if not set explicitly as a parameter + /// If not set explicitly, the value is set depending on type of test (ping = 4, tcp = 1) /// [Parameter(ParameterSetName = DefaultPingParameterSet)] [Parameter(ParameterSetName = TcpPortParameterSet)] [ValidateRange(ValidateRangeKind.Positive)] - public int Count { get; set; } = 4; + public int Count { get; set; } = -1; /// /// Gets or sets the delay between ping attempts. @@ -148,6 +149,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// [Parameter(ParameterSetName = DefaultPingParameterSet)] [Parameter(ParameterSetName = RepeatPingParameterSet)] + [Parameter(ParameterSetName = TcpPortParameterSet)] [ValidateRange(ValidateRangeKind.Positive)] public int Delay { get; set; } = 1; @@ -174,6 +176,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Gets or sets whether to continue pinging until user presses Ctrl-C (or Int.MaxValue threshold reached). /// [Parameter(Mandatory = true, ParameterSetName = RepeatPingParameterSet)] + [Parameter(ParameterSetName = TcpPortParameterSet)] [Alias("Continuous")] public SwitchParameter Repeat { get; set; } @@ -232,14 +235,25 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// /// BeginProcessing implementation for TestConnectionCommand. + /// Sets Count for different types of tests unless specified explicitly. /// protected override void BeginProcessing() { - switch (ParameterSetName) + if (Count == -1) + { + if (ParameterSetName == TcpPortParameterSet) + { + Count = 1; + } + else + { + Count = 4; + } + } + + if (Repeat.IsPresent) { - case RepeatPingParameterSet: - Count = int.MaxValue; - break; + Count = int.MaxValue; } } @@ -286,7 +300,7 @@ protected override void StopProcessing() #region ConnectionTest private void ProcessConnectionByTCPPort(string targetNameOrAddress) - { + { if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress)) { return; @@ -361,6 +375,8 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) { WriteObject(testResult); } + + Thread.Sleep(new TimeSpan(0, 0, Delay)); } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index 86355c15268..5120c2db0c2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -302,7 +302,7 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { It "Test detailed connection to local host on working port" { $result = Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort - $result.Count | Should -Be 4 + $result.Count | Should -Be 1 $result[0].TestNum | Should -BeExactly 1 $result[0].Destination | Should -BeExactly '127.0.0.1' $result[0].DestinationAddress | Should -BeExactly '127.0.0.1' @@ -326,7 +326,7 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { It "Test detailed connection to unreachable host port 80" { $result = Test-Connection $UnreachableAddress -TcpPort 80 -TimeOut 1 - $result.Count | Should -Be 4 + $result.Count | Should -Be 1 $result[0].TestNum | Should -BeExactly 1 $result[0].Destination | Should -BeExactly $UnreachableAddress $result[0].DestinationAddress | Should -BeExactly $UnreachableAddress From cdbcd07d81431409bf75af695ad4214890c12b82 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 29 Dec 2019 01:26:11 -0800 Subject: [PATCH 09/46] Moved TcpConnectionTestResult enum into TcpTestStatus class --- .../management/TestConnectionCommand.cs | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index ebff26e1a0e..5ff804f9c2b 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -317,7 +317,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) targetAddress.ToString(), TcpPort, 0, - TcpConnectionTestResult.New + TcpTestStatus.TcpConnectionTestResult.New ); try @@ -337,25 +337,25 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (timeoutTask.Status == TaskStatus.Faulted || timeoutTask.Status == TaskStatus.Canceled) { - testResult.Result = TcpConnectionTestResult.Cancelled; + testResult.Result = TcpTestStatus.TcpConnectionTestResult.Cancelled; return; } if (timeoutTask.Status == TaskStatus.RanToCompletion) { - testResult.Result = TcpConnectionTestResult.Timeout; + testResult.Result = TcpTestStatus.TcpConnectionTestResult.Timeout; } if (connectionTask.Status == TaskStatus.RanToCompletion) { successfulConnections++; - testResult.Result = TcpConnectionTestResult.Success; + testResult.Result = TcpTestStatus.TcpConnectionTestResult.Success; testResult.Latency = stopwatch.ElapsedMilliseconds; } } catch { - testResult.Result = TcpConnectionTestResult.Failed; + testResult.Result = TcpTestStatus.TcpConnectionTestResult.Failed; } if (Quiet.IsPresent) @@ -971,37 +971,37 @@ internal TcpTestStatus(int testNum, string source, string destination, string de /// Gets or sets the result of the test /// public TcpConnectionTestResult Result { get; set; } - } - - /// - /// Results of the detailed TCP connection test - /// - public enum TcpConnectionTestResult - { - /// - /// Connection test has not run - /// - New, - - /// - /// Connection was successful. - /// - Success, /// - /// Test was not able to run. - /// - Failed, - - /// - /// Connection timed out. - /// - Timeout, - - /// - /// Test was cancelled - /// - Cancelled + /// Results of the detailed TCP connection test + /// + public enum TcpConnectionTestResult + { + /// + /// Connection test has not run + /// + New, + + /// + /// Connection was successful. + /// + Success, + + /// + /// Test was not able to run. + /// + Failed, + + /// + /// Connection timed out. + /// + Timeout, + + /// + /// Test was cancelled + /// + Cancelled + } } /// From a5d3d6c7739b6f9773d147ef370467239f4d4309 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 29 Dec 2019 11:14:32 -0800 Subject: [PATCH 10/46] Removed unnecessary whitespace --- .../management/TestConnectionCommand.cs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 5ff804f9c2b..05e08b5f3ad 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -230,7 +230,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [ValidateRange(0, 65535)] [Parameter(Mandatory = true, ParameterSetName = TcpPortParameterSet)] public int TcpPort { get; set; } - + #endregion Parameters /// @@ -250,7 +250,7 @@ protected override void BeginProcessing() Count = 4; } } - + if (Repeat.IsPresent) { Count = int.MaxValue; @@ -300,7 +300,7 @@ protected override void StopProcessing() #region ConnectionTest private void ProcessConnectionByTCPPort(string targetNameOrAddress) - { + { if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress)) { return; @@ -319,11 +319,11 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) 0, TcpTestStatus.TcpConnectionTestResult.New ); - + try { TcpClient client = new TcpClient(); - + Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); @@ -337,15 +337,15 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (timeoutTask.Status == TaskStatus.Faulted || timeoutTask.Status == TaskStatus.Canceled) { - testResult.Result = TcpTestStatus.TcpConnectionTestResult.Cancelled; + testResult.Result = TcpTestStatus.TcpConnectionTestResult.Cancelled; return; } - + if (timeoutTask.Status == TaskStatus.RanToCompletion) { testResult.Result = TcpTestStatus.TcpConnectionTestResult.Timeout; } - + if (connectionTask.Status == TaskStatus.RanToCompletion) { successfulConnections++; @@ -356,10 +356,10 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) catch { testResult.Result = TcpTestStatus.TcpConnectionTestResult.Failed; - } + } if (Quiet.IsPresent) - { + { if (i > successfulConnections) { WriteObject(false); @@ -381,7 +381,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) } #endregion ConnectionTest - + #region TracerouteTest private void ProcessTraceroute(string targetNameOrAddress) @@ -974,14 +974,14 @@ internal TcpTestStatus(int testNum, string source, string destination, string de /// /// Results of the detailed TCP connection test - /// + /// public enum TcpConnectionTestResult { /// /// Connection test has not run /// New, - + /// /// Connection was successful. /// @@ -1003,7 +1003,7 @@ public enum TcpConnectionTestResult Cancelled } } - + /// /// The class contains information about the source, the destination and ping results. /// From 8cddfbf615ac4c2d1bb54a29f296c48d6b9269d5 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 29 Dec 2019 11:33:56 -0800 Subject: [PATCH 11/46] Wrapped TcpClient() in Using statement --- .../management/TestConnectionCommand.cs | 53 +++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 05e08b5f3ad..7ffe54a3f92 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -320,43 +320,42 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) TcpTestStatus.TcpConnectionTestResult.New ); - try + using(TcpClient client = new TcpClient()) { - TcpClient client = new TcpClient(); - - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); + try + { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); - Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); - Task timeoutTask = Task.Delay(new TimeSpan(0, 0, TimeoutSeconds)); + Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); + Task timeoutTask = Task.Delay(new TimeSpan(0, 0, TimeoutSeconds)); - Task.WhenAny(connectionTask, timeoutTask).Result.Wait(); - stopwatch.Stop(); + Task.WhenAny(connectionTask, timeoutTask).Result.Wait(); + stopwatch.Stop(); - client.Close(); + if (timeoutTask.Status == TaskStatus.Faulted || timeoutTask.Status == TaskStatus.Canceled) + { + testResult.Result = TcpTestStatus.TcpConnectionTestResult.Cancelled; + return; + } - if (timeoutTask.Status == TaskStatus.Faulted || timeoutTask.Status == TaskStatus.Canceled) - { - testResult.Result = TcpTestStatus.TcpConnectionTestResult.Cancelled; - return; - } + if (timeoutTask.Status == TaskStatus.RanToCompletion) + { + testResult.Result = TcpTestStatus.TcpConnectionTestResult.Timeout; + } - if (timeoutTask.Status == TaskStatus.RanToCompletion) - { - testResult.Result = TcpTestStatus.TcpConnectionTestResult.Timeout; + if (connectionTask.Status == TaskStatus.RanToCompletion) + { + successfulConnections++; + testResult.Result = TcpTestStatus.TcpConnectionTestResult.Success; + testResult.Latency = stopwatch.ElapsedMilliseconds; + } } - - if (connectionTask.Status == TaskStatus.RanToCompletion) + catch { - successfulConnections++; - testResult.Result = TcpTestStatus.TcpConnectionTestResult.Success; - testResult.Latency = stopwatch.ElapsedMilliseconds; + testResult.Result = TcpTestStatus.TcpConnectionTestResult.Failed; } } - catch - { - testResult.Result = TcpTestStatus.TcpConnectionTestResult.Failed; - } if (Quiet.IsPresent) { From 55f598eedc9de34504a25035cdfe68f5faa8f17c Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 29 Dec 2019 11:45:08 -0800 Subject: [PATCH 12/46] Appended missing period on comments --- .../commands/management/TestConnectionCommand.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 7ffe54a3f92..d0624417e22 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -911,7 +911,7 @@ private static void OnPingComplete(object sender, PingCompletedEventArgs e) } /// - /// The class contains information about the TCP connection test + /// The class contains information about the TCP connection test. /// public class TcpTestStatus { @@ -921,7 +921,7 @@ public class TcpTestStatus /// The number of this test. /// The source machine name or IP of the test. /// The destination machine name or IP of the test. - /// The resolved IP from the destination + /// The resolved IP from the destination. /// The port used for the connection. /// The latency of the test. /// The result of the test. @@ -962,22 +962,22 @@ internal TcpTestStatus(int testNum, string source, string destination, string de public int Port { get; } /// - /// Gets or sets the latancy of the connection + /// Gets or sets the latancy of the connection. /// public long Latency { get; set; } /// - /// Gets or sets the result of the test + /// Gets or sets the result of the test. /// public TcpConnectionTestResult Result { get; set; } /// - /// Results of the detailed TCP connection test + /// Results of the detailed TCP connection test. /// public enum TcpConnectionTestResult { /// - /// Connection test has not run + /// Connection test has not run. /// New, @@ -997,7 +997,7 @@ public enum TcpConnectionTestResult Timeout, /// - /// Test was cancelled + /// Test was cancelled. /// Cancelled } From 7cb7621b6d230615c9f3ce93feec1c0245aa18d6 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 29 Dec 2019 11:55:52 -0800 Subject: [PATCH 13/46] Change TcpTestStatus to TcpPortStatus for better clarity --- .../management/TestConnectionCommand.cs | 20 +++++++++---------- .../PowerShellCore_format_ps1xml.cs | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index d0624417e22..f692a9a1410 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -28,7 +28,7 @@ namespace Microsoft.PowerShell.Commands [OutputType(typeof(PingMtuStatus), ParameterSetName = new string[] { MtuSizeDetectParameterSet })] [OutputType(typeof(int), ParameterSetName = new string[] { MtuSizeDetectParameterSet })] [OutputType(typeof(TraceStatus), ParameterSetName = new string[] { TraceRouteParameterSet })] - [OutputType(typeof(TcpTestStatus), ParameterSetName = new string[] { TcpPortParameterSet })] + [OutputType(typeof(TcpPortStatus), ParameterSetName = new string[] { TcpPortParameterSet })] public class TestConnectionCommand : PSCmdlet, IDisposable { #region Parameter Set Names @@ -310,14 +310,14 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) for (var i = 1; i <= Count; i++) { - TcpTestStatus testResult = new TcpTestStatus( + TcpPortStatus testResult = new TcpPortStatus( i, Source, targetNameOrAddress, targetAddress.ToString(), TcpPort, 0, - TcpTestStatus.TcpConnectionTestResult.New + TcpPortStatus.TcpConnectionTestResult.New ); using(TcpClient client = new TcpClient()) @@ -335,25 +335,25 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (timeoutTask.Status == TaskStatus.Faulted || timeoutTask.Status == TaskStatus.Canceled) { - testResult.Result = TcpTestStatus.TcpConnectionTestResult.Cancelled; + testResult.Result = TcpPortStatus.TcpConnectionTestResult.Cancelled; return; } if (timeoutTask.Status == TaskStatus.RanToCompletion) { - testResult.Result = TcpTestStatus.TcpConnectionTestResult.Timeout; + testResult.Result = TcpPortStatus.TcpConnectionTestResult.Timeout; } if (connectionTask.Status == TaskStatus.RanToCompletion) { successfulConnections++; - testResult.Result = TcpTestStatus.TcpConnectionTestResult.Success; + testResult.Result = TcpPortStatus.TcpConnectionTestResult.Success; testResult.Latency = stopwatch.ElapsedMilliseconds; } } catch { - testResult.Result = TcpTestStatus.TcpConnectionTestResult.Failed; + testResult.Result = TcpPortStatus.TcpConnectionTestResult.Failed; } } @@ -913,10 +913,10 @@ private static void OnPingComplete(object sender, PingCompletedEventArgs e) /// /// The class contains information about the TCP connection test. /// - public class TcpTestStatus + public class TcpPortStatus { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The number of this test. /// The source machine name or IP of the test. @@ -925,7 +925,7 @@ public class TcpTestStatus /// The port used for the connection. /// The latency of the test. /// The result of the test. - internal TcpTestStatus(int testNum, string source, string destination, string destinationAddress, int port, long latency, TcpConnectionTestResult result) + internal TcpPortStatus(int testNum, string source, string destination, string destinationAddress, int port, long latency, TcpConnectionTestResult result) { TestNum = testNum; Source = source; diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 08eba8a0f78..3f1bdc4e9a9 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -241,8 +241,8 @@ internal static IEnumerable GetFormatData() ViewsOf_Microsoft_PowerShell_MarkdownRender_MarkdownOptionInfo()); yield return new ExtendedTypeDefinition( - "Microsoft.PowerShell.Commands.TestConnectionCommand+TcpTestStatus", - ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TcpTestStatus()); + "Microsoft.PowerShell.Commands.TestConnectionCommand+TcpPortStatus", + ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TcpPortStatus()); yield return new ExtendedTypeDefinition( "Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", @@ -1841,11 +1841,11 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Ma .EndEntry() .EndList()); } - - private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TcpTestStatus() + + private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TcpPortStatus() { yield return new FormatViewDefinition( - "Microsoft.PowerShell.Commands.TestConnectionCommand+TcpTestStatus", + "Microsoft.PowerShell.Commands.TestConnectionCommand+TcpPortStatus", TableControl.Create() .AddHeader(Alignment.Right, label: "Test", width: 4) .AddHeader(Alignment.Left, label: "Source", width: 16) From c0960d8c3a91da4d655d8c8501dd082ad33849b8 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 29 Dec 2019 20:47:48 -0800 Subject: [PATCH 14/46] Set default count back to 4 --- .../management/TestConnectionCommand.cs | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index f692a9a1410..96005458cb8 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -134,14 +134,13 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public int MaxHops { get; set; } = DefaultMaxHops; /// - /// Gets or sets the number of test attempts. - /// The value is -1 if not set explicitly as a parameter - /// If not set explicitly, the value is set depending on type of test (ping = 4, tcp = 1) + /// Gets or sets the number of ping attempts. + /// The default (from Windows) is 4 times. /// [Parameter(ParameterSetName = DefaultPingParameterSet)] [Parameter(ParameterSetName = TcpPortParameterSet)] [ValidateRange(ValidateRangeKind.Positive)] - public int Count { get; set; } = -1; + public int Count { get; set; } = 4; /// /// Gets or sets the delay between ping attempts. @@ -239,21 +238,11 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// protected override void BeginProcessing() { - if (Count == -1) - { - if (ParameterSetName == TcpPortParameterSet) - { - Count = 1; - } - else - { - Count = 4; - } - } - - if (Repeat.IsPresent) + switch (ParameterSetName) { + case RepeatPingParameterSet: Count = int.MaxValue; + break; } } @@ -301,6 +290,15 @@ protected override void StopProcessing() private void ProcessConnectionByTCPPort(string targetNameOrAddress) { + if(Repeat.IsPresent) + { + Count = int.MaxValue; + } + else if (!MyInvocation.BoundParameters.ContainsKey("Count")) + { + Count = 1; + } + if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress)) { return; From 6edaa80831a663f43853a049c9b8e12099220c00 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 29 Dec 2019 21:22:54 -0800 Subject: [PATCH 15/46] Move stopwatch reset into finally block --- .../commands/management/TestConnectionCommand.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 96005458cb8..a41d817fab2 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -318,11 +318,12 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) TcpPortStatus.TcpConnectionTestResult.New ); + Stopwatch stopwatch = new Stopwatch(); + using(TcpClient client = new TcpClient()) { try { - Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); @@ -353,6 +354,10 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) { testResult.Result = TcpPortStatus.TcpConnectionTestResult.Failed; } + finally + { + stopwatch.Reset(); + } } if (Quiet.IsPresent) From aa19612ab9c864072fe2d548a99be1998ed45606 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 29 Dec 2019 22:34:17 -0800 Subject: [PATCH 16/46] Change TcpConnectionTestResult.New to TcpConnectionTestResult.None --- .../commands/management/TestConnectionCommand.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index a41d817fab2..e023f0ffef5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -315,7 +315,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) targetAddress.ToString(), TcpPort, 0, - TcpPortStatus.TcpConnectionTestResult.New + TcpPortStatus.TcpConnectionTestResult.None ); Stopwatch stopwatch = new Stopwatch(); @@ -982,7 +982,7 @@ public enum TcpConnectionTestResult /// /// Connection test has not run. /// - New, + None, /// /// Connection was successful. From dd904cc9db1c2b1dd6a4dde78127bf69d7f6cd30 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 29 Dec 2019 22:38:36 -0800 Subject: [PATCH 17/46] Formatting fixes --- .../commands/management/TestConnectionCommand.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e023f0ffef5..8c6aa23bddd 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -290,7 +290,7 @@ protected override void StopProcessing() private void ProcessConnectionByTCPPort(string targetNameOrAddress) { - if(Repeat.IsPresent) + if (Repeat.IsPresent) { Count = int.MaxValue; } @@ -320,7 +320,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) Stopwatch stopwatch = new Stopwatch(); - using(TcpClient client = new TcpClient()) + using (TcpClient client = new TcpClient()) { try { @@ -367,7 +367,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) WriteObject(false); return; } - else if(i == Count) + else if (i == Count) { WriteObject(true); return; From 24053d3a7b8d6bf5e78b40f36e12f2af38d84aef Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 2 Jan 2020 00:17:25 -0800 Subject: [PATCH 18/46] Increase column size for Result output --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 3f1bdc4e9a9..9a089ed0b4d 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1853,7 +1853,7 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .AddHeader(Alignment.Left, label: "DestinationAddress", width: 18) .AddHeader(Alignment.Right, label: "Port", width: 7) .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) - .AddHeader(Alignment.Left, label: "Result", width: 16) + .AddHeader(Alignment.Left, label: "Result", width: 24) .StartRowDefinition() .AddPropertyColumn("TestNum") .AddPropertyColumn("Source") From 6e6d9d2bb5c4f5c41a4ec286212372444e0ca284 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 2 Jan 2020 00:55:45 -0800 Subject: [PATCH 19/46] Update logic for -quiet option --- .../commands/management/TestConnectionCommand.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 8c6aa23bddd..bba247cf0b1 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -304,8 +304,6 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) return; } - int successfulConnections = 0; - for (var i = 1; i <= Count; i++) { TcpPortStatus testResult = new TcpPortStatus( @@ -345,7 +343,6 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (connectionTask.Status == TaskStatus.RanToCompletion) { - successfulConnections++; testResult.Result = TcpPortStatus.TcpConnectionTestResult.Success; testResult.Latency = stopwatch.ElapsedMilliseconds; } @@ -362,14 +359,14 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (Quiet.IsPresent) { - if (i > successfulConnections) + if (testResult.Result == TcpPortStatus.TcpConnectionTestResult.Success) { - WriteObject(false); + WriteObject(true); return; } else if (i == Count) { - WriteObject(true); + WriteObject(false); return; } } From 042ddbaec7ebddf93cec27c5fdaaccbe245e0355 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 2 Jan 2020 01:38:53 -0800 Subject: [PATCH 20/46] Change "TestNum" property to "Id" --- .../commands/management/TestConnectionCommand.cs | 8 ++++---- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 4 ++-- .../Test-Connection.Tests.ps1 | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index bba247cf0b1..34bc64a81ce 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -918,16 +918,16 @@ public class TcpPortStatus /// /// Initializes a new instance of the class. /// - /// The number of this test. + /// The number of this test. /// The source machine name or IP of the test. /// The destination machine name or IP of the test. /// The resolved IP from the destination. /// The port used for the connection. /// The latency of the test. /// The result of the test. - internal TcpPortStatus(int testNum, string source, string destination, string destinationAddress, int port, long latency, TcpConnectionTestResult result) + internal TcpPortStatus(int id, string source, string destination, string destinationAddress, int port, long latency, TcpConnectionTestResult result) { - TestNum = testNum; + Id = id; Source = source; Destination = destination; DestinationAddress = destinationAddress; @@ -939,7 +939,7 @@ internal TcpPortStatus(int testNum, string source, string destination, string de /// /// Gets and sets the count of the test. /// - public int TestNum { get; set; } + public int Id { get; set; } /// /// Gets the source from which the test was sent. diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 9a089ed0b4d..e3869d8ee6b 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1847,7 +1847,7 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co yield return new FormatViewDefinition( "Microsoft.PowerShell.Commands.TestConnectionCommand+TcpPortStatus", TableControl.Create() - .AddHeader(Alignment.Right, label: "Test", width: 4) + .AddHeader(Alignment.Right, label: "Id", width: 4) .AddHeader(Alignment.Left, label: "Source", width: 16) .AddHeader(Alignment.Left, label: "Destination", width: 16) .AddHeader(Alignment.Left, label: "DestinationAddress", width: 18) @@ -1855,7 +1855,7 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) .AddHeader(Alignment.Left, label: "Result", width: 24) .StartRowDefinition() - .AddPropertyColumn("TestNum") + .AddPropertyColumn("Id") .AddPropertyColumn("Source") .AddPropertyColumn("Destination") .AddPropertyColumn("DestinationAddress") diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index 5120c2db0c2..dcb540afb42 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -303,7 +303,7 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $result = Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort $result.Count | Should -Be 1 - $result[0].TestNum | Should -BeExactly 1 + $result[0].Id | Should -BeExactly 1 $result[0].Destination | Should -BeExactly '127.0.0.1' $result[0].DestinationAddress | Should -BeExactly '127.0.0.1' $result[0].Port | Should -Be $WebListener.HttpPort @@ -315,7 +315,7 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $result = Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort -Count 2 $result.Count | Should -Be 2 - $result[0].TestNum | Should -BeExactly 1 + $result[0].Id | Should -BeExactly 1 $result[0].Destination | Should -BeExactly '127.0.0.1' $result[0].DestinationAddress | Should -BeExactly '127.0.0.1' $result[0].Port | Should -Be $WebListener.HttpPort @@ -327,7 +327,7 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $result = Test-Connection $UnreachableAddress -TcpPort 80 -TimeOut 1 $result.Count | Should -Be 1 - $result[0].TestNum | Should -BeExactly 1 + $result[0].Id | Should -BeExactly 1 $result[0].Destination | Should -BeExactly $UnreachableAddress $result[0].DestinationAddress | Should -BeExactly $UnreachableAddress $result[0].Port | Should -Be 80 From 69091a80fc9676a96b9b10e7ddf04a7a4577bb9c Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Fri, 3 Jan 2020 01:03:28 -0800 Subject: [PATCH 21/46] Change Destination and DestinationAddress to Target and TargetAddress in TcpPortStatus --- .../management/TestConnectionCommand.cs | 20 +++++++++---------- .../PowerShellCore_format_ps1xml.cs | 10 +++++----- .../Test-Connection.Tests.ps1 | 12 +++++------ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 34bc64a81ce..52e677d5ed5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -310,7 +310,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) i, Source, targetNameOrAddress, - targetAddress.ToString(), + targetAddress, TcpPort, 0, TcpPortStatus.TcpConnectionTestResult.None @@ -920,17 +920,17 @@ public class TcpPortStatus /// /// The number of this test. /// The source machine name or IP of the test. - /// The destination machine name or IP of the test. - /// The resolved IP from the destination. + /// The target machine name or IP of the test. + /// The resolved IP from the target. /// The port used for the connection. /// The latency of the test. /// The result of the test. - internal TcpPortStatus(int id, string source, string destination, string destinationAddress, int port, long latency, TcpConnectionTestResult result) + internal TcpPortStatus(int id, string source, string target, IPAddress targetAddress, int port, long latency, TcpConnectionTestResult result) { Id = id; Source = source; - Destination = destination; - DestinationAddress = destinationAddress; + Target = target; + TargetAddress = targetAddress; Port = port; Latency = latency; Result = result; @@ -947,14 +947,14 @@ internal TcpPortStatus(int id, string source, string destination, string destina public string Source { get; } /// - /// Gets the destination name. + /// Gets the target name. /// - public string Destination { get; } + public string Target { get; } /// - /// Gets the resolved address for the destination. + /// Gets the resolved address for the target. /// - public string DestinationAddress { get; } + public IPAddress TargetAddress { get; } /// /// Gets the port used for the test. diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index e3869d8ee6b..8e4b2be8d2b 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1849,21 +1849,21 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co TableControl.Create() .AddHeader(Alignment.Right, label: "Id", width: 4) .AddHeader(Alignment.Left, label: "Source", width: 16) - .AddHeader(Alignment.Left, label: "Destination", width: 16) - .AddHeader(Alignment.Left, label: "DestinationAddress", width: 18) + .AddHeader(Alignment.Left, label: "Target", width: 16) + .AddHeader(Alignment.Left, label: "TargetAddress", width: 18) .AddHeader(Alignment.Right, label: "Port", width: 7) .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) .AddHeader(Alignment.Left, label: "Result", width: 24) .StartRowDefinition() .AddPropertyColumn("Id") .AddPropertyColumn("Source") - .AddPropertyColumn("Destination") - .AddPropertyColumn("DestinationAddress") + .AddPropertyColumn("Target") + .AddPropertyColumn("TargetAddress") .AddPropertyColumn("Port") .AddPropertyColumn("Latency") .AddPropertyColumn("Result") .EndRowDefinition() - .GroupByProperty("Destination") + .GroupByProperty("Target") .EndTable()); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index dcb540afb42..606fb4e0ef6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -304,8 +304,8 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $result.Count | Should -Be 1 $result[0].Id | Should -BeExactly 1 - $result[0].Destination | Should -BeExactly '127.0.0.1' - $result[0].DestinationAddress | Should -BeExactly '127.0.0.1' + $result[0].Target | Should -BeExactly '127.0.0.1' + $result[0].TargetAddress | Should -BeExactly '127.0.0.1' $result[0].Port | Should -Be $WebListener.HttpPort $result[0].Latency | Should -BeGreaterOrEqual 0 $result[0].Result | Should -BeExactly 'Success' @@ -316,8 +316,8 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $result.Count | Should -Be 2 $result[0].Id | Should -BeExactly 1 - $result[0].Destination | Should -BeExactly '127.0.0.1' - $result[0].DestinationAddress | Should -BeExactly '127.0.0.1' + $result[0].Target | Should -BeExactly '127.0.0.1' + $result[0].TargetAddress | Should -BeExactly '127.0.0.1' $result[0].Port | Should -Be $WebListener.HttpPort $result[0].Latency | Should -BeGreaterOrEqual 0 $result[0].Result | Should -BeExactly 'Success' @@ -328,8 +328,8 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $result.Count | Should -Be 1 $result[0].Id | Should -BeExactly 1 - $result[0].Destination | Should -BeExactly $UnreachableAddress - $result[0].DestinationAddress | Should -BeExactly $UnreachableAddress + $result[0].Target | Should -BeExactly $UnreachableAddress + $result[0].TargetAddress | Should -BeExactly $UnreachableAddress $result[0].Port | Should -Be 80 $result[0].Latency | Should -BeExactly 0 $result[0].Result | Should -Not -BeExactly 'Success' From bb527035922ce98a5a693af1bd5f7d0f290d2054 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Fri, 3 Jan 2020 01:29:37 -0800 Subject: [PATCH 22/46] Implement new form of using declaration --- .../management/TestConnectionCommand.cs | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 52e677d5ed5..ebbffd79a73 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -318,44 +318,43 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) Stopwatch stopwatch = new Stopwatch(); - using (TcpClient client = new TcpClient()) - { - try - { - stopwatch.Start(); + using TcpClient client = new TcpClient(); - Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); - Task timeoutTask = Task.Delay(new TimeSpan(0, 0, TimeoutSeconds)); + try + { + stopwatch.Start(); - Task.WhenAny(connectionTask, timeoutTask).Result.Wait(); - stopwatch.Stop(); + Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); + Task timeoutTask = Task.Delay(new TimeSpan(0, 0, TimeoutSeconds)); - if (timeoutTask.Status == TaskStatus.Faulted || timeoutTask.Status == TaskStatus.Canceled) - { - testResult.Result = TcpPortStatus.TcpConnectionTestResult.Cancelled; - return; - } - - if (timeoutTask.Status == TaskStatus.RanToCompletion) - { - testResult.Result = TcpPortStatus.TcpConnectionTestResult.Timeout; - } + Task.WhenAny(connectionTask, timeoutTask).Result.Wait(); + stopwatch.Stop(); - if (connectionTask.Status == TaskStatus.RanToCompletion) - { - testResult.Result = TcpPortStatus.TcpConnectionTestResult.Success; - testResult.Latency = stopwatch.ElapsedMilliseconds; - } + if (timeoutTask.Status == TaskStatus.Faulted || timeoutTask.Status == TaskStatus.Canceled) + { + testResult.Result = TcpPortStatus.TcpConnectionTestResult.Cancelled; + return; } - catch + + if (timeoutTask.Status == TaskStatus.RanToCompletion) { - testResult.Result = TcpPortStatus.TcpConnectionTestResult.Failed; + testResult.Result = TcpPortStatus.TcpConnectionTestResult.Timeout; } - finally + + if (connectionTask.Status == TaskStatus.RanToCompletion) { - stopwatch.Reset(); + testResult.Result = TcpPortStatus.TcpConnectionTestResult.Success; + testResult.Latency = stopwatch.ElapsedMilliseconds; } } + catch + { + testResult.Result = TcpPortStatus.TcpConnectionTestResult.Failed; + } + finally + { + stopwatch.Reset(); + } if (Quiet.IsPresent) { From 9d7bbb81df0901c5240edba147214c5d7e19da60 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 5 Jan 2020 23:46:48 -0800 Subject: [PATCH 23/46] Fix indentation --- .../commands/management/TestConnectionCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index ebbffd79a73..1571a7a7808 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -241,7 +241,7 @@ protected override void BeginProcessing() switch (ParameterSetName) { case RepeatPingParameterSet: - Count = int.MaxValue; + Count = int.MaxValue; break; } } From f6bf3b6a61bf9ab0f95ca408b77283568f94f168 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Tue, 7 Jan 2020 01:05:41 -0800 Subject: [PATCH 24/46] Update output, improve logic and update tests --- .../management/TestConnectionCommand.cs | 81 +++++++------------ .../PowerShellCore_format_ps1xml.cs | 10 +-- .../Test-Connection.Tests.ps1 | 12 +-- 3 files changed, 38 insertions(+), 65 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 1571a7a7808..6221b6a2956 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -313,7 +313,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) targetAddress, TcpPort, 0, - TcpPortStatus.TcpConnectionTestResult.None + false ); Stopwatch stopwatch = new Stopwatch(); @@ -324,32 +324,31 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) { stopwatch.Start(); - Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); - Task timeoutTask = Task.Delay(new TimeSpan(0, 0, TimeoutSeconds)); - - Task.WhenAny(connectionTask, timeoutTask).Result.Wait(); - stopwatch.Stop(); - - if (timeoutTask.Status == TaskStatus.Faulted || timeoutTask.Status == TaskStatus.Canceled) - { - testResult.Result = TcpPortStatus.TcpConnectionTestResult.Cancelled; - return; - } - - if (timeoutTask.Status == TaskStatus.RanToCompletion) + if (client.ConnectAsync(targetAddress, TcpPort).Wait(TimeoutSeconds * 1000)) { - testResult.Result = TcpPortStatus.TcpConnectionTestResult.Timeout; + testResult.Connected = true; + testResult.Latency = stopwatch.ElapsedMilliseconds; } - - if (connectionTask.Status == TaskStatus.RanToCompletion) + else { - testResult.Result = TcpPortStatus.TcpConnectionTestResult.Success; - testResult.Latency = stopwatch.ElapsedMilliseconds; + testResult.Status = SocketError.TimedOut; } } - catch + catch (AggregateException ae) { - testResult.Result = TcpPortStatus.TcpConnectionTestResult.Failed; + ae.Handle((ex) => + { + if (ex is SocketException) + { + SocketException? socketException = ex as SocketException; + testResult.Status = socketException!.SocketErrorCode; + return true; + } + else + { + return false; + } + }); } finally { @@ -358,7 +357,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (Quiet.IsPresent) { - if (testResult.Result == TcpPortStatus.TcpConnectionTestResult.Success) + if (testResult.Connected) { WriteObject(true); return; @@ -923,8 +922,8 @@ public class TcpPortStatus /// The resolved IP from the target. /// The port used for the connection. /// The latency of the test. - /// The result of the test. - internal TcpPortStatus(int id, string source, string target, IPAddress targetAddress, int port, long latency, TcpConnectionTestResult result) + /// If the test connection succeeded. + internal TcpPortStatus(int id, string source, string target, IPAddress targetAddress, int port, long latency, bool connected) { Id = id; Source = source; @@ -932,7 +931,7 @@ internal TcpPortStatus(int id, string source, string target, IPAddress targetAdd TargetAddress = targetAddress; Port = port; Latency = latency; - Result = result; + Connected = connected; } /// @@ -968,38 +967,12 @@ internal TcpPortStatus(int id, string source, string target, IPAddress targetAdd /// /// Gets or sets the result of the test. /// - public TcpConnectionTestResult Result { get; set; } + public bool Connected { get; set; } /// - /// Results of the detailed TCP connection test. + /// Gets or sets the state of the socket after the test. /// - public enum TcpConnectionTestResult - { - /// - /// Connection test has not run. - /// - None, - - /// - /// Connection was successful. - /// - Success, - - /// - /// Test was not able to run. - /// - Failed, - - /// - /// Connection timed out. - /// - Timeout, - - /// - /// Test was cancelled. - /// - Cancelled - } + public SocketError Status { get; set; } } /// diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 8e4b2be8d2b..d795fcf7000 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1849,19 +1849,19 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co TableControl.Create() .AddHeader(Alignment.Right, label: "Id", width: 4) .AddHeader(Alignment.Left, label: "Source", width: 16) - .AddHeader(Alignment.Left, label: "Target", width: 16) - .AddHeader(Alignment.Left, label: "TargetAddress", width: 18) + .AddHeader(Alignment.Left, label: "Address", width: 25) .AddHeader(Alignment.Right, label: "Port", width: 7) .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) - .AddHeader(Alignment.Left, label: "Result", width: 24) + .AddHeader(Alignment.Left, label: "Connected", width: 10) + .AddHeader(Alignment.Left, label: "Status", width: 24) .StartRowDefinition() .AddPropertyColumn("Id") .AddPropertyColumn("Source") - .AddPropertyColumn("Target") .AddPropertyColumn("TargetAddress") .AddPropertyColumn("Port") .AddPropertyColumn("Latency") - .AddPropertyColumn("Result") + .AddPropertyColumn("Connected") + .AddPropertyColumn("Status") .EndRowDefinition() .GroupByProperty("Target") .EndTable()); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index 606fb4e0ef6..58decdfeaf9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -304,11 +304,11 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $result.Count | Should -Be 1 $result[0].Id | Should -BeExactly 1 - $result[0].Target | Should -BeExactly '127.0.0.1' $result[0].TargetAddress | Should -BeExactly '127.0.0.1' $result[0].Port | Should -Be $WebListener.HttpPort $result[0].Latency | Should -BeGreaterOrEqual 0 - $result[0].Result | Should -BeExactly 'Success' + $result[0].Connected | Should -BeTrue + $result[0].Status | Should -BeExactly 'Success' } It "Test detailed connection to local host on working port with modified count" { @@ -316,11 +316,11 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $result.Count | Should -Be 2 $result[0].Id | Should -BeExactly 1 - $result[0].Target | Should -BeExactly '127.0.0.1' $result[0].TargetAddress | Should -BeExactly '127.0.0.1' $result[0].Port | Should -Be $WebListener.HttpPort $result[0].Latency | Should -BeGreaterOrEqual 0 - $result[0].Result | Should -BeExactly 'Success' + $result[0].Connected | Should -BeTrue + $result[0].Status | Should -BeExactly 'Success' } It "Test detailed connection to unreachable host port 80" { @@ -328,10 +328,10 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $result.Count | Should -Be 1 $result[0].Id | Should -BeExactly 1 - $result[0].Target | Should -BeExactly $UnreachableAddress $result[0].TargetAddress | Should -BeExactly $UnreachableAddress $result[0].Port | Should -Be 80 $result[0].Latency | Should -BeExactly 0 - $result[0].Result | Should -Not -BeExactly 'Success' + $result[0].Connected | Should -BeFalse + $result[0].Status | Should -Not -BeExactly 'Success' } } From fa04847b7f9ecdebed3379dcddd1ddf20769e08a Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Wed, 8 Jan 2020 00:26:05 -0800 Subject: [PATCH 25/46] Add CancellationToken to allow cancel before timeout --- .../management/TestConnectionCommand.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 6221b6a2956..97966ba7e7a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -69,6 +69,20 @@ public class TestConnectionCommand : PSCmdlet, IDisposable #endregion + #region Cancellation support + + private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); + + private CancellationToken cancellationToken + { + get + { + return cancellationTokenSource.Token; + } + } + + #endregion + #region Parameters /// @@ -284,6 +298,7 @@ protected override void ProcessRecord() protected override void StopProcessing() { _sender?.SendAsyncCancel(); + cancellationTokenSource.Cancel(); } #region ConnectionTest @@ -324,7 +339,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) { stopwatch.Start(); - if (client.ConnectAsync(targetAddress, TcpPort).Wait(TimeoutSeconds * 1000)) + if (client.ConnectAsync(targetAddress, TcpPort).Wait(TimeoutSeconds * 1000, cancellationToken)) { testResult.Connected = true; testResult.Latency = stopwatch.ElapsedMilliseconds; From 57878210116af051fbce5f2504ca5f5336d39d1f Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Wed, 8 Jan 2020 22:41:23 -0800 Subject: [PATCH 26/46] Assign testResult.Connected to client.Connected --- .../commands/management/TestConnectionCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 97966ba7e7a..81e3b12c97d 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -341,7 +341,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (client.ConnectAsync(targetAddress, TcpPort).Wait(TimeoutSeconds * 1000, cancellationToken)) { - testResult.Connected = true; + testResult.Connected = client.Connected; testResult.Latency = stopwatch.ElapsedMilliseconds; } else From 750b3a473a542a4956943624df4d07f6348d68a4 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 13 Jan 2020 00:01:45 -0800 Subject: [PATCH 27/46] Move TcpPortStatus object creation to after connection test --- .../management/TestConnectionCommand.cs | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 81e3b12c97d..529779af15c 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -321,15 +321,8 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) for (var i = 1; i <= Count; i++) { - TcpPortStatus testResult = new TcpPortStatus( - i, - Source, - targetNameOrAddress, - targetAddress, - TcpPort, - 0, - false - ); + long latency = 0; + SocketError status = SocketError.SocketError; Stopwatch stopwatch = new Stopwatch(); @@ -341,12 +334,12 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (client.ConnectAsync(targetAddress, TcpPort).Wait(TimeoutSeconds * 1000, cancellationToken)) { - testResult.Connected = client.Connected; - testResult.Latency = stopwatch.ElapsedMilliseconds; + latency = stopwatch.ElapsedMilliseconds; + status = SocketError.Success; } else { - testResult.Status = SocketError.TimedOut; + status = SocketError.TimedOut; } } catch (AggregateException ae) @@ -356,7 +349,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (ex is SocketException) { SocketException? socketException = ex as SocketException; - testResult.Status = socketException!.SocketErrorCode; + status = socketException!.SocketErrorCode; return true; } else @@ -372,7 +365,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (Quiet.IsPresent) { - if (testResult.Connected) + if (status == SocketError.Success) { WriteObject(true); return; @@ -385,7 +378,16 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) } else { - WriteObject(testResult); + WriteObject(new TcpPortStatus( + i, + Source, + targetNameOrAddress, + targetAddress, + TcpPort, + latency, + status == SocketError.Success, + status + )); } Thread.Sleep(new TimeSpan(0, 0, Delay)); @@ -938,7 +940,8 @@ public class TcpPortStatus /// The port used for the connection. /// The latency of the test. /// If the test connection succeeded. - internal TcpPortStatus(int id, string source, string target, IPAddress targetAddress, int port, long latency, bool connected) + /// Status of the underlying socket. + internal TcpPortStatus(int id, string source, string target, IPAddress targetAddress, int port, long latency, bool connected, SocketError status) { Id = id; Source = source; @@ -947,6 +950,7 @@ internal TcpPortStatus(int id, string source, string target, IPAddress targetAdd Port = port; Latency = latency; Connected = connected; + Status = status; } /// From 0e8397717660189c3ead9620bdda061584f5eab3 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 13 Jan 2020 00:16:53 -0800 Subject: [PATCH 28/46] Remove delay from final connection test in loop --- .../commands/management/TestConnectionCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 529779af15c..881a6eed17e 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -390,7 +390,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) )); } - Thread.Sleep(new TimeSpan(0, 0, Delay)); + Task.Delay(new TimeSpan(0, 0, i == Count ? 0 : Delay)).Wait(cancellationToken); } } From 8a6011beb75fdd3a20508e0656df38a732166787 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 13 Jan 2020 00:55:49 -0800 Subject: [PATCH 29/46] Move Count logic from ProcessConnectionByTCPPort into own function --- .../commands/management/TestConnectionCommand.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 881a6eed17e..958e378bb2f 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -303,7 +303,7 @@ protected override void StopProcessing() #region ConnectionTest - private void ProcessConnectionByTCPPort(string targetNameOrAddress) + private void SetCountForTcpTest() { if (Repeat.IsPresent) { @@ -311,8 +311,13 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) } else if (!MyInvocation.BoundParameters.ContainsKey("Count")) { - Count = 1; + Count = 1; } + } + + private void ProcessConnectionByTCPPort(string targetNameOrAddress) + { + SetCountForTcpTest(); if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress)) { From c4a46b798b7ec0b7b9fb09306a968796a5cbf399 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Tue, 21 Jan 2020 23:42:09 -0800 Subject: [PATCH 30/46] Fix small formatting error --- .../commands/management/TestConnectionCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e6f3a1fd9e5..165d1aa99df 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -307,7 +307,7 @@ private void SetCountForTcpTest() } else if (!MyInvocation.BoundParameters.ContainsKey("Count")) { - Count = 1; + Count = 1; } } From 19b560ae1d725b3c3608c4cb7aab695f18e68ea3 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Wed, 22 Jan 2020 08:13:48 -0800 Subject: [PATCH 31/46] Add handling for TaskCanceledException --- .../commands/management/TestConnectionCommand.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 165d1aa99df..f27045594c8 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -347,6 +347,10 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) { ae.Handle((ex) => { + if (ex is TaskCanceledException) + { + throw new PipelineStoppedException(); + } if (ex is SocketException) { SocketException? socketException = ex as SocketException; From 9196819277df826c8cd56f3611ed8e326d9c3a83 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 23 Jan 2020 07:37:37 -0800 Subject: [PATCH 32/46] Remove redundant CancellationToken getter --- .../commands/management/TestConnectionCommand.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index f27045594c8..491ef35b172 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -69,14 +69,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - private CancellationToken cancellationToken - { - get - { - return cancellationTokenSource.Token; - } - } - #endregion #region Parameters @@ -333,7 +325,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) { stopwatch.Start(); - if (client.ConnectAsync(targetAddress, TcpPort).Wait(TimeoutSeconds * 1000, cancellationToken)) + if (client.ConnectAsync(targetAddress, TcpPort).Wait(TimeoutSeconds * 1000, cancellationTokenSource.Token)) { latency = stopwatch.ElapsedMilliseconds; status = SocketError.Success; @@ -395,7 +387,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) )); } - Task.Delay(new TimeSpan(0, 0, i == Count ? 0 : Delay)).Wait(cancellationToken); + Task.Delay(new TimeSpan(0, 0, i == Count ? 0 : Delay)).Wait(cancellationTokenSource.Token); } } From 05467647f393db40b2aef7e04a312bbe03549c95 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 23 Jan 2020 22:40:23 -0800 Subject: [PATCH 33/46] Clean up handling of SocketException --- .../commands/management/TestConnectionCommand.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 491ef35b172..202b0dddc58 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -343,10 +343,9 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) { throw new PipelineStoppedException(); } - if (ex is SocketException) + if (ex is SocketException socketException) { - SocketException? socketException = ex as SocketException; - status = socketException!.SocketErrorCode; + status = socketException.SocketErrorCode; return true; } else From c899f2c12dc4afb5f68921cb66499aba94a9aefe Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Fri, 24 Jan 2020 20:33:16 -0800 Subject: [PATCH 34/46] Increase readability of final loop check --- .../commands/management/TestConnectionCommand.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 202b0dddc58..ffbc4e55ab6 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -386,7 +386,10 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) )); } - Task.Delay(new TimeSpan(0, 0, i == Count ? 0 : Delay)).Wait(cancellationTokenSource.Token); + if (i < Count) + { + Task.Delay(new TimeSpan(0, 0, Delay)).Wait(cancellationTokenSource.Token); + } } } From 558bda4284470b7ec5c61597b962d14c0cca0600 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 26 Jan 2020 22:04:38 -0800 Subject: [PATCH 35/46] Remove unnecessary TimeSpan invocation --- .../commands/management/TestConnectionCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index ffbc4e55ab6..f318f845893 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -388,7 +388,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (i < Count) { - Task.Delay(new TimeSpan(0, 0, Delay)).Wait(cancellationTokenSource.Token); + Task.Delay(Delay * 1000).Wait(cancellationTokenSource.Token); } } } From 48d9434260824b22c7146ce08041b2346f6d5dd7 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 26 Jan 2020 23:16:07 -0800 Subject: [PATCH 36/46] Rename cancellationTokenSource to match convention --- .../commands/management/TestConnectionCommand.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index f318f845893..ad430011e0e 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -67,7 +67,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable #region Cancellation support - private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); + private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); #endregion @@ -286,7 +286,7 @@ protected override void ProcessRecord() protected override void StopProcessing() { _sender?.SendAsyncCancel(); - cancellationTokenSource.Cancel(); + _cancellationTokenSource.Cancel(); } #region ConnectionTest @@ -325,7 +325,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) { stopwatch.Start(); - if (client.ConnectAsync(targetAddress, TcpPort).Wait(TimeoutSeconds * 1000, cancellationTokenSource.Token)) + if (client.ConnectAsync(targetAddress, TcpPort).Wait(TimeoutSeconds * 1000, _cancellationTokenSource.Token)) { latency = stopwatch.ElapsedMilliseconds; status = SocketError.Success; @@ -388,7 +388,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (i < Count) { - Task.Delay(Delay * 1000).Wait(cancellationTokenSource.Token); + Task.Delay(Delay * 1000).Wait(_cancellationTokenSource.Token); } } } From 980bcbedcba24a8e401932bd68cfad5b5e50ccf0 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 27 Jan 2020 00:27:08 -0800 Subject: [PATCH 37/46] Move SetCountForTcpTest() into BeginProcessing() --- .../commands/management/TestConnectionCommand.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index ad430011e0e..de2d5bdacf0 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -245,6 +245,9 @@ protected override void BeginProcessing() case RepeatPingParameterSet: Count = int.MaxValue; break; + case TcpPortParameterSet: + SetCountForTcpTest(); + break; } } @@ -305,8 +308,6 @@ private void SetCountForTcpTest() private void ProcessConnectionByTCPPort(string targetNameOrAddress) { - SetCountForTcpTest(); - if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress)) { return; From a44c3a3806c762381d3c75b05a7c6578e6e6c9e1 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 30 Jan 2020 01:33:34 -0800 Subject: [PATCH 38/46] Added nameof to Count check --- .../commands/management/TestConnectionCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index de2d5bdacf0..b05c5c591b5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -300,7 +300,7 @@ private void SetCountForTcpTest() { Count = int.MaxValue; } - else if (!MyInvocation.BoundParameters.ContainsKey("Count")) + else if (!MyInvocation.BoundParameters.ContainsKey(nameof(Count))) { Count = 1; } From b7be2d315dd7b068a347f7ebe6de41688665be0b Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 30 Jan 2020 01:42:20 -0800 Subject: [PATCH 39/46] Removed explicit type declaration for TcpClient --- .../commands/management/TestConnectionCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index b05c5c591b5..94660c380d8 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -320,7 +320,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) Stopwatch stopwatch = new Stopwatch(); - using TcpClient client = new TcpClient(); + using var client = new TcpClient(); try { From 9c9beb07f96c9f2faf119166fb13739750d78fd8 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 30 Jan 2020 19:17:36 -0800 Subject: [PATCH 40/46] Move delay and timeout evaluation out of the cycle --- .../commands/management/TestConnectionCommand.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 94660c380d8..6e7812b5ff8 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -313,6 +313,9 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) return; } + int timeoutMilliseconds = TimeoutSeconds * 1000; + int delayMilliseconds = Delay * 1000; + for (var i = 1; i <= Count; i++) { long latency = 0; @@ -326,7 +329,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) { stopwatch.Start(); - if (client.ConnectAsync(targetAddress, TcpPort).Wait(TimeoutSeconds * 1000, _cancellationTokenSource.Token)) + if (client.ConnectAsync(targetAddress, TcpPort).Wait(timeoutMilliseconds, _cancellationTokenSource.Token)) { latency = stopwatch.ElapsedMilliseconds; status = SocketError.Success; @@ -389,7 +392,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (i < Count) { - Task.Delay(Delay * 1000).Wait(_cancellationTokenSource.Token); + Task.Delay(delayMilliseconds).Wait(_cancellationTokenSource.Token); } } } From ff2436182f0295df478ea4a02a9df633ad4013eb Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Thu, 25 Jun 2020 18:32:48 -0700 Subject: [PATCH 41/46] Remove redundant CancellationTokenSource --- .../commands/management/TestConnectionCommand.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e154bc0702e..48d8699ad99 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -67,12 +67,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable #endregion - #region Cancellation support - - private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); - - #endregion - #region Parameters /// @@ -336,7 +330,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) { stopwatch.Start(); - if (client.ConnectAsync(targetAddress, TcpPort).Wait(timeoutMilliseconds, _cancellationTokenSource.Token)) + if (client.ConnectAsync(targetAddress, TcpPort).Wait(timeoutMilliseconds, _dnsLookupCancel.Token)) { latency = stopwatch.ElapsedMilliseconds; status = SocketError.Success; @@ -399,7 +393,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (i < Count) { - Task.Delay(delayMilliseconds).Wait(_cancellationTokenSource.Token); + Task.Delay(delayMilliseconds).Wait(_dnsLookupCancel.Token); } } } From 2a8aa2abc06ee6beb8f7e9ae8d7b2c0b46badb73 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Wed, 8 Jul 2020 23:16:01 -0700 Subject: [PATCH 42/46] Initialize detailed SwitchParameter --- .../commands/management/TestConnectionCommand.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 48d8699ad99..32408dad600 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -185,6 +185,13 @@ public class TestConnectionCommand : PSCmdlet, IDisposable [Parameter] public SwitchParameter Quiet; + /// + /// Gets or sets whether to enable detailed output mode while running a TCP connection test. + /// Without this flag, the TCP test will return a boolean result. + /// + [Parameter] + public SwitchParameter Detailed; + /// /// Gets or sets the timeout value for an individual ping in seconds. /// If a response is not received in this time, no response is assumed. From f92cde701c46e6dad6b560b4217004d97e32dd00 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Wed, 8 Jul 2020 23:23:53 -0700 Subject: [PATCH 43/46] Add check for Detailed parameter --- .../commands/management/TestConnectionCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 32408dad600..240bd2d881a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -371,7 +371,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) stopwatch.Reset(); } - if (Quiet.IsPresent) + if (!Detailed.IsPresent) { if (status == SocketError.Success) { From 6816d02994a72639b1111888df65606b7d45be3f Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Wed, 8 Jul 2020 23:45:20 -0700 Subject: [PATCH 44/46] Remove redundant tests for non-detailed output --- .../commands/management/TestConnectionCommand.cs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 240bd2d881a..f467cb4a752 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -373,16 +373,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (!Detailed.IsPresent) { - if (status == SocketError.Success) - { - WriteObject(true); - return; - } - else if (i == Count) - { - WriteObject(false); - return; - } + WriteObject(status == SocketError.Success) } else { From 629bb36b08fce4671a7ab175c52f7fe1432cd3cd Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Wed, 8 Jul 2020 23:50:57 -0700 Subject: [PATCH 45/46] Add return after first boolean output --- .../commands/management/TestConnectionCommand.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index f467cb4a752..42d750fdc86 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -373,7 +373,8 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) if (!Detailed.IsPresent) { - WriteObject(status == SocketError.Success) + WriteObject(status == SocketError.Success); + return; } else { From fd2841050eeb0a9e022c77474f5a8bb111059d67 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Wed, 8 Jul 2020 23:51:30 -0700 Subject: [PATCH 46/46] Update tests with new detailed parameter set --- .../Test-Connection.Tests.ps1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index c1237f2b7d9..aa6ceac09b2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -325,16 +325,16 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { $UnreachableAddress = "10.11.12.13" } - It "Test quiet connection to local host on working port" { - Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort -Quiet | Should -BeTrue + It "Test connection to local host on working port" { + Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort | Should -BeTrue } - It "Test quiet connection to unreachable host port 80" { - Test-Connection $UnreachableAddress -TcpPort 80 -TimeOut 1 -Quiet | Should -BeFalse + It "Test connection to unreachable host port 80" { + Test-Connection $UnreachableAddress -TcpPort 80 -TimeOut 1 | Should -BeFalse } It "Test detailed connection to local host on working port" { - $result = Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort + $result = Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort -Detailed $result.Count | Should -Be 1 $result[0].Id | Should -BeExactly 1 @@ -346,7 +346,7 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { } It "Test detailed connection to local host on working port with modified count" { - $result = Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort -Count 2 + $result = Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort -Detailed -Count 2 $result.Count | Should -Be 2 $result[0].Id | Should -BeExactly 1 @@ -358,7 +358,7 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { } It "Test detailed connection to unreachable host port 80" { - $result = Test-Connection $UnreachableAddress -TcpPort 80 -TimeOut 1 + $result = Test-Connection $UnreachableAddress -TcpPort 80 -Detailed -TimeOut 1 $result.Count | Should -Be 1 $result[0].Id | Should -BeExactly 1