omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / tls / test / acvp / acvpcmp.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | """Compare an ACVP response against ACVP-Server's expectedResults.json, field by field. |
| 4 | |
| 5 | usage: acvpcmp.py EXPECTED RESPONSE MAP LABEL [--subset] [--flip] |
| 6 | Every expected test must be either answered in the response or listed as not applicable in the map |
| 7 | (and then absent from the response). Answered tests must carry exactly the expected result fields |
| 8 | (--subset: only the fields the response carries, each of which must exist in the expected test) with |
| 9 | equal values; hex compares case-insensitively. Prints one line: |
| 10 | ACVP LABEL PASS tests=N not_applicable=M (exit 0) |
| 11 | ACVP LABEL FAIL tests=N fail=K not_applicable=M (exit 1), first mismatches on stderr. |
| 12 | --flip is the negative control: one result value of the first answered expected test is corrupted in |
| 13 | memory (first hex digit changed, or a boolean inverted) before comparing; the run must then FAIL. |
| 14 | """ |
| 15 | import json |
| 16 | import sys |
| 17 | |
| 18 | |
| 19 | def body(path): |
| 20 | j = json.load(open(path)) |
| 21 | return j[1] if isinstance(j, list) else j |
| 22 | |
| 23 | |
| 24 | def same(a, b): |
| 25 | if isinstance(a, str) and isinstance(b, str): |
| 26 | return a.upper() == b.upper() |
| 27 | if isinstance(a, list) and isinstance(b, list): |
| 28 | return len(a) == len(b) and all(same(x, y) for x, y in zip(a, b)) |
| 29 | if isinstance(a, dict) and isinstance(b, dict): |
| 30 | return a.keys() == b.keys() and all(same(a[k], b[k]) for k in a) |
| 31 | return type(a) is type(b) and a == b |
| 32 | |
| 33 | |
| 34 | def corrupt(v): |
| 35 | if isinstance(v, bool): |
| 36 | return not v |
| 37 | if isinstance(v, str): |
| 38 | return ("1" if v[:1] == "0" else "0") + v[1:] |
| 39 | if isinstance(v, list): |
| 40 | return [corrupt(v[0])] + v[1:] |
| 41 | if isinstance(v, dict): |
| 42 | k = sorted(v)[0] |
| 43 | return dict(v, **{k: corrupt(v[k])}) |
| 44 | raise SystemExit(f"cannot corrupt {v!r}") |
| 45 | |
| 46 | |
| 47 | def main(): |
| 48 | exp_path, resp_path, map_path, label = sys.argv[1:5] |
| 49 | subset = "--subset" in sys.argv[5:] |
| 50 | exp, resp, m = body(exp_path), body(resp_path), json.load(open(map_path)) |
| 51 | na = {(n[0], n[1]) for n in m["na"]} |
| 52 | got = {(g["tgId"], t["tcId"]): t for g in resp["testGroups"] for t in g["tests"]} |
| 53 | if "--flip" in sys.argv[5:]: |
| 54 | g, t = next((g, t) for g in exp["testGroups"] for t in g["tests"] if (g["tgId"], t["tcId"]) in got) |
| 55 | k = sorted(f for f in t if f != "tcId" and f in got[(g["tgId"], t["tcId"])])[0] |
| 56 | t[k] = corrupt(t[k]) |
| 57 | ok = bad = skipped = 0 |
| 58 | msgs = [] |
| 59 | for k in ("vsId", "algorithm", "revision"): |
| 60 | if exp.get(k) != resp.get(k): |
| 61 | bad += 1 |
| 62 | msgs.append(f"header {k}: expected {exp.get(k)} got {resp.get(k)}") |
| 63 | seen = set() |
| 64 | for g in exp["testGroups"]: |
| 65 | for t in g["tests"]: |
| 66 | key = (g["tgId"], t["tcId"]) |
| 67 | want = {k: v for k, v in t.items() if k != "tcId"} |
| 68 | have = got.get(key) |
| 69 | if key in na and not subset: |
| 70 | skipped += 1 |
| 71 | if have is not None: |
| 72 | bad += 1 |
| 73 | msgs.append(f"tg {key[0]} tc {key[1]}: answered but listed not applicable") |
| 74 | continue |
| 75 | if have is None: |
| 76 | if subset: |
| 77 | continue |
| 78 | bad += 1 |
| 79 | msgs.append(f"tg {key[0]} tc {key[1]}: no response") |
| 80 | continue |
| 81 | seen.add(key) |
| 82 | have = {k: v for k, v in have.items() if k != "tcId"} |
| 83 | if subset: |
| 84 | want = {k: want[k] for k in have if k in want} |
| 85 | if same(want, have): |
| 86 | ok += 1 |
| 87 | else: |
| 88 | bad += 1 |
| 89 | diff = [k for k in sorted(set(want) | set(have)) if not same(want.get(k), have.get(k))] |
| 90 | msgs.append(f"tg {key[0]} tc {key[1]}: fields {diff} differ" |
| 91 | + (f" ({have['error']})" if "error" in have else "")) |
| 92 | extra = set(got) - seen |
| 93 | if extra: |
| 94 | bad += len(extra) |
| 95 | msgs.append(f"{len(extra)} response tests not in expectedResults") |
| 96 | if ok == 0 and bad == 0: |
| 97 | bad += 1 |
| 98 | msgs.append("nothing compared") |
| 99 | for s in msgs[:5]: |
| 100 | print(f" {label}: {s}", file=sys.stderr) |
| 101 | verdict = "PASS" if bad == 0 else "FAIL" |
| 102 | tail = f" fail={bad}" if bad else "" |
| 103 | print(f"ACVP {label} {verdict} tests={ok}{tail} not_applicable={skipped}") |
| 104 | sys.exit(0 if bad == 0 else 1) |
| 105 | |
| 106 | |
| 107 | if __name__ == "__main__": |
| 108 | main() |