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 / receipt / parity / corpus.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | """corpus.py OUTDIR: write the receipt parity corpus, one record stream per file (OUTDIR/<stream>.bin), |
| 4 | and print "<stream> <records>" per stream. |
| 5 | |
| 6 | A record is an op byte, then the payload length, a and b as 8 decimal digits each, then the payload |
| 7 | (see the harness notes at the end of om/receipt/receipt.om). Everything is deterministic: the |
| 8 | random parts use fixed seeds, so the same corpus comes out on every run. The three Rust-written |
| 9 | receipts are read from tests/resolve/receipts/ of the checkout this file lives in. |
| 10 | """ |
| 11 | import json |
| 12 | import os |
| 13 | import random |
| 14 | import sys |
| 15 | |
| 16 | ROOT = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "..")) |
| 17 | ZERO = "0" * 64 |
| 18 | RUST = ["54f597d22b3753e47d6bd7706b6a7a422c189024912ec4626347e860b5f44a8c", |
| 19 | "7fdff014cb20a0af5b5a5b377532292be29fc15da36d434944ad4521e69de5b3", |
| 20 | "8e885832de2e7123555c47803547161c02196646b270d41078d8b16d4beaf765"] |
| 21 | TIERS = ["TEST_ONLY_TRUST", "HOST_TEST", "QEMU", "QEMU_SECURITY", "MACHINE1_READ_ONLY", "MACHINE1_ATTENDED", |
| 22 | "MACHINE1_MUTATING", "PRODUCTION"] |
| 23 | MUTS = ["NONE", "VOLATILE_ONLY", "REMOVABLE_MEDIA_ONLY", "ONE_TIME_BOOT_SELECTION", "BOUNDED_TEST_REGION_WRITE", |
| 24 | "BOOT_CONFIGURATION_CHANGE", "TRUST_ROOT_CHANGE", "TPM_POLICY_CHANGE", "DESTRUCTIVE_STORAGE"] |
| 25 | MAX = 4 * 1024 * 1024 |
| 26 | |
| 27 | |
| 28 | def rec(op, payload, a=0, b=0): |
| 29 | if isinstance(payload, str): |
| 30 | payload = payload.encode() |
| 31 | return op.encode() + b"%08d%08d%08d" % (len(payload), a, b) + payload |
| 32 | |
| 33 | |
| 34 | def js(s): |
| 35 | """a JSON string literal for s (ASCII escapes for non-ASCII, like serde would accept)""" |
| 36 | return json.dumps(s) |
| 37 | |
| 38 | |
| 39 | # ---- the receipt shape of tests/test_omega_resolve.c (rj_emit), as an ordered list of (key, raw JSON) |
| 40 | def rj(kind="host-v1", tier="HOST_TEST", env_class=None, result="PASS", schema="EvidenceReceiptV1", |
| 41 | decl="NONE", obs="NONE", procedure="fixture", reserved="", feature=None, version=1, inputs=(), |
| 42 | deps=(), root="6" * 64, assert_pass=True, extra=False, dup=False, lease=False, no_assertion=False, |
| 43 | dirty=False, authority="", idhex=ZERO): |
| 44 | f = [("schema", js(schema)), ("version", str(version)), ("id", js(idhex)), ("kind", js(kind))] |
| 45 | if dup: |
| 46 | f.append(("kind", js(kind))) |
| 47 | if extra: |
| 48 | f.append(("surprise", "1")) |
| 49 | f += [("tier", js(tier)), ("result", js(result)), ("timestamp", "1786000000"), ("repo", js("fixture")), |
| 50 | ("commit", js("a" * 40)), ("dirty", "true" if dirty else "false"), ("toolchain", js("fixture")), |
| 51 | ("procedure", js(procedure)), ("machine", js("host")), ("env_class", js(env_class or tier)), |
| 52 | ("input_artifacts", "[" + ", ".join(js(x) for x in inputs) + "]"), ("output_artifacts", "[]")] |
| 53 | if no_assertion: |
| 54 | f.append(("assertions", "[]")) |
| 55 | else: |
| 56 | f.append(("assertions", '[{"id": "fixture_check", "expected": "pass", "observed": "%s", "pass": %s, ' |
| 57 | '"source": "test", "note": ""}]' % ("pass" if assert_pass else "fail", |
| 58 | "true" if assert_pass else "false"))) |
| 59 | f += [("dependencies", "[" + ", ".join(js(x) for x in deps) + "]"), ("declared_mutation", js(decl)), |
| 60 | ("observed_mutation", js(obs)), ("authority", js(authority)), ("output_digest", js(root)), |
| 61 | ("external_refs", "[]"), ("ledger", "null"), |
| 62 | ("lease", '{"hold_id": "%s", "resource": "fixture-lease"}' % ("1" * 64) if lease else "null"), |
| 63 | ("required_features", "[%s]" % js(feature) if feature is not None else "[]"), |
| 64 | ("reserved", js(reserved))] |
| 65 | return f |
| 66 | |
| 67 | |
| 68 | def emit(fields, sep=",\n", kv=": ", head="{\n", tail="\n}\n"): |
| 69 | return head + sep.join(js(k) + kv + v for k, v in fields) + tail |
| 70 | |
| 71 | |
| 72 | def setf(fields, key, raw): |
| 73 | return [(k, raw if k == key else v) for k, v in fields] |
| 74 | |
| 75 | |
| 76 | def dropf(fields, key): |
| 77 | return [(k, v) for k, v in fields if k != key] |
| 78 | |
| 79 | |
| 80 | def sha(c): |
| 81 | return "sha256:" + c * 64 |
| 82 | |
| 83 | |
| 84 | def full(): |
| 85 | """a receipt that uses every field: ledger, lease, lists, several assertions, unicode""" |
| 86 | f = rj(inputs=[sha("1"), sha("2"), "blake3:" + "ab" * 32, "C" * 64], |
| 87 | deps=["8e885832de2e7123555c47803547161c02196646b270d41078d8b16d4beaf765", |
| 88 | "54F597D22B3753E47D6BD7706B6A7A422C189024912EC4626347E860B5F44A8C"]) |
| 89 | f = setf(f, "output_artifacts", "[%s, %s]" % (js("sha512:" + "e" * 128), js("blake3:" + "0f" * 16))) |
| 90 | f = setf(f, "assertions", '[{"id": "zeta", "expected": "1", "observed": "1", "pass": true, "source": "s", ' |
| 91 | '"note": "n\\u00e9"}, {"id": "alpha", "pass": false}, {"id": "\\u03b1lpha", "expected": "x", ' |
| 92 | '"pass": true, "note": "\\ud83d\\ude00"}]') |
| 93 | f = setf(f, "external_refs", '["https://example.invalid/b", "https://example.invalid/a", ' |
| 94 | '"https://example.invalid/a"]') |
| 95 | f = setf(f, "ledger", '{"index": 18446744073709551615, "hash": "%s"}' % ("Ab" * 32)) |
| 96 | f = setf(f, "lease", '{"resource": " r ", "hold_id": "%s"}' % ("c" * 64)) |
| 97 | f = setf(f, "required_features", '["v1-base", "v1-base"]') |
| 98 | f = setf(f, "kind", js("kind \u00e9\u4e2d\U0001F600 \"q\" \\ / \b\f\n\r\t")) |
| 99 | return f |
| 100 | |
| 101 | |
| 102 | def s_rust(): |
| 103 | out = [] |
| 104 | for h in RUST: |
| 105 | raw = open(os.path.join(ROOT, "tests", "resolve", "receipts", h + ".json"), "rb").read() |
| 106 | out.append(rec("R", raw)) |
| 107 | out.append(rec("R", raw.replace(h.encode(), h.upper().encode()))) |
| 108 | doc = json.loads(raw) |
| 109 | out.append(rec("R", json.dumps(doc, separators=(",", ":")))) |
| 110 | out.append(rec("R", json.dumps(doc, indent=4))) |
| 111 | out.append(rec("R", json.dumps(dict(reversed(list(doc.items())))))) |
| 112 | out.append(rec("R", json.dumps(doc, ensure_ascii=True).replace('"PASS"', '"P\\u0041SS"'))) |
| 113 | return out |
| 114 | |
| 115 | |
| 116 | def shape_variants(): |
| 117 | v = [rj(), rj(dup=True), rj(extra=True), rj(schema="EvidenceReceiptV2"), rj(version=2), |
| 118 | rj(feature="v2-extension"), rj(feature="v1-base"), rj(reserved="x"), rj(env_class="QEMU"), |
| 119 | rj(obs="VOLATILE_ONLY"), rj(decl="VOLATILE_ONLY", obs="VOLATILE_ONLY"), rj(no_assertion=True), |
| 120 | rj(no_assertion=True, result="FAIL"), rj(tier="PRODUCTION", kind="production-v1", authority="release-key-1"), |
| 121 | rj(lease=True), rj(dirty=True), rj(assert_pass=False, result="FAIL"), |
| 122 | rj(inputs=[sha("1"), sha("2")]), rj(inputs=[sha("2"), sha("1")]), rj(inputs=[sha("1"), sha("2")], procedure="another procedure"), |
| 123 | rj(deps=RUST[:2]), rj(deps=[RUST[1], RUST[0], RUST[1]])] |
| 124 | for a in ["", " ", " ", "\t", "TEST-ONLY signer", "a test_only key", "TestOnly", "test-only", "TEST_ONLY"]: |
| 125 | v.append(rj(tier="PRODUCTION", kind="production-v1", authority=a)) |
| 126 | return v |
| 127 | |
| 128 | |
| 129 | def s_shapes(): |
| 130 | out = [] |
| 131 | for f in shape_variants(): |
| 132 | d = emit(f) |
| 133 | out += [rec("R", d), rec("M", d), rec("M", d, 0, 257)] |
| 134 | d = emit(full()) |
| 135 | out += [rec("R", d), rec("M", d), rec("M", d, 0, 257)] |
| 136 | return out |
| 137 | |
| 138 | |
| 139 | BADVALS = ["1", "-1", "1.5", "1e3", "\"x\"", "\"\"", "[]", "[1]", "[\"x\"]", "{}", "null", "true", "false", |
| 140 | "18446744073709551615", "18446744073709551616", "0", "-0"] |
| 141 | |
| 142 | |
| 143 | def s_fields(): |
| 144 | out = [] |
| 145 | base = rj() |
| 146 | keys = [k for k, _ in base] |
| 147 | for k in keys: |
| 148 | out.append(rec("M", emit(dropf(base, k)))) |
| 149 | for bv in BADVALS: |
| 150 | out.append(rec("M", emit(setf(base, k, bv)))) |
| 151 | for k in ["schema", "kind", "tier", "repo", "commit", "toolchain", "procedure", "machine", "env_class", |
| 152 | "authority", "reserved"]: |
| 153 | for s in ["", " ", "\\u0041", "h\\u00e9llo", "\\ud83d\\ude00", "\\/\\\\\\\"", "raw \u00e9\u4e2d\U0001F600"]: |
| 154 | out.append(rec("M", emit(setf(base, k, '"%s"' % s)))) |
| 155 | for n in ["0", "1", "2", "01", "00", "4294967295", "4294967296", "18446744073709551615", |
| 156 | "18446744073709551616", "99999999999999999999", "100000000000000000000", "1.0", "1e0", "1E0", |
| 157 | "-1", "-0", "1x", "1.", "1e+-.5", " 7 "]: |
| 158 | out.append(rec("M", emit(setf(base, "version", n)))) |
| 159 | out.append(rec("M", emit(setf(base, "timestamp", n)))) |
| 160 | for c in ["a" * 40, "a" * 64, "a" * 39, "a" * 41, "A" * 40, "g" * 40, "0123456789abcdef" * 4, "\\u0061" * 40, "a" * 63]: |
| 161 | out.append(rec("M", emit(setf(base, "commit", js(c) if "\\" not in c else '"%s"' % c)))) |
| 162 | for t in TIERS + ["host_test", "HOST_TEST ", "", "PRODUCTIO", "PRODUCTIONS"]: |
| 163 | out.append(rec("M", emit(setf(setf(base, "tier", js(t)), "env_class", js(t))))) |
| 164 | out.append(rec("M", emit(setf(base, "tier", js(t))))) |
| 165 | for r in ["PASS", "FAIL", "BLOCKED", "INCOMPLETE", "SKIPPED", "pass", "", "PASSED"]: |
| 166 | out.append(rec("M", emit(setf(base, "result", js(r))))) |
| 167 | out.append(rec("M", emit(setf(setf(base, "result", js(r)), "assertions", "[]")))) |
| 168 | # the mutation and tier rules: every declared x observed x tier |
| 169 | for t in TIERS: |
| 170 | for d in MUTS: |
| 171 | for o in MUTS: |
| 172 | f = rj(tier=t, decl=d, obs=o, authority="release-key" if t == "PRODUCTION" else "") |
| 173 | out.append(rec("M", emit(f))) |
| 174 | for m in ["none", "", "NONE ", "DESTRUCTIVE"]: |
| 175 | out.append(rec("M", emit(setf(base, "declared_mutation", js(m))))) |
| 176 | out.append(rec("M", emit(setf(base, "observed_mutation", js(m))))) |
| 177 | digests = [sha("1"), "sha512:" + "a" * 128, "blake3:" + "a" * 32, "blake3:" + "a" * 31, "blake3:" + "a" * 33, |
| 178 | "blake3:" + "A" * 34, "blake3:", "blake3:" + "g" * 32, "blake3:" + "a" * 32 + ":", "md5:" + "a" * 32, |
| 179 | "sha256x:" + "a" * 64, "sha25:" + "a" * 64, ":" + "a" * 64, "a" * 64, "A" * 64, "a" * 63, "a" * 65, |
| 180 | "g" * 64, "", "BLAKE3:" + "a" * 64, "sha256::" + "a" * 64, "\\u0073ha256:" + "a" * 64] |
| 181 | for d in digests: |
| 182 | lit = '"%s"' % d |
| 183 | out.append(rec("M", emit(setf(base, "input_artifacts", "[%s]" % lit)))) |
| 184 | out.append(rec("M", emit(setf(base, "output_artifacts", "[%s, %s]" % (js(sha("3")), lit))))) |
| 185 | for h in ["a" * 64, "A" * 64, "a" * 63, "a" * 65, "g" * 64, "", "\\u0061" * 64]: |
| 186 | lit = '"%s"' % h |
| 187 | out.append(rec("M", emit(setf(base, "output_digest", lit)))) |
| 188 | out.append(rec("M", emit(setf(base, "dependencies", "[%s]" % lit)))) |
| 189 | out.append(rec("M", emit(setf(base, "ledger", '{"index": 3, "hash": %s}' % lit)))) |
| 190 | out.append(rec("M", emit(setf(base, "lease", '{"hold_id": %s, "resource": "x"}' % lit)))) |
| 191 | # the id field itself (R: the placeholder stays, verify reports the mismatch or the parse error) |
| 192 | for h in ["a" * 63, "a" * 65, "g" * 64, "", "A" * 64]: |
| 193 | out.append(rec("R", emit(setf(base, "id", '"%s"' % h)))) |
| 194 | for lg in ["null", "1", "\"x\"", "[]", "{}", '{"index": 1}', '{"hash": "%s"}' % ("a" * 64), |
| 195 | '{"index": -1, "hash": "%s"}' % ("a" * 64), '{"index": 1, "hash": 5}', |
| 196 | '{"index": 1, "hash": "%s", "x": 1}' % ("a" * 64), '{"index": 0, "hash": "%s"}' % ("a" * 64)]: |
| 197 | out.append(rec("M", emit(setf(base, "ledger", lg)))) |
| 198 | for ls in ["null", "1", "[]", "{}", '{"hold_id": "%s"}' % ("1" * 64), '{"resource": "x"}', |
| 199 | '{"hold_id": "%s", "resource": ""}' % ("1" * 64), '{"hold_id": "%s", "resource": " \\t\\n\\r"}' % ("1" * 64), |
| 200 | '{"hold_id": "%s", "resource": " x"}' % ("1" * 64), '{"hold_id": "%s", "resource": 1}' % ("1" * 64), |
| 201 | '{"hold_id": 1, "resource": "x"}', '{"hold_id": "%s", "resource": "x", "y": 2}' % ("1" * 64)]: |
| 202 | out.append(rec("M", emit(setf(base, "lease", ls)))) |
| 203 | for a in ['[]', '[1]', '"x"', '{}', '[{}]', '[{"id": "a"}]', '[{"pass": true}]', '[{"id": "", "pass": true}]', |
| 204 | '[{"id": "a", "pass": true}, {"id": "a", "pass": false}]', '[{"id": "a", "pass": 1}]', |
| 205 | '[{"id": 1, "pass": true}]', '[{"id": "a", "pass": true, "extra": 1}]', |
| 206 | '[{"id": "a", "pass": true, "note": null}]', '[{"id": "b", "pass": true}, {"id": "a", "pass": true}]', |
| 207 | '[{"id": "a", "pass": true}, {"id": "\\u0061", "pass": true}]', '[{"id": "a", "pass": true}, 7]', |
| 208 | '[{"id": "a", "pass": true, "expected": "e", "observed": "o", "source": "s", "note": "n"}]', |
| 209 | '[{"note": "n", "source": "s", "pass": false, "observed": "o", "expected": "e", "id": "z"}]', |
| 210 | '[{"id": "a", "pass": true, "expected": 1}]', '[{"id": "a", "pass": true, "observed": []}]', |
| 211 | '[{"id": "a", "pass": true, "source": {}}]', '[{"id": "a", "pass": null}]']: |
| 212 | out.append(rec("M", emit(setf(base, "assertions", a)))) |
| 213 | out.append(rec("M", emit(setf(setf(base, "assertions", a), "result", '"FAIL"')))) |
| 214 | for fl in ['[]', '["v1-base"]', '["v1-base", "x"]', '["x"]', '[1]', '["v1-bas"]', '["v1-base "]', '"v1-base"']: |
| 215 | out.append(rec("M", emit(setf(base, "required_features", fl)))) |
| 216 | for ex in ['[]', '["b", "a", "b", "", "\\u00e9", "\\u00e8", "z"]', '[1]', '"x"', 'null']: |
| 217 | out.append(rec("M", emit(setf(base, "external_refs", ex)))) |
| 218 | # sorting: byte order of the decoded strings, multi-byte UTF-8 and escapes, duplicates |
| 219 | rnd = random.Random(7) |
| 220 | alpha = ["a", "b", "A", "Z", "0", ":", "~", "\u00e9", "\u4e2d", "\U0001F600", "\\u0062", "\\/"] |
| 221 | for i in range(40): |
| 222 | items = [] |
| 223 | for j in range(rnd.randint(0, 12)): |
| 224 | items.append("".join(rnd.choice(alpha) for _ in range(rnd.randint(0, 5)))) |
| 225 | lit = "[" + ", ".join('"%s"' % x for x in items) + "]" |
| 226 | out.append(rec("M", emit(setf(base, "external_refs", lit)))) |
| 227 | deps = [rnd.choice(RUST) for _ in range(rnd.randint(0, 6))] |
| 228 | deps = [d.upper() if rnd.random() < 0.4 else d for d in deps] |
| 229 | out.append(rec("M", emit(setf(base, "dependencies", "[" + ", ".join(js(d) for d in deps) + "]")))) |
| 230 | asr = ", ".join('{"id": "%s", "pass": true, "note": "%d"}' % (x, k) for k, x in enumerate(dict.fromkeys(items))) |
| 231 | out.append(rec("M", emit(setf(base, "assertions", "[" + asr + "]")))) |
| 232 | for a in ["release-key", "", " ", " \t\r\n", "x", "test-only", "TEST-ONLY", "Test_Only", "testonly", "TESTONLY", |
| 233 | "my-test-onl", "test only", "testonl", "xtest-onlyx", "\\u0074est-only", "t\\u0045st_only", |
| 234 | "aaaaaaaaaatestonly", "tes\u00e9tonly", "\u00e9testonly"]: |
| 235 | out.append(rec("M", emit(rj(tier="PRODUCTION", authority=a)))) |
| 236 | out.append(rec("M", emit(rj(tier="MACHINE1_MUTATING", authority=a)))) |
| 237 | out.append(rec("M", emit(dropf(rj(tier="PRODUCTION"), "authority")))) |
| 238 | # formatting: compact, other whitespace, key order (canonical bytes do not depend on them) |
| 239 | for f in [base, full()]: |
| 240 | out.append(rec("M", emit(f, sep=",", kv=":", head="{", tail="}"))) |
| 241 | out.append(rec("M", emit(f, sep=" ,\r\n\t", kv=" :\t", head=" \n\t{ ", tail=" } \r\n "))) |
| 242 | out.append(rec("M", emit(list(reversed(f))))) |
| 243 | out.append(rec("M", emit(base) + " x")) |
| 244 | out.append(rec("M", emit(base, tail="\n}\n,"))) |
| 245 | return out |
| 246 | |
| 247 | |
| 248 | JSON_CASES = [ |
| 249 | "", " ", "{", "}", "[", "]", "{}", "[]", "null", "true", "false", "nul", "tru", "fals", "0", "-", "-0", "01", "-01", |
| 250 | "1.", "1e", "1x", "\"", "\"abc", "\"a\\", "\"\\x\"", "\"\\u12\"", "\"\\u12g4\"", "\"\\ud800\"", "\"\\ud800\\u0041\"", |
| 251 | "\"\\ud800\\udc00\"", "\"\\udc00\"", "\"\\u0000\"", "\"\\ud800x\"", "\"\\ud800\\\"", "\"\\ud800\\udbff\"", |
| 252 | "\"\\ud800\\ue000\"", "\"\x01\"", "\"\x1f\"", "\"\x7f\"", "\"\xc0\x80\"", "\"\xc2\x80\"", "\"\xc2\"", "\"\xe0\x80\x80\"", |
| 253 | "\"\xe0\xa0\x80\"", "\"\xed\xa0\x80\"", "\"\xef\xbf\xbf\"", "\"\xf0\x80\x80\x80\"", "\"\xf0\x90\x80\x80\"", |
| 254 | "\"\xf4\x8f\xbf\xbf\"", "\"\xf4\x90\x80\x80\"", "\"\xf5\x80\x80\x80\"", "\"\xff\"", "\"\xe2\x82\"", "\"\xf0\x9f\x98\"", |
| 255 | "{\"a\"}", "{\"a\" 1}", "{\"a\":}", "{\"a\":1,}", "{\"a\":1 \"b\":2}", "{\"a\":1,\"a\":2}", "{\"a\":1,\"\\u0061\":2}", |
| 256 | "{\"a\":{\"a\":1},\"b\":{\"a\":2,\"a\":3}}", "{1:2}", "{,}", "[1,]", "[,1]", "[1 2]", "[1,,2]", "{\"a\":[1,{\"b\":2}],\"c\":3}", |
| 257 | "[[[[[[1]]]]]]", "[[[[[[[1]]]]]]]", "[[[[[[]]]]]]", "[[[[[[[]]]]]]]", "{\"a\":{\"b\":{\"c\":{\"d\":{\"e\":{}}}}}}", |
| 258 | "{\"a\":{\"b\":{\"c\":{\"d\":{\"e\":{\"f\":1}}}}}}", "1 2", "{} {}", "{}x", " {} ", "\t\n\r{}\r\n\t", "{}\x00", |
| 259 | "\x00", "\xef\xbb\xbf{}", "{\"a\":\"\\b\\f\\n\\r\\t\\/\\\\\\\"\"}", "{\"a\":1e5}", "{\"a\":-1.5e-3}", "{\"a\":1E+2}", |
| 260 | "{\"a\":nul}", "{\"a\":truex}", "{\"a\":\"x\"}", "[\"a\",\"a\"]", "{\"\":1,\"\":2}", "{\"a\":1}", "[1]", "\"x\"", "7", |
| 261 | "{\"schema\":1}", "{\"zzz\":1}", "[{}]", "{\"a\":[1,2,3,{\"b\":[4,{\"c\":5}]}]}", "{\"a\":01}", "{\"a\":-}", |
| 262 | "{\"a\":.5}", "{\"a\":+1}", "{\"a\":\"\\u00\"}", "{\"a\":\"\\uD83D\\uDE00\"}", "{\"a\":\"\\uDE00\\uD83D\"}", |
| 263 | "{\"a\" : 1 , \"b\" : [ ] , \"c\" : { } }", "{\"a\":1,\"b\":2,\"c\":3,\"b\":4}", |
| 264 | ] |
| 265 | |
| 266 | |
| 267 | def fuzz_json(rnd, depth=0): |
| 268 | r = rnd.random() |
| 269 | if depth > 7 or r < 0.3: |
| 270 | return rnd.choice(["1", "-2", "0", "3.5", "1e9", "true", "false", "null", "\"s\"", "\"\\u00e9\"", |
| 271 | "\"\\ud83d\\ude00\"", "\"a\\nb\"", "18446744073709551616", "\"k\""]) |
| 272 | if r < 0.65: |
| 273 | return "[" + ",".join(fuzz_json(rnd, depth + 1) for _ in range(rnd.randint(0, 4))) + "]" |
| 274 | keys = ["a", "b", "c", "\\u0061", "id", "\u00e9"] |
| 275 | return "{" + ",".join('"%s":%s' % (rnd.choice(keys), fuzz_json(rnd, depth + 1)) |
| 276 | for _ in range(rnd.randint(0, 4))) + "}" |
| 277 | |
| 278 | |
| 279 | def s_json(): |
| 280 | out = [rec("R", c.encode("latin-1")) for c in JSON_CASES] |
| 281 | rnd = random.Random(11) |
| 282 | for _ in range(1500): |
| 283 | d = fuzz_json(rnd).encode() |
| 284 | out.append(rec("R", d)) |
| 285 | if d: |
| 286 | b = bytearray(d) |
| 287 | p = rnd.randrange(len(b)) |
| 288 | b[p] = rnd.choice(b' {}[]:,"\\x0\x01\xff-') |
| 289 | out.append(rec("R", bytes(b))) |
| 290 | for _ in range(300): |
| 291 | out.append(rec("R", bytes(rnd.randrange(256) for _ in range(rnd.randint(1, 40))))) |
| 292 | big = "{" + ",".join('"k%d":%d' % (i, i) for i in range(3000)) + "}" |
| 293 | out.append(rec("R", big)) |
| 294 | out.append(rec("R", big[:-1] + ',"k2999":1}')) |
| 295 | return out |
| 296 | |
| 297 | |
| 298 | def s_mutants(): |
| 299 | out = [] |
| 300 | rnd = random.Random(1726) |
| 301 | raws = [open(os.path.join(ROOT, "tests", "resolve", "receipts", h + ".json"), "rb").read() for h in RUST] |
| 302 | # every position of one Rust receipt with its 0x20 bit flipped (case flips, quote/space swaps) |
| 303 | r0 = raws[0] |
| 304 | for p in range(len(r0)): |
| 305 | b = bytearray(r0) |
| 306 | b[p] ^= 0x20 |
| 307 | out.append(rec("R", bytes(b))) |
| 308 | # every truncation of another |
| 309 | r1 = raws[1] |
| 310 | for n in range(len(r1)): |
| 311 | out.append(rec("R", r1[:n])) |
| 312 | # random single-byte flips of all three |
| 313 | for _ in range(600): |
| 314 | r = rnd.choice(raws) |
| 315 | b = bytearray(r) |
| 316 | p = rnd.randrange(len(b)) |
| 317 | b[p] ^= rnd.randint(1, 255) |
| 318 | out.append(rec("R", bytes(b))) |
| 319 | # receipts built here (id computed by the reader under test), then flipped or truncated |
| 320 | bases = [emit(f) for f in shape_variants()[:4]] + [emit(full()), emit(rj(lease=True, inputs=[sha("9")]))] |
| 321 | for _ in range(1200): |
| 322 | d = rnd.choice(bases) |
| 323 | if rnd.random() < 0.2: |
| 324 | out.append(rec("M", d, rnd.randrange(len(d)), 256)) |
| 325 | else: |
| 326 | out.append(rec("M", d, rnd.randrange(len(d)), rnd.randint(1, 255))) |
| 327 | return out |
| 328 | |
| 329 | |
| 330 | def s_big(): |
| 331 | out = [] |
| 332 | many = [sha("%x" % (i % 16)) [:7] + "%064x" % (i * 2654435761) for i in range(4096)] |
| 333 | out.append(rec("M", emit(setf(rj(), "input_artifacts", "[" + ", ".join(js(x) for x in many) + "]")))) |
| 334 | out.append(rec("M", emit(setf(rj(), "input_artifacts", "[" + ", ".join(js(x) for x in many + [sha("f")]) + "]")))) |
| 335 | out.append(rec("M", emit(setf(rj(), "external_refs", "[" + ", ".join(js(x) for x in many[::-1] + many[:100]) + "]")))) |
| 336 | over = "[" + ", ".join(js(x) for x in many + [sha("f")]) + "]" |
| 337 | for k in ["output_artifacts", "dependencies", "required_features"]: |
| 338 | out.append(rec("M", emit(setf(rj(), k, over)))) |
| 339 | asr = ", ".join('{"id": "a%05d", "pass": true}' % ((i * 7919) % 4096) for i in range(4096)) |
| 340 | out.append(rec("M", emit(setf(rj(), "assertions", "[" + asr + "]")))) |
| 341 | out.append(rec("M", emit(setf(rj(), "assertions", "[" + asr + ', {"id": "x", "pass": true}]')))) |
| 342 | # canonical bytes over 1 MiB (several harness windows): 4096 long digests |
| 343 | longd = ["sha512:" + ("%04x" % i) * 250 for i in range(4096)] |
| 344 | out.append(rec("M", emit(setf(rj(), "output_artifacts", "[" + ", ".join(js(x) for x in longd) + "]")))) |
| 345 | out.append(rec("M", emit(rj(tier="PRODUCTION", authority="k" * (1 << 20))))) |
| 346 | out.append(rec("M", emit(rj(tier="PRODUCTION", authority="k" * (1 << 20) + "test-only")))) |
| 347 | out.append(rec("M", emit(setf(rj(), "toolchain", '"' + "\u00e9" * 1500000 + '"')))) |
| 348 | out.append(rec("M", emit(setf(rj(), "toolchain", '"' + "\\u4e2d" * 600000 + '"')))) |
| 349 | d = emit(rj()) |
| 350 | out.append(rec("M", d + " " * (MAX - len(d)))) |
| 351 | out.append(rec("M", d + " " * (MAX - len(d) + 1))) |
| 352 | out.append(rec("R", d + " " * (MAX - len(d) + 1))) |
| 353 | out.append(rec("R", b"")) |
| 354 | out.append(rec("M", emit(setf(rj(), "timestamp", "1" + "0" * 2000000)))) |
| 355 | out.append(rec("M", emit(setf(rj(), "version", "1." + "0" * 1500000)))) |
| 356 | out.append(rec("R", "[" + "0," * 1500000 + "0]")) |
| 357 | out.append(rec("R", " " * (MAX - 2) + "{}")) |
| 358 | return out |
| 359 | |
| 360 | |
| 361 | def s_tier(): |
| 362 | out = [rec("T", t) for t in TIERS] |
| 363 | out += [rec("T", t) for t in ["", "host_test", "HOST_TEST ", " HOST_TEST", "PRODUCTIO", "PRODUCTIONX", "QEMU_", |
| 364 | "TEST_ONLY_TRUST\xe9", "x" * 300]] |
| 365 | out.append(rec("S", b"")) |
| 366 | return out |
| 367 | |
| 368 | |
| 369 | STREAMS = [("rust", s_rust), ("shapes", s_shapes), ("fields", s_fields), ("json", s_json), |
| 370 | ("mutants", s_mutants), ("big", s_big), ("tier", s_tier)] |
| 371 | |
| 372 | |
| 373 | def main(): |
| 374 | if len(sys.argv) != 2: |
| 375 | sys.exit(__doc__) |
| 376 | os.makedirs(sys.argv[1], exist_ok=True) |
| 377 | for name, fn in STREAMS: |
| 378 | recs = fn() |
| 379 | with open(os.path.join(sys.argv[1], name + ".bin"), "wb") as f: |
| 380 | for r in recs: |
| 381 | f.write(r) |
| 382 | print(name, len(recs)) |
| 383 | |
| 384 | |
| 385 | if __name__ == "__main__": |
| 386 | main() |