Skip to content

Commit cdcc8d3

Browse files
authored
Fix tilde expand for Windows paths with backslashes (#768)
* add backslash support for Windows paths * add pester tests for tilde_expand * fix typo
1 parent 0c3137f commit cdcc8d3

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

misc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,15 @@ tilde_expand(const char *filename, uid_t uid, char **retp)
12611261
path = NULL; /* ~/ */
12621262
else
12631263
path = copy; /* ~/path */
1264+
#ifdef WINDOWS
1265+
// also need to account for backward slashes on Windows
1266+
} else if (*copy == '\\') {
1267+
copy += strspn(copy, "\\");
1268+
if (*copy == '\0')
1269+
path = NULL; /* ~\ */
1270+
else
1271+
path = copy; /* ~\path */
1272+
#endif /* WINDOWS */
12641273
} else {
12651274
user = copy;
12661275
if ((path = strchr(copy, '/')) != NULL) {

regress/pesterTests/SSH.Tests.ps1

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Import-Module $PSScriptRoot\CommonUtils.psm1 -Force
55
$tC = 1
66
$tI = 0
77
$suite = "sshclient"
8-
8+
99
Describe "E2E scenarios for ssh client" -Tags "CI" {
10-
BeforeAll {
10+
BeforeAll {
1111
if($OpenSSHTestInfo -eq $null)
1212
{
1313
Throw "`$OpenSSHTestInfo is null. Please run Set-OpenSSHTestEnvironment to set test environments."
@@ -27,12 +27,12 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
2727
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($ssouser, $rights, "ContainerInherit,Objectinherit", "None", "Allow")
2828
$acl.SetAccessRule($accessRule)
2929
Set-Acl -Path $testDir -AclObject $acl
30-
#skip on ps 2 becase non-interactive cmd require a ENTER before it returns on ps2
30+
#skip on ps 2 because non-interactive cmd require a ENTER before it returns on ps2
3131
$skip = $IsWindows -and ($PSVersionTable.PSVersion.Major -le 2)
3232

3333
<#$testData = @(
3434
@{
35-
Title = 'Simple logon no option';
35+
Title = 'Simple logon no option';
3636
LogonStr = "$($server.localAdminUserName)@$($server.MachineName)"
3737
Options = ""
3838
},
@@ -42,7 +42,7 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
4242
Options = "-C -l $($server.localAdminUserName)"
4343
}
4444
)
45-
45+
4646
$testData1 = @(
4747
@{
4848
Title = "logon using -i -q option"
@@ -84,7 +84,7 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
8484
[string] $default_shell_path,
8585
[string] $default_shell_cmd_option_val = $null
8686
)
87-
87+
8888
if (!(Test-Path $dfltShellRegPath)) {
8989
New-Item -Path $dfltShellRegPath -Force | Out-Null
9090
}
@@ -99,12 +99,12 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
9999
$stderrFile=Join-Path $testDir "$tC.$tI.stderr.txt"
100100
$stdoutFile=Join-Path $testDir "$tC.$tI.stdout.txt"
101101
$logFile = Join-Path $testDir "$tC.$tI.log.txt"
102-
}
102+
}
103103

104104
AfterEach {$tI++;}
105105

106106
Context "$tC - Basic Scenarios" {
107-
107+
108108
BeforeAll {$tI=1}
109109
AfterAll{$tC++}
110110

@@ -117,13 +117,13 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
117117
iex "cmd /c `"ssh -? 2> $stderrFile`""
118118
$stderrFile | Should Contain "usage: ssh"
119119
}
120-
120+
121121
It "$tC.$tI - remote echo command" {
122122
iex "$sshDefaultCmd echo 1234" | Should Be "1234"
123123
}
124124

125125
}
126-
126+
127127
Context "$tC - exit code (exit-status.sh)" {
128128
BeforeAll {$tI=1}
129129
AfterAll{$tC++}
@@ -132,12 +132,12 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
132132
foreach ($i in (0,1,4,5,44)) {
133133
ssh -p $port $ssouser@$server exit $i
134134
$LASTEXITCODE | Should Be $i
135-
}
135+
}
136136
}
137137
}
138138

139139
Context "$tC - Redirection Scenarios" {
140-
140+
141141
BeforeAll {$tI=1}
142142
AfterAll{$tC++}
143143

@@ -192,7 +192,7 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
192192

193193
}
194194
}
195-
195+
196196
Context "$tC - configure powershell default shell Scenarios" {
197197
BeforeAll {
198198
$tI=1
@@ -205,13 +205,13 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
205205
$tC++
206206
Remove-ItemProperty -Path $dfltShellRegPath -Name $dfltShellRegKeyName -ErrorAction SilentlyContinue
207207
Remove-ItemProperty -Path $dfltShellRegPath -Name $dfltShellCmdOptionRegKeyName -ErrorAction SilentlyContinue
208-
}
208+
}
209209

210210
It "$tC.$tI - basic powershell" -skip:$skip {
211211
$o = ssh test_target Write-Output 1234
212212
$o | Should Be "1234"
213213
}
214-
214+
215215
It "$tC.$tI - basic in powershell cmdlet" -skip:$skip {
216216
$o = ssh test_target "cd `$env:ProgramFiles;pwd"
217217
$LASTEXITCODE | Should Be 0
@@ -240,7 +240,7 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
240240
It "$tC.$tI - single quotes in powershell cmdlet" -skip:$skip {
241241
# actual command line ssh target echo '$env:computername'
242242
$o = ssh test_target "echo '`$env:computername'"
243-
$LASTEXITCODE | Should Be 0
243+
$LASTEXITCODE | Should Be 0
244244
$o | Should Be `$env:computername
245245
}
246246
}
@@ -257,9 +257,9 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
257257
Remove-ItemProperty -Path $dfltShellRegPath -Name $dfltShellRegKeyName -ErrorAction SilentlyContinue
258258
Remove-ItemProperty -Path $dfltShellRegPath -Name $dfltShellCmdOptionRegKeyName -ErrorAction SilentlyContinue
259259
}
260-
It "$tC.$tI - default shell as cmd" -skip:$skip {
260+
It "$tC.$tI - default shell as cmd" -skip:$skip {
261261
$o = ssh test_target where cmd
262-
$o | Should Contain "cmd"
262+
$o | Should Contain "cmd"
263263
}
264264
It "$tC.$tI - cmd as default shell and double quotes in cmdline" {
265265
# actual command line ssh target echo "hello"
@@ -269,7 +269,7 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
269269
It "$tC.$tI - single quotes in powershell cmdlet" -skip:$skip {
270270
# actual command line ssh target echo '$env:computername'
271271
$o = ssh test_target "echo 'hello'"
272-
$LASTEXITCODE | Should Be 0
272+
$LASTEXITCODE | Should Be 0
273273
$o | Should Be "'hello'"
274274
}
275275
}
@@ -290,15 +290,15 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
290290
$o | Should Be "`"hello`""
291291
}
292292
}
293-
294-
Context "$tC - cmdline parameters" {
293+
294+
Context "$tC - cmdline parameters" {
295295
BeforeAll {$tI=1}
296296
AfterAll{$tC++}
297297

298298
It "$tC.$tI - verbose to file (-v -E)" {
299299
$o = ssh -v -E $logFile test_target echo 1234
300300
$o | Should Be "1234"
301-
#TODO - checks below are very inefficient (time taking).
301+
#TODO - checks below are very inefficient (time taking).
302302
$logFile | Should Contain "OpenSSH_"
303303
$logFile | Should Contain "Exit Status 0"
304304
}
@@ -333,11 +333,11 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
333333
" Port $port" | Add-Content $goodConfigFile
334334
" User $ssouser" | Add-Content $goodConfigFile
335335
$o = ssh -F $goodConfigFile myhost echo 1234
336-
$o | Should Be "1234"
336+
$o | Should Be "1234"
337337
}
338338

339339
It "$tC.$tI - IP options - (-4) (-6)" {
340-
# TODO - this test assumes target is localhost.
340+
# TODO - this test assumes target is localhost.
341341
# make it work independent of target
342342
#-4
343343
$o = ssh -4 -v -E $logFile test_target echo 1234
@@ -346,11 +346,23 @@ Describe "E2E scenarios for ssh client" -Tags "CI" {
346346
#-4
347347
$o = ssh -6 -v -E $logFile test_target echo 1234
348348
$o | Should Be "1234"
349-
$logFile | Should Contain "[::1]"
349+
$logFile | Should Contain "[::1]"
350+
}
351+
352+
It "$tC.$tI - tilde expand for path with forward slash" {
353+
$o = ssh -v -i ~/test/key/path -E $logFile test_target echo 1234
354+
$o | Should Be "1234"
355+
$logFile | Should Not Contain "tilde_expand: No such user"
356+
}
357+
358+
It "$tC.$tI - tilde expand for path with backslash" {
359+
$o = ssh -v -i ~\test\key\path -E $logFile test_target echo 1234
360+
$o | Should Be "1234"
361+
$logFile | Should Not Contain "tilde_expand: No such user"
350362
}
351363

352364
It "$tC.$tI - auto populate known hosts" {
353-
365+
354366
$kh = Join-Path $testDir "$tC.$tI.known_hosts"
355367
$nul | Set-Content $kh
356368
# doing via cmd to intercept and drain stderr output

0 commit comments

Comments
 (0)