Skip to content

Commit

Permalink
Clean up Key / ValueName parsing to handle multiple and/or leading / …
Browse files Browse the repository at this point in the history
…trailing slashes
  • Loading branch information
dlwyatt committed Feb 12, 2018
1 parent 3b0eff2 commit 6200810
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
15 changes: 6 additions & 9 deletions Common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -559,16 +559,13 @@ function ParseKeyValueName
{
param ([string] $KeyValueName)

if ($KeyValueName.EndsWith('\'))
{
$key = $KeyValueName -replace '\\$'
$valueName = ''
}
else
$key = $KeyValueName -replace '^\\+|\\+$'
$valueName = ''

if ($KeyValueName -match '^\\*(?<Key>.+?)\\+(?<ValueName>[^\\]*)$')
{
$KeyValueNameArray = $KeyValueName.split("\")
$key = $KeyValueNameArray[0..($KeyValueNameArray.Count-2)] -join "\"
$valueName = $KeyValueNameArray[-1]
$key = $matches['Key'] -replace '\\{2,}', '\'
$valueName = $matches['ValueName']
}

return $key, $valueName
Expand Down
56 changes: 56 additions & 0 deletions PolicyFileEditor.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,62 @@ try
$gpoPath = 'TestDrive:\TestGpo'
$gptIniPath = "$gpoPath\gpt.ini"

Describe 'KeyValueName parsing' {
InModuleScope PolicyFileEditor {
$testCases = @(
@{
KeyValueName = 'Left\Right'
ExpectedKey = 'Left'
ExpectedValue = 'Right'
Description = 'Simple'
}

@{
KeyValueName = 'Left\\Right'
ExpectedKey = 'Left'
ExpectedValue = 'Right'
Description = 'Multiple consecutive separators'
}

@{
KeyValueName = '\Left\Right'
ExpectedKey = 'Left'
ExpectedValue = 'Right'
Description = 'Leading separator'
}

@{
KeyValueName = 'Left\Right\'
ExpectedKey = 'Left\Right'
ExpectedValue = ''
Description = 'Trailing separator'
}

@{
KeyValueName = '\\\Left\\\\Right\\\\\'
ExpectedKey = 'Left\Right'
ExpectedValue = ''
Description = 'Ridiculous with trailing separator'
}

@{
KeyValueName = '\\\\\\\\Left\\\\\\\Right'
ExpectedKey = 'Left'
ExpectedValue = 'Right'
Description = 'Ridiculous with no trailing separator'
}
)

It -TestCases $testCases 'Properly parses KeyValueName with <Description>' {
param ($KeyValueName, $ExpectedKey, $ExpectedValue)
$key, $valueName = ParseKeyValueName $KeyValueName

$key | Should Be $ExpectedKey
$valueName | Should Be $ExpectedValue
}
}
}

Describe 'Happy Path' {
BeforeEach {
CreateDefaultGpo -Path $gpoPath
Expand Down

0 comments on commit 6200810

Please sign in to comment.