From 58a296ed494a5edee819f1fa99e4e7a978ed4528 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 1 Sep 2017 22:50:15 +0400 Subject: [PATCH] Ensure GetNetworkCredential() returns null if PSCredential has null or empty user name (#4697) * GetNetworkCredential() returns null if PSCredential has empty user name * Fix test --- 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 26a1d0ac0c..6056173d39 100644 --- a/src/System.Management.Automation/engine/Credential.cs +++ b/src/System.Management.Automation/engine/Credential.cs @@ -330,6 +330,12 @@ namespace System.Management.Automation 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 0000000000..3612efb4c6 --- /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() | Should Be $null + } +}