// Host disk transition check for a future two-process K2160 run. This alone // cannot prove that either guest issued I/O or that QEMU shut down cleanly. import assert from 'node:assert/strict'; import {createHash} from 'node:crypto'; const SECTOR=512, DISK_BYTES=1024*1024, LOGICAL=1537; const writable=new Set([18,120,128,129,130,131]); const hash=bytes=>createHash('sha256').update(bytes).digest(); const byteAt=(seed,index)=>{ const n=BigInt(index),shift=BigInt(index%64),mask=(1n<<64n)-1n; const rotated=((seed<>(64n-shift)))&mask; return Number(((n*37n)^(n>>5n)^rotated)&255n); }; const identity=(value,label)=>{ assert.equal(typeof value,'bigint',label); assert.ok(value>0n&&value<=(1n<<64n)-1n,label); }; function exactDisk(name,bytes){assert.ok(Buffer.isBuffer(bytes),name);assert.equal(bytes.length,DISK_BYTES,name);} function assertOnlySectorsChanged(before,after,allowed){ for(let lba=0;lba=0&&caseId<=4,'caseId'); assert.notEqual(writerBoot,readerBoot,'two independent boot identities'); assert.equal(claimedPreviousBoot,caseId===3?readerBoot:writerBoot,'claimed prior boot'); assert.equal(initial.readUInt16LE(510),0xaa55,'MBR'); assert.equal(initial.readUInt32LE(16*SECTOR+32),3,'three root entries'); assert.equal(initial.readUInt32LE(18*SECTOR+64),3,'third inode'); assert.equal(initial.readBigUInt64LE(18*SECTOR+72),0n,'third inode initially empty'); assert.equal(initial.readBigUInt64LE(18*SECTOR+80),0n,'third inode initially unallocated'); assert.equal(initial.subarray(17*SECTOR+72,17*SECTOR+85).toString(),'persisted.txt','directory name'); const data=expectedData(seed),inodes=expectedInodes(initial),journal=Buffer.alloc(SECTOR); journal.write('K2160FS1',0,'ascii');journal.writeBigUInt64LE(seed,8);journal.writeBigUInt64LE(writerBoot,16); hash(data.subarray(0,LOGICAL)).copy(journal,24); assertOnlySectorsChanged(initial,afterWriter,writable); assert.deepEqual(afterWriter.subarray(18*SECTOR,19*SECTOR),inodes,'persisted inode table'); assert.deepEqual(afterWriter.subarray(120*SECTOR,121*SECTOR),journal,'diagnostic marker'); assert.deepEqual(afterWriter.subarray(128*SECTOR,132*SECTOR),data,'persisted data and tail'); const readerExpected=Buffer.from(afterWriter); if(caseId===1)readerExpected[128*SECTOR+LOGICAL-1]^=1; if(caseId===2)readerExpected[16*SECTOR]^=1; if(caseId===4)initial.copy(readerExpected,128*SECTOR,128*SECTOR,132*SECTOR); assert.deepEqual(readerBefore,readerExpected,'reader starts with exactly the selected disk fault'); assert.deepEqual(afterReader,readerBefore,'read-only guest changed its disk'); return {scope:'host-disk-transition-only',guestAccepted:false,productAccepted:false, caseId,writerChangedSectors:[...writable].sort((a,b)=>a-b),readerReadOnly:true, logicalBytes:LOGICAL,logicalSha256:hash(data.subarray(0,LOGICAL)).toString('hex')}; }