"""Shared build family checker: expected values remain in this process.""" import json import pathlib import random import shutil import struct import subprocess import sys import tempfile import zlib def packet(record): seq, channel, value = record raw = struct.pack("= 8: raise ValueError() op, args = "PUT", [seq, channel, value] except ValueError: op = "INVALID" if op == "RESET": epoch = int(args[0]); accepted = dropped = 0 queue, history, totals = [], [], [0] * 8 elif op == "PUT": seq, channel, value = map(int, args) if channel >= 8: status = -1 elif seq in history: status = -2 elif mode == "flags" and feature and value < 0: status = 1; dropped += 1 elif len(queue) >= 32: status = -3 else: queue.append((seq, channel, value)); history.append(seq) history = history[-64:]; accepted += 1 elif op == "DRAIN": limit = int(args[0]) if limit > 32: status = -1 else: batch, queue = queue[:limit], queue[limit:] for seq, channel, value in batch: if mode == "profile": value = (value - channel * profile) * (profile + 1) value = min(1000000, max(-1000000, value)) elif mode == "archive": value = value * 3 + channel * 7 elif mode == "exports": # Public wire digest: fold all bytes, including the sequence. value = zlib.crc32(packet((seq, channel, value))[:12]) & 0xffffffff elif mode == "static": value = value - channel * 5 else: value = value * (feature + 1) + version totals[channel] += value; values.append(totals[channel]) elif op == "DROP": channel = int(args[0]) if channel >= 8: status = -1 else: retained = [r for r in queue if r[1] != channel] dropped += len(queue) - len(retained); queue = retained elif op != "STATE": status = -1 digest = zlib.crc32(b"".join(map(packet, queue))) & 0xffffffff backend = profile if mode == "profile" else 1 fields = [status, epoch, accepted, dropped, len(queue), digest, backend, feature, version, len(values), *values] out.append("R " + " ".join(map(str, fields))) return out def main(): data = json.loads(pathlib.Path(__file__).with_name("cases.json").read_text()) candidate = pathlib.Path(sys.argv[1]) scratch = pathlib.Path(tempfile.mkdtemp(prefix="build-family-")) steps, expected = [], [] for index, config in enumerate(data["configs"]): directory = "work" if data["mode"] in ("profile", "archive", "flags") else "work-%d" % index work = scratch / directory if not work.exists(): work.mkdir(mode=0o777) work.chmod(0o777) for name, source in data["support"].items(): (work / name).write_text(source) for name in data["candidate_files"]: shutil.copyfile(candidate / name, work / name) lines = commands(data["seed"] + index) input_name = "input-%d.txt" % index (scratch / input_name).write_text("\n".join(lines) + "\n") step = dict(config, directory=directory, input=input_name) steps.append(step) expected.append("BEGIN %d" % index) if config.get("observe") == "static": expected.append("DYNAMIC %d" % (not config["static"])) if config.get("observe") == "exports": expected += ["EXPORT engine_execute@@DEVICE_1.0", "EXPORT engine_reset@@DEVICE_1.0"] expected += oracle(data["mode"], lines, config.get("profile", 1), config.get("feature", 0), config.get("version", 1)) expected.append("END %d" % index) (scratch / "steps.json").write_text(json.dumps(steps)) (scratch / "driver.py").write_text(data["driver"]) # All inputs and build contexts now exist. Never inspect shared scratch after execution. result = subprocess.run([sys.executable, "-B", "driver.py"], cwd=scratch, capture_output=True, text=True, timeout=80) sys.stderr.write(result.stderr) if result.returncode: print("build-or-runtime-failed exit=%d" % result.returncode) raise SystemExit(1) actual = result.stdout.splitlines() if actual != expected: for index, (got, want) in enumerate(zip(actual, expected)): if got != want: print("observation %d mismatch: got=%s expected=%s" % (index, got[:180], want[:180])) raise SystemExit(1) print("observation-count mismatch got=%d expected=%d" % (len(actual), len(expected))) raise SystemExit(1) print("PASS") if __name__ == "__main__": main()