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

@ -106,7 +106,7 @@ Class Machine
} }
[void] InitializeClient() { [void] InitializeClient() {
$this.ClientKeyDirectory = join-path ($env:USERPROFILE) ".ssh" $this.ClientKeyDirectory = join-path $PSScriptRoot "clientkeys"
if(-not (Test-path $this.ClientKeyDirectory -PathType Container)) if(-not (Test-path $this.ClientKeyDirectory -PathType Container))
{ {
New-Item -Path $this.ClientKeyDirectory -ItemType Directory -Force -ErrorAction silentlycontinue New-Item -Path $this.ClientKeyDirectory -ItemType Directory -Force -ErrorAction silentlycontinue
@ -132,7 +132,6 @@ Class Machine
$this.clientPublicKeyPaths += "$keyPath.pub" $this.clientPublicKeyPaths += "$keyPath.pub"
$str = ".\ssh-keygen -t $key -P """" -f $keyPath" $str = ".\ssh-keygen -t $key -P """" -f $keyPath"
$this.RunCmd($str) $this.RunCmd($str)
} }
} }
@ -212,7 +211,12 @@ Class Machine
} }
[void] CleanupServer() { [void] CleanupServer() {
Remove-Item -Path $this.localAdminAuthorizedKeyPath -Force -ea silentlycontinue $sshPath = split-path $this.localAdminAuthorizedKeyPath -Parent
if(Test-Path $sshPath -PathType Container )
{
Remove-item -path $sshPath -force -Recurse
}
if ( $this.Platform -eq [PlatformType]::Windows ) if ( $this.Platform -eq [PlatformType]::Windows )
{ {
$this.CleanupLocalAccountTokenFilterPolicy() $this.CleanupLocalAccountTokenFilterPolicy()
@ -220,7 +224,8 @@ Class Machine
} }
[void] CleanupClient() { [void] CleanupClient() {
Remove-Item -Path "$this.clientKeyPath\*" -Force -ea silentlycontinue Remove-item -path $($this.ClientKeyDirectory) -force -Recurse -ea silentlycontinue
$this.CleanupPasswordSetting()
} }
[void] RunCmd($Str) { [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 #Set LocalAccountTokenFilterPolicy
[void] SetLocalAccountTokenFilterPolicy($setting) { [void] SetLocalAccountTokenFilterPolicy($setting) {
$path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\system" $path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\system"
@ -308,6 +326,7 @@ Class Machine
$shell_app = $null $shell_app = $null
} }
#this does not work when "using module"; works fine when import the module
[void] DownloadPStools() [void] DownloadPStools()
{ {
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'MACHINE') $machinePath = [Environment]::GetEnvironmentVariable('Path', 'MACHINE')

View File

@ -100,29 +100,28 @@ Describe "Tests for scp command" -Tags "CI" {
} }
}#> }#>
#this context only run on windows
Context "Key is Secured in ssh-agent on server" { Context "Key is Secured in ssh-agent on server" {
BeforeAll { BeforeAll {
$Server.SecureHostKeys($server.PrivateHostKeyPaths) $Server.SecureHostKeys($server.PrivateHostKeyPaths)
$identifyFile = $client.clientPrivateKeyPaths[0] $privateKeyFile = $client.clientPrivateKeyPaths[0]
} }
AfterAll { AfterAll {
$Server.CleanupHostKeys() $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) 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. #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 = @(Compare-Object (Get-ChildItem -path $SourceFilePath) (Get-ChildItem -path $DestinationFilePath) -Property Name, Length).Length -eq 0
$equal | Should Be $true $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) 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 = @(Compare-Object (Get-Item -path $SourceDir ) (Get-Item -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0
$equal | Should Be $true $equal | Should Be $true
@ -133,7 +132,6 @@ Describe "Tests for scp command" -Tags "CI" {
} }
} }
#this context only run on windows
Context "Single signon with keys -p -v -c option Secured in ssh-agent" { Context "Single signon with keys -p -v -c option Secured in ssh-agent" {
BeforeAll { BeforeAll {
$Server.SecureHostKeys($server.PrivateHostKeyPaths) $Server.SecureHostKeys($server.PrivateHostKeyPaths)
@ -149,14 +147,14 @@ Describe "Tests for scp command" -Tags "CI" {
.\ssh-add.exe -D .\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" .\scp -S .\ssh.exe $SourceFilePath "$($server.localAdminUserName)@$($server.MachineName):$DestinationFilePath"
#validate file content. DestPath is the path to the file. #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 = @(Compare-Object (Get-ChildItem -path $SourceFilePath) (Get-ChildItem -path $DestinationFilePath) -Property Name, Length).Length -eq 0
$equal | Should Be $true $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) param([string]$Title, $Source, $Destination)
.\scp -p -c aes128-ctr -v -C $Source $Destination .\scp -p -c aes128-ctr -v -C $Source $Destination
@ -165,11 +163,11 @@ Describe "Tests for scp command" -Tags "CI" {
$equal | Should Be $true $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) param([string]$Title, $Source, $Destination)
.\scp -r -p -c aes128-ctr -v $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 | 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 = @(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 { BeforeAll {
$identifyFile = $client.clientPrivateKeyPaths[0] $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) param([string]$Title, $Source, $Destination)
.\scp -i $identifyFile -C -q $Source $Destination .\scp -i $identifyFile -C -q $Source $Destination
@ -191,11 +189,10 @@ Describe "Tests for scp command" -Tags "CI" {
$equal | Should Be $true $equal | Should Be $true
} }
It 'Directory recursive copy with -i -C -r and -q options: <Title> ' -TestCases:$testData1 {
It 'Directory recursive Copy with -i and -q options: <Title> ' -TestCases:$testData1 {
param([string]$Title, $Source, $Destination) 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 = @(Compare-Object (Get-Item -path $SourceDir ) (Get-Item -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0
$equal | Should Be $true $equal | Should Be $true
@ -203,4 +200,34 @@ Describe "Tests for scp command" -Tags "CI" {
$equal | Should Be $true $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

@ -127,6 +127,29 @@ Describe "Tests for ssh command" -Tags "CI" {
Remove-Item -Path $filePath -Force -ea silentlycontinue 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
}
}
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 { It '<Title>' -TestCases:$testData {
param([string]$Title, $LogonStr, $Options) param([string]$Title, $LogonStr, $Options)