interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / tls / test / net / hostile_run.py

101 lines · 4737 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3# Test tooling only: runs every hostile.tsv case (or the ones named after the table) against a
4# build of test/tls/client.om and judges it. One hostile.py server per case on 127.0.0.1.
5# usage: hostile_run.py CLIENT PKIDIR TSV [CASE...]
6# Prints one row per case and "HOSTILE PASS|FAIL cases=N pass=P fail=F"; exit 0 only on PASS.
7# Client outcome: OK = exit 0 and "HTTP/1.0 200 ok" on stdout; otherwise the TLSERR name from
8# stderr; HANG = killed by the 40 s timeout; TRAP = any other exit without a TLSERR line.
9import os, re, socket, subprocess, sys
10
11client, pki, tsv = sys.argv[1], sys.argv[2], sys.argv[3]
12only = set(sys.argv[4:])
13here = os.path.dirname(os.path.abspath(__file__))
14rows = []
15for line in open(tsv):
16 if line.startswith("#") or not line.strip():
17 continue
18 f = line.rstrip("\n").split("\t")
19 if len(f) != 6:
20 print("TABLE FAULT: %r" % line); sys.exit(2)
21 if not only or f[0] in only:
22 rows.append(f)
23if only and len(rows) != len(only):
24 print("TABLE FAULT: unknown case in %s" % sorted(only - {r[0] for r in rows})); sys.exit(2)
25
26
27def freeport():
28 s = socket.socket(); s.bind(("127.0.0.1", 0)); p = s.getsockname()[1]; s.close()
29 return p
30
31
32npass = nfail = 0
33for case, expect, alerts, check, rfc, desc in rows:
34 port = freeport()
35 srv = subprocess.Popen([sys.executable, os.path.join(here, "hostile.py"), str(port), pki, case],
36 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
37 if srv.stdout.readline().strip() != "READY":
38 srv.kill(); print("FAIL | %-22s | server did not start: %s" % (case, srv.stderr.read()[-300:])); nfail += 1; continue
39 try:
40 cp = subprocess.run([client, "localhost", str(port), "get", "127.0.0.1", os.path.join(pki, "root.pem"), "/", "0,8000,0"],
41 capture_output=True, timeout=40)
42 rc, cout, cerr = cp.returncode, cp.stdout.decode("latin-1"), cp.stderr.decode("latin-1")
43 except subprocess.TimeoutExpired:
44 rc, cout, cerr = None, "", ""
45 try:
46 sout, serr = srv.communicate(timeout=25)
47 except subprocess.TimeoutExpired:
48 srv.kill(); sout, serr = srv.communicate()
49 facts = {}
50 for l in sout.splitlines():
51 if l.startswith("SRV "):
52 facts.update(kv.split("=", 1) for kv in l[4:].split() if "=" in kv)
53 m = re.search(r"TLSERR TLS_ERR_([A-Z_]+) detail (\d+)", cerr)
54 if rc is None:
55 got = "HANG"
56 elif rc == 0 and "HTTP/1.0 200 ok" in cout:
57 got = "OK"
58 elif m:
59 got = m.group(1)
60 else:
61 got = "TRAP(rc=%s)" % rc
62 alert = facts.get("alert", "none")
63 why = []
64 if "CLOSE_NOT_FAILED" in cerr:
65 why.append("tls_close revived a failed connection")
66 if expect == "OK":
67 if got != "OK":
68 why.append("expected OK")
69 elif expect == "ABORT_OR_OK":
70 if got != "OK" and not m:
71 why.append("neither OK nor a TLS error")
72 else:
73 if not m or got == "OK":
74 why.append("expected an abort")
75 elif got == "TIMEOUT" and expect != "ABORT:TIMEOUT":
76 # the client's own handshake budget ran out: it waited for a server that stopped
77 # sending, so the case never reached the condition it tests
78 why.append("client timed out waiting (case not exercised)")
79 elif ":" in expect and got not in expect.split(":", 1)[1].split(","):
80 why.append("expected TLS_ERR_%s" % expect.split(":", 1)[1])
81 if m and alerts not in ("*", "-") and got not in ("ALERT_RECEIVED", "EOF", "TIMEOUT", "IO"):
82 if alert not in alerts.split(","):
83 why.append("alert %s, RFC wants %s" % (alert, alerts))
84 if check != "-":
85 for ck in check.split(","):
86 if ck.startswith("!out:"):
87 if ck[5:] in cout:
88 why.append("stdout has %s" % ck[5:])
89 else:
90 k, v = ck.split("=", 1)
91 if facts.get(k) != v:
92 why.append("server saw %s=%s, wants %s" % (k, facts.get(k), v))
93 if serr.strip() and "Traceback" in serr:
94 why.append("server error: " + serr.strip().splitlines()[-1])
95 v = "FAIL" if why else "PASS"
96 npass += not why; nfail += bool(why)
97 extra = " ".join("%s=%s" % kv for kv in facts.items() if kv[0] != "alert")
98 print("%s | %-22s | expect %-28s alert %-9s | got %-22s alert %-5s | %s | %s%s" % (
99 v, case, expect, alerts, got, alert, extra or "-", rfc, (" | WHY: " + "; ".join(why)) if why else ""), flush=True)
100print("HOSTILE %s cases=%d pass=%d fail=%d" % ("PASS" if nfail == 0 else "FAIL", npass + nfail, npass, nfail))
101sys.exit(0 if nfail == 0 else 1)