Password authention tests for SCP and SSH tests (#52)

This commit is contained in:
Yanbing 2017-01-11 21:54:44 -08:00 committed by Manoj Ampalam
parent 3508cdc624
commit 50e4499fe0
3 changed files with 102 additions and 33 deletions

View File

@ -85,7 +85,7 @@ Class Machine
[string] $ClientKeyDirectory
[string] $knownHostOfCurrentUser
[string] $OpenSSHdir = $PSScriptRoot
[string] $ToolsPath = "$env:ProgramData\chocolatey\lib\sysinternals\tools"
[string] $ToolsPath = "$env:ProgramData\chocolatey\lib\sysinternals\tools"
Machine() {
$this.Platform = Set-Platform
@ -106,13 +106,13 @@ Class Machine
}
[void] InitializeClient() {
$this.ClientKeyDirectory = join-path ($env:USERPROFILE) ".ssh"
$this.ClientKeyDirectory = join-path $PSScriptRoot "clientkeys"
if(-not (Test-path $this.ClientKeyDirectory -PathType Container))
{
New-Item -Path $this.ClientKeyDirectory -ItemType Directory -Force -ErrorAction silentlycontinue
}
Remove-Item -Path "$($this.ClientKeyDirectory)\*" -Force -ea silentlycontinue
Remove-Item -Path "$($this.ClientKeyDirectory)\*" -Force -ea silentlycontinue
$this.knownHostOfCurrentUser = join-path ($env:USERPROFILE) ".ssh/known_hosts"
@ -131,8 +131,7 @@ Class Machine
$this.clientPrivateKeyPaths += $keyPath
$this.clientPublicKeyPaths += "$keyPath.pub"
$str = ".\ssh-keygen -t $key -P """" -f $keyPath"
$this.RunCmd($str)
$this.RunCmd($str)
}
}
@ -211,16 +210,22 @@ Class Machine
}
}
[void] CleanupServer() {
Remove-Item -Path $this.localAdminAuthorizedKeyPath -Force -ea silentlycontinue
[void] CleanupServer() {
$sshPath = split-path $this.localAdminAuthorizedKeyPath -Parent
if(Test-Path $sshPath -PathType Container )
{
Remove-item -path $sshPath -force -Recurse
}
if ( $this.Platform -eq [PlatformType]::Windows )
{
$this.CleanupLocalAccountTokenFilterPolicy()
}
}
[void] CleanupClient() {
Remove-Item -Path "$this.clientKeyPath\*" -Force -ea silentlycontinue
[void] CleanupClient() {
Remove-item -path $($this.ClientKeyDirectory) -force -Recurse -ea silentlycontinue
$this.CleanupPasswordSetting()
}
[void] RunCmd($Str) {
@ -248,6 +253,19 @@ Class Machine
}
}
[void] AddPasswordSetting([string] $pass) {
if ($this.Platform -eq [PlatformType]::Windows) {
$env:SSH_ASKPASS="$($env:ComSpec) /c echo $pass"
}
}
[void] CleanupPasswordSetting() {
if ($this.Platform -eq [PlatformType]::Windows -and (Test-Path env:SSH_ASKPASS))
{
remove-item "env:SSH_ASKPASS" -ErrorAction SilentlyContinue
}
}
#Set LocalAccountTokenFilterPolicy
[void] SetLocalAccountTokenFilterPolicy($setting) {
$path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\system"
@ -308,6 +326,7 @@ Class Machine
$shell_app = $null
}
#this does not work when "using module"; works fine when import the module
[void] DownloadPStools()
{
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'MACHINE')

View File

@ -98,31 +98,30 @@ Describe "Tests for scp command" -Tags "CI" {
It 'SCP usage' {
#TODO: usage output does not redirect to file
}
}#>
#this context only run on windows
}#>
Context "Key is Secured in ssh-agent on server" {
BeforeAll {
$Server.SecureHostKeys($server.PrivateHostKeyPaths)
$identifyFile = $client.clientPrivateKeyPaths[0]
$privateKeyFile = $client.clientPrivateKeyPaths[0]
}
AfterAll {
$Server.CleanupHostKeys()
}
It 'File Copy with -i option: <Title> ' -TestCases:$testData {
It 'File copy with -i option and private key: <Title> ' -TestCases:$testData {
param([string]$Title, $Source, $Destination)
.\scp -i $identifyFile $Source $Destination
.\scp -i $privateKeyFile $Source $Destination
#validate file content. DestPath is the path to the file.
$equal = @(Compare-Object (Get-ChildItem -path $SourceFilePath) (Get-ChildItem -path $DestinationFilePath) -Property Name, Length).Length -eq 0
$equal | Should Be $true
}
}
It 'Directory recursive Copy with -i option: <Title> ' -TestCases:$testData1 {
It 'Directory recursive copy with -i option and private key: <Title> ' -TestCases:$testData1 {
param([string]$Title, $Source, $Destination)
.\scp -r -i $identifyFile $Source $Destination
.\scp -r -i $privateKeyFile $Source $Destination
$equal = @(Compare-Object (Get-Item -path $SourceDir ) (Get-Item -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0
$equal | Should Be $true
@ -130,10 +129,9 @@ Describe "Tests for scp command" -Tags "CI" {
$equal = @(Compare-Object (Get-ChildItem -Recurse -path $SourceDir) (Get-ChildItem -Recurse -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0
$equal | Should Be $true
}
}
}
#this context only run on windows
Context "Single signon with keys -p -v -c option Secured in ssh-agent" {
BeforeAll {
$Server.SecureHostKeys($server.PrivateHostKeyPaths)
@ -149,27 +147,27 @@ Describe "Tests for scp command" -Tags "CI" {
.\ssh-add.exe -D
}
It 'File Copy with -S option (positive)' {
It 'File copy with -S option (positive)' {
.\scp -S .\ssh.exe $SourceFilePath "$($server.localAdminUserName)@$($server.MachineName):$DestinationFilePath"
#validate file content. DestPath is the path to the file.
$equal = @(Compare-Object (Get-ChildItem -path $SourceFilePath) (Get-ChildItem -path $DestinationFilePath) -Property Name, Length).Length -eq 0
$equal | Should Be $true
}
It 'File Copy with -p -c -v option: <Title> ' -TestCases:$testData {
It 'File copy with -p -c -v option: <Title> ' -TestCases:$testData {
param([string]$Title, $Source, $Destination)
.\scp -p -c aes128-ctr -v -C $Source $Destination
.\scp -p -c aes128-ctr -v -C $Source $Destination
#validate file content. DestPath is the path to the file.
$equal = @(Compare-Object (Get-ChildItem -path $SourceFilePath) (Get-ChildItem -path $DestinationFilePath) -Property Name, Length, LastWriteTime.DateTime).Length -eq 0
$equal | Should Be $true
}
It 'Directory recursive Copy with -r -p -v option: <Title> ' -TestCases:$testData1 {
It 'Directory recursive copy with -r -p -v option: <Title> ' -TestCases:$testData1 {
param([string]$Title, $Source, $Destination)
.\scp -r -p -c aes128-ctr -v $Source $Destination
$equal = @(Compare-Object (Get-Item -path $SourceDir ) (Get-Item -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0
$equal = @(Compare-Object (Get-Item -path $SourceDir ) (Get-Item -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length, LastWriteTime.DateTime).Length -eq 0
$equal | Should Be $true
$equal = @(Compare-Object (Get-ChildItem -Recurse -path $SourceDir) (Get-ChildItem -Recurse -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length, LastWriteTime.DateTime).Length -eq 0
@ -177,12 +175,12 @@ Describe "Tests for scp command" -Tags "CI" {
}
}
Context "Key based authentication with -i -C -q options. host keys are not secured on server" {
Context "Private key authentication with -i -C -q options. host keys are not secured on server" {
BeforeAll {
$identifyFile = $client.clientPrivateKeyPaths[0]
}
It 'File Copy with -i -C -q options: <Title> ' -TestCases:$testData{
It 'File copy with -i -C -q options: <Title> ' -TestCases:$testData{
param([string]$Title, $Source, $Destination)
.\scp -i $identifyFile -C -q $Source $Destination
@ -191,16 +189,45 @@ Describe "Tests for scp command" -Tags "CI" {
$equal | Should Be $true
}
It 'Directory recursive Copy with -i and -q options: <Title> ' -TestCases:$testData1 {
It 'Directory recursive copy with -i -C -r and -q options: <Title> ' -TestCases:$testData1 {
param([string]$Title, $Source, $Destination)
.\scp -i $identifyFile -r -q $Source $Destination
.\scp -i $identifyFile -C -r -q $Source $Destination
$equal = @(Compare-Object (Get-Item -path $SourceDir ) (Get-Item -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0
$equal | Should Be $true
$equal = @(Compare-Object (Get-ChildItem -Recurse -path $SourceDir) (Get-ChildItem -Recurse -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0
$equal | Should Be $true
}
}
}
Context "Password authentication" {
BeforeAll {
$client.AddPasswordSetting($server.localAdminPassword)
}
AfterAll {
$client.CleanupPasswordSetting()
}
It 'File copy with -p and -v options: <Title> ' -TestCases:$testData {
param([string]$Title, $Source, $Destination)
.\scp -v -p $Source $Destination
#validate file content. DestPath is the path to the file.
$equal = @(Compare-Object (Get-ChildItem -path $SourceFilePath) (Get-ChildItem -path $DestinationFilePath) -Property Name, Length, LastWriteTime.DateTime).Length -eq 0
$equal | Should Be $true
}
It 'Directory recursive copy with -p and -v options: <Title> ' -TestCases:$testData1 {
param([string]$Title, $Source, $Destination)
.\scp -r -v $Source $Destination
$equal = @(Compare-Object (Get-Item -path $SourceDir ) (Get-Item -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length, LastWriteTime.DateTime).Length -eq 0
$equal | Should Be $true
$equal = @(Compare-Object (Get-ChildItem -Recurse -path $SourceDir) (Get-ChildItem -Recurse -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length, LastWriteTime.DateTime).Length -eq 0
$equal | Should Be $true
}
}
}

View File

@ -135,5 +135,28 @@ Describe "Tests for ssh command" -Tags "CI" {
#validate file content.
Get-Content $filePath | Should be $server.MachineName
}
}
}
Context "password authentication" {
BeforeAll {
$client.AddPasswordSetting($server.localAdminPassword)
Remove-Item -Path $filePath -Force -ea silentlycontinue
}
AfterAll {
$client.CleanupPasswordSetting()
}
AfterEach {
Remove-Item -Path $filePath -Force -ea silentlycontinue
}
It '<Title>' -TestCases:$testData {
param([string]$Title, $LogonStr, $Options)
$str = ".\ssh $($Options) $($LogonStr) hostname > $filePath"
$client.RunCmd($str)
#validate file content.
Get-Content $filePath | Should be $server.MachineName
}
}
}