| 1 | import assert from 'node:assert/strict'; |
| 2 | import {execFileSync} from 'node:child_process'; |
| 3 | import {createHash} from 'node:crypto'; |
| 4 | import {chmod,mkdir,mkdtemp,readFile,readdir,rm,writeFile} from 'node:fs/promises'; |
| 5 | import {tmpdir} from 'node:os'; |
| 6 | import {join} from 'node:path'; |
| 7 | import {fileURLToPath} from 'node:url'; |
| 8 | import test from 'node:test'; |
| 9 | const collector=fileURLToPath(new URL('./bundle-qemu-sources.sh',import.meta.url)); |
| 10 | test('source collector includes pinned firmware and authenticated package sources with a complete manifest',async t=>{ |
| 11 | const root=await mkdtemp(join(tmpdir(),'cage-qemu-source-test-'));t.after(()=>rm(root,{recursive:true,force:true})); |
| 12 | const repository=join(root,'repository'),runtime=join(root,'runtime'),bin=join(root,'bin'),artifact=join(root,'bundle.tar.gz'),keyring=join(root,'installed-keyring'); |
| 13 | for(const path of [bin,runtime,keyring,join(repository,'packaging/windows'),join(repository,'scripts'),join(repository,'patches/qemu')])await mkdir(path,{recursive:true}); |
| 14 | for(const name of ['pubring.gpg','trustdb.gpg','private-keys-must-not-copy'])await writeFile(join(keyring,name),name); |
| 15 | const sha='a'.repeat(40),content='fixture source archive\n'; |
| 16 | await writeFile(join(repository,'packaging/windows/dependencies.json'),JSON.stringify({qemuSource:{repository:'https://source.invalid/locked-qemu.git',commit:sha},qemuKeycodemap:{url:'https://source.invalid/keymap.tar.gz',hash:createHash('sha256').update(content).digest('hex')}})); |
| 17 | for(const name of ['build-source-packages.txt','build-packages.txt','cage-build.json'])await writeFile(join(runtime,name),name==='build-source-packages.txt'?'mingw-w64-fixture 1.2.3-1\n':'fixture'); |
| 18 | for(const name of ['build-qemu-windows.sh','build-qemu-windows.ps1','bundle-qemu-sources.sh'])await writeFile(join(repository,'scripts',name),'fixture build input'); |
| 19 | await writeFile(join(repository,'patches/qemu/fixture.patch'),'fixture patch'); |
| 20 | const command=async(name,body)=>{const path=join(bin,name);await writeFile(path,'#!/bin/bash\nset -e\n'+body+'\n');await chmod(path,0o755)}; |
| 21 | await command('cygpath','printf "%s\\n" "$2"'); |
| 22 | await command('node','echo "The collector must use the explicit CAGE_NODE path" >&2; exit 99'); |
| 23 | await command('pacman-conf','[[ "$1" == gpgdir ]]; printf "%s\\n" "$SOURCE_TEST_KEYRING"'); |
| 24 | await command('git',`if [[ "$*" == *' init '* ]];then mkdir -p "\${@: -1}/.git";fi |
| 25 | if [[ "$*" == *' fetch '* ]];then [[ "$*" == *'fetch --depth=1 https://source.invalid/locked-qemu.git ${sha}' ]] || exit 98;fi |
| 26 | if [[ "$*" == *rev-parse* ]];then printf '${sha}\\n';fi |
| 27 | if [[ "$*" == *'submodule update'* ]];then for ((i=1;i<=$#;i++));do if [[ "\${!i}" == -C ]];then j=$((i+1));q="\${!j}";fi;done;mkdir -p "$q/roms/seabios";echo 'firmware code' > "$q/roms/seabios/code.c";echo 'gitdir: private' > "$q/roms/seabios/.git";fi |
| 28 | if [[ "$*" == *'submodule status'* ]];then printf ' ${sha} roms/seabios\\n';fi`); |
| 29 | await command('curl',`while [[ $# -gt 0 ]];do if [[ "$1" == -o ]];then shift;destination=$1;fi;shift;done;printf '${content.replaceAll('\n','\\n')}' > "$destination"`); |
| 30 | await command('pacman-key','[[ "$1" == --gpgdir && "$3" == --verify && "$2" != "$SOURCE_TEST_KEYRING" ]]; [[ $(cat "$2/pubring.gpg") == pubring.gpg && $(cat "$2/trustdb.gpg") == trustdb.gpg ]]; [[ ! -e "$2/private-keys-must-not-copy" ]]; grep -qx lock-never "$2/gpg.conf"; printf "%s\\n" "$*" >> "$SOURCE_TEST_VERIFICATIONS"'); |
| 31 | execFileSync('bash',[collector,repository,runtime,artifact],{env:{...process.env,PATH:bin+':'+process.env.PATH,CAGE_NODE:process.execPath,SOURCE_TEST_KEYRING:keyring,SOURCE_TEST_VERIFICATIONS:join(root,'verified')},stdio:'pipe'}); |
| 32 | assert.deepEqual((await readdir(keyring)).sort(),['private-keys-must-not-copy','pubring.gpg','trustdb.gpg']); |
| 33 | const listing=execFileSync('tar',['-tzf',artifact],{encoding:'utf8'}); |
| 34 | for(const path of ['source/firmware-revisions.txt','source/qemu/roms/seabios/code.c','source/packages/mingw-w64-fixture-1.2.3-1.src.tar.zst','source/cage/patches/fixture.patch','source/SHA256SUMS'])assert.ok(listing.includes(path),path); |
| 35 | assert.ok(!listing.includes('/.git')); |
| 36 | assert.ok((await readFile(join(root,'verified'),'utf8')).includes('--verify')); |
| 37 | const extract=join(root,'extracted');await mkdir(extract);execFileSync('tar',['-xzf',artifact,'-C',extract]); |
| 38 | execFileSync('shasum',['-a','256','-c','SHA256SUMS'],{cwd:join(extract,'source'),stdio:'pipe'}); |
| 39 | }); |