import assert from 'node:assert/strict'; import {copyFileSync,mkdirSync,mkdtempSync,readdirSync,readFileSync,writeFileSync,cpSync} from 'node:fs'; import {dirname,join,relative} from 'node:path'; import {spawnSync} from 'node:child_process'; import {frozenSnapshot,hashAt} from './boot-contract-report.mjs'; const root=process.cwd(),directory=mkdtempSync(join(root,'reports/os-implementation/K1860-icmp-echo-staging-local-')); const before=frozenSnapshot(root),source=join(directory,'source'),sources=[]; const artifact=p=>({path:relative(root,p),sha256:hashAt(root,relative(root,p))}); const copy=path=>{const target=join(source,path);mkdirSync(dirname(target),{recursive:true});copyFileSync(join(root,path),target);sources.push(artifact(target));}; function tree(path){for(const entry of readdirSync(join(root,path),{withFileTypes:true})){const child=path+'/'+entry.name;if(entry.isDirectory())tree(child);else copy(child);}} for(const component of ['os-icmp-echo','os-ipv4-fragments','os-ipv4-packet','os-ethernet-frame']){copy(component+'/Cargo.toml');tree(component+'/src');} for(const path of ['os-icmp-echo/Cargo.lock','kernel/tests/os/icmp_echo.rs','kernel/ICMP_ECHO_CONTRACT.md','scripts/os-acceptance/icmp-echo-prepare.mjs','scripts/os-acceptance/icmp-echo-source.mjs'])copy(path); const env={...process.env,CARGO_TARGET_DIR:join(directory,'target')};delete env.RUSTFLAGS;delete env.CARGO_ENCODED_RUSTFLAGS; const report={schema:'turkos-k1860-icmp-echo-staging/v1',status:'RUNNING',guestAccepted:false,productAccepted:false,sources,tests:{},frozenBefore:before,scope:'Bounded ICMP echo replies, verified checksums, token bucket and explicit drop accounting; fragments, options, non-echo ICMP refused. Host preparation only.'}; const run=(name,args,expected)=>{const result=spawnSync('cargo',args,{env,cwd:root,encoding:'utf8',timeout:600000,maxBuffer:64*1024*1024});const log=join(directory,name+'.log');writeFileSync(log,(result.stdout??'')+(result.stderr??''));report.tests[name]={exitCode:result.status,log:artifact(log)};assert.equal(result.error,undefined);assert.equal(result.signal,null);assert.equal(result.status,expected,name);return readFileSync(log,'utf8');}; try{ const manifest=join(source,'os-icmp-echo/Cargo.toml'); const green=run('host',['test','--manifest-path',manifest,'--offline','--locked','--test','icmp_echo'],0);assert.match(green,/2 passed; 0 failed/); run('aarch64',['check','--manifest-path',manifest,'--offline','--locked','--lib','--target','aarch64-unknown-none'],0); for(const [name,guard,replacement]of [['checksum','if checksum(body)!=0','if false'],['rate','if next.tokens==0','if false']]){ const mutant=join(directory,'mutant-'+name);cpSync(source,mutant,{recursive:true}); const model=join(mutant,'os-icmp-echo/src/echo.rs'),text=readFileSync(model,'utf8');assert.equal(text.split(guard).length,2);writeFileSync(model,text.replace(guard,replacement)); report['mutant_'+name]=artifact(model);env.CARGO_TARGET_DIR=join(directory,'mutant-target-'+name); const red=run(name+'-mutant',['test','--manifest-path',join(mutant,'os-icmp-echo/Cargo.toml'),'--offline','--locked','--test','icmp_echo'],101);assert.match(red,/os_icmp_echo_negative \.\.\. FAILED/); } report.status='STAGING_LOCAL_PASSED'; }catch(error){report.status='STAGING_LOCAL_FAILED';report.error=error.stack;process.exitCode=1;} report.frozenAfter=frozenSnapshot(root);assert.deepEqual(report.frozenAfter,before); const path=join(directory,'report.json');writeFileSync(path,JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({status:report.status,report:relative(root,path)}));