Files
ECC/tests/lib/eval-harness/gate.test.js
T

135 lines
6.2 KiB
JavaScript

'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
const gate = require('../../../scripts/lib/eval-harness/gate');
const { test, tempDir, cleanup, finish } = require('./helpers');
const example = path.resolve(__dirname,'../../../examples/eval-harness');
const baseline = path.join(example,'variants/baseline');
const candidate = path.join(example,'variants/candidate');
const taskset = path.join(example,'taskset.json');
test('directory digests are stable and distinguish baseline from candidate',()=>{
assert.equal(gate.digestDir(candidate),gate.digestDir(candidate));
assert.notEqual(gate.digestDir(candidate),gate.digestDir(baseline));
assert.match(gate.digestDir(candidate),/^[0-9a-f]{64}$/);
});
test('directory digests frame file contents, so NUL bytes cannot merge or split files',()=>{
const root=tempDir('gate-digest-framing');
try {
const merged=path.join(root,'merged');const split=path.join(root,'split');
fs.mkdirSync(merged);fs.mkdirSync(split);
fs.writeFileSync(path.join(merged,'a'),'x\0b\0y');
fs.writeFileSync(path.join(split,'a'),'x');fs.writeFileSync(path.join(split,'b'),'y');
assert.notEqual(gate.digestDir(merged),gate.digestDir(split));
const empty=path.join(root,'empty');const nul=path.join(root,'nul');
fs.mkdirSync(empty);fs.mkdirSync(nul);
fs.writeFileSync(path.join(empty,'a'),'');fs.writeFileSync(path.join(nul,'a'),'\0');
assert.notEqual(gate.digestDir(empty),gate.digestDir(nul));
} finally {cleanup(root);}
});
test('directory digests order entries by UTF-16 code unit, independent of the host locale',()=>{
const root=tempDir('gate-digest-locale');
try {
for (const name of ['a.js','B.js','z.js','ä.js','_x.js','-x.js']) fs.writeFileSync(path.join(root,name),name);
fs.mkdirSync(path.join(root,'a'));fs.writeFileSync(path.join(root,'a','b.js'),'nested');
const digestUnder=locale=>{
const child=spawnSync(process.execPath,['-e',`process.stdout.write(require(${JSON.stringify(require.resolve('../../../scripts/lib/eval-harness/gate'))}).digestDir(${JSON.stringify(root)}))`],
{encoding:'utf8',env:{...process.env,LC_ALL:locale,LANG:locale}});
assert.equal(child.status,0,child.stderr);
return child.stdout;
};
const reference=gate.digestDir(root);
for (const locale of ['en_US.UTF-8','sv_SE.UTF-8','tr_TR.UTF-8','C']) assert.equal(digestUnder(locale),reference,locale);
} finally {cleanup(root);}
});
test('variant and taskset inspection remains available without execution',()=>{
const v=gate.loadVariant(candidate);const t=gate.loadTaskset(taskset);
assert.equal(v.entry,'run.js');assert.equal(t.tasks.length,12);assert.match(t.digest,/^[0-9a-f]{64}$/);
assert.equal(gate.scanTripwires(v).length,0);
});
test('known reward-hack fixture is inspectable but cannot run',()=>{
const v=gate.loadVariant(path.join(example,'variants/reward-hack'));
const rules=new Set(gate.scanTripwires(v).map(hit=>hit.rule));
assert.ok(rules.has('hidden_network'));assert.ok(rules.has('checker_probe'));
assert.throws(()=>gate.runVariant(v,[],'.',{trusted_local:true}),e=>e.code==='gate.isolation_required');
});
test('effect-class expansion remains visible in static tripwire inspection',()=>{
const v={...gate.loadVariant(candidate),effect_class:'SE3'};
assert.ok(gate.scanTripwires(v,{max_effect_class:'SE1'}).some(hit=>hit.rule==='effect_class_expansion'));
});
test('honest example also refuses without OS containment and writes no false receipt',()=>{
const work=tempDir('gate-disabled');
try {
assert.throws(()=>gate.runGate({taskset,baseline,candidate,work_dir:work,trusted_local:true}),e=>e.code==='gate.isolation_required');
assert.deepEqual(fs.readdirSync(work),[]);
} finally {cleanup(work);}
});
test('malformed tasksets and missing variant manifests reject during inspection',()=>{
const root=tempDir('gate-invalid');
try {
const file=path.join(root,'bad.json');fs.writeFileSync(file,JSON.stringify({version:'1',family:'f',tasks:[{id:'t',input:0}]}));
assert.throws(()=>gate.loadTaskset(file),e=>e.code==='gate.taskset_invalid');
assert.throws(()=>gate.loadVariant(root),e=>e.code==='gate.variant_missing');
} finally {cleanup(root);}
});
test('manifest replacement after validation never changes the object read', () => {
const root = tempDir('manifest-race');
const manifest = path.join(root, 'variant.json');
const saved = path.join(root, 'saved.json');
const original = JSON.stringify({ name: 'candidate', effect_class: 'SE0' });
const replacement = JSON.stringify({ name: 'replacement_marker', effect_class: 'SE0' });
fs.writeFileSync(manifest, original);
fs.writeFileSync(path.join(root, 'run.js'), 'module.exports={solve:()=>1};');
const read = fs.readFileSync;
let swapped = false;
let observed;
fs.readFileSync = function(file, ...args) {
if (!swapped && (file === manifest || typeof file === 'number')) {
swapped = true;
fs.renameSync(manifest, saved);
fs.writeFileSync(manifest, replacement);
observed = read.call(this, file, ...args);
return observed;
}
return read.call(this, file, ...args);
};
try {
gate.loadVariant(root);
assert.ok(swapped, 'replacement boundary was exercised');
assert.strictEqual(String(observed), original, 'read must stay bound to the validated descriptor');
} finally {
fs.readFileSync = read;
cleanup(root);
}
});
test('manifest descriptors close when parsing fails', () => {
const root = tempDir('manifest-close');
fs.writeFileSync(path.join(root, 'variant.json'), '{invalid');
const open = fs.openSync;
const close = fs.closeSync;
const active = new Set();
fs.openSync = function(...args) {
const fd = open.apply(this, args);
active.add(fd);
return fd;
};
fs.closeSync = function(fd) {
const result = close.call(this, fd);
active.delete(fd);
return result;
};
try {
assert.throws(() => gate.loadVariant(root), SyntaxError);
assert.strictEqual(active.size, 0, 'failed inspection must not leak descriptors');
} finally {
fs.openSync = open;
fs.closeSync = close;
for (const fd of active) close(fd);
cleanup(root);
}
});
finish('gate');