@@ -15,7 +15,7 @@ $PubKeyUser = "sshtest_pubkeyuser"
15
15
$PasswdUser = " sshtest_passwduser"
16
16
$OpenSSHTestAccountsPassword = " P@ssw0rd_1"
17
17
$OpenSSHTestAccounts = $Script :SSOUser , $Script :PubKeyUser , $Script :PasswdUser
18
- $OpenSSHConfigPath = Join-Path $ env: ProgramData " ssh "
18
+ $SSHDTestSvcName = " sshdTestSvc "
19
19
20
20
$Script :TestDataPath = " $env: SystemDrive \OpenSSHTests"
21
21
$Script :SetupTestResultsFile = Join-Path $TestDataPath $SetupTestResultsFileName
@@ -24,6 +24,7 @@ $Script:E2ETestResultsFile = Join-Path $TestDataPath $E2ETestResultsFileName
24
24
$Script :UnitTestResultsFile = Join-Path $TestDataPath $UnitTestResultsFileName
25
25
$Script :TestSetupLogFile = Join-Path $TestDataPath $TestSetupLogFileName
26
26
$Script :E2ETestDirectory = Join-Path $repositoryRoot.FullName - ChildPath " regress\pesterTests"
27
+ $Script :E2ETestDataDirectory = Join-Path $Script :E2ETestDirectory data
27
28
$Script :WindowsInBox = $false
28
29
$Script :NoLibreSSL = $false
29
30
$Script :EnableAppVerifier = $true
@@ -81,19 +82,11 @@ function Set-OpenSSHTestEnvironment
81
82
}
82
83
$Global :OpenSSHTestInfo.Add (" PostmortemDebugging" , $Script :PostmortemDebugging )
83
84
84
- # start service if not already started
85
- Start-Service - Name sshd
86
-
87
85
$description = @"
88
86
WARNING: Following changes will be made to OpenSSH configuration
89
- - sshd_config will be backed up as sshd_config.ori
90
- - will be replaced with a test sshd_config
91
- - $HOME \.ssh\known_hosts will be backed up as known_hosts.ori
92
- - will be replaced with a test known_hosts
93
- - $HOME \.ssh\config will be backed up as config.ori
94
- - will be replaced with a test config
87
+ - $HOME \.ssh\known_hosts will be amended with test endpoint entries
88
+ - $HOME \.ssh\config will be amended with test endpoint entries
95
89
- sshd test listener will be on port 47002
96
- - $HOME \.ssh\known_hosts will be modified with test host key entry
97
90
- test accounts - ssouser, pubkeyuser, and passwduser will be added
98
91
- Setup single signon for ssouser
99
92
- To cleanup - Run Clear-OpenSSHTestEnvironment
@@ -109,60 +102,96 @@ WARNING: Following changes will be made to OpenSSH configuration
109
102
110
103
Install-OpenSSHTestDependencies
111
104
112
- $backupConfigPath = Join-Path $OpenSSHConfigPath sshd_config.ori
113
- $targetsshdConfig = Join-Path $OpenSSHConfigPath sshd_config
114
- # Backup existing OpenSSH configuration
115
- if ((Test-Path $targetsshdConfig - PathType Leaf) -and (-not (Test-Path $backupConfigPath - PathType Leaf))) {
116
- Copy-Item $targetsshdConfig $backupConfigPath - Force
117
- }
118
- # copy new sshd_config
119
- Copy-Item (Join-Path $Script :E2ETestDirectory sshd_config) $targetsshdConfig - Force
120
- if ($DebugMode ) {
121
- $con = (Get-Content $targetsshdConfig | Out-String ).Replace(" #SyslogFacility AUTH" , " SyslogFacility LOCAL0" )
122
- Set-Content - Path $targetsshdConfig - Value " $con " - Force
123
- }
124
- $sshAgentSvc = Get-service ssh- agent
125
- if ($sshAgentSvc.StartType -eq [System.ServiceProcess.ServiceStartMode ]::Disabled)
105
+
106
+ # #### START: install sshd test service
107
+ # delete service if exists
108
+ if (Get-Service $SSHDTestSvcName - ErrorAction SilentlyContinue)
126
109
{
127
- Set-service ssh- agent - StartupType Manual
110
+ Stop-Service $SSHDTestSvcName
111
+ sc.exe delete $SSHDTestSvcName 1> $null
128
112
}
129
- Start-Service ssh- agent
130
113
131
- # copy sshtest keys
132
- Copy-Item " $ ( $Script :E2ETestDirectory ) \sshtest*hostkey*" $OpenSSHConfigPath - Force
133
- Get-ChildItem " $ ( $OpenSSHConfigPath ) \sshtest*hostkey*" - Exclude * .pub| % {
134
- Repair-SshdHostKeyPermission - FilePath $_.FullName - confirm:$false
114
+ # prepare config directory
115
+ $testSvcConfigDir = Join-Path $Global :OpenSSHTestInfo [" TestDataPath" ] " serviceconfig"
116
+ Remove-Item $testSvcConfigDir - Force - Recurse - ErrorAction SilentlyContinue
117
+ New-Item - ItemType Directory - Path $testSvcConfigDir
118
+ $Global :OpenSSHTestInfo [" ServiceConfigDir" ] = $testSvcConfigDir
119
+
120
+ # copy sshd_config
121
+ $testSshdConfig = Join-Path $testSvcConfigDir sshd_config
122
+ Copy-Item (Join-Path $Script :E2ETestDataDirectory sshd_config) $testSshdConfig - Force
123
+ $con = (Get-Content $testSshdConfig | Out-String ).Replace(" ___TEST_SERVICE_CONFIG_DIR___" , $testSvcConfigDir )
124
+ Set-Content - Path $testSshdConfig - Value " $con " - Force
125
+ if ($DebugMode ) {
126
+ $con = (Get-Content $testSshdConfig | Out-String ).Replace(" #SyslogFacility AUTH" , " SyslogFacility LOCAL0" )
127
+ Set-Content - Path $testSshdConfig - Value " $con " - Force
135
128
}
136
129
130
+ # copy sshtest keys
131
+ Copy-Item " $ ( $Script :E2ETestDataDirectory ) \sshtest*hostkey*" $testSvcConfigDir - Force
132
+
137
133
# copy ca pubkey to ssh config path
138
- Copy-Item " $ ( $Script :E2ETestDirectory ) \sshtest_ca_userkeys.pub" $OpenSSHConfigPath - Force
134
+ Copy-Item " $ ( $Script :E2ETestDataDirectory ) \sshtest_ca_userkeys.pub" $testSvcConfigDir - Force
135
+
136
+ $acl = New-Object System.Security.AccessControl.DirectorySecurity
137
+ $rule = New-Object System.Security.AccessControl.FileSystemAccessRule(" Administrators" , " FullControl" , " Allow" )
138
+ $acl.AddAccessRule ($rule )
139
+ $rule = New-Object System.Security.AccessControl.FileSystemAccessRule(" System" , " FullControl" , " Allow" )
140
+ $acl.AddAccessRule ($rule )
141
+ $acl.SetAccessRuleProtection ($true , $true )
142
+
143
+ Get-ChildItem $testSvcConfigDir | foreach {$acl | set-acl $_.FullName }
144
+
145
+
146
+ $SSHDTestSvcNameCmdLine = (Join-Path $script :OpenSSHBinPath sshd) + " -f " + $testSshdConfig
147
+ New-Service - Name $SSHDTestSvcName - DisplayName " OpenSSH SSH Test Server for E2E tests" - BinaryPathName $SSHDTestSvcNameCmdLine - StartupType Manual | Out-Null
148
+ sc.exe privs $SSHDTestSvcName SeAssignPrimaryTokenPrivilege/ SeTcbPrivilege/ SeBackupPrivilege/ SeRestorePrivilege/ SeImpersonatePrivilege
149
+
150
+ Start-Service $SSHDTestSvcName
151
+ # #### END: install sshd test service
152
+
139
153
140
154
# copy ca private key to test dir
141
155
$ca_priv_key = (Join-Path $Global :OpenSSHTestInfo [" TestDataPath" ] sshtest_ca_userkeys)
142
- Copy-Item (Join-Path $Script :E2ETestDirectory sshtest_ca_userkeys) $ca_priv_key - Force
156
+ Copy-Item (Join-Path $Script :E2ETestDataDirectory sshtest_ca_userkeys) $ca_priv_key - Force
143
157
Repair-UserSshConfigPermission - FilePath $ca_priv_key - confirm:$false
144
158
$Global :OpenSSHTestInfo [" CA_Private_Key" ] = $ca_priv_key
145
159
146
- Restart-Service sshd - Force
147
-
148
- # Backup existing known_hosts and replace with test version
149
- # TODO - account for custom known_hosts locations
160
+ $sshAgentSvc = Get-service ssh- agent
161
+ if ($sshAgentSvc.StartType -eq [System.ServiceProcess.ServiceStartMode ]::Disabled)
162
+ {
163
+ Set-service ssh- agent - StartupType Manual
164
+ }
165
+ Start-Service ssh- agent
166
+
167
+
168
+
169
+ # Prepare user config - known_hosts and ssh_config
150
170
$dotSshDirectoryPath = Join-Path $home .ssh
151
- $knowHostsFilePath = Join-Path $dotSshDirectoryPath known_hosts
152
171
if (-not (Test-Path $dotSshDirectoryPath - PathType Container))
153
172
{
154
173
New-Item - ItemType Directory - Path $dotSshDirectoryPath - Force - ErrorAction SilentlyContinue | out-null
155
174
}
156
- if ((Test-Path $knowHostsFilePath - PathType Leaf) -and (-not (Test-Path (Join-Path $dotSshDirectoryPath known_hosts.ori) - PathType Leaf))) {
157
- Copy-Item $knowHostsFilePath (Join-Path $dotSshDirectoryPath known_hosts.ori) - Force
175
+
176
+ $knowHostsFilePath = Join-Path $dotSshDirectoryPath known_hosts
177
+ if (-not (Test-Path $knowHostsFilePath - PathType Leaf)) {
178
+ Copy-Item (Join-Path $Script :E2ETestDataDirectory known_hosts) $knowHostsFilePath - Force
179
+ }
180
+ $con = Get-Content $knowHostsFilePath
181
+ if (($con -eq $null ) -or (-not ($con.Contains (" ###OpenSSHE2ETests" )))) {
182
+ Get-Content (Join-Path $Script :E2ETestDataDirectory known_hosts) | Add-Content $knowHostsFilePath
158
183
}
159
- Copy-Item (Join-Path $Script :E2ETestDirectory known_hosts) $knowHostsFilePath - Force
160
184
161
185
$sshConfigFilePath = Join-Path $dotSshDirectoryPath config
162
- if ((Test-Path $sshConfigFilePath - PathType Leaf) -and (-not (Test-Path (Join-Path $dotSshDirectoryPath config.ori) - PathType Leaf))) {
163
- Copy-Item $sshConfigFilePath (Join-Path $dotSshDirectoryPath config.ori) - Force
186
+ if (-not (Test-Path (Join-Path $dotSshDirectoryPath config) - PathType Leaf)) {
187
+ Copy-Item (Join-Path $Script :E2ETestDataDirectory ssh_config) $sshConfigFilePath - Force
188
+ }
189
+ $con = Get-Content $sshConfigFilePath
190
+ if (($con -eq $null ) -or (-not ($con.Contains (" ###OpenSSHE2ETests" )))) {
191
+ Get-Content (Join-Path $Script :E2ETestDataDirectory ssh_config) | Add-Content $sshConfigFilePath
164
192
}
165
- Copy-Item (Join-Path $Script :E2ETestDirectory ssh_config) $sshConfigFilePath - Force
193
+
194
+ Copy-Item (Join-Path $Script :E2ETestDataDirectory ssh_config) $sshConfigFilePath - Force
166
195
Repair-UserSshConfigPermission - FilePath $sshConfigFilePath - confirm:$false
167
196
168
197
# create test accounts
@@ -188,11 +217,11 @@ WARNING: Following changes will be made to OpenSSH configuration
188
217
189
218
New-Item - ItemType Directory - Path (Join-Path $ssouserProfile .ssh) - Force - ErrorAction SilentlyContinue | out-null
190
219
$authorizedKeyPath = Join-Path $ssouserProfile .ssh\authorized_keys
191
- $testPubKeyPath = Join-Path $Script :E2ETestDirectory sshtest_userssokey_ed25519.pub
220
+ $testPubKeyPath = Join-Path $Script :E2ETestDataDirectory sshtest_userssokey_ed25519.pub
192
221
Copy-Item $testPubKeyPath $authorizedKeyPath - Force - ErrorAction SilentlyContinue
193
222
Repair-AuthorizedKeyPermission - FilePath $authorizedKeyPath - confirm:$false
194
223
195
- copy-item (Join-Path $Script :E2ETestDirectory sshtest_userssokey_ed25519) $Global :OpenSSHTestInfo [" TestDataPath" ]
224
+ copy-item (Join-Path $Script :E2ETestDataDirectory sshtest_userssokey_ed25519) $Global :OpenSSHTestInfo [" TestDataPath" ]
196
225
$testPriKeypath = Join-Path $Global :OpenSSHTestInfo [" TestDataPath" ] sshtest_userssokey_ed25519
197
226
cmd / c " ssh-add -D 2>&1 >> $Script :TestSetupLogFile "
198
227
Repair-UserKeyPermission - FilePath $testPriKeypath - confirm:$false
@@ -214,8 +243,6 @@ WARNING: Following changes will be made to OpenSSH configuration
214
243
New-ItemProperty " HKLM:Software\Microsoft\Windows NT\CurrentVersion\AeDebug" - Name Auto - Type String - Value " 1" - Force - ErrorAction SilentlyContinue | Out-Null
215
244
}
216
245
}
217
-
218
- Backup-OpenSSHTestInfo
219
246
}
220
247
221
248
function Set-BasicTestInfo
@@ -504,42 +531,22 @@ function Clear-OpenSSHTestEnvironment
504
531
Remove-ItemProperty " HKLM:Software\Microsoft\Windows NT\CurrentVersion\AeDebug" - Name Debugger - ErrorAction SilentlyContinue - Force | Out-Null
505
532
Remove-ItemProperty " HKLM:Software\Microsoft\Windows NT\CurrentVersion\AeDebug" - Name Auto - ErrorAction SilentlyContinue - Force | Out-Null
506
533
}
507
-
508
- Remove-Item " $OpenSSHConfigPath \sshtest*hostkey*" - Force - ErrorAction SilentlyContinue
509
- Remove-Item " $OpenSSHConfigPath \sshtest*ca_userkeys*" - Force - ErrorAction SilentlyContinue
510
-
511
- # Restore sshd_config
512
- $backupConfigPath = Join-Path $OpenSSHConfigPath sshd_config.ori
513
- if (Test-Path $backupConfigPath - PathType Leaf) {
514
- Copy-Item $backupConfigPath (Join-Path $OpenSSHConfigPath sshd_config) - Force - ErrorAction SilentlyContinue
515
- Remove-Item (Join-Path $OpenSSHConfigPath sshd_config.ori) - Force - ErrorAction SilentlyContinue
516
- Restart-Service sshd
517
- }
518
-
519
- # Restore known_hosts
520
- $originKnowHostsPath = Join-Path $home .ssh\known_hosts.ori
521
- if (Test-Path $originKnowHostsPath )
522
- {
523
- Copy-Item $originKnowHostsPath (Join-Path $home .ssh\known_hosts) - Force - ErrorAction SilentlyContinue
524
- Remove-Item $originKnowHostsPath - Force - ErrorAction SilentlyContinue
525
- }
526
534
527
- # Restore ssh_config
528
- $originConfigPath = Join-Path $home .ssh\config.ori
529
- if (Test-Path $originConfigPath )
535
+ # delete service if exists
536
+ if (Get-Service $SSHDTestSvcName - ErrorAction SilentlyContinue)
530
537
{
531
- Copy-Item $originConfigPath ( Join-Path $home .ssh\config) - Force - ErrorAction SilentlyContinue
532
- Remove-Item $originConfigPath - Force - ErrorAction SilentlyContinue
538
+ Stop-Service $SSHDTestSvcName
539
+ sc.exe delete $SSHDTestSvcName 1> $null
533
540
}
534
-
541
+
535
542
# Delete accounts
536
543
foreach ($user in $OpenSSHTestAccounts )
537
544
{
538
545
net user $user / delete
539
546
}
540
547
541
548
# remove registered keys
542
- cmd / c " ssh-add -d (Join-Path $Script :E2ETestDirectory sshtest_userssokey_ed25519) 2>&1 >> $Script :TestSetupLogFile "
549
+ cmd / c " ssh-add -d (Join-Path $Script :E2ETestDataDirectory sshtest_userssokey_ed25519) 2>&1 >> $Script :TestSetupLogFile "
543
550
544
551
if ($Global :OpenSSHTestInfo -ne $null )
545
552
{
@@ -728,56 +735,6 @@ function Invoke-OpenSSHUnitTest
728
735
$testfailed
729
736
}
730
737
731
- function Backup-OpenSSHTestInfo
732
- {
733
- param
734
- (
735
- [string ] $BackupFile = $null
736
- )
737
-
738
- if ($Global :OpenSSHTestInfo -eq $null ) {
739
- Throw " `$ OpenSSHTestInfo is null. Did you run Set-OpenSSHTestEnvironment yet?"
740
- }
741
-
742
- $testInfo = $Global :OpenSSHTestInfo
743
-
744
- if ([String ]::IsNullOrEmpty($BackupFile )) {
745
- $BackupFile = Join-Path $testInfo [" TestDataPath" ] " OpenSSHTestInfo_backup.txt"
746
- }
747
-
748
- $null | Set-Content $BackupFile
749
-
750
- foreach ($key in $testInfo.Keys ) {
751
- $value = $testInfo [$key ]
752
- Add-Content $BackupFile " $key ,$value "
753
- }
754
- }
755
-
756
- function Restore-OpenSSHTestInfo
757
- {
758
- param
759
- (
760
- [Parameter (Mandatory = $true )]
761
- [ValidateNotNullOrEmpty ()]
762
- [string ] $BackupFile
763
- )
764
-
765
- if ($Global :OpenSSHTestInfo -ne $null )
766
- {
767
- $Global :OpenSSHTestInfo.Clear ()
768
- $Global :OpenSSHTestInfo = $null
769
- }
770
-
771
- $Global :OpenSSHTestInfo = @ {}
772
-
773
- $entries = Get-Content $BackupFile
774
-
775
- foreach ($entry in $entries ) {
776
- $data = $entry.Split (" ," )
777
- $Global :OpenSSHTestInfo [$data [0 ]] = $data [1 ]
778
- }
779
- }
780
-
781
738
<#
782
739
Write-Log
783
740
#>
@@ -799,4 +756,4 @@ function Write-Log
799
756
}
800
757
}
801
758
802
- Export-ModuleMember - Function Set-BasicTestInfo , Set-OpenSSHTestEnvironment , Clear-OpenSSHTestEnvironment , Invoke-OpenSSHSetupTest , Invoke-OpenSSHUnitTest , Invoke-OpenSSHE2ETest , Invoke-OpenSSHUninstallTest , Backup-OpenSSHTestInfo , Restore-OpenSSHTestInfo
759
+ Export-ModuleMember - Function Set-BasicTestInfo , Set-OpenSSHTestEnvironment , Clear-OpenSSHTestEnvironment , Invoke-OpenSSHSetupTest , Invoke-OpenSSHUnitTest , Invoke-OpenSSHE2ETest , Invoke-OpenSSHUninstallTest
0 commit comments