File size: 16,395 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe -Name "Windows MSI" -Fixture {
    BeforeAll {
        Set-StrictMode -Off
        function Test-Elevated {
            [CmdletBinding()]
            [OutputType([bool])]
            Param()

            # if the current Powershell session was called with administrator privileges,
            # the Administrator Group's well-known SID will show up in the Groups for the current identity.
            # Note that the SID won't show up unless the process is elevated.
            return (([Security.Principal.WindowsIdentity]::GetCurrent()).Groups -contains "S-1-5-32-544")
        }

        function Test-IsMuEnabled {
            $sm = (New-Object -ComObject Microsoft.Update.ServiceManager)
            $mu = $sm.Services | Where-Object { $_.ServiceId -eq '7971f918-a847-4430-9279-4a52d1efe18d' }
            if ($mu) {
                return $true
            }
            return $false
        }

        function Invoke-TestAndUploadLogOnFailure {
            param (
                [scriptblock] $Test
            )

            try {
                & $Test
            }
            catch {
                Send-VstsLogFile -Path $msiLog
                throw
            }
        }

        function Get-UseMU {
            $useMu = $null
            $key = 'HKLM:\SOFTWARE\Microsoft\PowerShellCore\'
            if ($runtime -like '*x86*') {
                $key = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\PowerShellCore\'
            }

            try {
                $useMu = Get-ItemPropertyValue -Path $key -Name UseMU -ErrorAction SilentlyContinue
            } catch {}

            if (!$useMu) {
                $useMu = 0
            }

            return $useMu
        }

        function Set-UseMU {
            param(
                [int]
                $Value
            )
            $key = 'HKLM:\SOFTWARE\Microsoft\PowerShellCore\'
            if ($runtime -like '*x86*') {
                $key = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\PowerShellCore\'
            }

            Set-ItemProperty -Path $key -Name UseMU -Value $Value -Type DWord

            return $useMu
        }

        function Invoke-Msiexec {
            param(
                [Parameter(ParameterSetName = 'Install', Mandatory)]
                [Switch]$Install,

                [Parameter(ParameterSetName = 'Uninstall', Mandatory)]
                [Switch]$Uninstall,

                [Parameter(Mandatory)]
                [ValidateScript({Test-Path -Path $_})]
                [String]$MsiPath,

                [Parameter(ParameterSetName = 'Install')]
                [HashTable] $Properties

            )
            $action = "$($PSCmdlet.ParameterSetName)ing"
            if ($Install.IsPresent) {
                $switch = '/I'
            } else {
                $switch = '/x'
            }

            $additionalOptions = @()
            if ($Properties) {
                foreach ($key in $Properties.Keys) {
                    $additionalOptions += "$key=$($Properties.$key)"
                }
            }

            $argumentList = "$switch $MsiPath /quiet /l*vx $msiLog $additionalOptions"
            Write-Verbose -Message "running msiexec $argumentList"
            $msiExecProcess = Start-Process msiexec.exe -Wait -ArgumentList $argumentList -NoNewWindow -PassThru
            if ($msiExecProcess.ExitCode -ne 0) {
                $exitCode = $msiExecProcess.ExitCode
                throw "$action MSI failed and returned error code $exitCode."
            }
        }

        $msiX64Path = $env:PsMsiX64Path
        $channel = $env:PSMsiChannel
        $runtime = $env:PSMsiRuntime
        $muEnabled = Test-IsMuEnabled

        if ($runtime -like '*x86*') {
            $propertiesRegKeyParent = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\PowerShellCore"
        } else {
            $propertiesRegKeyParent = "HKLM:\SOFTWARE\Microsoft\PowerShellCore"
        }

        if ($channel -eq "preview") {
            $propertiesRegKeyName = "PreviewInstallerProperties"
        } else {
            $propertiesRegKeyName = "InstallerProperties"
        }

        # Rename the registry key that contains the saved installer
        # properties so that the tests don't overwrite them.
        $propertiesRegKeyPath = Join-Path -Path $propertiesRegKeyParent -ChildPath $propertiesRegKeyName
        $propertiesBackupRegKeyName = "BackupInstallerProperties"
        $propertiesBackupRegKeyPath = Join-Path -Path $propertiesRegKeyParent -ChildPath $propertiesBackupRegKeyName
        if (Test-Path -Path $propertiesRegKeyPath) {
            Rename-Item -Path $propertiesRegKeyPath -NewName $propertiesBackupRegKeyName
        }

        # Get any existing powershell in the path
        $beforePath = @(([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
                Where-Object {$_ -like '*files\powershell*'})

        $msiLog = Join-Path -Path $TestDrive -ChildPath 'msilog.txt'

        foreach ($pathPart in $beforePath) {
            Write-Warning "Found existing PowerShell path: $pathPart"
        }

        if (!(Test-Elevated)) {
            Write-Warning "Tests must be elevated"
        }
        $uploadedLog = $false
    }

    AfterAll {
        Set-StrictMode -Version 3.0

        # Restore the original saved installer properties registry key.
        Remove-Item -Path $propertiesRegKeyPath -ErrorAction SilentlyContinue
        if (Test-Path -Path $propertiesBackupRegKeyPath) {
            Rename-Item -Path $propertiesBackupRegKeyPath -NewName $propertiesRegKeyName
        }
    }

    BeforeEach {
        $error.Clear()
        Remove-Item -Path $propertiesRegKeyPath -ErrorAction SilentlyContinue
    }

    Context "Upgrade code" {
        BeforeAll {
            Write-Verbose "cr-$channel-$runtime" -Verbose
            $pwshPath = Join-Path $env:ProgramFiles -ChildPath "PowerShell"
            $pwshx86Path = Join-Path ${env:ProgramFiles(x86)} -ChildPath "PowerShell"
            $regKeyPath = "HKLM:\SOFTWARE\Microsoft\PowerShellCore\InstalledVersions"

            switch ("$channel-$runtime") {
                "preview-win7-x64" {
                    $versionPath = Join-Path -Path $pwshPath -ChildPath '7-preview'
                    $revisionRange = 0, 99
                    $msiUpgradeCode = '39243d76-adaf-42b1-94fb-16ecf83237c8'
                    $regKeyPath = Join-Path $regKeyPath -ChildPath $msiUpgradeCode
                }
                "stable-win7-x64" {
                    $versionPath = Join-Path -Path $pwshPath -ChildPath '7'
                    $revisionRange = 500, 500
                    $msiUpgradeCode = '31ab5147-9a97-4452-8443-d9709f0516e1'
                    $regKeyPath = Join-Path $regKeyPath -ChildPath $msiUpgradeCode
                }
                "preview-win7-x86" {
                    $versionPath = Join-Path -Path $pwshx86Path -ChildPath '7-preview'
                    $revisionRange = 0, 99
                    $msiUpgradeCode = '86abcfbd-1ccc-4a88-b8b2-0facfde29094'
                    $regKeyPath = Join-Path $regKeyPath -ChildPath $msiUpgradeCode
                }
                "stable-win7-x86" {
                    $versionPath = Join-Path -Path $pwshx86Path -ChildPath '7'
                    $revisionRange = 500, 500
                    $msiUpgradeCode = '1d00683b-0f84-4db8-a64f-2f98ad42fe06'
                    $regKeyPath = Join-Path $regKeyPath -ChildPath $msiUpgradeCode
                }
                default {
                    throw "'$_' not a valid channel runtime combination"
                }
            }
        }

        It "$Channel MSI should not be installed before test" -Skip:(!(Test-Elevated)) {
            $result = @(Get-CimInstance -Query "SELECT Value FROM Win32_Property WHERE Property='UpgradeCode' and Value = '{$msiUpgradeCode}'")
            $result.Count | Should -Be 0 -Because "Query should return nothing if $channel $runtime is not installed"
        }

        It "MSI should install without error" -Skip:(!(Test-Elevated)) {
            {
                Invoke-MsiExec -Install -MsiPath $msiX64Path -Properties @{ADD_PATH = 1}
            } | Should -Not -Throw
        }

        It "Upgrade code should be correct" -Skip:(!(Test-Elevated)) {
            $result = @(Get-CimInstance -Query "SELECT Value FROM Win32_Property WHERE Property='UpgradeCode' and Value = '{$msiUpgradeCode}'")
            $result.Count | Should -Be 1 -Because "Query should return 1 result if Upgrade code is for $runtime $channel"
        }

        It "Revision should be in correct range" -Skip:(!(Test-Elevated)) {
            $pwshDllPath = Join-Path -Path $versionPath -ChildPath "pwsh.dll"
            [version] $version = (Get-ChildItem $pwshDllPath).VersionInfo.FileVersion
            Write-Verbose "pwsh.dll version: $version" -Verbose
            $version.Revision | Should -BeGreaterOrEqual $revisionRange[0] -Because "$channel revision should between $($revisionRange[0]) and $($revisionRange[1])"
            $version.Revision | Should -BeLessOrEqual $revisionRange[1] -Because "$channel revision should between $($revisionRange[0]) and $($revisionRange[1])"
        }

        It 'MSI should add ProductCode in registry' -Skip:(!(Test-Elevated)) {

            $productCode = if ($msiUpgradeCode -eq '39243d76-adaf-42b1-94fb-16ecf83237c8' -or
                $msiUpgradeCode -eq '31ab5147-9a97-4452-8443-d9709f0516e1') {
                # x64
                $regKeyPath | Should -Exist
                Get-ItemPropertyValue -Path $regKeyPath -Name 'ProductCode'
            } elseif ($msiUpgradeCode -eq '86abcfbd-1ccc-4a88-b8b2-0facfde29094' -or
                $msiUpgradeCode -eq '1d00683b-0f84-4db8-a64f-2f98ad42fe06') {
                # x86 - need to open the 32bit reghive
                $wow32RegKey = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Registry32)
                $subKey = $wow32RegKey.OpenSubKey("Software\Microsoft\PowerShellCore\InstalledVersions\$msiUpgradeCode")
                $subKey.GetValue("ProductCode")
            }

            $productCode | Should -Not -BeNullOrEmpty
            $productCodeGuid = [Guid]$productCode
            $productCodeGuid | Should -BeOfType "Guid"
            $productCodeGuid.Guid | Should -Not -Be $msiUpgradeCode
        }

        It "MSI should uninstall without error" -Skip:(!(Test-Elevated)) {
            {
                Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
            } | Should -Not -Throw
        }
    }

    Context "Add Path disabled" {
        BeforeAll {
            Set-UseMU -Value 0
        }

        It "UseMU should be 0 before install" -Skip:(!(Test-Elevated)) {
            $useMu = Get-UseMU
            $useMu | Should -Be 0
        }

        It "MSI should install without error" -Skip:(!(Test-Elevated)) {
            {
                Invoke-MsiExec -Install -MsiPath $msiX64Path -Properties @{ADD_PATH = 0; USE_MU = 1; ENABLE_MU = 1}
            } | Should -Not -Throw
        }

        It "MSI should have not be updated path" -Skip:(!(Test-Elevated)) {
            $psPath = ([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
                Where-Object { $_ -like '*files\powershell*' -and $_ -notin $beforePath }

            $psPath | Should -BeNullOrEmpty
        }

        It "UseMU should be 1" -Skip:(!(Test-Elevated)) {
            Invoke-TestAndUploadLogOnFailure -Test {
                $useMu = Get-UseMU
                $useMu | Should -Be 1
            }
        }

        It "MSI should uninstall without error" -Skip:(!(Test-Elevated)) {
            {
                Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
            } | Should -Not -Throw
        }
    }

    Context "USE_MU disabled" {
        BeforeAll {
            Set-UseMU -Value 0
        }

        It "UseMU should be 0 before install" -Skip:(!(Test-Elevated)) {
            $useMu = Get-UseMU
            $useMu | Should -Be 0
        }

        It "MSI should install without error" -Skip:(!(Test-Elevated)) {
            {
                Invoke-MsiExec -Install -MsiPath $msiX64Path -Properties @{USE_MU = 0}
            } | Should -Not -Throw
        }

        It "UseMU should be 0" -Skip:(!(Test-Elevated)) {
            Invoke-TestAndUploadLogOnFailure -Test {
                $useMu = Get-UseMU
                $useMu | Should -Be 0
            }
        }

        It "MSI should uninstall without error" -Skip:(!(Test-Elevated)) {
            {
                Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
            } | Should -Not -Throw
        }
    }

    Context "Add Path enabled" {
        It "MSI should install without error" -Skip:(!(Test-Elevated)) {
            {
                Invoke-MsiExec -Install -MsiPath $msiX64Path -Properties @{ADD_PATH = 1}
            } | Should -Not -Throw
        }

        It "MSI should have updated path" -Skip:(!(Test-Elevated)) {
            if ($channel -eq 'preview') {
                $pattern = '*files*\powershell*\preview*'
            } else {
                $pattern = '*files*\powershell*'
            }

            $psPath = ([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
            Where-Object { $_ -like $pattern -and $_ -notin $beforePath }

            if (!$psPath) {
                ([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
                Where-Object { $_ -notin $beforePath } |
                ForEach-Object { Write-Verbose -Verbose $_ }
            }

            $psPath | Should -Not -BeNullOrEmpty
        }

        It "MSI should uninstall without error" -Skip:(!(Test-Elevated)) {
            {
                Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
            } | Should -Not -Throw
        }

        Context "Disable Telemetry" {
            It "MSI should set POWERSHELL_TELEMETRY_OPTOUT env variable when MSI property DISABLE_TELEMETRY is set to 1" -Skip:(!(Test-Elevated)) {
                try {
                    $originalValue = [System.Environment]::GetEnvironmentVariable('POWERSHELL_TELEMETRY_OPTOUT', [System.EnvironmentVariableTarget]::Machine)
                    [System.Environment]::SetEnvironmentVariable('POWERSHELL_TELEMETRY_OPTOUT', '0', [System.EnvironmentVariableTarget]::Machine)
                    {
                        Invoke-MsiExec -Install -MsiPath $msiX64Path -Properties @{DISABLE_TELEMETRY = 1 }
                    } | Should -Not -Throw
                    [System.Environment]::GetEnvironmentVariable('POWERSHELL_TELEMETRY_OPTOUT', [System.EnvironmentVariableTarget]::Machine) |
                        Should -Be 1
                }
                finally {
                    [System.Environment]::SetEnvironmentVariable('POWERSHELL_TELEMETRY_OPTOUT', $originalValue, [System.EnvironmentVariableTarget]::Machine)
                    {
                        Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
                    } | Should -Not -Throw
                }
            }

            It "MSI should not change POWERSHELL_TELEMETRY_OPTOUT env variable when MSI property DISABLE_TELEMETRY not set" -Skip:(!(Test-Elevated)) {
                try {
                    $originalValue = [System.Environment]::GetEnvironmentVariable('POWERSHELL_TELEMETRY_OPTOUT', [System.EnvironmentVariableTarget]::Machine)
                    [System.Environment]::SetEnvironmentVariable('POWERSHELL_TELEMETRY_OPTOUT', 'untouched', [System.EnvironmentVariableTarget]::Machine)
                    {
                        Invoke-MsiExec -Install -MsiPath $msiX64Path
                    } | Should -Not -Throw
                    [System.Environment]::GetEnvironmentVariable('POWERSHELL_TELEMETRY_OPTOUT', [System.EnvironmentVariableTarget]::Machine) |
                        Should -Be 'untouched'
                }
                finally {
                    [System.Environment]::SetEnvironmentVariable('POWERSHELL_TELEMETRY_OPTOUT', $originalValue, [System.EnvironmentVariableTarget]::Machine)
                    {
                        Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
                    } | Should -Not -Throw
                }
            }
        }
    }
}