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 / tests / test_omega_vcstore.c

967 lines · 47938 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* test_omega_vcstore.c -- VC1 stage 3: the Verified Crumb Store.
18 *
19 * Usage (run from the repository root; golden vectors are read from tests/vcstore/golden/):
20 * test_omega_vcstore run every check; exit 0 only if all pass
21 * test_omega_vcstore <mutant> the binary was linked against a deliberately broken copy of
22 * src/omega_vcstore.c (built by `make test-vcstore`); run every
23 * check and require the mapped check to FAIL. Exit 1 = mutant
24 * killed (what the Makefile asserts), 3 = survived, 2 = unknown.
25 *
26 * Nothing here is hardcoded PASS: accepted ids come from golden/expected.txt (computed by
27 * aien-protocols with sha256sum, independent of this code), the state digest and the store file
28 * are re-derived here by a second, independent implementation, and graphs are built with a
29 * test-side encoder that does not share code with the decoder under test.
30 */
31#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36
37#include "omega_genesis.h"
38#include "omega_vcstore.h"
39#include "omega_vcstore_priv.h"
40#include "sha256.h"
41
42#define GOLDEN "tests/vcstore/golden"
43
44static const struct { const char *mutant, *check; } MUTANTS[] = {
45 /* decoder */
46 { "refuse-valid", "accept-v01_minimal" },
47 { "refuse-existing-dep", "accept-v02_deps" },
48 { "caps-reversed", "accept-v03_capabilities" },
49 { "kind-source-only", "accept-v04_ir_kind" },
50 { "id-hash", "accept-ids-match-golden" },
51 { "dec-dep-dup", "refuse-r01_duplicate_dep" },
52 { "dec-dep-order", "refuse-r02_unsorted_deps" },
53 { "dec-receipt", "refuse-r03_zero_receipt_id" },
54 { "dec-dep-self", "refuse-r04_self_dependency" },
55 { "dec-trailing", "refuse-r05_trailing_byte" },
56 { "dec-version", "refuse-r06_unknown_version" },
57 { "dec-truncated", "refuse-r07_truncated" },
58 { "dec-tag", "refuse-r08_bad_domain_tag" },
59 { "dec-exports-order", "refuse-r09_unsorted_exports" },
60 { "dec-kind", "refuse-r10_bad_digest_kind" },
61 { "dec-zero-ids", "refuse-r11_zero_id" },
62 { "dec-string-char", "refuse-r12_bad_string" },
63 { "dec-count", "refuse-r13_too_many_entries" },
64 { "dec-real-order", "refuse-unsorted-realizations" },
65 { "dec-caps-order", "refuse-unsorted-capabilities" },
66 { "dec-dep-zero", "refuse-zero-dependency-id" },
67 { "dec-evroot", "refuse-zero-evidence-root" },
68 { "dec-string-empty", "refuse-bad-string-empty" },
69 { "dec-string-max", "refuse-bad-string-too-long" },
70 /* insert */
71 { "claimed-required", "null-claimed-id-refused" },
72 { "insert-idcheck", "claimed-id-mismatch-refused" },
73 { "idempotent", "identical-insert-is-noop" },
74 { "idempotent-ignores-kind", "kind-switch-refused" },
75 { "immutable", "immutable-conflict-refused" },
76 { "dep-exists", "missing-dep-refused" },
77 { "dep-contract", "dep-contract-mismatch-refused" },
78 { "no-cap", "store-grows-past-old-caps" },
79 { "sorted-insert", "digest-independent-of-insert-order" },
80 { "kind-bootstrap", "insert-bootstrap-unlisted-refused" },
81 { "insert-genesis", "insert-bootstrap-unlisted-refused" },
82 { "load-genesis", "load-bootstrap-unlisted-refused" },
83 /* get, receipt */
84 { "get-recompute", "get-detects-corrupt-bytes" },
85 { "get-key", "get-detects-key-mismatch" },
86 { "receipt-of", "receipt-of-returns-receipt-id" },
87 /* closure */
88 { "walk-order", "closure-order-deterministic" },
89 { "walk-visited", "closure-visits-each-id-once" },
90 { "walk-capacity", "closure-capacity-refused" },
91 { "walk-dep-exists", "closure-missing-dep-refused" },
92 { "walk-contract", "closure-contract-mismatch-refused" },
93 { "walk-cycle", "closure-cycle-refused" },
94 { "walk-names", "names-do-not-affect-resolution" },
95 /* digests */
96 { "digest-magic", "digest-matches-reference" },
97 { "digest-kind", "bootstrap-vs-verified-digest-differ" },
98 { "digest-bytes", "digest-covers-bytes" },
99 { "digest-names", "name-index-never-changes-object-digest" },
100 { "namedigest-id", "name-index-digest-covers-id" },
101 { "name-sorted", "name-index-digest-order-independent" },
102 /* names */
103 { "name-many", "name-bind-many-to-one" },
104 { "name-no-silent-rebind", "name-no-silent-rebind" },
105 { "rebind-write", "name-rebind-explicit" },
106 { "rebind-exists", "name-rebind-unbound-refused" },
107 { "rebind-id-exists", "name-rebind-unknown-id-refused" },
108 { "name-id-exists", "name-bind-unknown-id-refused" },
109 { "name-valid", "name-invalid-refused" },
110 { "resolve", "resolve-name-returns-id" },
111 /* persistence */
112 { "load-names", "save-load-round-trip" },
113 { "load-fail", "load-failure-leaves-store-unchanged" },
114 { "load-magic", "tamper-every-byte-and-prefix-refused" },
115 { "load-digest-obj", "tamper-object-digest-refused" },
116 { "load-digest-names", "tamper-name-digest-refused" },
117 { "load-trailing", "trailing-byte-refused" },
118 { "load-kind", "load-bad-kind-refused" },
119 { "load-order", "load-unsorted-objects-refused" },
120 { "load-graph", "load-cycle-refused" },
121 { "load-graph-missing", "load-missing-dep-refused" },
122 { "load-name-id", "load-name-unknown-id-refused" },
123 { "save-verify", "save-refuses-corrupt-store" },
124};
125#define N_MUT (sizeof MUTANTS / sizeof MUTANTS[0])
126
127static const char *g_mutant, *g_expect_check;
128static int g_failed, g_expect_failed;
129
130static void check(const char *name, int ok) {
131 printf("%s %s\n", ok ? "PASS" : "FAIL", name);
132 if (!ok) {
133 g_failed++;
134 if (g_expect_check && strcmp(name, g_expect_check) == 0) g_expect_failed = 1;
135 }
136}
137/* A setup step that cannot complete is a hard error. Under a mutant it is accepted only if the
138 * mapped check had already failed (the Makefile accepts exactly "KILLED by", exit 1). */
139static void setup_fail(const char *what) {
140 if (g_mutant && g_expect_failed) { printf("MUTANT %s KILLED by %s (a later fixture then failed: %s)\n", g_mutant, g_expect_check, what); exit(1); }
141 if (g_mutant) { printf("MUTANT %s SETUP-BROKEN before check %s ran (%s)\n", g_mutant, g_expect_check, what); exit(4); }
142 fprintf(stderr, "setup failed: %s\n", what);
143 exit(2);
144}
145
146/* ---- byte helpers ---- */
147typedef struct { uint8_t *p; size_t n, cap; } B;
148static void bput(B *b, const void *d, size_t k) {
149 if (b->n + k > b->cap) { b->cap = (b->n + k) * 2 + 64; b->p = realloc(b->p, b->cap); if (!b->p) setup_fail("oom"); }
150 if (k) memcpy(b->p + b->n, d, k);
151 b->n += k;
152}
153static void bu32(B *b, uint32_t v) { uint8_t t[4] = { (uint8_t)(v >> 24), (uint8_t)(v >> 16), (uint8_t)(v >> 8), (uint8_t)v }; bput(b, t, 4); }
154static void bu64(B *b, uint64_t v) { bu32(b, (uint32_t)(v >> 32)); bu32(b, (uint32_t)v); }
155static void bstr(B *b, const char *s) { size_t n = strlen(s); bu64(b, n); bput(b, s, n); }
156
157static void mkid(uint8_t out[32], uint8_t b0) { memset(out, 0x77, 32); out[0] = b0; }
158static void mkid2(uint8_t out[32], uint8_t b0, uint8_t b1) { memset(out, 0x77, 32); out[0] = b0; out[1] = b1; }
159/* the contract id of a program is its id with the top bit of byte 0 flipped */
160static void con_of(const uint8_t sid[32], uint8_t out[32]) { memcpy(out, sid, 32); out[0] ^= 0x80; }
161
162/* ---- test-side encoder (writes lists exactly as given: no sorting, no validation) ---- */
163typedef struct {
164 uint32_t ver;
165 uint8_t sem[32], con[32], src[32], rcpt[32], root[32];
166 uint8_t kind;
167 uint32_t n_real; const uint8_t (*real)[32];
168 uint32_t n_dep; const uint8_t (*dep)[64];
169 const char *prof, *pver;
170 uint32_t n_exp; const char *const *exp;
171 uint32_t n_cap; const char *const *cap;
172} Spec;
173
174static void spec_default(Spec *s, const uint8_t sem[32]) {
175 memset(s, 0, sizeof *s);
176 s->ver = 1; s->kind = 1;
177 memcpy(s->sem, sem, 32);
178 con_of(sem, s->con);
179 memset(s->src, 0x33, 32);
180 memset(s->rcpt, 0x55, 32); s->rcpt[0] = sem[0]; s->rcpt[1] = sem[1];
181 memset(s->root, 0x66, 32);
182 s->prof = "test-profile"; s->pver = "1.0.0";
183}
184static void encode(B *o, const Spec *s) {
185 o->n = 0;
186 bput(o, "AIEN_VERIFIED_CRUMB_V1", 22);
187 bu32(o, s->ver);
188 bput(o, s->sem, 32); bput(o, s->con, 32); bput(o, &s->kind, 1); bput(o, s->src, 32);
189 bu32(o, s->n_real); for (uint32_t i = 0; i < s->n_real; i++) bput(o, s->real[i], 32);
190 bu32(o, s->n_dep); for (uint32_t i = 0; i < s->n_dep; i++) bput(o, s->dep[i], 64);
191 bput(o, s->rcpt, 32); bstr(o, s->prof); bstr(o, s->pver); bput(o, s->root, 32);
192 bu32(o, s->n_exp); for (uint32_t i = 0; i < s->n_exp; i++) bstr(o, s->exp[i]);
193 bu32(o, s->n_cap); for (uint32_t i = 0; i < s->n_cap; i++) bstr(o, s->cap[i]);
194}
195/* VC-GENESIS-1 is EMPTY in the real source, so no BOOTSTRAP record can be inserted here. A test
196 * that needs a BOOTSTRAP-kind object (digest and persistence checks) inserts it as VERIFIED and
197 * then flips the kind byte in the store, which the header declares public for exactly this
198 * (tamper) purpose. Real BOOTSTRAP admission, with a non-empty list, is test_omega_genesis_set. */
199static void flip_to_bootstrap(OmegaVcStore *st, const uint8_t sid[32]) {
200 for (size_t i = 0; i < st->count; i++)
201 if (memcmp(st->objs[i].semantic_id, sid, 32) == 0) { st->objs[i].admission_kind = OMEGA_VCS_ADMISSION_BOOTSTRAP; return; }
202 setup_fail("flip_to_bootstrap: no such object");
203}
204static int ins_kind(OmegaVcStore *st, const Spec *sp, int bootstrap) {
205 B b = { 0, 0, 0 };
206 encode(&b, sp);
207 uint8_t id[32];
208 omega_vc_compute_id(b.p, b.n, id);
209 int rc = omega_vcstore_insert(st, b.p, b.n, id);
210 if (rc == 0 && bootstrap) flip_to_bootstrap(st, sp->sem);
211 free(b.p);
212 return rc;
213}
214static int ins(OmegaVcStore *st, const Spec *sp) { return ins_kind(st, sp, 0); }
215
216/* a node whose dependencies are given by id; each required contract is the dependency's own contract */
217static int node_kind(OmegaVcStore *st, const uint8_t sid[32], const uint8_t (*deps)[32], uint32_t nd, int bootstrap) {
218 Spec sp;
219 spec_default(&sp, sid);
220 uint8_t (*d)[64] = calloc(nd ? nd : 1, 64);
221 if (!d) setup_fail("oom");
222 for (uint32_t i = 0; i < nd; i++) { memcpy(d[i], deps[i], 32); con_of(deps[i], d[i] + 32); }
223 sp.n_dep = nd; sp.dep = (const uint8_t (*)[64])d;
224 int rc = ins_kind(st, &sp, bootstrap);
225 free(d);
226 return rc;
227}
228static int node(OmegaVcStore *st, const uint8_t sid[32], const uint8_t (*deps)[32], uint32_t nd) { return node_kind(st, sid, deps, nd, 0); }
229
230static OmegaVcStore *new_store(void) {
231 OmegaVcStore *s = calloc(1, sizeof *s);
232 if (!s || omega_vcstore_init(s) != 0) setup_fail("store");
233 return s;
234}
235static void free_store(OmegaVcStore *s) { omega_vcstore_destroy(s); free(s); }
236static void dig(const OmegaVcStore *s, uint8_t out[32]) { if (omega_vcstore_digest(s, out) != 0) setup_fail("digest"); }
237static int same_digest(const OmegaVcStore *a, const OmegaVcStore *b) {
238 uint8_t x[32], y[32]; dig(a, x); dig(b, y); return memcmp(x, y, 32) == 0;
239}
240
241/* the diamond: R needs B and C, both need D. Ids ascend with the letters' order below. */
242static uint8_t D[32], Bn[32], C[32], R[32];
243static void diamond_ids(void) { mkid(D, 0x10); mkid(Bn, 0x20); mkid(C, 0x30); mkid(R, 0x40); }
244static int build_diamond(OmegaVcStore *s, int swap_bc) {
245 uint8_t dD[1][32], dR[2][32];
246 memcpy(dD[0], D, 32);
247 memcpy(dR[0], Bn, 32); memcpy(dR[1], C, 32);
248 int rc = node(s, D, NULL, 0);
249 if (swap_bc) { rc |= node(s, C, dD, 1); rc |= node(s, Bn, dD, 1); }
250 else { rc |= node(s, Bn, dD, 1); rc |= node(s, C, dD, 1); }
251 rc |= node(s, R, dR, 2);
252 return rc;
253}
254
255/* ---- golden corpus ---- */
256static uint8_t *read_hex(const char *path, size_t *n_out) {
257 FILE *f = fopen(path, "rb");
258 if (!f) return NULL;
259 size_t cap = 4096, n = 0; char *t = malloc(cap); int ch;
260 while ((ch = fgetc(f)) != EOF) {
261 if (ch == '\n' || ch == '\r' || ch == ' ') continue;
262 if (n + 1 > cap) { cap *= 2; t = realloc(t, cap); }
263 t[n++] = (char)ch;
264 }
265 fclose(f);
266 uint8_t *b = malloc(n / 2 + 1);
267 for (size_t i = 0; i + 1 < n; i += 2) {
268 int hi = t[i] <= '9' ? t[i] - '0' : t[i] - 'a' + 10, lo = t[i + 1] <= '9' ? t[i + 1] - '0' : t[i + 1] - 'a' + 10;
269 b[i / 2] = (uint8_t)(hi << 4 | lo);
270 }
271 free(t); *n_out = n / 2;
272 return b;
273}
274
275static void golden(void) {
276 char path[512], line[512];
277 snprintf(path, sizeof path, "%s/expected.txt", GOLDEN);
278 FILE *f = fopen(path, "r");
279 if (!f) setup_fail("golden/expected.txt (run from the repository root)");
280 int n_acc = 0, n_ref = 0, ids_ok = 1;
281 while (fgets(line, sizeof line, f)) {
282 char name[256], verb[16], arg[128], cname[300];
283 if (sscanf(line, "%255s %15s %127s", name, verb, arg) != 3) continue;
284 snprintf(path, sizeof path, "%s/%s.hex", GOLDEN, name);
285 size_t n; uint8_t *b = read_hex(path, &n);
286 if (!b) setup_fail("golden vector file");
287 OmegaVcStore *s = new_store();
288 uint8_t claimed[32];
289 omega_vc_compute_id(b, n, claimed);
290 if (strcmp(verb, "ACCEPT") == 0) {
291 n_acc++;
292 /* the expected id is the one aien-protocols computed with sha256sum */
293 for (int i = 0; i < 32; i++) { unsigned v; sscanf(arg + 2 * i, "%2x", &v); claimed[i] = (uint8_t)v; }
294 /* dependencies the vector names must exist first: synthesize them from the vector itself */
295 OmegaVcView *v = malloc(sizeof *v);
296 if (omega_vc_decode(b, n, v) == 0) {
297 for (uint32_t i = 0; i < v->n_dependencies; i++) {
298 Spec sp;
299 spec_default(&sp, v->dependencies + 64 * (size_t)i);
300 memcpy(sp.con, v->dependencies + 64 * (size_t)i + 32, 32);
301 if (ins(s, &sp) != 0) setup_fail("golden dependency");
302 }
303 }
304 free(v);
305 int rc = omega_vcstore_insert(s, b, n, claimed);
306 snprintf(cname, sizeof cname, "accept-%s", name);
307 check(cname, rc == 0);
308 OmegaVcRecord *rec = malloc(sizeof *rec);
309 uint8_t sid[32];
310 OmegaVcView *v2 = malloc(sizeof *v2);
311 if (omega_vc_decode(b, n, v2) == 0) memcpy(sid, v2->semantic_id, 32); else memset(sid, 0x99, 32);
312 free(v2);
313 if (omega_vcstore_get(s, sid, rec) != 0 || memcmp(rec->vc_id, claimed, 32) != 0) ids_ok = 0;
314 free(rec);
315 } else {
316 n_ref++;
317 int rc = omega_vcstore_insert(s, b, n, claimed);
318 snprintf(cname, sizeof cname, "refuse-%s", name);
319 check(cname, rc != 0 && strcmp(omega_vcstore_code_name(rc), arg) == 0 && omega_vcstore_count(s) == 0);
320 }
321 free(b);
322 free_store(s);
323 }
324 fclose(f);
325 if (n_acc != 4 || n_ref != 13) setup_fail("golden corpus is not 4 accept + 13 refuse vectors");
326 check("accept-ids-match-golden", ids_ok);
327}
328
329/* ---- synthetic refusals ---- */
330static int refuse_code(const Spec *sp) {
331 OmegaVcStore *s = new_store();
332 int rc = ins(s, sp);
333 if (omega_vcstore_count(s) != 0 && rc != 0) rc = -1; /* a refusal must leave the store empty */
334 free_store(s);
335 return rc;
336}
337
338static void synthetic_refusals(void) {
339 uint8_t sem[32]; mkid(sem, 0x11);
340 Spec sp;
341 uint8_t reals[2][32]; mkid(reals[0], 0x90); mkid(reals[1], 0x80); /* descending */
342 spec_default(&sp, sem); sp.n_real = 2; sp.real = (const uint8_t (*)[32])reals;
343 check("refuse-unsorted-realizations", refuse_code(&sp) == OMEGA_VCS_NONCANONICAL_SET);
344 const char *caps[2] = { "b-cap", "a-cap" };
345 spec_default(&sp, sem); sp.n_cap = 2; sp.cap = caps;
346 check("refuse-unsorted-capabilities", refuse_code(&sp) == OMEGA_VCS_NONCANONICAL_SET);
347 uint8_t dz[1][64]; mkid(dz[0], 0x22); memset(dz[0] + 32, 0, 32);
348 spec_default(&sp, sem); sp.n_dep = 1; sp.dep = (const uint8_t (*)[64])dz;
349 check("refuse-zero-dependency-id", refuse_code(&sp) == OMEGA_VCS_ZERO_ID);
350 spec_default(&sp, sem); memset(sp.root, 0, 32);
351 check("refuse-zero-evidence-root", refuse_code(&sp) == OMEGA_VCS_ZERO_ID);
352 spec_default(&sp, sem); sp.prof = "";
353 check("refuse-bad-string-empty", refuse_code(&sp) == OMEGA_VCS_BAD_STRING);
354 char longs[258]; memset(longs, 'a', 257); longs[257] = 0;
355 spec_default(&sp, sem); sp.pver = longs;
356 check("refuse-bad-string-too-long", refuse_code(&sp) == OMEGA_VCS_BAD_STRING);
357}
358
359/* ---- store behaviour ---- */
360static const uint8_t ZERO[32];
361
362static void insert_checks(void) {
363 uint8_t sem[32]; mkid(sem, 0x11);
364 Spec sp; spec_default(&sp, sem);
365 B b = { 0, 0, 0 };
366 encode(&b, &sp);
367 uint8_t id[32]; omega_vc_compute_id(b.p, b.n, id);
368
369 OmegaVcStore *s = new_store();
370 check("null-claimed-id-refused",
371 omega_vcstore_insert(s, b.p, b.n, NULL) == OMEGA_VCS_VCSTORE_MALFORMED && omega_vcstore_count(s) == 0);
372 uint8_t wrong[32]; memcpy(wrong, id, 32); wrong[7] ^= 1;
373 check("claimed-id-mismatch-refused",
374 omega_vcstore_insert(s, b.p, b.n, wrong) == OMEGA_VCS_VCSTORE_ID_MISMATCH && omega_vcstore_count(s) == 0);
375
376 /* identical bytes twice: success, one object, same digest */
377 int r1 = omega_vcstore_insert(s, b.p, b.n, id);
378 uint8_t d1[32], d2[32]; dig(s, d1);
379 int r2 = omega_vcstore_insert(s, b.p, b.n, id);
380 dig(s, d2);
381 check("identical-insert-is-noop", r1 == 0 && r2 == 0 && omega_vcstore_count(s) == 1 && memcmp(d1, d2, 32) == 0);
382
383 /* same program id, different record (other receipt and exports): refused, original untouched */
384 Spec sp2; spec_default(&sp2, sem); sp2.rcpt[5] ^= 0xFF;
385 const char *ex[1] = { "other" }; sp2.n_exp = 1; sp2.exp = ex;
386 int rc = ins(s, &sp2);
387 OmegaVcRecord *rec = malloc(sizeof *rec);
388 int g = omega_vcstore_get(s, sem, rec);
389 dig(s, d2);
390 check("immutable-conflict-refused",
391 rc == OMEGA_VCS_VCSTORE_IMMUTABLE_CONFLICT && g == 0 && memcmp(rec->vc_id, id, 32) == 0 &&
392 memcmp(d1, d2, 32) == 0 && omega_vcstore_count(s) == 1);
393 free(rec);
394 /* same bytes, other admission kind: also a conflict, never a silent relabel (the stored
395 * record is flipped to BOOTSTRAP, then the identical bytes are offered as VERIFIED) */
396 flip_to_bootstrap(s, sem);
397 uint8_t kind = 0;
398 rc = omega_vcstore_insert(s, b.p, b.n, id);
399 int g2 = omega_vcstore_admission_kind(s, sem, &kind);
400 check("kind-switch-refused", rc == OMEGA_VCS_VCSTORE_IMMUTABLE_CONFLICT && g2 == 0 && kind == OMEGA_VCS_ADMISSION_BOOTSTRAP);
401 free_store(s);
402
403 /* VC-GENESIS-1 gate: a BOOTSTRAP record whose id is not on the list is refused with its own
404 * code and leaves the store untouched; the list is the only thing that decides */
405 s = new_store();
406 uint8_t g0[32]; dig(s, g0);
407 uint8_t ug[32]; mkid(ug, 0x12);
408 Spec spg; spec_default(&spg, ug);
409 B bg = { 0, 0, 0 };
410 encode(&bg, &spg);
411 uint8_t gid[32]; omega_vc_compute_id(bg.p, bg.n, gid);
412 int rg = omega_vcstore_insert_bootstrap(s, bg.p, bg.n, gid), rv = omega_vcstore_insert(s, bg.p, bg.n, gid);
413 uint8_t g1[32]; dig(s, g1);
414 check("insert-bootstrap-unlisted-refused",
415 !omega_genesis_contains(ug) && rg == OMEGA_VCS_GENESIS_NOT_LISTED && strcmp(omega_vcstore_code_name(rg), "GENESIS_NOT_LISTED") == 0 &&
416 rv == 0 && omega_vcstore_count(s) == 1 && memcmp(g0, g1, 32) != 0);
417 free(bg.p);
418 free_store(s);
419
420 /* receipt_of: the record's receipt_id, nothing else; absent id is refused */
421 s = new_store();
422 if (ins(s, &sp) != 0) setup_fail("receipt_of insert");
423 uint8_t out[32]; memset(out, 0xEE, 32);
424 int ro = omega_vcstore_receipt_of(s, sem, out);
425 uint8_t missing[32]; mkid(missing, 0xF1);
426 uint8_t out2[32]; memset(out2, 0xEE, 32);
427 int ro2 = omega_vcstore_receipt_of(s, missing, out2);
428 check("receipt-of-returns-receipt-id", ro == 0 && memcmp(out, sp.rcpt, 32) == 0 &&
429 ro2 == OMEGA_VCS_UNVERIFIED_DEPENDENCY && out2[0] == 0xEE);
430 free_store(s);
431 free(b.p);
432
433 /* missing dependency: the golden v02 on its own */
434 {
435 char path[512]; size_t n;
436 snprintf(path, sizeof path, "%s/v02_deps.hex", GOLDEN);
437 uint8_t *v2 = read_hex(path, &n);
438 if (!v2) setup_fail("v02");
439 uint8_t cid[32]; omega_vc_compute_id(v2, n, cid);
440 s = new_store();
441 rc = omega_vcstore_insert(s, v2, n, cid);
442 check("missing-dep-refused", rc == OMEGA_VCS_UNVERIFIED_DEPENDENCY && omega_vcstore_count(s) == 0);
443 free_store(s); free(v2);
444 }
445
446 /* dependency present but under another contract */
447 s = new_store();
448 uint8_t dd[32]; mkid(dd, 0x10);
449 if (node(s, dd, NULL, 0) != 0) setup_fail("dep node");
450 Spec sp3; spec_default(&sp3, sem);
451 uint8_t depw[1][64]; memcpy(depw[0], dd, 32); mkid(depw[0] + 32, 0x01); /* wrong contract */
452 sp3.n_dep = 1; sp3.dep = (const uint8_t (*)[64])depw;
453 check("dep-contract-mismatch-refused", ins(s, &sp3) == OMEGA_VCS_UNVERIFIED_DEPENDENCY && omega_vcstore_count(s) == 1);
454 free_store(s);
455}
456
457static void growth_checks(void) {
458 /* more objects than the old library cap (128) and one record with the maximum 256 dependencies */
459 OmegaVcStore *s = new_store();
460 int ok = 1;
461 for (int i = 999; i >= 0; i--) {
462 uint8_t id[32]; mkid2(id, (uint8_t)(1 + i / 250), (uint8_t)(i % 250 + 1));
463 if (node(s, id, NULL, 0) != 0) { ok = 0; break; }
464 }
465 size_t cnt = omega_vcstore_count(s);
466 int sorted = 1;
467 for (size_t i = 1; i < s->count; i++) if (memcmp(s->objs[i - 1].semantic_id, s->objs[i].semantic_id, 32) >= 0) sorted = 0;
468 check("store-grows-past-old-caps", ok && cnt == 1000 && sorted);
469 free_store(s);
470
471 s = new_store();
472 uint8_t (*deps)[32] = calloc(256, 32);
473 if (!deps) setup_fail("oom");
474 for (int i = 0; i < 256; i++) {
475 mkid2(deps[i], 0x20, (uint8_t)i);
476 if (node(s, deps[i], NULL, 0) != 0) setup_fail("leaf");
477 }
478 uint8_t top[32]; mkid(top, 0x50);
479 int rc = node(s, top, (const uint8_t (*)[32])deps, 256);
480 uint8_t *out = malloc(32 * 300); size_t n = 0;
481 int rc2 = omega_vcstore_closure(s, top, out, 300, &n);
482 check("closure-keeps-all-256-dependencies", rc == 0 && rc2 == 0 && n == 257 && memcmp(out + 32 * 256, top, 32) == 0);
483 free(out); free(deps); free_store(s);
484}
485
486static void closure_checks(void) {
487 diamond_ids();
488 uint8_t want[4][32];
489 memcpy(want[0], D, 32); memcpy(want[1], Bn, 32); memcpy(want[2], C, 32); memcpy(want[3], R, 32);
490
491 OmegaVcStore *a = new_store(), *b = new_store();
492 if (build_diamond(a, 0) != 0 || build_diamond(b, 1) != 0) setup_fail("diamond");
493 uint8_t oa[4 * 32], ob[4 * 32]; size_t na = 99, nb = 99;
494 int ra = omega_vcstore_closure(a, R, oa, 4, &na), rb = omega_vcstore_closure(b, R, ob, 4, &nb);
495 check("closure-order-deterministic",
496 ra == 0 && rb == 0 && na == 4 && nb == 4 && memcmp(oa, want, sizeof want) == 0 && memcmp(ob, want, sizeof want) == 0);
497 check("closure-visits-each-id-once", ra == 0 && na == 4); /* D appears once although B and C both need it */
498
499 uint8_t two[2 * 32]; size_t n2 = 0;
500 int rcap = omega_vcstore_closure(a, R, two, 2, &n2);
501 size_t n0 = 0;
502 int rcap0 = omega_vcstore_closure(a, R, NULL, 0, &n0);
503 uint8_t exact[4 * 32]; size_t n4 = 0;
504 int rex = omega_vcstore_closure(a, R, exact, 4, &n4);
505 check("closure-capacity-refused",
506 rcap == OMEGA_VCS_VCSTORE_CAPACITY && n2 == 4 && rcap0 == OMEGA_VCS_VCSTORE_CAPACITY && n0 == 4 && rex == 0 && n4 == 4);
507
508 /* the closure of a leaf is itself; an unknown id is refused */
509 uint8_t one[32]; size_t n1 = 0;
510 int rleaf = omega_vcstore_closure(a, D, one, 1, &n1);
511 uint8_t unknown[32]; mkid(unknown, 0xF7); size_t nu = 5;
512 int run = omega_vcstore_closure(a, unknown, one, 1, &nu);
513 check("closure-leaf-and-unknown", rleaf == 0 && n1 == 1 && memcmp(one, D, 32) == 0 && run == OMEGA_VCS_UNVERIFIED_DEPENDENCY && nu == 0);
514
515 /* names never take part in resolution */
516 uint8_t before[4 * 32], after[4 * 32]; size_t nbf = 0, naf = 0;
517 int r0 = omega_vcstore_closure(a, R, before, 4, &nbf);
518 int ok = omega_vcstore_name_bind(a, "alpha", D) == 0 && omega_vcstore_name_bind(a, "beta", R) == 0 &&
519 omega_vcstore_name_rebind(a, "alpha", C) == 0;
520 OmegaVcRecord *g1 = malloc(sizeof *g1), *g2 = malloc(sizeof *g2);
521 int gr1 = omega_vcstore_get(b, R, g1);
522 int r1 = omega_vcstore_closure(a, R, after, 4, &naf);
523 int gr2 = omega_vcstore_get(a, R, g2);
524 check("names-do-not-affect-resolution",
525 ok && r0 == 0 && r1 == 0 && nbf == naf && memcmp(before, after, sizeof before) == 0 &&
526 gr1 == 0 && gr2 == 0 && memcmp(g1->vc_id, g2->vc_id, 32) == 0);
527 free(g1); free(g2);
528
529 /* damage the graph in memory: drop D */
530 {
531 OmegaVcStore *m = new_store();
532 if (build_diamond(m, 0) != 0) setup_fail("diamond");
533 free(m->objs[0].bytes);
534 memmove(&m->objs[0], &m->objs[1], (m->count - 1) * sizeof m->objs[0]);
535 m->count--;
536 uint8_t o[4 * 32]; size_t n = 7;
537 int rc = omega_vcstore_closure(m, R, o, 4, &n);
538 check("closure-missing-dep-refused", rc == OMEGA_VCS_UNVERIFIED_DEPENDENCY && n == 0);
539 free_store(m);
540 }
541 /* damage the graph in memory: B is replaced by a record with another contract id */
542 {
543 OmegaVcStore *m = new_store();
544 if (build_diamond(m, 0) != 0) setup_fail("diamond");
545 Spec sp; spec_default(&sp, Bn);
546 mkid(sp.con, 0x0B); /* not the contract R requires */
547 uint8_t dd[1][64]; memcpy(dd[0], D, 32); con_of(D, dd[0] + 32);
548 sp.n_dep = 1; sp.dep = (const uint8_t (*)[64])dd;
549 B enc = { 0, 0, 0 }; encode(&enc, &sp);
550 size_t bi = 1; /* sorted: D, B, C, R */
551 free(m->objs[bi].bytes);
552 m->objs[bi].bytes = enc.p; m->objs[bi].len = enc.n;
553 omega_vc_compute_id(enc.p, enc.n, m->objs[bi].vc_id);
554 uint8_t o[4 * 32]; size_t n = 7;
555 int rc = omega_vcstore_closure(m, R, o, 4, &n);
556 check("closure-contract-mismatch-refused", rc == OMEGA_VCS_UNVERIFIED_DEPENDENCY && n == 0);
557 free_store(m);
558 }
559 /* damage the graph in memory: Y is replaced by a record that needs X, which needs Y */
560 {
561 OmegaVcStore *m = new_store();
562 uint8_t X[32], Y[32], dy[1][32];
563 mkid(X, 0x50); mkid(Y, 0x60); memcpy(dy[0], Y, 32);
564 if (node(m, Y, NULL, 0) != 0 || node(m, X, (const uint8_t (*)[32])dy, 1) != 0) setup_fail("xy");
565 Spec sp; spec_default(&sp, Y);
566 uint8_t dd[1][64]; memcpy(dd[0], X, 32); con_of(X, dd[0] + 32);
567 sp.n_dep = 1; sp.dep = (const uint8_t (*)[64])dd;
568 B enc = { 0, 0, 0 }; encode(&enc, &sp);
569 free(m->objs[1].bytes); /* sorted: X, Y */
570 m->objs[1].bytes = enc.p; m->objs[1].len = enc.n;
571 omega_vc_compute_id(enc.p, enc.n, m->objs[1].vc_id);
572 uint8_t o[2 * 32]; size_t n = 7;
573 int rc = omega_vcstore_closure(m, X, o, 2, &n);
574 check("closure-cycle-refused", rc == OMEGA_VCS_DEPENDENCY_CYCLE && n == 0);
575 free_store(m);
576 }
577 free_store(a); free_store(b);
578}
579
580static void integrity_checks(void) {
581 uint8_t sem[32]; mkid(sem, 0x11);
582 Spec sp; spec_default(&sp, sem);
583 OmegaVcStore *s = new_store();
584 if (ins(s, &sp) != 0) setup_fail("insert");
585 /* flip one bit of the source digest: still a well-formed record, but not the bytes that were hashed */
586 s->objs[0].bytes[91] ^= 1;
587 OmegaVcRecord *rec = malloc(sizeof *rec);
588 check("get-detects-corrupt-bytes", omega_vcstore_get(s, sem, rec) == OMEGA_VCS_VCSTORE_ID_MISMATCH);
589 free(rec);
590 free_store(s);
591
592 s = new_store();
593 if (ins(s, &sp) != 0) setup_fail("insert");
594 s->objs[0].semantic_id[0] ^= 1; /* the key no longer matches the record inside */
595 rec = malloc(sizeof *rec);
596 check("get-detects-key-mismatch", omega_vcstore_get(s, s->objs[0].semantic_id, rec) == OMEGA_VCS_VCSTORE_ID_MISMATCH);
597 free(rec);
598 free_store(s);
599}
600
601/* ---- digest: second implementation ---- */
602static void ref_u64(sha256_ctx *c, uint64_t v) { uint8_t b[8]; for (int i = 0; i < 8; i++) b[i] = (uint8_t)(v >> (56 - 8 * i)); sha256_update(c, b, 8); }
603typedef struct { const uint8_t *bytes; size_t len; uint8_t kind; } FObj;
604typedef struct { const char *name; const uint8_t *id; } FName;
605static int fobj_cmp(const void *x, const void *y) { return memcmp(((const FObj *)x)->bytes + 26, ((const FObj *)y)->bytes + 26, 32); }
606static void ref_digests(const FObj *objs, size_t n, const FName *names, size_t nn, uint8_t d1[32], uint8_t d2[32]) {
607 FObj *sorted = malloc((n ? n : 1) * sizeof *sorted);
608 memcpy(sorted, objs, n * sizeof *sorted);
609 qsort(sorted, n, sizeof *sorted, fobj_cmp);
610 sha256_ctx c; sha256_init(&c);
611 sha256_update(&c, (const uint8_t *)"VCS1", 4); ref_u64(&c, n);
612 for (size_t i = 0; i < n; i++) {
613 sha256_update(&c, sorted[i].bytes + 26, 32); /* semantic_id sits at offset 22 + 4 */
614 sha256_update(&c, &sorted[i].kind, 1);
615 ref_u64(&c, sorted[i].len);
616 sha256_update(&c, sorted[i].bytes, sorted[i].len);
617 }
618 sha256_final(&c, d1);
619 free(sorted);
620 sha256_init(&c);
621 sha256_update(&c, (const uint8_t *)"VCN1", 4); ref_u64(&c, nn);
622 for (size_t i = 0; i < nn; i++) {
623 size_t l = strlen(names[i].name);
624 ref_u64(&c, l); sha256_update(&c, (const uint8_t *)names[i].name, l); sha256_update(&c, names[i].id, 32);
625 }
626 sha256_final(&c, d2);
627}
628/* write a store file by hand (ascending names expected); trailer digests are the reference ones */
629static void write_file(const char *path, const FObj *objs, size_t n, const FName *names, size_t nn) {
630 B b = { 0, 0, 0 };
631 bput(&b, "AIEN_VCSTORE_V1", 15);
632 bu64(&b, n);
633 for (size_t i = 0; i < n; i++) { bput(&b, &objs[i].kind, 1); bu64(&b, objs[i].len); bput(&b, objs[i].bytes, objs[i].len); }
634 bu64(&b, nn);
635 for (size_t i = 0; i < nn; i++) { bu64(&b, strlen(names[i].name)); bput(&b, names[i].name, strlen(names[i].name)); bput(&b, names[i].id, 32); }
636 uint8_t d1[32], d2[32];
637 ref_digests(objs, n, names, nn, d1, d2);
638 bput(&b, d1, 32); bput(&b, d2, 32);
639 FILE *f = fopen(path, "wb");
640 if (!f || fwrite(b.p, 1, b.n, f) != b.n) setup_fail("write file");
641 fclose(f);
642 free(b.p);
643}
644static uint8_t *slurp(const char *path, size_t *n) {
645 FILE *f = fopen(path, "rb");
646 if (!f) setup_fail("slurp");
647 fseek(f, 0, SEEK_END); long l = ftell(f); fseek(f, 0, SEEK_SET);
648 uint8_t *p = malloc((size_t)l + 1);
649 if (fread(p, 1, (size_t)l, f) != (size_t)l) setup_fail("read");
650 fclose(f); *n = (size_t)l;
651 return p;
652}
653static void spit(const char *path, const uint8_t *p, size_t n) {
654 FILE *f = fopen(path, "wb");
655 if (!f || (n && fwrite(p, 1, n, f) != n)) setup_fail("spit");
656 fclose(f);
657}
658
659static void order_checks(void) {
660 diamond_ids();
661 /* same set, different insert orders: same digest */
662 OmegaVcStore *a = new_store(), *b = new_store(), *c = new_store();
663 uint8_t leaf[6][32];
664 for (int i = 0; i < 6; i++) mkid2(leaf[i], (uint8_t)(0x80 + i * 7), (uint8_t)(i + 1));
665 int ok = build_diamond(a, 0) == 0;
666 for (int i = 0; i < 6; i++) ok &= node(a, leaf[i], NULL, 0) == 0;
667 for (int i = 5; i >= 0; i--) ok &= node(b, leaf[i], NULL, 0) == 0;
668 ok &= build_diamond(b, 1) == 0;
669 ok &= node(c, leaf[3], NULL, 0) == 0;
670 ok &= build_diamond(c, 0) == 0;
671 for (int i = 0; i < 6; i++) if (i != 3) ok &= node(c, leaf[i], NULL, 0) == 0;
672 check("digest-independent-of-insert-order", ok && same_digest(a, b) && same_digest(a, c) && omega_vcstore_count(a) == 10);
673 free_store(a); free_store(b); free_store(c);
674}
675
676static void digest_checks(void) {
677 diamond_ids();
678 /* reference digest equals the library digest (verified and bootstrap mixed) */
679 OmegaVcStore *s = new_store();
680 uint8_t dD[1][32], dR[2][32]; memcpy(dD[0], D, 32); memcpy(dR[0], Bn, 32); memcpy(dR[1], C, 32);
681 int rc = node_kind(s, D, NULL, 0, 1); /* D is flipped to BOOTSTRAP (see flip_to_bootstrap) */
682 rc |= node(s, Bn, dD, 1); rc |= node(s, C, dD, 1); rc |= node(s, R, dR, 2);
683 if (rc) setup_fail("digest store");
684 FObj objs[4]; Spec sp; B enc[4] = { {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
685 const uint8_t *ids[4] = { D, Bn, C, R };
686 for (int i = 0; i < 4; i++) {
687 spec_default(&sp, ids[i]);
688 uint8_t (*d)[64] = calloc(2, 64);
689 if (i == 1 || i == 2) { memcpy(d[0], D, 32); con_of(D, d[0] + 32); sp.n_dep = 1; }
690 if (i == 3) { memcpy(d[0], Bn, 32); con_of(Bn, d[0] + 32); memcpy(d[1], C, 32); con_of(C, d[1] + 32); sp.n_dep = 2; }
691 sp.dep = (const uint8_t (*)[64])d;
692 encode(&enc[i], &sp);
693 free(d);
694 objs[i].bytes = enc[i].p; objs[i].len = enc[i].n; objs[i].kind = i == 0 ? 2 : 1;
695 }
696 uint8_t r1[32], r2[32], l1[32];
697 ref_digests(objs, 4, NULL, 0, r1, r2);
698 dig(s, l1);
699 check("digest-matches-reference", memcmp(r1, l1, 32) == 0);
700
701 /* the digest covers the bytes of every object */
702 OmegaVcStore *m = new_store();
703 if (build_diamond(m, 0) != 0) setup_fail("diamond");
704 uint8_t x1[32], x2[32];
705 dig(m, x1);
706 m->objs[2].bytes[91] ^= 1;
707 dig(m, x2);
708 check("digest-covers-bytes", memcmp(x1, x2, 32) != 0);
709 free_store(m);
710
711
712 /* the same record admitted as BOOTSTRAP and as VERIFIED never shares a digest */
713 OmegaVcStore *v = new_store(), *bo = new_store();
714 int rv = node_kind(v, D, NULL, 0, 0), rb = node_kind(bo, D, NULL, 0, 1);
715 uint8_t kv = 0, kb = 0;
716 omega_vcstore_admission_kind(v, D, &kv); omega_vcstore_admission_kind(bo, D, &kb);
717 check("bootstrap-kind-reported", rv == 0 && rb == 0 && kv == OMEGA_VCS_ADMISSION_VERIFIED && kb == OMEGA_VCS_ADMISSION_BOOTSTRAP);
718 check("bootstrap-vs-verified-digest-differ", rv == 0 && rb == 0 && !same_digest(v, bo));
719 free_store(v); free_store(bo);
720
721 for (int i = 0; i < 4; i++) free(enc[i].p);
722 free_store(s);
723}
724
725static void name_checks(void) {
726 diamond_ids();
727 OmegaVcStore *s = new_store();
728 if (build_diamond(s, 0) != 0) setup_fail("diamond");
729 uint8_t od0[32], od1[32], od2[32];
730 dig(s, od0);
731
732 /* many names, one id */
733 int r1 = omega_vcstore_name_bind(s, "first", D), r2 = omega_vcstore_name_bind(s, "second", D), r3 = omega_vcstore_name_bind(s, "third.v-2", D);
734 uint8_t g1[32], g2[32], g3[32];
735 int q1 = omega_vcstore_resolve_name(s, "first", g1), q2 = omega_vcstore_resolve_name(s, "second", g2), q3 = omega_vcstore_resolve_name(s, "third.v-2", g3);
736 check("name-bind-many-to-one", r1 == 0 && r2 == 0 && r3 == 0 && q1 == 0 && q2 == 0 && q3 == 0 &&
737 memcmp(g1, D, 32) == 0 && memcmp(g2, D, 32) == 0 && memcmp(g3, D, 32) == 0 && s->n_names == 3 &&
738 omega_vcstore_name_bind(s, "first", D) == 0); /* same name, same id: no-op */
739 dig(s, od1);
740 check("name-index-never-changes-object-digest", memcmp(od0, od1, 32) == 0);
741
742 /* a bound name cannot be taken over by a plain bind */
743 int rx = omega_vcstore_name_bind(s, "first", R);
744 uint8_t gx[32];
745 int qx = omega_vcstore_resolve_name(s, "first", gx);
746 check("name-no-silent-rebind", rx == OMEGA_VCS_VCSTORE_NAME_EXISTS && qx == 0 && memcmp(gx, D, 32) == 0);
747
748 /* explicit rebind works, changes the name digest, still not the object digest */
749 uint8_t n1[32], n2[32];
750 omega_vcstore_name_index_digest(s, n1);
751 int rr = omega_vcstore_name_rebind(s, "first", R);
752 int qr = omega_vcstore_resolve_name(s, "first", gx);
753 omega_vcstore_name_index_digest(s, n2);
754 dig(s, od2);
755 check("name-rebind-explicit", rr == 0 && qr == 0 && memcmp(gx, R, 32) == 0);
756 check("name-index-digest-covers-id", memcmp(n1, n2, 32) != 0 && memcmp(od0, od2, 32) == 0);
757
758 uint8_t nobody[32]; mkid(nobody, 0xF3);
759 size_t before = s->n_names;
760 check("name-rebind-unbound-refused", omega_vcstore_name_rebind(s, "never-bound", D) == OMEGA_VCS_VCSTORE_NOT_FOUND && s->n_names == before);
761 check("name-rebind-unknown-id-refused", omega_vcstore_name_rebind(s, "first", nobody) == OMEGA_VCS_UNVERIFIED_DEPENDENCY &&
762 omega_vcstore_resolve_name(s, "first", gx) == 0 && memcmp(gx, R, 32) == 0);
763 check("name-bind-unknown-id-refused", omega_vcstore_name_bind(s, "ghost", nobody) == OMEGA_VCS_UNVERIFIED_DEPENDENCY &&
764 s->n_names == before && omega_vcstore_resolve_name(s, "ghost", gx) == OMEGA_VCS_VCSTORE_NOT_FOUND);
765
766 char long129[130]; memset(long129, 'a', 129); long129[129] = 0;
767 char long128[129]; memset(long128, 'a', 128); long128[128] = 0;
768 int bad = 1;
769 const char *bads[] = { "", "1abc", "a b", "-x", ".x", "a/b", "caf\xc3\xa9", long129 };
770 for (size_t i = 0; i < sizeof bads / sizeof *bads; i++) bad &= omega_vcstore_name_bind(s, bads[i], D) == OMEGA_VCS_VCSTORE_MALFORMED;
771 check("name-invalid-refused", bad && omega_vcstore_name_bind(s, long128, D) == 0 && omega_vcstore_name_bind(s, "_ok.Name-9", D) == 0);
772
773 uint8_t rg[32]; memset(rg, 0xEE, 32);
774 int none = omega_vcstore_resolve_name(s, "does-not-exist", rg);
775 uint8_t rg2[32];
776 int found = omega_vcstore_resolve_name(s, "second", rg2);
777 check("resolve-name-returns-id", none == OMEGA_VCS_VCSTORE_NOT_FOUND && rg[0] == 0xEE && found == 0 && memcmp(rg2, D, 32) == 0);
778 free_store(s);
779
780 /* the name digest does not depend on the order names were bound */
781 OmegaVcStore *a = new_store(), *b = new_store();
782 if (build_diamond(a, 0) != 0 || build_diamond(b, 0) != 0) setup_fail("diamond");
783 omega_vcstore_name_bind(a, "zeta", D); omega_vcstore_name_bind(a, "alpha", R); omega_vcstore_name_bind(a, "mid", C);
784 omega_vcstore_name_bind(b, "mid", C); omega_vcstore_name_bind(b, "alpha", R); omega_vcstore_name_bind(b, "zeta", D);
785 uint8_t da[32], db[32];
786 omega_vcstore_name_index_digest(a, da); omega_vcstore_name_index_digest(b, db);
787 check("name-index-digest-order-independent", memcmp(da, db, 32) == 0);
788 free_store(a); free_store(b);
789}
790
791/* ---- persistence ---- */
792static char g_path[256];
793
794static void persistence_checks(void) {
795 diamond_ids();
796 snprintf(g_path, sizeof g_path, "/tmp/omega_vcstore_test_%d.bin", (int)getpid());
797 char g_path2[300]; snprintf(g_path2, sizeof g_path2, "%s.t", g_path);
798 OmegaVcStore *s = new_store();
799 uint8_t dD[1][32]; memcpy(dD[0], D, 32);
800 uint8_t dR[2][32]; memcpy(dR[0], Bn, 32); memcpy(dR[1], C, 32);
801 int rc = node(s, D, NULL, 0);
802 rc |= node(s, Bn, dD, 1); rc |= node(s, C, dD, 1); rc |= node(s, R, dR, 2);
803 rc |= omega_vcstore_name_bind(s, "root", R); rc |= omega_vcstore_name_bind(s, "leaf", D);
804 if (rc) setup_fail("persist store");
805
806 /* round trip into a store that already holds something else */
807 int sv = omega_vcstore_save(s, g_path);
808 OmegaVcStore *t = new_store();
809 uint8_t other[32]; mkid(other, 0xC4);
810 node(t, other, NULL, 0);
811 int ld = omega_vcstore_load(t, g_path);
812 uint8_t o1[32], o2[32], n1[32], n2[32];
813 dig(s, o1); dig(t, o2); omega_vcstore_name_index_digest(s, n1); omega_vcstore_name_index_digest(t, n2);
814 uint8_t rc1[32], rc2[32], res[32]; uint8_t kind = 0;
815 int a1 = omega_vcstore_receipt_of(s, R, rc1), a2 = omega_vcstore_receipt_of(t, R, rc2);
816 int a3 = omega_vcstore_resolve_name(t, "root", res);
817 int a4 = omega_vcstore_admission_kind(t, D, &kind);
818 uint8_t cl[4 * 32]; size_t ncl = 0;
819 int a5 = omega_vcstore_closure(t, R, cl, 4, &ncl);
820 check("save-load-round-trip", sv == 0 && ld == 0 && omega_vcstore_count(t) == 4 && memcmp(o1, o2, 32) == 0 && memcmp(n1, n2, 32) == 0 &&
821 a1 == 0 && a2 == 0 && memcmp(rc1, rc2, 32) == 0 && a3 == 0 && memcmp(res, R, 32) == 0 &&
822 a4 == 0 && kind == OMEGA_VCS_ADMISSION_VERIFIED && a5 == 0 && ncl == 4 && memcmp(cl, D, 32) == 0);
823 free_store(t);
824
825 size_t fn; uint8_t *file = slurp(g_path, &fn);
826
827 /* a failed load leaves the target as it was */
828 uint8_t bad1[1] = { 0 };
829 spit(g_path2, bad1, 1);
830 t = new_store();
831 node(t, other, NULL, 0);
832 uint8_t t1[32], t2[32]; dig(t, t1);
833 int lf = omega_vcstore_load(t, g_path2);
834 dig(t, t2);
835 check("load-failure-leaves-store-unchanged", lf != 0 && memcmp(t1, t2, 32) == 0 && omega_vcstore_count(t) == 1);
836 free_store(t);
837
838 /* every single-bit change and every proper prefix is refused, and refusal leaves nothing behind */
839 int all = 1;
840 for (size_t i = 0; i < fn && all; i++) {
841 file[i] ^= 1;
842 spit(g_path2, file, fn);
843 file[i] ^= 1;
844 t = new_store();
845 if (omega_vcstore_load(t, g_path2) == 0 || omega_vcstore_count(t) != 0) all = 0;
846 free_store(t);
847 }
848 for (size_t k = 0; k < fn && all; k++) {
849 spit(g_path2, file, k);
850 t = new_store();
851 if (omega_vcstore_load(t, g_path2) == 0 || omega_vcstore_count(t) != 0) all = 0;
852 free_store(t);
853 }
854 check("tamper-every-byte-and-prefix-refused", all);
855
856 /* digest trailers: flip one byte of each; the object trailer also when the record stays well-formed */
857 file[fn - 64] ^= 1; spit(g_path2, file, fn); file[fn - 64] ^= 1;
858 t = new_store();
859 int e1 = omega_vcstore_load(t, g_path2);
860 free_store(t);
861 check("tamper-object-digest-refused", e1 == OMEGA_VCS_VCSTORE_ID_MISMATCH);
862 file[fn - 1] ^= 1; spit(g_path2, file, fn); file[fn - 1] ^= 1;
863 t = new_store();
864 int e2 = omega_vcstore_load(t, g_path2);
865 free_store(t);
866 check("tamper-name-digest-refused", e2 == OMEGA_VCS_VCSTORE_ID_MISMATCH);
867
868 uint8_t *plus = malloc(fn + 1);
869 memcpy(plus, file, fn); plus[fn] = 0;
870 spit(g_path2, plus, fn + 1);
871 t = new_store();
872 int e3 = omega_vcstore_load(t, g_path2);
873 free_store(t);
874 check("trailing-byte-refused", e3 == OMEGA_VCS_VCSTORE_MALFORMED);
875 free(plus);
876 free(file);
877
878 /* a corrupt store is never written out */
879 OmegaVcStore *c = new_store();
880 uint8_t sem[32]; mkid(sem, 0x11);
881 Spec sp; spec_default(&sp, sem);
882 ins(c, &sp);
883 c->objs[0].bytes[91] ^= 1;
884 remove(g_path2);
885 int sc = omega_vcstore_save(c, g_path2);
886 check("save-refuses-corrupt-store", sc == OMEGA_VCS_VCSTORE_ID_MISMATCH && access(g_path2, F_OK) != 0);
887 free_store(c);
888
889 /* hand-built files: each checks one load refusal with correct reference digests */
890 Spec spX, spY, spYc, spA, spB;
891 uint8_t X[32], Y[32]; mkid(X, 0x50); mkid(Y, 0x60);
892 uint8_t dxy[1][64]; memcpy(dxy[0], Y, 32); con_of(Y, dxy[0] + 32);
893 uint8_t dyx[1][64]; memcpy(dyx[0], X, 32); con_of(X, dyx[0] + 32);
894 spec_default(&spX, X); spX.n_dep = 1; spX.dep = (const uint8_t (*)[64])dxy;
895 spec_default(&spYc, Y); spYc.n_dep = 1; spYc.dep = (const uint8_t (*)[64])dyx;
896 spec_default(&spY, Y);
897 spec_default(&spA, other);
898 uint8_t other2[32]; mkid(other2, 0xC9); spec_default(&spB, other2);
899 B bx = { 0, 0, 0 }, by = { 0, 0, 0 }, byc = { 0, 0, 0 }, ba = { 0, 0, 0 }, bb = { 0, 0, 0 };
900 encode(&bx, &spX); encode(&by, &spY); encode(&byc, &spYc); encode(&ba, &spA); encode(&bb, &spB);
901
902 { FObj o[2] = { { bx.p, bx.n, 1 }, { byc.p, byc.n, 1 } }; /* X -> Y -> X */
903 write_file(g_path2, o, 2, NULL, 0);
904 t = new_store(); int r = omega_vcstore_load(t, g_path2);
905 check("load-cycle-refused", r == OMEGA_VCS_DEPENDENCY_CYCLE && omega_vcstore_count(t) == 0); free_store(t); }
906 { FObj o[1] = { { bx.p, bx.n, 1 } }; /* X needs Y, Y absent */
907 write_file(g_path2, o, 1, NULL, 0);
908 t = new_store(); int r = omega_vcstore_load(t, g_path2);
909 check("load-missing-dep-refused", r == OMEGA_VCS_UNVERIFIED_DEPENDENCY && omega_vcstore_count(t) == 0); free_store(t); }
910 { FObj o[2] = { { ba.p, ba.n, 3 }, { bb.p, bb.n, 1 } }; /* admission kind 3 does not exist */
911 write_file(g_path2, o, 2, NULL, 0);
912 t = new_store(); int r = omega_vcstore_load(t, g_path2);
913 check("load-bad-kind-refused", r == OMEGA_VCS_VCSTORE_MALFORMED); free_store(t); }
914 { FObj o[2] = { { bb.p, bb.n, 1 }, { ba.p, ba.n, 1 } }; /* 0xC9 before 0xC4: not ascending */
915 write_file(g_path2, o, 2, NULL, 0);
916 t = new_store(); int r = omega_vcstore_load(t, g_path2);
917 check("load-unsorted-objects-refused", r == OMEGA_VCS_VCSTORE_MALFORMED); free_store(t); }
918 { FObj o[1] = { { ba.p, ba.n, 1 } }; /* name bound to an id that is not stored */
919 uint8_t ghost[32]; mkid(ghost, 0xF9);
920 FName nm[1] = { { "ghost", ghost } };
921 write_file(g_path2, o, 1, nm, 1);
922 t = new_store(); int r = omega_vcstore_load(t, g_path2);
923 check("load-name-unknown-id-refused", r == OMEGA_VCS_UNVERIFIED_DEPENDENCY); free_store(t); }
924 /* and a valid hand-built file does load (the reference writer and the loader agree) */
925 { FObj o[2] = { { ba.p, ba.n, 1 }, { bb.p, bb.n, 1 } };
926 write_file(g_path2, o, 2, NULL, 0);
927 t = new_store(); int r = omega_vcstore_load(t, g_path2);
928 check("load-reference-written-file", r == 0 && omega_vcstore_count(t) == 2); free_store(t); }
929 /* a file that labels an unlisted record BOOTSTRAP is refused whole, and the target store keeps what it had */
930 { FObj o[2] = { { ba.p, ba.n, 1 }, { bb.p, bb.n, 2 } };
931 write_file(g_path2, o, 2, NULL, 0);
932 t = new_store(); node(t, other, NULL, 0); uint8_t t1[32], t2[32]; dig(t, t1);
933 int r = omega_vcstore_load(t, g_path2); dig(t, t2);
934 check("load-bootstrap-unlisted-refused", !omega_genesis_contains(other2) && r == OMEGA_VCS_GENESIS_NOT_LISTED && memcmp(t1, t2, 32) == 0 && omega_vcstore_count(t) == 1); free_store(t); }
935 free(bx.p); free(by.p); free(byc.p); free(ba.p); free(bb.p);
936
937 remove(g_path); remove(g_path2);
938 free_store(s);
939}
940
941int main(int argc, char **argv) {
942 if (argc > 1) {
943 g_mutant = argv[1];
944 for (size_t i = 0; i < N_MUT; i++)
945 if (strcmp(MUTANTS[i].mutant, g_mutant) == 0) g_expect_check = MUTANTS[i].check;
946 if (!g_expect_check) { fprintf(stderr, "unknown mutant '%s'\n", g_mutant); return 2; }
947 }
948 (void)ZERO;
949 order_checks();
950 golden();
951 synthetic_refusals();
952 insert_checks();
953 growth_checks();
954 closure_checks();
955 integrity_checks();
956 digest_checks();
957 name_checks();
958 persistence_checks();
959
960 if (!g_mutant) {
961 printf("%s: %d check(s) failed\n", g_failed ? "FAIL" : "PASS", g_failed);
962 return g_failed ? 1 : 0;
963 }
964 if (g_expect_failed) { printf("MUTANT %s KILLED by %s\n", g_mutant, g_expect_check); return 1; }
965 printf("MUTANT %s SURVIVED (check %s did not fail)\n", g_mutant, g_expect_check);
966 return 3;
967}