"""Meter the independent check and a few semantic corruptions separately. Copies live in a temporary directory. The checked artifact and checker remain byte-identical through these controls. The main checker fails on its first error, keeping the negative controls inside the checking reserve. """ import copy import hashlib import json from pathlib import Path import resource import subprocess import sys import tempfile import time HERE = Path(__file__).resolve().parent artifact = HERE / 'union1088-v2.json' checker = HERE / 'check1088-v2.py' reference = HERE / 'job1068' / 'kfold1068.json' base = resource.getrusage(resource.RUSAGE_CHILDREN) before_hashes = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in (artifact, checker)} def cpu(): r = resource.getrusage(resource.RUSAGE_CHILDREN) return r.ru_utime + r.ru_stime - base.ru_utime - base.ru_stime def run(path, out, err): remaining = max(1, int(150 - cpu())) def limits(): resource.setrlimit(resource.RLIMIT_CPU, (remaining, remaining + 1)) before = cpu() start = time.monotonic() with out.open('wb') as stdout, err.open('wb') as stderr: p = subprocess.run([sys.executable, str(checker), str(path), str(reference)], stdout=stdout, stderr=stderr, preexec_fn=limits, timeout=190) return {'exit_code': p.returncode, 'cpu_seconds': cpu() - before, 'wall_seconds': time.monotonic() - start} result = {'artifact_sha256': before_hashes[artifact.name], 'checker_sha256': before_hashes[checker.name]} result['main'] = run(artifact, HERE / 'check1088-v2.out', HERE / 'check1088-v2.err') print(json.dumps({'main': result['main']}), flush=True) if result['main']['exit_code']: (HERE / 'check-receipt1088-v2.json').write_text(json.dumps(result, indent=2) + '\n') raise SystemExit(result['main']['exit_code']) original = json.loads(artifact.read_text()) changes = { 'lost_deduplication': lambda d: next(r for r in d['unions'] if r['p']==223 and r['duplicates_removed']).update(union_size=296), 'wrong_revision': lambda d: d.update(revision=6), 'wrong_member_F1': lambda d: d['members'][0].update(F1_member=1), 'false_prior_identity': lambda d: d['members'][0].update(proved_against_443=True), 'wrong_union_ratio': lambda d: d['unions'][0]['certificate']['ratio'].__setitem__(0, 0)} result['controls'] = {} with tempfile.TemporaryDirectory(prefix='controls1088-v2-', dir=HERE) as temporary: temporary = Path(temporary) for name, change in changes.items(): d = copy.deepcopy(original) change(d) path = temporary / (name + '.json') path.write_text(json.dumps(d, sort_keys=True, indent=2) + '\n') observation = run(path, temporary / (name + '.out'), temporary / (name + '.err')) observation['failure_stdout'] = (temporary / (name + '.out')).read_text() observation['failure_stderr'] = (temporary / (name + '.err')).read_text() result['controls'][name] = observation print(json.dumps({'control': name, **observation}), flush=True) assert observation['exit_code'] == 1 assert before_hashes == {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in (artifact, checker)} result['total_cpu_seconds'] = cpu() result['child_peak_rss_bytes'] = resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss * (1 if sys.platform == 'darwin' else 1024) result['original_hashes_unchanged'] = True (HERE / 'check-receipt1088-v2.json').write_text(json.dumps(result, sort_keys=True, indent=2) + '\n') print(json.dumps({'checking_cpu_seconds': cpu(), 'controls_passed': len(changes)}), flush=True)