import assert from 'node:assert/strict'; import {copyFileSync,cpSync,existsSync,mkdirSync,mkdtempSync,readFileSync,readdirSync,writeFileSync} from 'node:fs'; import {spawnSync} from 'node:child_process'; import {dirname,join,relative,resolve} from 'node:path'; import {hashAt,frozenSnapshot} from './boot-contract-report.mjs'; import {rngLimitsRuntimeSource,rngLimitsSourceRuntimeSource} from './rng-limits-source.mjs'; assert.equal(process.argv.length,3); const root=process.cwd(),predecessorPath=relative(root,resolve(process.argv[2])),predecessor=JSON.parse(readFileSync(predecessorPath));assert.equal(predecessor.status,'K1905_RNG_SOURCE_DEVELOPMENT_MATRIX_VALIDATED');assert.equal(predecessor.guestAccepted,true); const frozenBefore=frozenSnapshot(root),directory=mkdtempSync(join(root,'reports/os-implementation/K1910-rng-limits-local-')),source=join(directory,'source'),work='reports/os-implementation/K1910-rng-limits-work-sources'; const artifact=path=>({path:relative(root,path),sha256:hashAt(root,relative(root,path))}); const copy=(path,target=join(source,path))=>{mkdirSync(dirname(target),{recursive:true});copyFileSync(path,target);return artifact(target);}; const sources={};for(const[key,path]of Object.entries({model:'os-rng-limits/src/limits.rs',sourceModel:'os-rng-limits/src/source.rs',baseSource:'os-rng-source/src/source.rs',baseAdapter:'scripts/os-acceptance/rng-source-source.mjs',noStdRoot:'os-rng-limits/src/lib.rs',manifest:'os-rng-limits/Cargo.toml',lock:'os-rng-limits/Cargo.lock',fixture:'kernel/tests/os/rng_limits.rs',contract:'kernel/RNG_LIMITS_CONTRACT.md',modelAdapter:'scripts/os-acceptance/rng-limits-source.mjs',probeTemplate:'scripts/os-acceptance/k1910-rng-limits-probe.rs',capture:'scripts/os-acceptance/rng-limits-capture.mjs',proof:'scripts/os-acceptance/rng-limits-proof.mjs',proofTest:'scripts/os-acceptance/test-rng-limits-proof.mjs',physicalFixture:'kernel/tests/os/fixtures/rng-limits-physical-v1.bin',fixtureBuilder:'scripts/os-acceptance/rng-limits-physical-fixture.mjs',fixtureData:'scripts/os-acceptance/rng-limits-fixture-data.mjs',physicalProof:'scripts/os-acceptance/physical-checkpoint-proof.mjs',diskFixture:'scripts/os-acceptance/file-map-disk.mjs',runtimeModel:work+'/kernel/src/runtime/rng_limits.rs',runtimeSource:work+'/kernel/src/runtime/rng_limits_source.rs',runtimeMetadata:work+'/kernel/src/runtime/frame_metadata.rs',metadata:'os-frame-metadata/src/metadata.rs'}))sources[key]=copy(path); sources.harness=artifact(join(root,'scripts/os-acceptance/rng-limits-local.mjs')); const librarySources=[]; for(const component of readdirSync(work).filter(name=>!['bootloader','userspace'].includes(name)&&existsSync(join(work,name,'Cargo.toml')))){ cpSync(join(work,component,'src'),join(source,component,'src'),{recursive:true});copyFileSync(join(work,component,'Cargo.toml'),join(source,component,'Cargo.toml'));librarySources.push({source:component+'/Cargo.toml',...artifact(join(source,component,'Cargo.toml'))}); function walk(path){for(const entry of readdirSync(path,{withFileTypes:true})){const child=join(path,entry.name);if(entry.isDirectory())walk(child);else librarySources.push({source:relative(source,child),...artifact(child)});}}walk(join(source,component,'src')); } assert.equal(sources.runtimeModel.sha256,artifact(join(source,'kernel/src/runtime/rng_limits.rs')).sha256);assert.equal(sources.runtimeSource.sha256,artifact(join(source,'kernel/src/runtime/rng_limits_source.rs')).sha256);assert.equal(rngLimitsSourceRuntimeSource(readFileSync(sources.sourceModel.path,'utf8'),readFileSync(sources.baseSource.path,'utf8')),readFileSync(sources.runtimeSource.path,'utf8'));const parentModel=JSON.parse(readFileSync(predecessor.local.path)).sources.model;assert.equal(parentModel.sha256,sources.baseSource.sha256); assert.equal(rngLimitsRuntimeSource(readFileSync(sources.model.path,'utf8')),readFileSync(sources.runtimeModel.path,'utf8')); assert.equal(sources.runtimeMetadata.sha256,artifact(join(source,'kernel/src/runtime/frame_metadata.rs')).sha256);assert.ok(readFileSync(sources.runtimeMetadata.path,'utf8').startsWith(readFileSync(sources.metadata.path,'utf8'))); assert.equal(hashAt(root,predecessor.candidate.path),predecessor.candidate.sha256);const priorCandidate=JSON.parse(readFileSync(predecessor.candidate.path));assert.equal(sources.runtimeMetadata.sha256,priorCandidate.sources.metadata.sha256); let env={...process.env,CARGO_TARGET_DIR:join(directory,'target')};delete env.RUSTFLAGS;delete env.CARGO_ENCODED_RUSTFLAGS; const run=(name,args,expected,executable='cargo')=>{const r=spawnSync(executable,args,{cwd:root,env,encoding:'utf8',timeout:600000,maxBuffer:64*1024*1024});const log=join(directory,name+'.log');writeFileSync(log,(r.stdout??'')+(r.stderr??''));assert.ifError(r.error);assert.equal(r.signal,null);assert.equal(r.status,expected,artifact(log).path);return{exitCode:r.status,log:artifact(log)};}; const manifest=join(source,'os-rng-limits/Cargo.toml'); const tests={green:run('host-tests',['test','--manifest-path',manifest,'--locked','--offline','--test','rng_limits'],0),target:run('aarch64-kernel-check',['check','--manifest-path',join(source,'kernel/Cargo.toml'),'--offline','--bin','turkos-kernel','--target','aarch64-unknown-none','--no-default-features','--features','os-runtime-product'],0),proof:run('proof-tests',[join(source,'scripts/os-acceptance/test-rng-limits-proof.mjs')],0,process.execPath)}; const proof=JSON.parse(readFileSync(tests.proof.log.path));assert.equal(proof.status,'RNG_LIMITS_PROOF_TESTS_PASSED');assert.equal(proof.positive,1);assert.ok(proof.forgedControlsRejected>=60);assert.match(readFileSync(tests.green.log.path,'utf8'),/2 passed; 0 failed/); const mutants=[]; for(const [key,name,guard,replacement]of [['red','deadline','if nowself.state.byte_quota||requests>self.state.request_quota','if false'],['redReset','reset','if status!=0||ready!=0','if false']]{ const mutantRoot=join(directory,'mutant-'+name);cpSync(source,mutantRoot,{recursive:true});const path=join(mutantRoot,'os-rng-limits/src/limits.rs'),original=readFileSync(path,'utf8');assert.equal(original.split(guard).length,2);writeFileSync(path,original.replace(guard,replacement));mutants.push(artifact(path));env={...env,CARGO_TARGET_DIR:join(directory,'mutant-'+name+'-target')};tests[key]=run(name+'-mutant-tests',['test','--manifest-path',join(mutantRoot,'os-rng-limits/Cargo.toml'),'--locked','--offline','--test','rng_limits'],101);assert.match(readFileSync(tests[key].log.path,'utf8'),/os_rng_limits_negative \.\.\. FAILED/); } const frozenAfter=frozenSnapshot(root);assert.deepEqual(frozenAfter,frozenBefore); const scope='A private principal-bound diagnostic RNG service owns its accepted source and request token, with a 32-byte and one-request quota. Two real Virtio episodes prove a normal 32-byte completion followed by quota refusal, and deliberately withheld backend input followed by an architectural-clock timeout. Fourteen physical checkpoints bind the actual owned service, source and pending request, output refusal, device reset ordering, retained DMA pins, history erasure and fourteen zeroed pages returned to frame authority. Host controls include absent-source EAGAIN, malformed lengths, stale and foreign tokens, clocks, failed-reset quarantine and short-completion charging. No cryptographic provider, public random ABI or production entropy is claimed.'; const report={schema:'turkos-k1910-rng-limits-local/v1',status:'K1906_K1909_LOCAL_VALIDATED',guestAccepted:false,productAccepted:false,predecessor:artifact(join(root,predecessorPath)),sources,librarySources,mutants,tests:{...tests,greenCount:2,mutationRejected:true,mutationsRejected:3},frozenBefore,frozenAfter,scope};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),tests:2,mutationsRejected:3}));