omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / tkasig / test / check.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Test-only binary protocol referee. Abnormal or incomplete runs never kill a mutant.""" |
| 3 | import json |
| 4 | import pathlib |
| 5 | import struct |
| 6 | import subprocess |
| 7 | import sys |
| 8 | |
| 9 | unit = pathlib.Path(__file__).resolve().parent.parent |
| 10 | repo = unit.parent.parent |
| 11 | rows = [json.loads(x) for x in (unit / 'test/vectors.jsonl').read_text().splitlines()] |
| 12 | wire = bytearray() |
| 13 | expected = bytearray() |
| 14 | for row in rows: |
| 15 | data = bytes.fromhex(row['Input']) |
| 16 | keys = b''.join(bytes.fromhex(k) for k in row['Keys']) |
| 17 | wire += struct.pack('<IH', len(data), len(keys)//32) + bytes.fromhex(row['Node']) + keys + data |
| 18 | if not row['Decode']: |
| 19 | expected += b'\0' |
| 20 | else: |
| 21 | canon = bytes.fromhex(row['Canon']) |
| 22 | expected += b'\1' + struct.pack('<I', len(canon)) + canon + bytes.fromhex(row['Hash']) + bytes([row['Auth']]) |
| 23 | |
| 24 | def complete(out): |
| 25 | pos = 0 |
| 26 | for row in rows: |
| 27 | if pos >= len(out): |
| 28 | return False |
| 29 | status = out[pos] |
| 30 | pos += 1 |
| 31 | if status == 1: |
| 32 | if pos+4 > len(out): |
| 33 | return False |
| 34 | n, = struct.unpack_from('<I', out, pos) |
| 35 | if n > 65536: |
| 36 | return False |
| 37 | pos += 4+n+33 |
| 38 | elif status != 0: |
| 39 | return False |
| 40 | if pos > len(out): |
| 41 | return False |
| 42 | return pos == len(out) |
| 43 | |
| 44 | def run(binary): |
| 45 | p = subprocess.run([str(binary)], input=wire, capture_output=True, timeout=300) |
| 46 | if p.returncode != 0 or not complete(p.stdout): |
| 47 | raise RuntimeError(f'ABNORMAL/incomplete {binary.name}: rc={p.returncode}, bytes={len(p.stdout)}, stderr={p.stderr!r}') |
| 48 | return p.stdout |
| 49 | |
| 50 | def explain(out): |
| 51 | pos = 0 |
| 52 | for row in rows: |
| 53 | start = pos |
| 54 | status = out[pos]; pos += 1 |
| 55 | target = b'\0' |
| 56 | if row['Decode']: |
| 57 | c = bytes.fromhex(row['Canon']) |
| 58 | target = b'\1'+struct.pack('<I',len(c))+c+bytes.fromhex(row['Hash'])+bytes([row['Auth']]) |
| 59 | if status: |
| 60 | n, = struct.unpack_from('<I',out,pos); pos += 4+n+33 |
| 61 | if out[start:pos] != target: |
| 62 | raise AssertionError(f"{row['Name']}: got {out[start:pos].hex()} expected {target.hex()}") |
| 63 | |
| 64 | build = pathlib.Path(sys.argv[2]) |
| 65 | omc = sys.argv[1] |
| 66 | parts = [repo/'om'/u/(u+'.om') for u in ('blake2s','sha512','ed25519','cbor')] |
| 67 | prefix = '\n'.join(p.read_text() for p in parts)+'\n' |
| 68 | source = (unit/'tkasig.om').read_text() |
| 69 | driver = (unit/'test/driver.om').read_text() |
| 70 | def compile_one(name, body): |
| 71 | p = subprocess.run([omc], input=(prefix+body+'\n'+driver).encode(), capture_output=True) |
| 72 | if p.returncode: |
| 73 | raise RuntimeError(f'compile {name}: {p.stderr.decode()}') |
| 74 | binary = build/name |
| 75 | binary.write_bytes(p.stdout); binary.chmod(0o700) |
| 76 | return binary |
| 77 | |
| 78 | binary = compile_one('driver',source) |
| 79 | out = run(binary) |
| 80 | explain(out) |
| 81 | assert out == expected |
| 82 | assert not complete(out[:-1]), 'incomplete final result accepted' |
| 83 | print(f'PASS tkasig differential {len(rows)} cases') |
| 84 | mutants = { |
| 85 | 'node-binding': ('if ne nksbyte d t get m 1 add j 2 get node j','if eq ulong 0 1'), |
| 86 | 'rotation-consensus': ('nkscheck d t m r pk 0 0','nkscheck d t m r pk 1 0'), |
| 87 | 'direct-strict': ('ret nkscheck d t m r pk 1','ret nkscheck d t m r pk 0'), |
| 88 | 'hash-includes-signature': ('nksser d t m o r 1','nksser d t m o r 0'), |
| 89 | 'unknown-kind': ('if gt cbrval t v 255','if gt cbrval t v 254'), |
| 90 | 'duplicate-alias': ('if ne and seen bit 0','if eq ulong 0 1'), |
| 91 | 'credential-details': ('if gt r 0','if eq ulong 0 1'), |
| 92 | 'simple-kind-rejected': ('if eq nksuint t v 0','if ne cbrmaj t v 0'), |
| 93 | 'simple-bytes-rejected': ('if eq nksuint t j 0','if ne cbrmaj t j 0'), |
| 94 | } |
| 95 | for name,(before,after) in mutants.items(): |
| 96 | assert source.count(before)==1,(name,source.count(before)) |
| 97 | binary=compile_one(name,source.replace(before,after)) |
| 98 | out=run(binary) |
| 99 | assert out != expected, f'undetected mutant {name}' |
| 100 | print(f'PASS mutant {name}: completed result mismatch') |
| 101 | # Prove abnormal execution is a harness error, not a detected source mutant. |
| 102 | binary=compile_one('trap-probe',source.replace('ret edverify pk msg 32 sg mode','if eq mode 1\ntrap 3\nend\nret edverify pk msg 32 sg mode')) |
| 103 | try: |
| 104 | run(binary) |
| 105 | except RuntimeError as e: |
| 106 | print('PASS harness rejects trapping mutant') |
| 107 | else: |
| 108 | raise AssertionError('trap probe was not rejected') |