// Run-owned two-boot inputs. No QEMU launch, disk repair or acceptance here. import assert from 'node:assert/strict'; import {createHash} from 'node:crypto'; import {populateFileMapFixture} from './file-map-disk.mjs'; export const LOGICAL_BYTES=1537; export const CASES=Object.freeze([ {id:0,name:'matched',error:null}, {id:1,name:'last-logical-byte-corrupted',error:'HashMismatch'}, {id:2,name:'superblock-corrupted',error:'Format'}, {id:3,name:'same-boot',error:'SameBoot'}, {id:4,name:'old-data',error:'HashMismatch'}, ]); const mask=(1n<<64n)-1n; export const sha256=bytes=>createHash('sha256').update(bytes).digest('hex'); export function checkedIdentity(value){assert.equal(typeof value,'bigint');assert.ok(value>0n&&value<=mask);return value;} export function sourceBytes(seed){ checkedIdentity(seed);const b=Buffer.alloc(2048,0xe7); for(let i=0;i>(64n-k)))&mask,index=BigInt(i); b[i]=Number(((index*37n)^(index>>5n)^rotated)&255n); } return b; } export function inodeBytes(){ const b=Buffer.alloc(512); for(const [index,size,lba]of [[0,513,30],[1,512,32],[2,LOGICAL_BYTES,128]]){ b.writeUInt32LE(index+1,index*32);b.writeBigUInt64LE(BigInt(size),index*32+8);b.writeBigUInt64LE(BigInt(lba),index*32+16); } return b; } export function journalBytes(seed,writerBoot){ checkedIdentity(seed);checkedIdentity(writerBoot);const b=Buffer.alloc(512); b.write('K2160FS1');b.writeBigUInt64LE(seed,8);b.writeBigUInt64LE(writerBoot,16); Buffer.from(sha256(sourceBytes(seed).subarray(0,LOGICAL_BYTES)),'hex').copy(b,24);return b; } export function beforeDisk(journalFixture){ assert.equal(journalFixture.length,128);const b=Buffer.alloc(1024*1024); b[510]=0x55;b[511]=0xaa;b[446]=0x80;b[450]=0x83;b.writeUInt32LE(1,454);b.writeUInt32LE(100,458); const second=446+32;b[second+4]=0x7f;b.writeUInt32LE(101,second+8);b.writeUInt32LE(1947,second+12); for(const[lba,value]of [[100,0x5a],[101,0xa6],[104,0x6b],[108,0x7c]])b.fill(value,lba*512,(lba+1)*512); journalFixture.copy(b,112*512);populateFileMapFixture(b); // Keep the inherited TAIL and NEIGHBOR data/metadata. The new file exists // as an empty third inode until the first guest's actual metadata write. b.writeUInt32LE(3,16*512+32);const at=17*512+64;b[at]=1;b[at+1]=13;b.writeUInt32LE(3,at+4);b.write('persisted.txt',at+8); b.writeUInt32LE(3,18*512+64);b.fill(0xc1,120*512,121*512);b.fill(0xcc,128*512,132*512);return b; } export function expectedAfter(before,seed,writerBoot){ assert.equal(before.length,1024*1024);assert.equal(before.readBigUInt64LE(18*512+72),0n,'fixture was already written'); assert.equal(before.readBigUInt64LE(18*512+80),0n);const b=Buffer.from(before); sourceBytes(seed).copy(b,128*512);inodeBytes().copy(b,18*512);journalBytes(seed,writerBoot).copy(b,120*512);return b; } export function readerDisk(before,after,caseId){ assert.equal(before.length,1024*1024);assert.equal(after.length,before.length);assert.ok(CASES.some(c=>c.id===caseId)); const b=Buffer.from(after); if(caseId===1)b[128*512+LOGICAL_BYTES-1]^=1; if(caseId===2)b[16*512]^=1; if(caseId===4)before.copy(b,128*512,128*512,132*512); // Case 3 changes the claimed previous boot, never bytes in the disk. return b; }