From a82738b5e965c9eff200d69d54c9216387e6af4e Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 29 Aug 2017 15:03:41 +0300 Subject: [PATCH 1/2] GetNetworkCredential() returns null if PSCredential has empty user name --- src/System.Management.Automation/engine/Credential.cs | 6 ++++++ test/powershell/engine/Basic/Credential.Tests.ps1 | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 test/powershell/engine/Basic/Credential.Tests.ps1 diff --git a/src/System.Management.Automation/engine/Credential.cs b/src/System.Management.Automation/engine/Credential.cs index 26a1d0ac0c2..6056173d396 100644 --- a/src/System.Management.Automation/engine/Credential.cs +++ b/src/System.Management.Automation/engine/Credential.cs @@ -330,6 +330,12 @@ private static bool IsValidUserName(string input, out string user, out string domain) { + if (String.IsNullOrEmpty(input)) + { + user = domain = null; + return false; + } + SplitUserDomain(input, out user, out domain); if ((user == null) || diff --git a/test/powershell/engine/Basic/Credential.Tests.ps1 b/test/powershell/engine/Basic/Credential.Tests.ps1 new file mode 100644 index 00000000000..8deeb51d700 --- /dev/null +++ b/test/powershell/engine/Basic/Credential.Tests.ps1 @@ -0,0 +1,6 @@ +Describe "Credential tests" -Tags "CI" { + It "Explicit cast for an empty credential returns null" { + # We should explicitly check that the expression returns $null + [PSCredential]::Empty.GetNetworkCredential() -eq $null | Should Be $true + } +} From 979ff8ebb65de20df411104ee37341695968cec4 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 31 Aug 2017 17:42:34 +0300 Subject: [PATCH 2/2] Fix test --- test/powershell/engine/Basic/Credential.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/engine/Basic/Credential.Tests.ps1 b/test/powershell/engine/Basic/Credential.Tests.ps1 index 8deeb51d700..3612efb4c6a 100644 --- a/test/powershell/engine/Basic/Credential.Tests.ps1 +++ b/test/powershell/engine/Basic/Credential.Tests.ps1 @@ -1,6 +1,6 @@ Describe "Credential tests" -Tags "CI" { It "Explicit cast for an empty credential returns null" { # We should explicitly check that the expression returns $null - [PSCredential]::Empty.GetNetworkCredential() -eq $null | Should Be $true + [PSCredential]::Empty.GetNetworkCredential() | Should Be $null } }