2016-12-19 23:48:14 +01:00
|
|
|
|
using module .\PlatformAbstractLayer.psm1
|
|
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
|
$fileName1 = "test.txt"
|
|
|
|
|
$fileName2 = "test2.txt"
|
|
|
|
|
$SourceDirName = "SourceDir"
|
|
|
|
|
$SourceDir = Join-Path ${TestDrive} $SourceDirName
|
|
|
|
|
$SourceFilePath = Join-Path $SourceDir $fileName1
|
|
|
|
|
$DestinationDir = Join-Path ${TestDrive} "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
|
|
|
|
|
"Test content111" | Set-content -Path $SourceFilePath
|
|
|
|
|
"Test content in nested dir" | Set-content -Path $NestedSourceFilePath
|
|
|
|
|
$null = New-Item $DestinationDir -ItemType directory -Force
|
|
|
|
|
|
|
|
|
|
[Machine] $client = [Machine]::new([MachineRole]::Client)
|
|
|
|
|
[Machine] $server = [Machine]::new([MachineRole]::Server)
|
|
|
|
|
$client.SetupClient($server)
|
|
|
|
|
$server.SetupServer($client)
|
2017-01-27 19:47:20 +01:00
|
|
|
|
$script:logNum = 0
|
2016-12-19 23:48:14 +01:00
|
|
|
|
|
2016-12-22 06:17:14 +01:00
|
|
|
|
$testData = @(
|
2016-12-19 23:48:14 +01:00
|
|
|
|
@{
|
|
|
|
|
Title = 'Simple copy local file to local file'
|
|
|
|
|
Source = $SourceFilePath
|
|
|
|
|
Destination = $DestinationFilePath
|
2016-12-22 06:17:14 +01:00
|
|
|
|
},
|
2016-12-19 23:48:14 +01:00
|
|
|
|
@{
|
|
|
|
|
Title = 'Simple copy local file to remote file'
|
|
|
|
|
Source = $SourceFilePath
|
2017-01-27 19:47:20 +01:00
|
|
|
|
Destination = "$($server.localAdminUserName)@$($server.MachineName):$DestinationFilePath"
|
2016-12-19 23:48:14 +01:00
|
|
|
|
},
|
|
|
|
|
@{
|
|
|
|
|
Title = 'Simple copy remote file to local file'
|
|
|
|
|
Source = "$($server.localAdminUserName)@$($server.MachineName):$SourceFilePath"
|
|
|
|
|
Destination = $DestinationFilePath
|
2016-12-22 06:17:14 +01:00
|
|
|
|
},
|
2016-12-19 23:48:14 +01:00
|
|
|
|
@{
|
|
|
|
|
Title = 'Simple copy local file to local dir'
|
|
|
|
|
Source = $SourceFilePath
|
|
|
|
|
Destination = $DestinationDir
|
2016-12-22 06:17:14 +01:00
|
|
|
|
},
|
2016-12-19 23:48:14 +01:00
|
|
|
|
@{
|
|
|
|
|
Title = 'simple copy local file to remote dir'
|
|
|
|
|
Source = $SourceFilePath
|
|
|
|
|
Destination = "$($server.localAdminUserName)@$($server.MachineName):$DestinationDir"
|
|
|
|
|
},
|
|
|
|
|
@{
|
|
|
|
|
Title = 'simple copy remote file to local dir'
|
|
|
|
|
Source = "$($server.localAdminUserName)@$($server.MachineName):$SourceFilePath"
|
|
|
|
|
Destination = $DestinationDir
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$testData1 = @(
|
|
|
|
|
@{
|
|
|
|
|
Title = 'copy from local dir to remote dir'
|
|
|
|
|
Source = $sourceDir
|
|
|
|
|
Destination = "$($server.localAdminUserName)@$($server.MachineName):$DestinationDir"
|
2016-12-22 06:17:14 +01:00
|
|
|
|
},
|
2017-02-04 00:18:37 +01:00
|
|
|
|
<# @{
|
2016-12-19 23:48:14 +01:00
|
|
|
|
Title = 'copy from local dir to local dir'
|
|
|
|
|
Source = $sourceDir
|
|
|
|
|
Destination = $DestinationDir
|
2017-02-04 00:18:37 +01:00
|
|
|
|
},#>
|
2016-12-19 23:48:14 +01:00
|
|
|
|
@{
|
|
|
|
|
Title = 'copy from remote dir to local dir'
|
|
|
|
|
Source = "$($server.localAdminUserName)@$($server.MachineName):$sourceDir"
|
|
|
|
|
Destination = $DestinationDir
|
|
|
|
|
}
|
|
|
|
|
)
|
2017-01-27 19:47:20 +01:00
|
|
|
|
|
|
|
|
|
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++
|
|
|
|
|
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
return $true
|
|
|
|
|
}
|
2016-12-19 23:48:14 +01:00
|
|
|
|
}
|
|
|
|
|
AfterAll {
|
|
|
|
|
|
|
|
|
|
$client.CleanupClient()
|
|
|
|
|
$server.CleanupServer()
|
|
|
|
|
|
2016-12-23 00:21:42 +01:00
|
|
|
|
Get-Item $SourceDir | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
|
|
|
|
|
Get-Item $DestinationDir | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
|
2016-12-19 23:48:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-23 00:21:42 +01:00
|
|
|
|
BeforeAll {
|
2017-01-27 19:47:20 +01:00
|
|
|
|
$null = New-Item $DestinationDir -ItemType directory -Force -ErrorAction SilentlyContinue
|
2016-12-19 23:48:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AfterEach {
|
2016-12-23 00:21:42 +01:00
|
|
|
|
Get-ChildItem $DestinationDir -Recurse | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
|
2016-12-19 23:48:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<#Context "SCP usage" {
|
|
|
|
|
It 'SCP usage' {
|
|
|
|
|
#TODO: usage output does not redirect to file
|
|
|
|
|
}
|
2017-01-12 06:54:44 +01:00
|
|
|
|
}#>
|
|
|
|
|
|
2016-12-19 23:48:14 +01:00
|
|
|
|
Context "Key is Secured in ssh-agent on server" {
|
|
|
|
|
BeforeAll {
|
|
|
|
|
$Server.SecureHostKeys($server.PrivateHostKeyPaths)
|
2017-01-12 06:54:44 +01:00
|
|
|
|
$privateKeyFile = $client.clientPrivateKeyPaths[0]
|
2016-12-19 23:48:14 +01:00
|
|
|
|
}
|
2017-01-27 19:47:20 +01:00
|
|
|
|
BeforeEach {
|
|
|
|
|
if ($env:DebugMode)
|
|
|
|
|
{
|
|
|
|
|
Stop-Service ssh-agent -Force
|
|
|
|
|
Start-Sleep 2
|
|
|
|
|
Remove-Item .\logs\ssh-agent.log -Force -ErrorAction ignore
|
|
|
|
|
Remove-Item .\logs\sshd.log -Force -ErrorAction ignore
|
|
|
|
|
Start-Service sshd
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-19 23:48:14 +01:00
|
|
|
|
|
|
|
|
|
AfterAll {
|
|
|
|
|
$Server.CleanupHostKeys()
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-12 06:54:44 +01:00
|
|
|
|
It 'File copy with -i option and private key: <Title> ' -TestCases:$testData {
|
2017-01-27 19:47:20 +01:00
|
|
|
|
param([string]$Title, $Source, $Destination)
|
2017-01-12 06:54:44 +01:00
|
|
|
|
.\scp -i $privateKeyFile $Source $Destination
|
2017-01-27 19:47:20 +01:00
|
|
|
|
$LASTEXITCODE | Should Be 0
|
|
|
|
|
|
2016-12-19 23:48:14 +01:00
|
|
|
|
#validate file content. DestPath is the path to the file.
|
2017-01-27 19:47:20 +01:00
|
|
|
|
CheckTarget -target $DestinationFilePath | Should Be $true
|
2016-12-19 23:48:14 +01:00
|
|
|
|
$equal = @(Compare-Object (Get-ChildItem -path $SourceFilePath) (Get-ChildItem -path $DestinationFilePath) -Property Name, Length).Length -eq 0
|
|
|
|
|
$equal | Should Be $true
|
2017-01-27 19:47:20 +01:00
|
|
|
|
}
|
2016-12-19 23:48:14 +01:00
|
|
|
|
|
2017-01-12 06:54:44 +01:00
|
|
|
|
It 'Directory recursive copy with -i option and private key: <Title> ' -TestCases:$testData1 {
|
2017-01-27 19:47:20 +01:00
|
|
|
|
param([string]$Title, $Source, $Destination)
|
2016-12-19 23:48:14 +01:00
|
|
|
|
|
2017-01-12 06:54:44 +01:00
|
|
|
|
.\scp -r -i $privateKeyFile $Source $Destination
|
2017-01-27 19:47:20 +01:00
|
|
|
|
$LASTEXITCODE | Should Be 0
|
|
|
|
|
CheckTarget -target (join-path $DestinationDir $SourceDirName) | Should Be $true
|
2016-12-19 23:48:14 +01:00
|
|
|
|
|
|
|
|
|
$equal = @(Compare-Object (Get-Item -path $SourceDir ) (Get-Item -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0
|
|
|
|
|
$equal | Should Be $true
|
2016-12-22 06:17:14 +01:00
|
|
|
|
|
|
|
|
|
$equal = @(Compare-Object (Get-ChildItem -Recurse -path $SourceDir) (Get-ChildItem -Recurse -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0
|
2017-01-27 19:47:20 +01:00
|
|
|
|
$equal | Should Be $true
|
2017-01-12 06:54:44 +01:00
|
|
|
|
}
|
2016-12-19 23:48:14 +01:00
|
|
|
|
}
|
2017-01-12 06:54:44 +01:00
|
|
|
|
|
2016-12-19 23:48:14 +01:00
|
|
|
|
Context "Single signon with keys -p -v -c option Secured in ssh-agent" {
|
|
|
|
|
BeforeAll {
|
|
|
|
|
$Server.SecureHostKeys($server.PrivateHostKeyPaths)
|
|
|
|
|
$identifyFile = $client.clientPrivateKeyPaths[0]
|
|
|
|
|
#setup single signon
|
|
|
|
|
.\ssh-add.exe $identifyFile
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AfterAll {
|
|
|
|
|
$Server.CleanupHostKeys()
|
|
|
|
|
|
|
|
|
|
#cleanup single signon
|
|
|
|
|
.\ssh-add.exe -D
|
2016-12-23 00:21:42 +01:00
|
|
|
|
}
|
2016-12-19 23:48:14 +01:00
|
|
|
|
|
2017-01-27 19:47:20 +01:00
|
|
|
|
It 'File copy with -S -v option (positive)' {
|
|
|
|
|
.\scp -S .\ssh.exe -v $SourceFilePath "$($server.localAdminUserName)@$($server.MachineName):$DestinationFilePath"
|
|
|
|
|
$LASTEXITCODE | Should Be 0
|
2016-12-19 23:48:14 +01:00
|
|
|
|
#validate file content. DestPath is the path to the file.
|
2017-01-27 19:47:20 +01:00
|
|
|
|
CheckTarget -target $DestinationFilePath | Should Be $true
|
2016-12-22 06:17:14 +01:00
|
|
|
|
$equal = @(Compare-Object (Get-ChildItem -path $SourceFilePath) (Get-ChildItem -path $DestinationFilePath) -Property Name, Length).Length -eq 0
|
2016-12-19 23:48:14 +01:00
|
|
|
|
$equal | Should Be $true
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 19:47:20 +01:00
|
|
|
|
It 'File copy with -p -c option: <Title> ' -TestCases:$testData {
|
2016-12-19 23:48:14 +01:00
|
|
|
|
param([string]$Title, $Source, $Destination)
|
2017-01-27 19:47:20 +01:00
|
|
|
|
|
|
|
|
|
.\scp -p -c aes128-ctr -C $Source $Destination
|
|
|
|
|
$LASTEXITCODE | Should Be 0
|
2016-12-19 23:48:14 +01:00
|
|
|
|
#validate file content. DestPath is the path to the file.
|
2017-01-27 19:47:20 +01:00
|
|
|
|
CheckTarget -target $DestinationFilePath | Should Be $true
|
2016-12-22 06:17:14 +01:00
|
|
|
|
$equal = @(Compare-Object (Get-ChildItem -path $SourceFilePath) (Get-ChildItem -path $DestinationFilePath) -Property Name, Length, LastWriteTime.DateTime).Length -eq 0
|
|
|
|
|
$equal | Should Be $true
|
2016-12-23 00:21:42 +01:00
|
|
|
|
}
|
2016-12-22 06:17:14 +01:00
|
|
|
|
|
2017-01-27 19:47:20 +01:00
|
|
|
|
It 'Directory recursive copy with -r -p -c option: <Title> ' -TestCases:$testData1 {
|
|
|
|
|
param([string]$Title, $Source, $Destination)
|
2016-12-19 23:48:14 +01:00
|
|
|
|
|
2017-01-27 19:47:20 +01:00
|
|
|
|
.\scp -r -p -c aes128-ctr $Source $Destination
|
|
|
|
|
$LASTEXITCODE | Should Be 0
|
|
|
|
|
CheckTarget -target (join-path $DestinationDir $SourceDirName) | Should Be $true
|
2017-01-12 06:54:44 +01:00
|
|
|
|
$equal = @(Compare-Object (Get-Item -path $SourceDir ) (Get-Item -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length, LastWriteTime.DateTime).Length -eq 0
|
2017-01-27 19:47:20 +01:00
|
|
|
|
$equal | Should Be $true
|
2016-12-22 06:17:14 +01:00
|
|
|
|
|
|
|
|
|
$equal = @(Compare-Object (Get-ChildItem -Recurse -path $SourceDir) (Get-ChildItem -Recurse -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length, LastWriteTime.DateTime).Length -eq 0
|
2016-12-19 23:48:14 +01:00
|
|
|
|
$equal | Should Be $true
|
2016-12-22 06:17:14 +01:00
|
|
|
|
}
|
2016-12-19 23:48:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-12 06:54:44 +01:00
|
|
|
|
Context "Private key authentication with -i -C -q options. host keys are not secured on server" {
|
2016-12-19 23:48:14 +01:00
|
|
|
|
BeforeAll {
|
|
|
|
|
$identifyFile = $client.clientPrivateKeyPaths[0]
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-12 06:54:44 +01:00
|
|
|
|
It 'File copy with -i -C -q options: <Title> ' -TestCases:$testData{
|
2016-12-19 23:48:14 +01:00
|
|
|
|
param([string]$Title, $Source, $Destination)
|
|
|
|
|
|
|
|
|
|
.\scp -i $identifyFile -C -q $Source $Destination
|
2017-01-27 19:47:20 +01:00
|
|
|
|
$LASTEXITCODE | Should Be 0
|
2016-12-19 23:48:14 +01:00
|
|
|
|
#validate file content. DestPath is the path to the file.
|
2017-01-27 19:47:20 +01:00
|
|
|
|
CheckTarget -target $DestinationFilePath | Should Be $true
|
2016-12-22 06:17:14 +01:00
|
|
|
|
$equal = @(Compare-Object (Get-ChildItem -path $SourceFilePath) (Get-ChildItem -path $DestinationFilePath) -Property Name, Length).Length -eq 0
|
2016-12-19 23:48:14 +01:00
|
|
|
|
$equal | Should Be $true
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-12 06:54:44 +01:00
|
|
|
|
It 'Directory recursive copy with -i -C -r and -q options: <Title> ' -TestCases:$testData1 {
|
2016-12-19 23:48:14 +01:00
|
|
|
|
param([string]$Title, $Source, $Destination)
|
|
|
|
|
|
2017-01-12 06:54:44 +01:00
|
|
|
|
.\scp -i $identifyFile -C -r -q $Source $Destination
|
2017-01-27 19:47:20 +01:00
|
|
|
|
$LASTEXITCODE | Should Be 0
|
|
|
|
|
CheckTarget -target (join-path $DestinationDir $SourceDirName) | Should Be $true
|
2016-12-19 23:48:14 +01:00
|
|
|
|
$equal = @(Compare-Object (Get-Item -path $SourceDir ) (Get-Item -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0
|
|
|
|
|
$equal | Should Be $true
|
2016-12-22 06:17:14 +01:00
|
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
}
|
2017-01-12 06:54:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Context "Password authentication" {
|
|
|
|
|
BeforeAll {
|
|
|
|
|
$client.AddPasswordSetting($server.localAdminPassword)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AfterAll {
|
|
|
|
|
$client.CleanupPasswordSetting()
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 21:03:42 +01:00
|
|
|
|
It 'File copy with -p options: <Title> ' -TestCases:$testData {
|
2017-01-12 06:54:44 +01:00
|
|
|
|
param([string]$Title, $Source, $Destination)
|
|
|
|
|
|
2017-01-27 19:47:20 +01:00
|
|
|
|
.\scp -p $Source $Destination
|
|
|
|
|
$LASTEXITCODE | Should Be 0
|
2017-01-12 06:54:44 +01:00
|
|
|
|
#validate file content. DestPath is the path to the file.
|
2017-01-27 19:47:20 +01:00
|
|
|
|
CheckTarget -target $DestinationFilePath | Should Be $true
|
2017-01-12 06:54:44 +01:00
|
|
|
|
$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)
|
|
|
|
|
|
2017-01-27 19:47:20 +01:00
|
|
|
|
.\scp -r -p $Source $Destination
|
|
|
|
|
$LASTEXITCODE | Should Be 0
|
|
|
|
|
CheckTarget -target (join-path $DestinationDir $SourceDirName) | Should Be $true
|
2017-01-12 06:54:44 +01:00
|
|
|
|
$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
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-19 23:48:14 +01:00
|
|
|
|
}
|