@@ -119,6 +119,8 @@ WARNING: Following changes will be made to OpenSSH configuration
119119 - will be replaced with a test sshd_config
120120 - $HOME \.ssh\known_hosts will be backed up as known_hosts.ori
121121 - will be replaced with a test known_hosts
122+ - $HOME \.ssh\config will be backed up as config.ori
123+ - will be replaced with a test config
122124 - sshd test listener will be on port 47002
123125 - $HOME \.ssh\known_hosts will be modified with test host key entry
124126 - test accounts - ssouser, pubkeyuser, and passwduser will be added
@@ -172,17 +174,23 @@ WARNING: Following changes will be made to OpenSSH configuration
172174
173175 # Backup existing known_hosts and replace with test version
174176 # TODO - account for custom known_hosts locations
175- $knowHostsDirectoryPath = Join-Path $home .ssh
176- $knowHostsFilePath = Join-Path $knowHostsDirectoryPath known_hosts
177- if (-not (Test-Path $knowHostsDirectoryPath - PathType Container))
177+ $dotSshDirectoryPath = Join-Path $home .ssh
178+ $knowHostsFilePath = Join-Path $dotSshDirectoryPath known_hosts
179+ if (-not (Test-Path $dotSshDirectoryPath - PathType Container))
178180 {
179- New-Item - ItemType Directory - Path $knowHostsDirectoryPath - Force - ErrorAction SilentlyContinue | out-null
181+ New-Item - ItemType Directory - Path $dotSshDirectoryPath - Force - ErrorAction SilentlyContinue | out-null
180182 }
181- if ((Test-Path $knowHostsFilePath - PathType Leaf) -and (-not (Test-Path (Join-Path $knowHostsDirectoryPath known_hosts.ori) - PathType Leaf))) {
182- Copy-Item $knowHostsFilePath (Join-Path $knowHostsDirectoryPath known_hosts.ori) - Force
183+ if ((Test-Path $knowHostsFilePath - PathType Leaf) -and (-not (Test-Path (Join-Path $dotSshDirectoryPath known_hosts.ori) - PathType Leaf))) {
184+ Copy-Item $knowHostsFilePath (Join-Path $dotSshDirectoryPath known_hosts.ori) - Force
183185 }
184186 Copy-Item (Join-Path $Script :E2ETestDirectory known_hosts) $knowHostsFilePath - Force
185187
188+ $sshConfigFilePath = Join-Path $dotSshDirectoryPath config
189+ if ((Test-Path $sshConfigFilePath - PathType Leaf) -and (-not (Test-Path (Join-Path $dotSshDirectoryPath config.ori) - PathType Leaf))) {
190+ Copy-Item $sshConfigFilePath (Join-Path $dotSshDirectoryPath config.ori) - Force
191+ }
192+ Copy-Item (Join-Path $Script :E2ETestDirectory ssh_config) $sshConfigFilePath - Force
193+
186194 # create test accounts
187195 # TODO - this is Windows specific. Need to be in PAL
188196 foreach ($user in $OpenSSHTestAccounts )
@@ -212,6 +220,7 @@ WARNING: Following changes will be made to OpenSSH configuration
212220 $testPriKeypath = Join-Path $Script :E2ETestDirectory sshtest_userssokey_ed25519
213221 Cleanup- SecureFileACL - FilePath $testPriKeypath - owner $owner
214222 cmd / c " ssh-add $testPriKeypath 2>&1 >> $Script :TestSetupLogFile "
223+ Backup-OpenSSHTestInfo
215224}
216225# TODO - this is Windows specific. Need to be in PAL
217226function Get-LocalUserProfile
@@ -314,6 +323,14 @@ function Cleanup-OpenSSHTestEnvironment
314323 Remove-Item $originKnowHostsPath - Force - ErrorAction SilentlyContinue
315324 }
316325
326+ # Restore ssh_config
327+ $originConfigPath = Join-Path $home .ssh\config.ori
328+ if (Test-Path $originConfigPath )
329+ {
330+ Copy-Item $originConfigPath (Join-Path $home .ssh\config) - Force - ErrorAction SilentlyContinue
331+ Remove-Item $originConfigPath - Force - ErrorAction SilentlyContinue
332+ }
333+
317334 # Delete accounts
318335 foreach ($user in $OpenSSHTestAccounts )
319336 {
@@ -395,7 +412,7 @@ function Run-OpenSSHE2ETest
395412 # Discover all CI tests and run them.
396413 Push-Location $Script :E2ETestDirectory
397414 Write-Log - Message " Running OpenSSH E2E tests..."
398- $testFolders = Get-ChildItem * .tests.ps1 - Recurse - Exclude SSHDConfig.tests.ps1 , SSH.Tests.ps1 | ForEach-Object { Split-Path $_.FullName } | Sort-Object - Unique
415+ $testFolders = Get-ChildItem * .tests.ps1 - Recurse | ForEach-Object { Split-Path $_.FullName } | Sort-Object - Unique
399416 Invoke-Pester $testFolders - OutputFormat NUnitXml - OutputFile $Script :E2ETestResultsFile - Tag ' CI'
400417 Pop-Location
401418}
@@ -439,6 +456,56 @@ function Run-OpenSSHUnitTest
439456 $testfailed
440457}
441458
459+ function Backup-OpenSSHTestInfo
460+ {
461+ param
462+ (
463+ [string ] $BackupFile = $null
464+ )
465+
466+ if ($Global :OpenSSHTestInfo -eq $null ) {
467+ Throw " `$ OpenSSHTestInfo is null. Did you run Setup-OpenSSHTestEnvironment yet?"
468+ }
469+
470+ $testInfo = $Global :OpenSSHTestInfo
471+
472+ if ([String ]::IsNullOrEmpty($BackupFile )) {
473+ $BackupFile = Join-Path $testInfo [" TestDataPath" ] " OpenSSHTestInfo_backup.txt"
474+ }
475+
476+ $null | Set-Content $BackupFile
477+
478+ foreach ($key in $testInfo.Keys ) {
479+ $value = $testInfo [$key ]
480+ Add-Content $BackupFile " $key ,$value "
481+ }
482+ }
483+
484+ function Recover-OpenSSHTestInfo
485+ {
486+ param
487+ (
488+ [Parameter (Mandatory = $true )]
489+ [ValidateNotNullOrEmpty ()]
490+ [string ] $BackupFile
491+ )
492+
493+ if ($Global :OpenSSHTestInfo -ne $null )
494+ {
495+ $Global :OpenSSHTestInfo.Clear ()
496+ $Global :OpenSSHTestInfo = $null
497+ }
498+
499+ $Global :OpenSSHTestInfo = @ {}
500+
501+ $entries = Get-Content $BackupFile
502+
503+ foreach ($entry in $entries ) {
504+ $data = $entry.Split (" ," )
505+ $Global :OpenSSHTestInfo [$data [0 ]] = $data [1 ]
506+ }
507+ }
508+
442509<#
443510 Write-Log
444511#>
@@ -460,4 +527,4 @@ function Write-Log
460527 }
461528}
462529
463- Export-ModuleMember - Function Setup- OpenSSHTestEnvironment, Cleanup- OpenSSHTestEnvironment, Run- OpenSSHUnitTest, Run- OpenSSHE2ETest
530+ Export-ModuleMember - Function Setup- OpenSSHTestEnvironment, Cleanup- OpenSSHTestEnvironment, Run- OpenSSHUnitTest, Run- OpenSSHE2ETest, Backup-OpenSSHTestInfo , Recover - OpenSSHTestInfo
0 commit comments