|
| 1 | +# spell-checker:ignore SIGNCERTIFICATE CERTIFICATEPASSWORD codesign pfxfile |
| 2 | +Describe 'Code Signing Functions' { |
| 3 | + |
| 4 | + BeforeAll { |
| 5 | + $script:moduleName = 'PowerShellBuild' |
| 6 | + $script:moduleRoot = Split-Path -Path $PSScriptRoot -Parent |
| 7 | + Import-Module ([IO.Path]::Combine($script:moduleRoot, 'Output', $script:moduleName)) -Force |
| 8 | + |
| 9 | + # Create a temporary directory for test files |
| 10 | + $script:testPath = Join-Path -Path $TestDrive -ChildPath 'SigningTest' |
| 11 | + New-Item -Path $script:testPath -ItemType Directory -Force | Out-Null |
| 12 | + } |
| 13 | + |
| 14 | + Context 'Get-PSBuildCertificate' { |
| 15 | + |
| 16 | + BeforeEach { |
| 17 | + # Clear environment variables before each test |
| 18 | + Remove-Item env:\SIGNCERTIFICATE -ErrorAction SilentlyContinue |
| 19 | + Remove-Item env:\CERTIFICATEPASSWORD -ErrorAction SilentlyContinue |
| 20 | + } |
| 21 | + |
| 22 | + Context 'Auto mode' { |
| 23 | + It 'Defaults to Auto mode when no CertificateSource is specified' -Skip:(-not $IsWindows) { |
| 24 | + Mock Get-ChildItem {} |
| 25 | + $VerboseOutput = Get-PSBuildCertificate -Verbose -ErrorAction SilentlyContinue 4>&1 |
| 26 | + $VerboseOutput | Should -Match "CertificateSource is 'Auto'" |
| 27 | + } |
| 28 | + |
| 29 | + It 'Resolves to EnvVar mode when SIGNCERTIFICATE environment variable is set' { |
| 30 | + $env:SIGNCERTIFICATE = 'base64data' |
| 31 | + try { |
| 32 | + $VerboseOutput = Get-PSBuildCertificate -Verbose -WarningAction SilentlyContinue -ErrorAction SilentlyContinue 4>&1 |
| 33 | + $VerboseOutput | Should -Match "Resolved to 'EnvVar'" |
| 34 | + } catch { |
| 35 | + # Expected to fail with invalid base64, just checking the mode selection |
| 36 | + $_.Exception.Message | Should -Not -BeNullOrEmpty |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + It 'Resolves to Store mode when SIGNCERTIFICATE environment variable is not set' -Skip:(-not $IsWindows) { |
| 41 | + Remove-Item env:\SIGNCERTIFICATE -ErrorAction SilentlyContinue |
| 42 | + Mock Get-ChildItem {} |
| 43 | + $VerboseOutput = Get-PSBuildCertificate -Verbose 4>&1 |
| 44 | + $VerboseOutput | Should -Match "Resolved to 'Store'" |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + # Store mode only works on Windows |
| 49 | + Context 'Store mode' { |
| 50 | + It 'Searches the certificate store for a valid code-signing certificate' -Skip:(-not $IsWindows) { |
| 51 | + # On Windows, we can test the actual logic without mocking the cert store itself |
| 52 | + # Instead, just verify the function accepts the parameter and attempts the search |
| 53 | + $command = Get-Command Get-PSBuildCertificate |
| 54 | + $command.Parameters['CertificateSource'].Attributes.ValidValues | Should -Contain 'Store' |
| 55 | + |
| 56 | + # If no cert found, should return $null (not throw) |
| 57 | + { Get-PSBuildCertificate -CertificateSource Store -ErrorAction SilentlyContinue } | Should -Not -Throw |
| 58 | + } |
| 59 | + |
| 60 | + It 'Returns $null when no valid certificate is found' -Skip:(-not $IsWindows) { |
| 61 | + Mock Get-ChildItem { } |
| 62 | + $cert = Get-PSBuildCertificate -CertificateSource Store |
| 63 | + $cert | Should -BeNullOrEmpty |
| 64 | + } |
| 65 | + |
| 66 | + It 'Filters out expired certificates' -Skip:(-not $IsWindows) { |
| 67 | + Mock Get-ChildItem { |
| 68 | + # Return nothing (expired cert is filtered by Where-Object) |
| 69 | + } |
| 70 | + |
| 71 | + $cert = Get-PSBuildCertificate -CertificateSource Store |
| 72 | + $cert | Should -BeNullOrEmpty |
| 73 | + } |
| 74 | + |
| 75 | + It 'Filters out certificates without a private key' -Skip:(-not $IsWindows) { |
| 76 | + Mock Get-ChildItem { |
| 77 | + # Return nothing (cert without private key is filtered by Where-Object) |
| 78 | + } |
| 79 | + |
| 80 | + $cert = Get-PSBuildCertificate -CertificateSource Store |
| 81 | + $cert | Should -BeNullOrEmpty |
| 82 | + } |
| 83 | + |
| 84 | + It 'Uses custom CertStoreLocation when specified' -Skip:(-not $IsWindows) { |
| 85 | + # Just verify the parameter is accepted |
| 86 | + { Get-PSBuildCertificate -CertificateSource Store -CertStoreLocation 'Cert:\LocalMachine\My' -ErrorAction SilentlyContinue } | |
| 87 | + Should -Not -Throw |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + Context 'Thumbprint mode' { |
| 92 | + It 'Searches for a certificate with the specified thumbprint' -Skip:(-not $IsWindows) { |
| 93 | + $testThumbprint = 'ABCD1234EFGH5678' |
| 94 | + # Verify the function accepts the thumbprint parameter |
| 95 | + { Get-PSBuildCertificate -CertificateSource Thumbprint -Thumbprint $testThumbprint -ErrorAction SilentlyContinue } | |
| 96 | + Should -Not -Throw |
| 97 | + } |
| 98 | + |
| 99 | + It 'Returns $null when the specified thumbprint is not found' -Skip:(-not $IsWindows) { |
| 100 | + Mock Get-ChildItem { } |
| 101 | + $cert = Get-PSBuildCertificate -CertificateSource Thumbprint -Thumbprint 'NOTFOUND123' |
| 102 | + $cert | Should -BeNullOrEmpty |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Context 'EnvVar mode' { |
| 107 | + It 'Attempts to decode a Base64-encoded PFX from environment variable' { |
| 108 | + # Create a minimal mock certificate data (will fail to parse, but that's expected) |
| 109 | + $env:SIGNCERTIFICATE = [System.Convert]::ToBase64String([byte[]]@(1, 2, 3, 4, 5)) |
| 110 | + |
| 111 | + # This should fail because the data is not a valid PFX, but that proves it's trying to load it |
| 112 | + { Get-PSBuildCertificate -CertificateSource EnvVar -ErrorAction Stop } | Should -Throw |
| 113 | + } |
| 114 | + |
| 115 | + It 'Uses custom environment variable names when specified' { |
| 116 | + $env:MY_CUSTOM_CERT = [System.Convert]::ToBase64String([byte[]]@(1, 2, 3, 4, 5)) |
| 117 | + $env:MY_CUSTOM_PASS = 'password' |
| 118 | + |
| 119 | + try { |
| 120 | + Get-PSBuildCertificate -CertificateSource EnvVar ` |
| 121 | + -CertificateEnvVar 'MY_CUSTOM_CERT' ` |
| 122 | + -CertificatePasswordEnvVar 'MY_CUSTOM_PASS' ` |
| 123 | + -ErrorAction SilentlyContinue |
| 124 | + } catch { |
| 125 | + # Expected to fail with invalid certificate data |
| 126 | + } |
| 127 | + |
| 128 | + # Cleanup |
| 129 | + Remove-Item env:\MY_CUSTOM_CERT -ErrorAction SilentlyContinue |
| 130 | + Remove-Item env:\MY_CUSTOM_PASS -ErrorAction SilentlyContinue |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + Context 'PfxFile mode' { |
| 135 | + It 'Accepts a PfxFilePath parameter' { |
| 136 | + $testPfxPath = Join-Path -Path $TestDrive -ChildPath 'test.pfx' |
| 137 | + New-Item -Path $testPfxPath -ItemType File -Force | Out-Null |
| 138 | + |
| 139 | + try { |
| 140 | + Get-PSBuildCertificate -CertificateSource PfxFile ` |
| 141 | + -PfxFilePath $testPfxPath ` |
| 142 | + -ErrorAction SilentlyContinue |
| 143 | + } catch { |
| 144 | + # Expected to fail with invalid PFX file |
| 145 | + } |
| 146 | + |
| 147 | + # Just verify the parameter is accepted |
| 148 | + { Get-PSBuildCertificate -CertificateSource PfxFile -PfxFilePath $testPfxPath -ErrorAction Stop } | |
| 149 | + Should -Throw |
| 150 | + } |
| 151 | + |
| 152 | + It 'Accepts a PfxFilePassword parameter' { |
| 153 | + $testPfxPath = Join-Path -Path $TestDrive -ChildPath 'test.pfx' |
| 154 | + New-Item -Path $testPfxPath -ItemType File -Force | Out-Null |
| 155 | + $securePassword = ConvertTo-SecureString -String 'password' -AsPlainText -Force |
| 156 | + |
| 157 | + try { |
| 158 | + Get-PSBuildCertificate -CertificateSource PfxFile ` |
| 159 | + -PfxFilePath $testPfxPath ` |
| 160 | + -PfxFilePassword $securePassword ` |
| 161 | + -ErrorAction SilentlyContinue |
| 162 | + } catch { |
| 163 | + # Expected to fail with invalid PFX file |
| 164 | + } |
| 165 | + |
| 166 | + # Just verify the parameters are accepted |
| 167 | + $testPfxPath | Should -Exist |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + Context 'Parameter validation' { |
| 172 | + It 'ValidateSet accepts valid CertificateSource values' { |
| 173 | + $command = Get-Command Get-PSBuildCertificate |
| 174 | + $parameter = $command.Parameters['CertificateSource'] |
| 175 | + $validValues = $parameter.Attributes.ValidValues |
| 176 | + $validValues | Should -Contain 'Auto' |
| 177 | + $validValues | Should -Contain 'Store' |
| 178 | + $validValues | Should -Contain 'Thumbprint' |
| 179 | + $validValues | Should -Contain 'EnvVar' |
| 180 | + $validValues | Should -Contain 'PfxFile' |
| 181 | + } |
| 182 | + |
| 183 | + It 'Has correct default value for CertStoreLocation' { |
| 184 | + $command = Get-Command Get-PSBuildCertificate |
| 185 | + $parameter = $command.Parameters['CertStoreLocation'] |
| 186 | + $parameter.Attributes.Where({ $_.TypeId.Name -eq 'ParameterAttribute' })[0].Mandatory | |
| 187 | + Should -BeFalse |
| 188 | + } |
| 189 | + |
| 190 | + It 'Has correct default value for CertificateEnvVar' { |
| 191 | + $command = Get-Command Get-PSBuildCertificate |
| 192 | + $parameter = $command.Parameters['CertificateEnvVar'] |
| 193 | + $parameter.Attributes.Where({ $_.TypeId.Name -eq 'ParameterAttribute' })[0].Mandatory | |
| 194 | + Should -BeFalse |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments