diff --git a/appveyor.yml b/appveyor.yml index 422787ccb..a9a77d0ef 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -26,8 +26,6 @@ after_build: before_test: - ps: | Import-Module $env:APPVEYOR_BUILD_FOLDER\contrib\win32\openssh\AppveyorHelper.psm1 -DisableNameChecking - Install-OpenSSHTestDependencies - Deploy-OpenSSHTests Setup-OpenSSHTestEnvironment -Quiet test_script: diff --git a/contrib/win32/openssh/AppveyorHelper.psm1 b/contrib/win32/openssh/AppveyorHelper.psm1 index 081520722..f0f97c0fd 100644 --- a/contrib/win32/openssh/AppveyorHelper.psm1 +++ b/contrib/win32/openssh/AppveyorHelper.psm1 @@ -155,14 +155,13 @@ function Add-Artifact [string] $FileToAdd ) - if (($FileToAdd -ne $null ) -and (Test-Path $FileToAdd -PathType Leaf)) - { - - $null = $artifacts.Add($FileToAdd) - } + if ([string]::IsNullOrEmpty($FileToAdd) -or (-not (Test-Path $FileToAdd -PathType Leaf)) ) + { + Write-Host "Skip publishing package artifacts. $FileToAdd does not exist" + } else { - Write-Host "Skip publishing package artifacts. $FileToAdd does not exist" + $null = $artifacts.Add($FileToAdd) } } @@ -177,17 +176,19 @@ function Publish-Artifact # Get the build.log file for each build configuration Add-BuildLog -artifacts $artifacts -buildLog (Get-BuildLogFile -root $repoRoot.FullName) - - Add-Artifact -artifacts $artifacts -FileToAdd $Global:OpenSSHTestInfo["UnitTestResultsFile"] - Add-Artifact -artifacts $artifacts -FileToAdd $Global:OpenSSHTestInfo["E2ETestResultsFile"] - Add-Artifact -artifacts $artifacts -FileToAdd $Global:OpenSSHTestInfo["TestSetupLogFile"] + + if($Global:OpenSSHTestInfo) + { + Add-Artifact -artifacts $artifacts -FileToAdd $Global:OpenSSHTestInfo["UnitTestResultsFile"] + Add-Artifact -artifacts $artifacts -FileToAdd $Global:OpenSSHTestInfo["E2ETestResultsFile"] + Add-Artifact -artifacts $artifacts -FileToAdd $Global:OpenSSHTestInfo["TestSetupLogFile"] + } foreach ($artifact in $artifacts) { Write-Host "Publishing $artifact as Appveyor artifact" - # NOTE: attempt to publish subsequent artifacts even if the current one fails - # TODO - Fix this - # Push-AppveyorArtifact $artifact -ErrorAction Continue + # NOTE: attempt to publish subsequent artifacts even if the current one fails + Push-AppveyorArtifact $artifact -ErrorAction Continue } } @@ -211,18 +212,18 @@ function Run-OpenSSHTests Write-Host "All Unit tests passed!" Write-BuildMessage -Message "All Unit tests passed!" -Category Information } - # Run all pester tests. - <#Run-OpenSSHPesterTest - if (-not (Test-Path $global:PesterTestResultsFile)) + # Run all E2E tests. + Run-OpenSSHE2ETest + if (($OpenSSHTestInfo -eq $null) -or (-not (Test-Path $OpenSSHTestInfo["E2ETestResultsFile"]))) { - Write-Warning "Test result file $global:PesterTestResultsFile not found after tests." - Write-BuildMessage -Message "Test result file $global:PesterTestResultsFile not found after tests." -Category Error + Write-Warning "Test result file $OpenSSHTestInfo["E2ETestResultsFile"] not found after tests." + Write-BuildMessage -Message "Test result file $OpenSSHTestInfo["E2ETestResultsFile"] not found after tests." -Category Error Set-BuildVariable TestPassed False } - $xml = [xml](Get-Content -raw $global:PesterTestResultsFile) + $xml = [xml](Get-Content -raw $OpenSSHTestInfo["E2ETestResultsFile"]) if ([int]$xml.'test-results'.failures -gt 0) { - $errorMessage = "$($xml.'test-results'.failures) tests in regress\pesterTests failed. Detail test log is at $($global:PesterTestResultsFile)." + $errorMessage = "$($xml.'test-results'.failures) tests in regress\pesterTests failed. Detail test log is at $($OpenSSHTestInfo["E2ETestResultsFile"])." Write-Warning $errorMessage Write-BuildMessage -Message $errorMessage -Category Error Set-BuildVariable TestPassed False @@ -232,7 +233,7 @@ function Run-OpenSSHTests if ($Error.Count -gt 0) { Write-BuildMessage -Message "Tests Should clean $Error after success." -Category Warning - }#> + } } <# diff --git a/contrib/win32/openssh/OpenSSHTestHelper.psm1 b/contrib/win32/openssh/OpenSSHTestHelper.psm1 index 9a3d6e389..752fcdfa7 100644 --- a/contrib/win32/openssh/OpenSSHTestHelper.psm1 +++ b/contrib/win32/openssh/OpenSSHTestHelper.psm1 @@ -1,89 +1,213 @@ $ErrorActionPreference = 'Stop' Import-Module $PSScriptRoot\OpenSSHCommonUtils.psm1 -DisableNameChecking -# state object used by all tests, initialized in Setup-OpenSSHTetEnvironment -$Global:OpenSSHTestInfo = @{} -<# Hash Table definitions -#test listener name -$Global:OpenSSHTestInfo["Target"] - -# test listener port -$Global:OpenSSHTestInfo["Port"] - -# test user with single sign on capability -$Global:OpenSSHTestInfo["SSOUser"] - -# test user to be used with explicit key for key auth -$Global:OpenSSHTestInfo["PubKeyUser"] - -# test user for passwd based auth -$Global:OpenSSHTestInfo["PasswdUser"] - -# common password for all test accounts -$Global:OpenSSHTestInfo["TestAccountPW"] - -# openssh bin path -$Global:OpenSSHTestInfo["OpenSSHDir"] - -# openssh tests path -$Global:OpenSSHTestInfo["OpenSSHTestDir"] - -# openssh test setup log file -$Global:OpenSSHTestInfo["TestSetupLogFile"] - -# openssh E2E test results file -$Global:OpenSSHTestInfo["E2ETestResultsFile"] - -# openssh unittest test results file -$Global:OpenSSHTestInfo["UnitTestResultsFile"] - -#> - - +[System.IO.DirectoryInfo] $repositoryRoot = Get-RepositoryRoot # test environment parameters initialized with defaults -$Script:OpenSSHDir = "$env:SystemDrive\OpenSSH" -$Script:OpenSSHTestDir = "$env:SystemDrive\OpenSSHTests" -$Script:E2ETestResultsFile = Join-Path $Script:OpenSSHTestDir "E2ETestResultsFile.xml" -$Script:UnitTestResultsFile = Join-Path $Script:OpenSSHTestDir "UnitTestResults.txt" -$Script:TestSetupLogFile = Join-Path $Script:OpenSSHTestDir "TestSetupLog.txt" -$Script:SSOUser = "sshtest_ssouser" -$Script:PubKeyUser = "sshtest_pubkeyuser" -$Script:PasswdUser = "sshtest_passwduser" -$Script:OpenSSHTestAccountsPassword = "P@ssw0rd_1" -$Script:OpenSSHTestAccounts = $Script:SSOUser, $Script:PubKeyUser, $Script:PasswdUser +$E2ETestResultsFileName = "E2ETestResults.xml" +$UnitTestResultsFileName = "UnitTestResults.txt" +$TestSetupLogFileName = "TestSetupLog.txt" +$SSOUser = "sshtest_ssouser" +$PubKeyUser = "sshtest_pubkeyuser" +$PasswdUser = "sshtest_passwduser" +$OpenSSHTestAccountsPassword = "P@ssw0rd_1" +$OpenSSHTestAccounts = $Script:SSOUser, $Script:PubKeyUser, $Script:PasswdUser - -function Set-OpenSSHTestParams +$Script:TestDataPath = "$env:SystemDrive\OpenSSHTests" +$Script:E2ETestResultsFile = Join-Path $TestDataPath $E2ETestResultsFileName +$Script:UnitTestResultsFile = Join-Path $TestDataPath $UnitTestResultsFileName +$Script:TestSetupLogFile = Join-Path $TestDataPath $TestSetupLogFileName +$Script:E2ETestDirectory = Join-Path $repositoryRoot.FullName -ChildPath "regress\pesterTests" + +<# + .Synopsis + Setup-OpenSSHTestEnvironment + TODO - split these steps into client and server side +#> +function Setup-OpenSSHTestEnvironment { + [CmdletBinding()] param ( - [string] $OpenSSHDir = $Script:OpenSSHDir, - [string] $OpenSSHTestDir = $Script:OpenSSHTestDir, - [string] $E2ETestResultsFile = $Script:E2ETestResultsFile, - [string] $UnitTestResultsFile = $Script:UnitTestResultsFile, - [string] $TestSetupLogFile = $Script:TestSetupLogFile + [switch] $Quiet, + [string] $OpenSSHBinPath, + [string] $TestDataPath = "$env:SystemDrive\OpenSSHTests", + [Boolean] $DebugMode = $false ) + + if($Global:OpenSSHTestInfo -ne $null) + { + $Global:OpenSSHTestInfo.Clear() + $Global:OpenSSHTestInfo = $null + } + $Script:TestDataPath = $TestDataPath; + $Script:E2ETestResultsFile = Join-Path $TestDataPath "E2ETestResults.xml" + $Script:UnitTestResultsFile = Join-Path $TestDataPath "UnitTestResults.txt" + $Script:TestSetupLogFile = Join-Path $TestDataPath "TestSetupLog.txt" + $Script:UnitTestDirectory = Get-UnitTestDirectory + - $Script:OpenSSHDir = $OpenSSHDir - $Script:OpenSSHTestDir = $OpenSSHTestDir - $Script:E2ETestResultsFile = $E2ETestResultsFile - $Script:UnitTestResultsFile = $UnitTestResultsFile - $Script:TestSetupLogFile = $TestSetupLogFile -} + $Global:OpenSSHTestInfo = @{ + "Target"= "localhost"; # test listener name + "Port"= "47002"; # test listener port + "SSOUser"= $SSOUser; # test user with single sign on capability + "PubKeyUser"= $PubKeyUser; # test user to be used with explicit key for key auth + "PasswdUser"= $PasswdUser; # common password for all test accounts + "TestAccountPW"= $OpenSSHTestAccountsPassword; # common password for all test accounts + "TestDataPath" = $TestDataPath; # openssh tests path + "TestSetupLogFile" = $Script:TestSetupLogFile; # openssh test setup log file + "E2ETestResultsFile" = $Script:E2ETestResultsFile; # openssh E2E test results file + "UnitTestResultsFile" = $Script:UnitTestResultsFile; # openssh unittest test results file + "E2ETestDirectory" = $Script:E2ETestDirectory # the directory of E2E tests + "UnitTestDirectory" = $Script:UnitTestDirectory # the directory of unit tests + "DebugMode" = $DebugMode # run openssh E2E in debug mode + } + + #if user does not set path, pick it up + if([string]::IsNullOrEmpty($OpenSSHBinPath)) + { + $sshcmd = get-command ssh.exe -ErrorAction Ignore + if($sshcmd -eq $null) + { + Throw "Cannot find ssh.exe. Please specify -OpenSSHBinPath to the OpenSSH installed location." + } + elseif($Quiet) + { + $dirToCheck = split-path $sshcmd.Path + $script:OpenSSHBinPath = $dirToCheck + } + else + { + $dirToCheck = split-path $sshcmd.Path + $message = "Do you want to test openssh installed at $($dirToCheck)? [Yes] Y; [No] N (default is `"Y`")" + $response = Read-Host -Prompt $message + if( ($response -eq "") -or ($response -ieq "Y") -or ($response -ieq "Yes") ) + { + $script:OpenSSHBinPath = $dirToCheck + } + elseif( ($response -ieq "N") -or ($response -ieq "No") ) + { + Write-Host "User decided not to pick up ssh.exe from $dirToCheck. Please specify -OpenSSHBinPath to the OpenSSH installed location." + return + } + else + { + Throw "User entered invalid option ($response). Please specify -OpenSSHBinPath to the OpenSSH installed location" + } + } + } + else + { + if (-not (Test-Path (Join-Path $OpenSSHBinPath ssh.exe) -PathType Leaf)) + { + Throw "Cannot find OpenSSH binaries under $OpenSSHBinPath. Please specify -OpenSSHBinPathto the OpenSSH installed location" + } + else + { + $script:OpenSSHBinPath = $OpenSSHBinPath + } + } -function Dump-OpenSSHTestParams -{ - $out = @" -OpenSSHDir: $Script:OpenSSHDir -OpenSSHTestDir: $Script:OpenSSHTestDir -E2ETestResultsFile: $Script:E2ETestResultsFile -UnitTestResultsFile: $Script:UnitTestResultsFile -TestSetupLogFile: $Script:TestSetupLogFile + $Global:OpenSSHTestInfo.Add("OpenSSHBinPath", $script:OpenSSHBinPath) + + $warning = @" +WARNING: Following changes will be made to OpenSSH configuration + - sshd_config will be backed up as sshd_config.ori + - will be replaced with a test sshd_config + - $HOME\.ssh\known_hosts will be backed up as known_hosts.ori + - will be replaced with a test known_hosts + - sshd test listener will be on port 47002 + - $HOME\.ssh\known_hosts will be modified with test host key entry + - test accounts - ssouser, pubkeyuser, and passwduser will be added + - Setup single signon for ssouser + - To cleanup - Run Cleanup-OpenSSHTestEnvironment "@ - Write-Host $out -} + if (-not $Quiet) { + Write-Warning $warning + $continue = Read-Host -Prompt "Do you want to continue with the above changes? [Yes] Y; [No] N (default is `"Y`")" + if( ($continue -eq "") -or ($continue -ieq "Y") -or ($continue -ieq "Yes") ) + { + } + elseif( ($continue -ieq "N") -or ($continue -ieq "No") ) + { + Write-Host "User decided not to make the changes." + return + } + else + { + Throw "User entered invalid option ($continue). Exit now." + } + } + Install-OpenSSHTestDependencies + + #Backup existing OpenSSH configuration + $backupConfigPath = Join-Path $script:OpenSSHBinPath sshd_config.ori + if (-not (Test-Path $backupConfigPath -PathType Leaf)) { + Copy-Item (Join-Path $script:OpenSSHBinPath sshd_config) $backupConfigPath -Force + } + + # copy new sshd_config + Copy-Item (Join-Path $Script:E2ETestDirectory sshd_config) (Join-Path $script:OpenSSHBinPath sshd_config) -Force + Copy-Item "$($Script:E2ETestDirectory)\sshtest*hostkey*" $script:OpenSSHBinPath -Force + Restart-Service sshd -Force + + #Backup existing known_hosts and replace with test version + #TODO - account for custom known_hosts locations + $knowHostsDirectoryPath = Join-Path $home .ssh + $knowHostsFilePath = Join-Path $knowHostsDirectoryPath known_hosts + if(-not (Test-Path $knowHostsDirectoryPath -PathType Container)) + { + New-Item -ItemType Directory -Path $knowHostsDirectoryPath -Force -ErrorAction SilentlyContinue | out-null + } + if ((Test-Path $knowHostsFilePath -PathType Leaf) -and (-not (Test-Path (Join-Path $knowHostsDirectoryPath known_hosts.ori) -PathType Leaf))) { + Copy-Item $knowHostsFilePath (Join-Path $knowHostsDirectoryPath known_hosts.ori) -Force + } + Copy-Item (Join-Path $Script:E2ETestDirectory known_hosts) $knowHostsFilePath -Force + + # create test accounts + #TODO - this is Windows specific. Need to be in PAL + foreach ($user in $OpenSSHTestAccounts) + { + try + { + $objUser = New-Object System.Security.Principal.NTAccount($user) + $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier]) + } + catch + { + #only add the local user when it does not exists on the machine + net user $user $Script:OpenSSHTestAccountsPassword /ADD 2>&1 >> $Script:TestSetupLogFile + } + } + + #setup single sign on for ssouser + #TODO - this is Windows specific. Need to be in PAL + $ssousersid = Get-UserSID -User sshtest_ssouser + $ssouserProfileRegistry = Join-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" $ssousersid + if (-not (Test-Path $ssouserProfileRegistry) ) { + #create profile + if (-not($env:DISPLAY)) { $env:DISPLAY = 1 } + $env:SSH_ASKPASS="$($env:ComSpec) /c echo $($OpenSSHTestAccountsPassword)" + cmd /c "ssh -p 47002 sshtest_ssouser@localhost echo %userprofile% > profile.txt" + if ($env:DISPLAY -eq 1) { Remove-Item env:\DISPLAY } + remove-item "env:SSH_ASKPASS" -ErrorAction SilentlyContinue + } + $ssouserProfile = (Get-ItemProperty -Path $ssouserProfileRegistry -Name 'ProfileImagePath').ProfileImagePath + New-Item -ItemType Directory -Path (Join-Path $ssouserProfile .ssh) -Force -ErrorAction SilentlyContinue | out-null + $authorizedKeyPath = Join-Path $ssouserProfile .ssh\authorized_keys + $testPubKeyPath = Join-Path $Script:E2ETestDirectory sshtest_userssokey_ed25519.pub + #workaround for the cariggage new line added by git + (Get-Content $testPubKeyPath -Raw).Replace("`r`n","`n") | Set-Content $testPubKeyPath -Force + Copy-Item $testPubKeyPath $authorizedKeyPath -Force -ErrorAction SilentlyContinue + $acl = get-acl $authorizedKeyPath + $ar = New-Object System.Security.AccessControl.FileSystemAccessRule("NT Service\sshd", "Read", "Allow") + $acl.SetAccessRule($ar) + Set-Acl $authorizedKeyPath $acl + $testPriKeypath = Join-Path $Script:E2ETestDirectory sshtest_userssokey_ed25519 + (Get-Content $testPriKeypath -Raw).Replace("`r`n","`n") | Set-Content $testPriKeypath -Force + cmd /c "ssh-add $testPriKeypath 2>&1 >> $Script:TestSetupLogFile" +} <# .SYNOPSIS @@ -115,139 +239,6 @@ function Install-OpenSSHTestDependencies choco install sysinternals -y --force --limitoutput 2>&1 >> $Script:TestSetupLogFile } } - -<# - .Synopsis - Setup-OpenSSHTestEnvironment - TODO - split these steps into client and server side -#> -function Setup-OpenSSHTestEnvironment -{ - [CmdletBinding()] - param - ( - [switch] $Quiet - ) - - $warning = @" -WARNING: Following changes will be made to OpenSSH configuration - - sshd_config will be backed up as sshd_config.ori - - will be replaced with a test sshd_config - - $env:USERPROFILE\.ssh\known_hosts will be backed up as known_hosts.ori - - will be replaced with a test known_hosts - - sshd test listener will be on port 47002 - - $env:USERPROFILE\.ssh\known_hosts will be modified with test host key entry - - test accounts - ssouser, pubkeyuser and passwduser will be added - - To cleanup - Run Cleanup-OpenSSHTestEnvironment -"@ - - if (-not $Quiet) { - Write-Warning $warning - } - - if (-not (Test-Path (Join-Path $Script:OpenSSHDir ssh.exe) -PathType Leaf)) - { - Throw "cannot find OpenSSH binaries under $Script:OpenSSHDir Try Set-OpenSSHTestParams" - } - - $sshcmd = get-command ssh.exe -ErrorAction Ignore - if($sshcmd -eq $null) { - Throw 'Cannot find ssh.exe. Make sure OpenSSH binary path is in %PATH%' - } - - #ensure ssh.exe is being picked from $Script:OpenSSHDir. Multiple versions may exist - if ( (Split-Path $sshcmd.Source) -ine "$Script:OpenSSHDir" ) { - Throw "ssh.exe is being picked from $($sshcmd.Source) instead of $Script:OpenSSHDir. " - } - - if (-not (Test-Path $Script:OpenSSHTestDir -PathType Container )) { - Throw "$Script:OpenSSHTestDir does not exist. Run Deploy-OpenSSHTests to deploy tests." - } - - if ((Get-ChildItem $Script:OpenSSHTestDir).Count -eq 0) { - Throw "Nothing found in $Script:OpenSSHTestDir. Run Deploy-OpenSSHTests to deploy tests" - } - #Backup existing OpenSSH configuration - $backupConfigPath = Join-Path $Script:OpenSSHDir sshd_config.ori - if (-not (Test-Path $backupConfigPath -PathType Leaf)) { - Copy-Item (Join-Path $Script:OpenSSHDir sshd_config) $backupConfigPath -Force - } - - # copy new sshd_config - Copy-Item (Join-Path $Script:OpenSSHTestDir sshd_config) (Join-Path $Script:OpenSSHDir sshd_config) -Force - Copy-Item $Script:OpenSSHTestDir\sshtest*hostkey* $Script:OpenSSHDir -Force - Restart-Service sshd -Force - - #Backup existing known_hosts and replace with test version - #TODO - account for custom known_hosts locations - $knowHostsDirectoryPath = Join-Path $home .ssh - $knowHostsFilePath = Join-Path $knowHostsDirectoryPath known_hosts - if(-not (Test-Path $knowHostsDirectoryPath -PathType Container)) - { - New-Item -ItemType Directory -Path $knowHostsDirectoryPath -Force -ErrorAction SilentlyContinue | out-null - } - if (Test-Path $knowHostsFilePath -PathType Leaf) { - Copy-Item $knowHostsFilePath (Join-Path $knowHostsDirectoryPath known_hosts.ori) -Force - } - Copy-Item (Join-Path $Script:OpenSSHTestDir known_hosts) $knowHostsFilePath -Force - - # create test accounts - #TODO - this is Windows specific. Need to be in PAL - foreach ($user in $Script:OpenSSHTestAccounts) - { - net user $user $Script:OpenSSHTestAccountsPassword /ADD 2>&1 >> $Script:TestSetupLogFile - } - - #setup single sign on for ssouser - #TODO - this is Windows specific. Need to be in PAL - $ssousersid = Get-UserSID -User sshtest_ssouser - $ssouserProfileRegistry = Join-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" $ssousersid - if (-not (Test-Path $ssouserProfileRegistry) ) { - #create profile - if (-not($env:DISPLAY)) { $env:DISPLAY = 1 } - $env:SSH_ASKPASS="$($env:ComSpec) /c echo $($Script:OpenSSHTestAccountsPassword)" - cmd /c "ssh -p 47002 sshtest_ssouser@localhost echo %userprofile% > profile.txt" - if ($env:DISPLAY -eq 1) { Remove-Item env:\DISPLAY } - remove-item "env:SSH_ASKPASS" -ErrorAction SilentlyContinue - } - $ssouserProfile = (Get-ItemProperty -Path $ssouserProfileRegistry -Name 'ProfileImagePath').ProfileImagePath - New-Item -ItemType Directory -Path (Join-Path $ssouserProfile .ssh) -Force -ErrorAction SilentlyContinue | out-null - $authorizedKeyPath = Join-Path $ssouserProfile .ssh\authorized_keys - $testPubKeyPath = Join-Path $Script:OpenSSHTestDir sshtest_userssokey_ed25519.pub - (Get-Content $testPubKeyPath -Raw).Replace("`r`n","`n") | Set-Content $testPubKeyPath -Force - Copy-Item $testPubKeyPath $authorizedKeyPath -Force -ErrorAction SilentlyContinue - $acl = get-acl $authorizedKeyPath - $ar = New-Object System.Security.AccessControl.FileSystemAccessRule("NT Service\sshd", "Read", "Allow") - $acl.SetAccessRule($ar) - Set-Acl $authorizedKeyPath $acl - $testPriKeypath = Join-Path $Script:OpenSSHTestDir sshtest_userssokey_ed25519 - (Get-Content $testPriKeypath -Raw).Replace("`r`n","`n") | Set-Content $testPriKeypath -Force - cmd /c "ssh-add $testPriKeypath 2>&1 >> $Script:TestSetupLogFile" - - $Global:OpenSSHTestInfo = @{} - # test listener name - $Global:OpenSSHTestInfo.Add("Target","localhost") - # test listener port - $Global:OpenSSHTestInfo.Add("Port", "47002") - # test user with single sign on capability - $Global:OpenSSHTestInfo.Add("SSOUser", $Script:SSOUser) - # test user to be used with explicit key for key auth - $Global:OpenSSHTestInfo.Add("PubKeyUser", $Script:PubKeyUser) - # test user for passwd based auth - $Global:OpenSSHTestInfo.Add("PasswdUser", $Script:PasswdUser) - # common password for all test accounts - $Global:OpenSSHTestInfo.Add("TestAccountPW", $Script:OpenSSHTestAccountsPassword) - # openssh bin path - $Global:OpenSSHTestInfo.Add("OpenSSHDir", $Script:OpenSSHDir) - # openssh tests path - $Global:OpenSSHTestInfo.Add("OpenSSHTestDir", $Script:OpenSSHTestDir) - # openssh test setup log file - $Global:OpenSSHTestInfo.Add("TestSetupLogFile", $Script:TestSetupLogFile) - # openssh E2E test results file - $Global:OpenSSHTestInfo.Add("E2ETestResultsFile", $Script:E2ETestResultsFile) - # openssh unittest test results file - $Global:OpenSSHTestInfo.Add("UnitTestResultsFile", $Script:UnitTestResultsFile) -} <# .Synopsis Get-UserSID @@ -261,7 +252,7 @@ function Get-UserSID ) if([string]::IsNullOrEmpty($Domain)) { - $objUser = New-Object System.Security.Principal.NTAccount($User) + $objUser = New-Object System.Security.Principal.NTAccount($User) } else { @@ -276,19 +267,19 @@ function Get-UserSID Cleanup-OpenSSHTestEnvironment #> function Cleanup-OpenSSHTestEnvironment -{ +{ # .exe - Windows specific. TODO - PAL - if (-not (Test-Path (Join-Path $Script:OpenSSHDir ssh.exe) -PathType Leaf)) + if (-not (Test-Path (Join-Path $script:OpenSSHBinPath ssh.exe) -PathType Leaf)) { - Throw "Cannot find OpenSSH binaries under $($Script:OpenSSHDir). Try -OpenSSHDir parameter" + Throw "Cannot find OpenSSH binaries under $script:OpenSSHBinPath. " } #Restore sshd_config - $backupConfigPath = Join-Path $Script:OpenSSHDir sshd_config.ori + $backupConfigPath = Join-Path $Script:OpenSSHBinPath sshd_config.ori if (Test-Path $backupConfigPath -PathType Leaf) { - Copy-Item $backupConfigPath (Join-Path $Script:OpenSSHDir sshd_config) -Force -ErrorAction SilentlyContinue - Remove-Item (Join-Path $Script:OpenSSHDir sshd_config.ori) -Force -ErrorAction SilentlyContinue - Remove-Item $Script:OpenSSHDir\sshtest*hostkey* -Force -ErrorAction SilentlyContinue + Copy-Item $backupConfigPath (Join-Path $Script:OpenSSHBinPath sshd_config) -Force -ErrorAction SilentlyContinue + Remove-Item (Join-Path $Script:OpenSSHBinPath sshd_config.ori) -Force -ErrorAction SilentlyContinue + Remove-Item $Script:OpenSSHBinPath\sshtest*hostkey* -Force -ErrorAction SilentlyContinue Restart-Service sshd } @@ -301,38 +292,37 @@ function Cleanup-OpenSSHTestEnvironment } #Delete accounts - foreach ($user in $Script:OpenSSHTestAccounts) + foreach ($user in $OpenSSHTestAccounts) { net user $user /delete } # remove registered keys - cmd /c "ssh-add -d (Join-Path $Script:OpenSSHTestDir sshtest_userssokey_ed25519) 2>&1 >> $Script:TestSetupLogFile" + cmd /c "ssh-add -d (Join-Path $Script:E2ETestDirectory sshtest_userssokey_ed25519) 2>&1 >> $Script:TestSetupLogFile" - $Global:OpenSSHTestInfo = $null + if($Global:OpenSSHTestInfo -ne $null) + { + $Global:OpenSSHTestInfo.Clear() + $Global:OpenSSHTestInfo = $null + } } <# .Synopsis - After build and test run completes, upload all artifacts from the build machine. + Get-UnitTestDirectory. #> -function Deploy-OpenSSHTests +function Get-UnitTestDirectory { [CmdletBinding()] param ( - [ValidateSet('Debug', 'Release', '')] - [string]$Configuration = "", + [ValidateSet('Debug', 'Release')] + [string]$Configuration = "Release", [ValidateSet('x86', 'x64', '')] [string]$NativeHostArch = "" ) - if (-not (Test-Path -Path $Script:OpenSSHTestDir -PathType Container)) - { - $null = New-Item -Path $Script:OpenSSHTestDir -ItemType Directory -Force -ErrorAction Stop - } - [string] $platform = $env:PROCESSOR_ARCHITECTURE if(-not [String]::IsNullOrEmpty($NativeHostArch)) { @@ -369,25 +359,19 @@ function Deploy-OpenSSHTests { $RealConfiguration = $Configuration } - - [System.IO.DirectoryInfo] $repositoryRoot = Get-RepositoryRoot - #copy all pester tests - $sourceDir = Join-Path $repositoryRoot.FullName -ChildPath "regress\pesterTests" - Copy-Item -Path "$sourceDir\*" -Destination $Script:OpenSSHTestDir -Include *.ps1,*.psm1, sshd_config, known_hosts, sshtest_* -Force -ErrorAction Stop - #copy all unit tests. - $sourceDir = Join-Path $repositoryRoot.FullName -ChildPath "bin\$folderName\$RealConfiguration" - Copy-Item -Path "$sourceDir\*" -Destination "$($Script:OpenSSHTestDir)\" -Container -Include unittest-* -Recurse -Force -ErrorAction Stop + $unitTestdir = Join-Path $repositoryRoot.FullName -ChildPath "bin\$folderName\$RealConfiguration" + $unitTestDir } <# .Synopsis Run OpenSSH pester tests. #> -function Run-OpenSSHPesterTest +function Run-OpenSSHE2ETest { # Discover all CI tests and run them. - Push-Location $Script:OpenSSHTestDir - Write-Log -Message "Running OpenSSH Pester tests..." + Push-Location $Script:E2ETestDirectory + Write-Log -Message "Running OpenSSH E2E tests..." $testFolders = Get-ChildItem *.tests.ps1 -Recurse -Exclude SSHDConfig.tests.ps1, SSH.Tests.ps1 | ForEach-Object{ Split-Path $_.FullName} | Sort-Object -Unique Invoke-Pester $testFolders -OutputFormat NUnitXml -OutputFile $Script:E2ETestResultsFile -Tag 'CI' Pop-Location @@ -399,8 +383,8 @@ function Run-OpenSSHPesterTest #> function Run-OpenSSHUnitTest { - # Discover all CI tests and run them. - Push-Location $Script:OpenSSHTestDir + # Discover all CI tests and run them. + Push-Location $Script:UnitTestDirectory Write-Log -Message "Running OpenSSH unit tests..." if (Test-Path $Script:UnitTestResultsFile) { @@ -453,4 +437,4 @@ function Write-Log } } -Export-ModuleMember -Function Set-OpenSSHTestParams, Dump-OpenSSHTestParams, Install-OpenSSHTestDependencies, Setup-OpenSSHTestEnvironment, Cleanup-OpenSSHTestEnvironment, Deploy-OpenSSHTests, Run-OpenSSHUnitTest, Run-OpenSSHPesterTest +Export-ModuleMember -Function Setup-OpenSSHTestEnvironment, Cleanup-OpenSSHTestEnvironment, Run-OpenSSHUnitTest, Run-OpenSSHE2ETest diff --git a/regress/pesterTests/OpenSSHTestHelper.psm1 b/regress/pesterTests/OpenSSHTestHelper.psm1 deleted file mode 100644 index 3ef1c9331..000000000 --- a/regress/pesterTests/OpenSSHTestHelper.psm1 +++ /dev/null @@ -1,385 +0,0 @@ - - - - -# test environment parametes initialized with defaults -$script:OpenSSHDir = "$env:SystemDrive\OpenSSH" -$script:OpenSSHTestDir = "$env:SystemDrive\OpenSSHTests" -$script:PesterTestResultsFile = Join-Path $script:OpenSSHTestDir "PesterTestResults.xml" -$script:UnitTestResultsFile = Join-Path $script:OpenSSHTestDir "UnitTestResults.txt" -$script:TestSetupLogFile = Join-Path $script:OpenSSHTestDir "TestSetupLog.txt" - -function Set-OpenSSHTestParams -{ - param - ( - [string] $OpenSSHDir = $script:OpenSSHDir, - [string] $OpenSSHTestDir = $script:OpenSSHTestDir, - [string] $PesterTestResultsFile = $script:PesterTestResultsFile, - [string] $UnitTestResultsFile = $script:UnitTestResultsFile, - [string] $TestSetupLogFile = $script:TestSetupLogFile - ) - - $script:OpenSSHDir = $OpenSSHDir - $script:OpenSSHTestDir = $OpenSSHTestDir - $script:PesterTestResultsFile = $PesterTestResultsFile - $script:UnitTestResultsFile = $UnitTestResultsFile - $script:TestSetupLogFile = $TestSetupLogFile -} - -function Dump-OpenSSHTestParams -{ - Write-Host "OpenSSHDir: " $script:OpenSSHDir - Write-Host "OpenSSHTestDir: " $script:OpenSSHTestDir - Write-Host "PesterTestResultsFile: " $script:PesterTestResultsFile - Write-Host "UnitTestResultsFile: " $script:UnitTestResultsFile - Write-Host "TestSetupLogFile: " $script:TestSetupLogFile -} - - -<# - .SYNOPSIS - This function installs the tools required by our tests - 1) Pester for running the tests - 2) sysinternals required by the tests on windows. - #> -function Install-OpenSSHTestDependencies -{ - [CmdletBinding()] - param () - - # Install chocolatey - if(-not (Get-Command "choco" -ErrorAction SilentlyContinue)) - { - Write-Log -Message "Chocolatey not present. Installing chocolatey." - Invoke-Expression ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')) 2>&1 >> $script:TestSetupLogFile - } - - $isModuleAvailable = Get-Module 'Pester' -ListAvailable - if (-not ($isModuleAvailable)) - { - Write-Log -Message "Installing Pester..." - choco install Pester -y --force --limitoutput 2>&1 >> $script:TestSetupLogFile - } - - if ( -not (Test-Path "$env:ProgramData\chocolatey\lib\sysinternals\tools" ) ) { - Write-Log -Message "sysinternals not present. Installing sysinternals." - choco install sysinternals -y --force --limitoutput 2>&1 >> $script:TestSetupLogFile - } - <#if ( (-not (Test-Path "${env:ProgramFiles(x86)}\Windows Kits\8.1\Debuggers\" )) -and (-not (Test-Path "${env:ProgramFiles(x86)}\Windows Kits\10\Debuggers\" ))) { - Write-Log -Message "debugger not present. Installing windbg." - choco install windbg --force --limitoutput -y 2>&1 >> $script:TestSetupLogFile - } - Install-PSCoreFromGithub - $psCorePath = GetLocalPSCorePath - Set-BuildVariable -Name psPath -Value $psCorePath - #> - Write-BuildMessage -Message "All testDependencies installed!" -Category Information -} - - -$testaccounts = "sshtest_ssouser", "sshtest_pubkeyuser", "sshtest_passwduser" -$testaccountPassword = "P@ssw0rd_1" | ConvertTo-SecureString -AsPlainText -Force - -<# - .Synopsis - Setup-OpenSSHTestEnvironment -#> -function Setup-OpenSSHTestEnvironment -{ - [CmdletBinding()] - param - ( - [bool] $Quiet = $false - ) - - if ($Quiet -eq $false) { - Write-Host 'WARNING: Following changes will be made to OpenSSH configuration' - Write-Host ' - sshd_config will be backed up as sshd_config.ori' - Write-Host ' - will be replaced with a test sshd_config' - Write-Host ' - %user%\.ssh\known_hosts will be backed up as known_hosts.ori' - Write-Host ' - will be replaced with a test known_hosts' - Write-Host ' - sshd test listener will be on port 47002' - Write-Host ' - %userprofile%\.ssh\known_hosts will be modified with test host key entry' - Write-Host ' - test accounts - ssouser, pubkeyuser and passwduser will be added' - Write-Host 'To cleanup - Run Cleanup-OpenSSHTestEnvironment' - } - - if (-not(Test-Path(Join-Path $OpenSSHDir ssh.exe))) - { - Throw 'cannot find OpenSSH binaries under ' + $OpenSSHDir + '. Try -OpenSSHDir parameter' - } - - try - { - $sshcmd = get-command ssh.exe - } - catch [System.Exception] - { - Throw 'Cannot find ssh.exe. Make sure OpenSSH binary path is in %PATH%' - } - - # TODO - ensure ssh.exe is being picked from $OpenSSHDir. Multiple versions may exist - - if (-not(Test-Path($OpenSSHTestDir))) { - Throw $OpenSSHTestDir +' does not exist. Try setting -OpenSSHTestDir parameter' - } - - if ((Get-ChildItem $OpenSSHTestDir).Count -eq 0) { - Throw 'Nothing found in ' + $OpenSSHTestDir - } - - #Backup existing OpenSSH configuration - if (-not(Test-Path (Join-Path $OpenSSHDir sshd_config.ori))) { - Copy-Item (Join-Path $OpenSSHDir sshd_config) (Join-Path $OpenSSHDir sshd_config.ori) - } - - # copy new sshd_config - Stop-Service sshd - Stop-Service ssh-agent - Copy-Item (Join-Path $OpenSSHTestDir sshd_config) (Join-Path $OpenSSHDir sshd_config) - Copy-Item $OpenSSHTestDir\sshtest*hostkey* $OpenSSHDir - Start-Service sshd - - #Backup existing known_hosts and replace with test version - #TODO - account for custom known_hosts locations - if (Test-Path (Join-Path $home .ssh\known_hosts)) { - Copy-Item (Join-Path $home .ssh\known_hosts) (Join-Path $home .ssh\known_hosts.ori) - } - Copy-Item (Join-Path $OpenSSHTestDir known_hosts) (Join-Path $home .ssh\known_hosts) - - # create test accounts - foreach ($user in $testaccounts) { - New-LocalUser -Name $user -Password $testaccountPassword -ErrorAction SilentlyContinue - } - - #setup single sign on for ssouser - #TODO - this is Windows specific. Need to be in PAL - $ssouser = Get-LocalUser sshtest_ssouser - $ssouserProfileRegistry = Join-Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' $ssouser.SID - if (-not(Test-Path $ssouserProfileRegistry)) { - #create profile - if (-not($env:DISPLAY)) {$env:DISPLAY=1} - $env:SSH_ASKPASS="$($env:ComSpec) /c echo P@ssw0rd_1" - ssh -p 47002 sshtest_ssouser@localhost whoami - if ($env:DISPLAY -eq 1) {Remove-Item env:\DISPLAY} - remove-item "env:SSH_ASKPASS" -ErrorAction SilentlyContinue - } - $ssouserProfile = (Get-ItemProperty -Path $ssouserProfileRegistry -Name 'ProfileImagePath').ProfileImagePath - mkdir -Path (Join-Path $ssouserProfile .ssh) -ErrorAction SilentlyContinue - Copy-Item (Join-Path $OpenSSHTestDir sshtest_userssokey_ed25519.pub) (Join-Path $ssouserProfile .ssh\authorized_keys) - $acl = get-acl (Join-Path $ssouserProfile .ssh\authorized_keys) - $ar = New-Object System.Security.AccessControl.FileSystemAccessRule("NT Service\sshd", "Read", "Allow") - $acl.SetAccessRule($ar) - Set-Acl (Join-Path $ssouserProfile .ssh\authorized_keys) $acl - $ssouserpubkey = Join-Path $OpenSSHTestDir sshtest_userssokey_ed25519 - ssh-add $ssouserpubkey - - #TODO - scp tests need an admin user. This restriction should be removed - net localgroup Administrators sshtest_ssouser /add - -} - - -<# - .Synopsis - Cleanup-OpenSSHTestEnvironment -#> -function Cleanup-OpenSSHTestEnvironment -{ - - [CmdletBinding()] - param - () - - # .exe - Windows specific. TODO - PAL - if (-not(Test-Path(Join-Path $OpenSSHDir ssh.exe))) - { - Throw 'cannot find OpenSSH binaries under ' + $OpenSSHDir + '. Try -OpenSSHDir parameter' - } - - #Restore sshd_config - if (Test-Path (Join-Path $OpenSSHDir sshd_config.ori)) { - Stop-Service sshd - Stop-Service ssh-agent - Copy-Item (Join-Path $OpenSSHDir sshd_config.ori) (Join-Path $OpenSSHDir sshd_config) - Remove-Item (Join-Path $OpenSSHDir sshd_config.ori) -Force - Remove-Item $OpenSSHDir\sshtest*hostkey* -Force - Start-Service sshd - } - - #Restore known_hosts - if (Test-Path (Join-Path $home .ssh\known_hosts.ori)) { - Copy-Item (Join-Path $home .ssh\known_hosts.ori) (Join-Path $home .ssh\known_hosts) - Remove-Item (Join-Path $home .ssh\known_hosts.ori) -Force - } - - # delete accounts - foreach ($user in $testaccounts) { - Remove-LocalUser -Name $user -ErrorAction SilentlyContinue - } - - # remove registered keys - $ssouserpubkey = Join-Path $OpenSSHTestDir sshtest_userssokey_ed25519 - ssh-add -d $ssouserpubkey - -} - - -<# - .Synopsis - After build and test run completes, upload all artifacts from the build machine. -#> -function Deploy-OpenSSHTests -{ - [CmdletBinding()] - param - ( - [ValidateSet('Debug', 'Release', '')] - [string]$Configuration = "", - - [ValidateSet('x86', 'x64', '')] - [string]$NativeHostArch = "" - ) - - if (-not (Test-Path -Path $OpenSSHTestDir -PathType Container)) - { - $null = New-Item -Path $OpenSSHTestDir -ItemType Directory -Force -ErrorAction Stop - } - - [string] $platform = $env:PROCESSOR_ARCHITECTURE - if(-not [String]::IsNullOrEmpty($NativeHostArch)) - { - $folderName = $NativeHostArch - if($NativeHostArch -eq 'x86') - { - $folderName = "Win32" - } - } - else - { - if($platform -ieq "AMD64") - { - $folderName = "x64" - } - else - { - $folderName = "Win32" - } - } - - if([String]::IsNullOrEmpty($Configuration)) - { - if( $folderName -ieq "Win32" ) - { - $RealConfiguration = "Debug" - } - else - { - $RealConfiguration = "Release" - } - } - else - { - $RealConfiguration = $Configuration - } - - [System.IO.DirectoryInfo] $repositoryRoot = Get-RepositoryRoot - #copy all pester tests - $sourceDir = Join-Path $repositoryRoot.FullName -ChildPath "regress\pesterTests" - Copy-Item -Path "$sourceDir\*" -Destination $OpenSSHTestDir -Include *.ps1,*.psm1, sshd_config, known_hosts, sshtest_* -Force -ErrorAction Stop - #copy all unit tests. - $sourceDir = Join-Path $repositoryRoot.FullName -ChildPath "bin\$folderName\$RealConfiguration" - Copy-Item -Path "$sourceDir\*" -Destination "$OpenSSHTestDir\" -Container -Include unittest-* -Recurse -Force -ErrorAction Stop - - #restart the service to use the test copy of sshd_config - Restart-Service sshd -} - - -<# - .Synopsis - Run OpenSSH pester tests. -#> -function Run-OpenSSHPesterTest -{ - - # Discover all CI tests and run them. - Push-Location $OpenSSHTestDir - Write-Log -Message "Running OpenSSH Pester tests..." - $testFolders = Get-ChildItem *.tests.ps1 -Recurse -Exclude SSHDConfig.tests.ps1, SSH.Tests.ps1 | ForEach-Object{ Split-Path $_.FullName} | Sort-Object -Unique - Invoke-Pester $testFolders -OutputFormat NUnitXml -OutputFile $PesterTestResultsFile -Tag 'CI' - Pop-Location -} - -function Check-OpenSSHPesterTestResult -{ - if (-not (Test-Path $PesterTestResultsFile)) - { - Write-Warning "$($xml.'test-results'.failures) tests in regress\pesterTests failed" - Write-BuildMessage -Message "Test result file $PesterTestResultsFile not found after tests." -Category Error - Set-BuildVariable TestPassed False - } - $xml = [xml](Get-Content -raw $PesterTestResultsFile) - if ([int]$xml.'test-results'.failures -gt 0) - { - $errorMessage = "$($xml.'test-results'.failures) tests in regress\pesterTests failed. Detail test log is at TestResults.xml." - Write-Warning $errorMessage - Write-BuildMessage -Message $errorMessage -Category Error - Set-BuildVariable TestPassed False - } - - # Writing out warning when the $Error.Count is non-zero. Tests Should clean $Error after success. - if ($Error.Count -gt 0) - { - Write-BuildMessage -Message "Tests Should clean $Error after success." -Category Warning - $Error| Out-File "$testInstallFolder\TestError.txt" -Append - } -} - -<# - .Synopsis - Run unit tests. -#> -function Run-OpenSSHUnitTest -{ - - # Discover all CI tests and run them. - Push-Location $OpenSSHTestDir - Write-Log -Message "Running OpenSSH unit tests..." - if (Test-Path $unitTestOutputFile) - { - Remove-Item -Path $UnitTestResultsFile -Force -ErrorAction SilentlyContinue - } - $testFolders = Get-ChildItem unittest-*.exe -Recurse -Exclude unittest-sshkey.exe,unittest-kex.exe | - ForEach-Object{ Split-Path $_.FullName} | - Sort-Object -Unique - $testfailed = $false - if ($testFolders -ne $null) - { - $testFolders | % { - Push-Location $_ - $unittestFile = "$(Split-Path $_ -Leaf).exe" - Write-Output "Running OpenSSH unit $unittestFile ..." - & .\$unittestFile >> $UnitTestResultsFile - - $errorCode = $LASTEXITCODE - if ($errorCode -ne 0) - { - $testfailed = $true - $errorMessage = "$($_.FullName) test failed for OpenSSH.`nExitCode: $errorCode. Detail test log is at UnitTestResults.txt." - Write-Warning $errorMessage - Write-BuildMessage -Message $errorMessage -Category Error - Set-BuildVariable TestPassed False - } - Pop-Location - } - if(-not $testfailed) - { - Write-BuildMessage -Message "All Unit tests passed!" -Category Information - } - } - Pop-Location -} - -Export-ModuleMember -Function Set-OpenSSHTestParams, Dump-OpenSSHTestParams, Install-OpenSSHTestDependencies, Setup-OpenSSHTestEnvironment, Cleanup-OpenSSHTestEnvironment, Deploy-OpenSSHTests, Run-OpenSSHUnitTest, Run-OpenSSHPesterTest, Check-OpenSSHPesterTestResult diff --git a/regress/pesterTests/PortForwarding.Tests.ps1 b/regress/pesterTests/PortForwarding.Tests.ps1 index 05225e26d..8fd2ff16d 100644 --- a/regress/pesterTests/PortForwarding.Tests.ps1 +++ b/regress/pesterTests/PortForwarding.Tests.ps1 @@ -1,6 +1,11 @@ Describe "Tests for portforwarding" -Tags "CI" { - BeforeAll { + BeforeAll { + + if($OpenSSHTestInfo -eq $null) + { + Throw "`$OpenSSHTestInfo is null. Please run Setup-OpenSSHTestEnvironment to setup test environment." + } $fileName = "test.txt" $filePath = Join-Path ${TestDrive} $fileName $logName = "log.txt" @@ -24,9 +29,6 @@ Describe "Tests for portforwarding" -Tags "CI" { ) } - AfterAll { - } - AfterEach { Remove-Item -Path $filePath -Force -ea silentlycontinue Remove-Item -Path $logPath -Force -ea silentlycontinue diff --git a/regress/pesterTests/PowerShell.SSH.Tests.ps1 b/regress/pesterTests/PowerShell.SSH.Tests.ps1 index 4e87200c4..1f8acd70e 100644 --- a/regress/pesterTests/PowerShell.SSH.Tests.ps1 +++ b/regress/pesterTests/PowerShell.SSH.Tests.ps1 @@ -1,6 +1,4 @@ -using module .\PlatformAbstractLayer.psm1 - -Describe "Tests for powershell over ssh" -Tags "Scenario" { +Describe "Tests for powershell over ssh" -Tags "Scenario" { BeforeAll { $defaultParamValues = $PSDefaultParameterValues.Clone() #Skip on windows powershell. this feature only supported in powershell core from git diff --git a/regress/pesterTests/README.md b/regress/pesterTests/README.md new file mode 100644 index 000000000..f5bf7ff56 --- /dev/null +++ b/regress/pesterTests/README.md @@ -0,0 +1,34 @@ +Run OpenSSH Pester Tests: +================================== + +#### To setup the test environment before test run: + +```powershell +Import-Module .\openssh-portable\contrib\win32\openssh\OpenSSHTestHelper.psm1 –Force +Setup-OpenSSHTestEnvironment +``` + +`Setup-OpenSSHTestEnvironment` contains below parameters: +* `-OpenSSHBinPath`: Specify the location where ssh.exe should be picked up. If not specified, the function will prompt to user if he/she want to choose the first ssh.exe found in `$env:path` if exists. +* `-TestDataPath`: Specify the location where the test binaries deploy to. The default is `$env:SystemDrive\OpenSSHTests` if it not specified. +* `-Quiet`: If it is set, the function will do all the changes without prompting to user to confirm. +* `-DebugMode`: If it is set, the subsequent tests will be running in debug mode. User can modify by setting $OpenSSHTestInfo["DebugMode"] . + +#### To run the test suites: + +```powershell +Run-OpenSSHE2ETest +Run-OpenSSHUnitTest +``` + +#### To run a particular test, just run the script or the executatlbe directly + +```powershell +C:\git\openssh-portable\regress\pesterTests\SCP.Tests.ps1 +C:\git\openssh-portable\bin\x64\Release\unittest-bitmap\unittest-bitmap.exe +``` +#### To revert what's done in Setup-OpenSSHTestEnvironment: + +```powershell +Cleanup-OpenSSHTestEnvironment +``` diff --git a/regress/pesterTests/SCP.Tests.ps1 b/regress/pesterTests/SCP.Tests.ps1 index 7b13415e5..d0defd9d7 100644 --- a/regress/pesterTests/SCP.Tests.ps1 +++ b/regress/pesterTests/SCP.Tests.ps1 @@ -2,24 +2,35 @@ #covered -i -p -q -r -v -c -S -C #todo: -F, -l and -P should be tested over the network Describe "Tests for scp command" -Tags "CI" { - BeforeAll { + BeforeAll { + if($OpenSSHTestInfo -eq $null) + { + Throw "`$OpenSSHTestInfo is null. Please run Setup-OpenSSHTestEnvironment to setup test environment." + } + + if(-not (Test-Path $OpenSSHTestInfo["TestDataPath"])) + { + $null = New-Item $OpenSSHTestInfo["TestDataPath"] -ItemType directory -Force -ErrorAction SilentlyContinue + } + $fileName1 = "test.txt" $fileName2 = "test2.txt" $SourceDirName = "SourceDir" - $SourceDir = Join-Path ${TestDrive} $SourceDirName + $SourceDir = Join-Path "$($OpenSSHTestInfo["TestDataPath"])\SCP" $SourceDirName $SourceFilePath = Join-Path $SourceDir $fileName1 - $DestinationDir = Join-Path ${TestDrive} "DestDir" + $DestinationDir = Join-Path "$($OpenSSHTestInfo["TestDataPath"])\SCP" "DestDir" $DestinationFilePath = Join-Path $DestinationDir $fileName1 $NestedSourceDir= Join-Path $SourceDir "nested" $NestedSourceFilePath = Join-Path $NestedSourceDir $fileName2 - $null = New-Item $SourceDir -ItemType directory -Force - $null = New-Item $NestedSourceDir -ItemType directory -Force - $null = New-item -path $SourceFilePath -force - $null = New-item -path $NestedSourceFilePath -force + $null = New-Item $SourceDir -ItemType directory -Force -ErrorAction SilentlyContinue + $null = New-Item $NestedSourceDir -ItemType directory -Force -ErrorAction SilentlyContinue + $null = New-item -path $SourceFilePath -force -ErrorAction SilentlyContinue + $null = New-item -path $NestedSourceFilePath -force -ErrorAction SilentlyContinue "Test content111" | Set-content -Path $SourceFilePath "Test content in nested dir" | Set-content -Path $NestedSourceFilePath - $null = New-Item $DestinationDir -ItemType directory -Force - + $null = New-Item $DestinationDir -ItemType directory -Force -ErrorAction SilentlyContinue + $sshcmd = (get-command ssh).Path + $server = $OpenSSHTestInfo["Target"] $port = $OpenSSHTestInfo["Port"] $ssouser = $OpenSSHTestInfo["SSOUser"] @@ -30,31 +41,37 @@ Describe "Tests for scp command" -Tags "CI" { Title = 'Simple copy local file to local file' Source = $SourceFilePath Destination = $DestinationFilePath + Options = "-P $port " }, @{ Title = 'Simple copy local file to remote file' Source = $SourceFilePath - Destination = "$($ssouser)@$($server):$DestinationFilePath" + Destination = "$($ssouser)@$($server):$DestinationFilePath" + Options = "-P $port -S $sshcmd" }, @{ Title = 'Simple copy remote file to local file' Source = "$($ssouser)@$($server):$SourceFilePath" - Destination = $DestinationFilePath + Destination = $DestinationFilePath + Options = "-P $port -p -c aes128-ctr -C" }, @{ Title = 'Simple copy local file to local dir' Source = $SourceFilePath Destination = $DestinationDir + Options = "-P $port " }, @{ Title = 'simple copy local file to remote dir' Source = $SourceFilePath Destination = "$($ssouser)@$($server):$DestinationDir" + Options = "-P $port -C -q" }, @{ Title = 'simple copy remote file to local dir' Source = "$($ssouser)@$($server):$SourceFilePath" Destination = $DestinationDir + Options = "-P $port " } ) @@ -63,26 +80,42 @@ Describe "Tests for scp command" -Tags "CI" { Title = 'copy from local dir to remote dir' Source = $sourceDir Destination = "$($ssouser)@$($server):$DestinationDir" + Options = "-P $port -r -p -c aes128-ctr" }, - <# @{ + @{ Title = 'copy from local dir to local dir' Source = $sourceDir Destination = $DestinationDir - },#> + Options = "-r " + }, @{ Title = 'copy from remote dir to local dir' Source = "$($ssouser)@$($server):$sourceDir" Destination = $DestinationDir + Options = "-P $port -C -r -q" } ) + if ($OpenSSHTestInfo['DebugMode']) + { + Stop-Service ssh-agent -Force + Start-Sleep 2 + # Fix this - pick up logs from ssh installation dir, not test directory + Remove-Item "$($OpenSSHTestInfo['OpenSSHDir'])\logs\ssh-agent.log" -Force -ErrorAction ignore + Remove-Item "$($OpenSSHTestInfo['OpenSSHDir'])\logs\sshd.log" -Force -ErrorAction ignore + Start-Service sshd + } + function CheckTarget { param([string]$target) if(-not (Test-path $target)) - { - Copy-Item .\logs\ssh-agent.log ".\logs\failedagent$script:logNum.log" -Force - Copy-Item .\logs\sshd.log ".\logs\failedsshd$script:logNum.log" -Force - $script:logNum++ + { + if( $OpenSSHTestInfo["DebugMode"]) + { + Copy-Item "$($OpenSSHTestInfo['OpenSSHDir'])\logs\ssh-agent.log" "$($OpenSSHTestInfo['OpenSSHDir'])\logs\failedagent$script:logNum.log" -Force + Copy-Item "$($OpenSSHTestInfo['OpenSSHDir'])\logs\sshd.log" "$($OpenSSHTestInfo['OpenSSHDir'])\logs\failedsshd$script:logNum.log" -Force + $script:logNum++ + } return $false } @@ -91,8 +124,21 @@ Describe "Tests for scp command" -Tags "CI" { } AfterAll { - Get-Item $SourceDir | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue - Get-Item $DestinationDir | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue + if($OpenSSHTestInfo -eq $null) + { + #do nothing + } + elseif( -not $OpenSSHTestInfo['DebugMode']) + { + if(-not [string]::IsNullOrEmpty($SourceDir)) + { + Get-Item $SourceDir | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue + } + if(-not [string]::IsNullOrEmpty($DestinationDir)) + { + Get-Item $DestinationDir | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue + } + } } BeforeAll { @@ -101,163 +147,50 @@ Describe "Tests for scp command" -Tags "CI" { AfterEach { Get-ChildItem $DestinationDir -Recurse | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue - } - - <#Context "SCP usage" { - It 'SCP usage' { - #TODO: usage output does not redirect to file - } - }#> + } - Context "SCP -i option" { - BeforeAll { - } - BeforeEach { - if ($env:DebugMode) - { - Stop-Service ssh-agent -Force - Start-Sleep 2 - # Fix this - pick up logs from ssh installation dir, not test directory - Remove-Item .\logs\ssh-agent.log -Force -ErrorAction ignore - Remove-Item .\logs\sshd.log -Force -ErrorAction ignore - Start-Service sshd - } - } - AfterAll { - } - - It 'File copy with -i option: