@@ -70,35 +70,26 @@ try {
7070 [System.IO.File ]::WriteAllBytes($tamperedPath , $bytes )
7171 Assert-Rejected ' 签名后文件被篡改' $paths
7272
73- # 测试证书模式:用临时自签名证书模拟 SignPath 测试证书。
74- # 签名前后须为同一文件,并带有产品名和版本,才能通过元数据比对。
75- $unsignedFixture = Join-Path $taskDir ' unsigned.dll'
76- $selfSignedFixture = Join-Path $taskDir ' self-signed.dll'
77- $fixtureSource = @'
78- [assembly: System.Reflection.AssemblyProduct("WrongNotebook")]
79- [assembly: System.Reflection.AssemblyInformationalVersion("1.0.0")]
80- public static class WrongNotebookSigningFixture {}
81- '@
82- Add-Type - TypeDefinition $fixtureSource - OutputAssembly $unsignedFixture - OutputType Library
83- Copy-Item - LiteralPath $unsignedFixture - Destination $selfSignedFixture
73+ # 用临时自签名证书重签 Runner 自带程序的副本:签名前后的产品元数据一致,只有签名者不同。
74+ $selfSignedFixture = Join-Path $taskDir ' self-signed.exe'
75+ Copy-Item - LiteralPath $fixture - Destination $selfSignedFixture
8476 $testCert = New-SelfSignedCertificate - Type CodeSigningCert - Subject ' CN=WrongNotebook Signing Test' - CertStoreLocation Cert:\CurrentUser\My
8577 try {
8678 $result = Set-AuthenticodeSignature - LiteralPath $selfSignedFixture - Certificate $testCert
87- if ($null -eq $result.SignerCertificate ) { throw ' 无法生成自签名测试文件' }
79+ if ($null -eq $result.SignerCertificate -or (Get-AuthenticodeSignature - LiteralPath $selfSignedFixture ).SignerCertificate.Thumbprint -ne $testCert.Thumbprint ) {
80+ throw ' 无法生成自签名测试文件'
81+ }
8882 } finally {
8983 Remove-Item - LiteralPath " Cert:\CurrentUser\My\$ ( $testCert.Thumbprint ) " - Force
9084 }
9185
9286 $paths = New-FixtureSet ' test-certificate'
93- foreach ($name in $names ) {
94- Copy-Item - LiteralPath $unsignedFixture - Destination (Join-Path $paths.UnsignedDirectory $name ) - Force
95- Copy-Item - LiteralPath $selfSignedFixture - Destination (Join-Path $paths.SignedDirectory $name ) - Force
96- }
87+ foreach ($name in $names ) { Copy-Item - LiteralPath $selfSignedFixture - Destination (Join-Path $paths.SignedDirectory $name ) - Force }
9788 $production = @ { UnsignedDirectory = $paths.UnsignedDirectory ; SignedDirectory = $paths.SignedDirectory }
9889 Assert-Rejected ' 生产模式拒绝测试证书' $production
9990 & $verify @paths - TestCertificate
10091 foreach ($name in $names ) {
101- if ((Get-FileHash - LiteralPath (Join-Path $paths.UnsignedDirectory $name )).Hash -ne (Get-FileHash - LiteralPath $unsignedFixture ).Hash) {
92+ if ((Get-FileHash - LiteralPath (Join-Path $paths.UnsignedDirectory $name )).Hash -ne (Get-FileHash - LiteralPath $fixture ).Hash) {
10293 throw ' 测试证书模式不得覆盖发布文件'
10394 }
10495 }
0 commit comments