| 1 | import assert from 'node:assert/strict'; |
| 2 | import { createHash } from 'node:crypto'; |
| 3 | import { lstat, mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'; |
| 4 | import { tmpdir } from 'node:os'; |
| 5 | import { join } from 'node:path'; |
| 6 | import test from 'node:test'; |
| 7 | import { prepareReleaseAssets } from './prepare-release-assets.mjs'; |
| 8 | import { checksumFor } from '../www/scripts/sync-release.mjs'; |
| 9 | |
| 10 | const version = (await readFile(new URL('../VERSION', import.meta.url), 'utf8')).trim(); |
| 11 | const dependencies = JSON.parse(await readFile(new URL('../packaging/windows/dependencies.json', import.meta.url), 'utf8')); |
| 12 | const macCore = (await readFile(new URL('./fetch-proxy-core.sh', import.meta.url), 'utf8')).match(/^version=(v[\d.]+)$/m)[1]; |
| 13 | const names = { |
| 14 | mac: `Cage-${version}-macos-arm64.dmg`, |
| 15 | windows: `Cage-${version}-windows-amd64-setup.exe`, |
| 16 | macSource: `mihomo-${macCore}-source.tar.gz`, |
| 17 | macGuestSource: `cage-macos-guest-${version}-source.tar.gz`, |
| 18 | windowsSource: `mihomo-${dependencies.mihomo.version}-source.tar.gz`, |
| 19 | qemu: `qemu-${dependencies.qemu.version}-windows-full-source.zip`, |
| 20 | }; |
| 21 | const digest = data => createHash('sha256').update(data).digest('hex'); |
| 22 | |
| 23 | async function fixture(t) { |
| 24 | const root = await mkdtemp(join(tmpdir(), 'cage-release-test-')); |
| 25 | t.after(() => rm(root, { recursive: true, force: true })); |
| 26 | return { root, mac: join(root, 'mac'), windows: join(root, 'windows'), output: join(root, 'publish') }; |
| 27 | } |
| 28 | |
| 29 | async function platform(directory, assets) { |
| 30 | await mkdir(directory, { recursive: true }); |
| 31 | for (const [name, data] of Object.entries(assets)) await writeFile(join(directory, name), data); |
| 32 | await writeFile(join(directory, 'SHA256SUMS'), Object.entries(assets).map(([name, data]) => `${digest(data)} ${name}\n`).join('')); |
| 33 | } |
| 34 | |
| 35 | const macAssets = () => ({ [names.mac]: 'mac package fixture', [names.macSource]: 'Mihomo source fixture', [names.macGuestSource]: 'macOS guest source fixture' }); |
| 36 | const windowsAssets = () => ({ [names.windows]: 'Windows package fixture', [names.windowsSource]: 'Mihomo source fixture', [names.qemu]: 'complete QEMU source fixture' }); |
| 37 | |
| 38 | test('includes Linux deb only with its verified corresponding source and merges common sources', async t => { |
| 39 | const f = await fixture(t); |
| 40 | const linux = join(f.root, 'linux'); |
| 41 | const dependencies = JSON.parse(await readFile(new URL('../packaging/linux/dependencies.json', import.meta.url), 'utf8')); |
| 42 | const deb = `Cage-${version}-linux-amd64.deb`; |
| 43 | await platform(linux, { [deb]: 'Linux package fixture' }); |
| 44 | await assert.rejects(prepareReleaseAssets({ inputs: [linux], output: f.output }), /Missing Linux Mihomo source/); |
| 45 | await platform(linux, { [deb]: 'Linux package fixture', [dependencies.mihomoSource.file]: 'Mihomo source fixture' }); |
| 46 | await platform(f.mac, macAssets()); |
| 47 | const prepared = await prepareReleaseAssets({ inputs: [f.mac, linux], output: f.output }); |
| 48 | assert.ok(prepared.assets.includes(deb)); |
| 49 | assert.equal(await readFile(join(f.output, deb), 'utf8'), 'Linux package fixture'); |
| 50 | assert.equal(prepared.assets.filter(name => name === dependencies.mihomoSource.file).length, 1); |
| 51 | }); |
| 52 | |
| 53 | test('combines both platforms, deduplicates identical sources and keeps website checksum lookup valid', async t => { |
| 54 | const f = await fixture(t); |
| 55 | const extraSource = `${dependencies.qemuSource.file}.gz`; |
| 56 | const packagedSources = { |
| 57 | [extraSource]: 'QEMU repository source fixture', |
| 58 | [`qemu-keycodemapdb-${dependencies.qemuKeycodemap.version}-source.tar.gz`]: 'keymap source fixture', |
| 59 | [`qemu-${dependencies.qemu.version}-cage-changes-source.tar.gz`]: 'Cage QEMU patch fixture', |
| 60 | }; |
| 61 | await platform(f.mac, macAssets()); |
| 62 | await platform(f.windows, { ...windowsAssets(), ...packagedSources }); |
| 63 | const result = await prepareReleaseAssets({ inputs: [f.mac, f.windows], output: f.output }); |
| 64 | const expected = new Map(Object.entries({ ...macAssets(), ...windowsAssets(), ...packagedSources })); |
| 65 | assert.deepEqual(new Set(await readdir(f.output)), new Set([...expected.keys(), 'SHA256SUMS'])); |
| 66 | assert.equal(result.version, version); |
| 67 | const sums = await readFile(join(f.output, 'SHA256SUMS'), 'utf8'); |
| 68 | for (const [name, data] of expected) { |
| 69 | assert.equal(await readFile(join(f.output, name), 'utf8'), data); |
| 70 | assert.equal(checksumFor(sums, { name }), digest(data)); |
| 71 | } |
| 72 | assert.equal(sums.trimEnd().split('\n').length, expected.size); |
| 73 | }); |
| 74 | |
| 75 | test('supports a single Windows or macOS platform with its corresponding sources', async t => { |
| 76 | const f = await fixture(t); |
| 77 | await platform(f.mac, macAssets()); |
| 78 | await platform(f.windows, windowsAssets()); |
| 79 | assert.equal((await prepareReleaseAssets({ inputs: [f.mac], output: f.output })).assets.length, 3); |
| 80 | assert.equal((await prepareReleaseAssets({ inputs: [f.windows], output: join(f.root, 'windows-only') })).assets.length, 3); |
| 81 | }); |
| 82 | |
| 83 | test('rejects corrupted bytes before creating the output', async t => { |
| 84 | const f = await fixture(t); |
| 85 | await platform(f.mac, macAssets()); |
| 86 | await writeFile(join(f.mac, names.mac), 'changed after packaging'); |
| 87 | await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /SHA-256 mismatch/); |
| 88 | await assert.rejects(lstat(f.output), { code: 'ENOENT' }); |
| 89 | }); |
| 90 | |
| 91 | test('rejects conflicting contents under the same asset name', async t => { |
| 92 | const f = await fixture(t); |
| 93 | const second = join(f.root, 'second-mac'); |
| 94 | await platform(f.mac, macAssets()); |
| 95 | await platform(second, { ...macAssets(), [names.macSource]: 'different source archive' }); |
| 96 | await assert.rejects(prepareReleaseAssets({ inputs: [f.mac, second], output: f.output }), /Conflicting same-name/); |
| 97 | }); |
| 98 | |
| 99 | test('checks duplicate input bytes even when their manifests claim identical digests', async t => { |
| 100 | const f = await fixture(t); |
| 101 | const second = join(f.root, 'second-mac'); |
| 102 | await platform(f.mac, macAssets()); |
| 103 | await platform(second, macAssets()); |
| 104 | await writeFile(join(second, names.macSource), 'corrupted duplicate'); |
| 105 | await assert.rejects(prepareReleaseAssets({ inputs: [f.mac, second], output: f.output }), /SHA-256 mismatch/); |
| 106 | }); |
| 107 | |
| 108 | test('rejects unsigned development installers including unlisted leftovers', async t => { |
| 109 | const f = await fixture(t); |
| 110 | const unsigned = names.windows.replace(/\.exe$/, '-unsigned.exe'); |
| 111 | const assets = windowsAssets(); |
| 112 | assets[unsigned] = assets[names.windows]; |
| 113 | delete assets[names.windows]; |
| 114 | await platform(f.windows, assets); |
| 115 | await assert.rejects(prepareReleaseAssets({ inputs: [f.windows], output: f.output }), /Unsigned Windows development/); |
| 116 | await platform(f.windows, windowsAssets()); |
| 117 | await assert.rejects(prepareReleaseAssets({ inputs: [f.windows], output: f.output }), /Unsigned Windows development/); |
| 118 | }); |
| 119 | |
| 120 | test('requires Windows complete QEMU source in the same verified manifest', async t => { |
| 121 | const f = await fixture(t); |
| 122 | const assets = windowsAssets(); |
| 123 | delete assets[names.qemu]; |
| 124 | await platform(f.windows, assets); |
| 125 | await writeFile(join(f.windows, names.qemu), 'unlisted source is not verified'); |
| 126 | await assert.rejects(prepareReleaseAssets({ inputs: [f.windows], output: f.output }), /complete QEMU source/); |
| 127 | }); |
| 128 | |
| 129 | test('requires the shipped macOS guest corresponding source in the verified manifest', async t => { |
| 130 | const f = await fixture(t); |
| 131 | const assets = macAssets(); |
| 132 | delete assets[names.macGuestSource]; |
| 133 | await platform(f.mac, assets); |
| 134 | await writeFile(join(f.mac, names.macGuestSource), 'unlisted source is not verified'); |
| 135 | await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /macOS guest corresponding source/); |
| 136 | }); |
| 137 | |
| 138 | test('rejects wrong versions and manifest paths outside the release directory', async t => { |
| 139 | const f = await fixture(t); |
| 140 | await platform(f.mac, { ...macAssets(), 'Cage-99.0.0-macos-arm64.dmg': 'wrong version' }); |
| 141 | await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /Unsupported asset or version/); |
| 142 | await writeFile(join(f.mac, 'SHA256SUMS'), `${digest('outside')} ../escape.dmg\n`); |
| 143 | await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /Invalid or duplicate/); |
| 144 | }); |
| 145 | |
| 146 | test('never overwrites an existing output, including an empty one', async t => { |
| 147 | const f = await fixture(t); |
| 148 | await platform(f.mac, macAssets()); |
| 149 | await mkdir(f.output); |
| 150 | await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /Output must be a new directory/); |
| 151 | await writeFile(join(f.output, 'published-asset'), 'keep'); |
| 152 | await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /Output must be a new directory/); |
| 153 | assert.equal(await readFile(join(f.output, 'published-asset'), 'utf8'), 'keep'); |
| 154 | }); |