|
| 1 | +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { verifyWindowsInstaller } from '../scripts/verify-win-installer' |
| 6 | + |
| 7 | +const VERSION = '9.9.9' |
| 8 | + |
| 9 | +function portableExecutable(magic = 'MZ', signature = 'PE\0\0', offset = 0x80): Buffer { |
| 10 | + const image = Buffer.alloc(offset + signature.length) |
| 11 | + image.write(magic, 0, magic.length, 'latin1') |
| 12 | + image.writeUInt32LE(offset, 0x3c) |
| 13 | + image.write(signature, offset, signature.length, 'latin1') |
| 14 | + return image |
| 15 | +} |
| 16 | + |
| 17 | +async function withFixture(callback: (root: string) => Promise<void>): Promise<void> { |
| 18 | + const root = await mkdtemp(join(tmpdir(), 'pythinker-win-installer-')) |
| 19 | + try { |
| 20 | + await callback(root) |
| 21 | + } finally { |
| 22 | + await rm(root, { force: true, recursive: true }) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +async function createFixture(root: string, installer?: Buffer): Promise<void> { |
| 27 | + const dist = join(root, 'dist') |
| 28 | + await mkdir(join(dist, 'win-unpacked'), { recursive: true }) |
| 29 | + await writeFile(join(root, 'package.json'), JSON.stringify({ version: VERSION })) |
| 30 | + if (installer !== undefined) { |
| 31 | + await writeFile(join(dist, `Pythinker-${VERSION}-x64-Setup.exe`), installer) |
| 32 | + } |
| 33 | + await writeFile(join(dist, 'win-unpacked', 'Pythinker.exe'), portableExecutable()) |
| 34 | +} |
| 35 | + |
| 36 | +describe('Windows installer verification', () => { |
| 37 | + it('accepts the installer and unpacked shell when both are PE binaries', async () => { |
| 38 | + await withFixture(async (root) => { |
| 39 | + await createFixture(root, portableExecutable()) |
| 40 | + expect(() => verifyWindowsInstaller(root)).not.toThrow() |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('rejects a missing installer', async () => { |
| 45 | + await withFixture(async (root) => { |
| 46 | + await createFixture(root) |
| 47 | + expect(() => verifyWindowsInstaller(root)).toThrow('ENOENT') |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it('rejects an artifact without a DOS header', async () => { |
| 52 | + await withFixture(async (root) => { |
| 53 | + await createFixture(root, portableExecutable('ZZ')) |
| 54 | + expect(() => verifyWindowsInstaller(root)).toThrow(/no DOS header/) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + it('rejects an artifact with a truncated PE offset', async () => { |
| 59 | + await withFixture(async (root) => { |
| 60 | + const truncated = Buffer.alloc(0x44) |
| 61 | + truncated.write('MZ', 0, 2, 'latin1') |
| 62 | + truncated.writeUInt32LE(0x80, 0x3c) |
| 63 | + await createFixture(root, truncated) |
| 64 | + expect(() => verifyWindowsInstaller(root)).toThrow(/out-of-range PE offset/) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + it('rejects an artifact without a PE signature', async () => { |
| 69 | + await withFixture(async (root) => { |
| 70 | + await createFixture(root, portableExecutable('MZ', 'NE\0\0')) |
| 71 | + expect(() => verifyWindowsInstaller(root)).toThrow(/no PE signature/) |
| 72 | + }) |
| 73 | + }) |
| 74 | +}) |
0 commit comments