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 / tests / runtime / rx_cortex_canon.c
| 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 | /* |
| 18 | * rx_cortex_canon.c -- M20 Cortex canonicalization tests. |
| 19 | * |
| 20 | * Six checks, one per behavior the M20 brief names: canonical append, |
| 21 | * provenance relation, typed recall, restart persistence, execution to |
| 22 | * Cortex through the real World, duplicate-writer prevention. |
| 23 | */ |
| 24 | #include "runtime/rx_caproot.h" |
| 25 | #include "runtime/rx_cortex.h" |
| 26 | #include "runtime/rx_cortex_record.h" |
| 27 | #include "runtime/rx_world.h" |
| 28 | |
| 29 | #include <fcntl.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/wait.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | static int g_fail, g_checks; |
| 38 | static const char *g_test; |
| 39 | |
| 40 | #define CHECK(cond, ...) do { \ |
| 41 | g_checks++; \ |
| 42 | if (!(cond)) { \ |
| 43 | g_fail++; \ |
| 44 | fprintf(stderr, " FAIL %s:%d [%s] ", __FILE__, __LINE__, g_test); \ |
| 45 | fprintf(stderr, __VA_ARGS__); \ |
| 46 | fputc('\n', stderr); \ |
| 47 | } \ |
| 48 | } while (0) |
| 49 | |
| 50 | static char g_dir[256]; |
| 51 | |
| 52 | static void path_of(char *out, size_t n, const char *name) { |
| 53 | snprintf(out, n, "%s/%s", g_dir, name); |
| 54 | } |
| 55 | |
| 56 | static CxHeader hdr(uint32_t cls, uint32_t kind, uint64_t subject, uint64_t t) { |
| 57 | CxHeader h; |
| 58 | memset(&h, 0, sizeof h); |
| 59 | h.cls = cls; |
| 60 | h.kind = kind; |
| 61 | h.subject = subject; |
| 62 | h.t = t; |
| 63 | return h; |
| 64 | } |
| 65 | |
| 66 | /* 1. canonical append ------------------------------------------------------ */ |
| 67 | static void test_append(void) { |
| 68 | g_test = "canonical append"; |
| 69 | printf("[*] %s\n", g_test); |
| 70 | CxStore s; |
| 71 | CHECK(cx_init(&s, 8) == CX_OK, "init"); |
| 72 | uint64_t pay[3] = { 7, 8, 9 }, id1 = 0, id2 = 0; |
| 73 | CxHeader h = hdr(CX_CLAIM, CX_K_CLAIM, 2, 10); |
| 74 | CHECK(cx_append(&s, &h, pay, 3, &id1) == CX_OK && id1 == 1, "first append id %llu", |
| 75 | (unsigned long long)id1); |
| 76 | h = hdr(CX_OBSERVATION, CX_K_EVIDENCE_REF, 2, 11); |
| 77 | CHECK(cx_append(&s, &h, pay, 1, &id2) == CX_OK && id2 == 2, "second append"); |
| 78 | h = hdr(CX_CLAIM, CX_K_CLAIM, 2, 5); |
| 79 | CHECK(cx_append(&s, &h, pay, 1, NULL) == CX_ERR_ORDER, "time went backwards"); |
| 80 | h = hdr(0, 0, 2, 20); |
| 81 | CHECK(cx_append(&s, &h, NULL, 0, NULL) == CX_ERR_ARG, "class 0 accepted"); |
| 82 | h = hdr(CX_CLAIM, 0, 8, 20); |
| 83 | CHECK(cx_append(&s, &h, NULL, 0, NULL) == CX_ERR_ARG, "subject out of range accepted"); |
| 84 | CHECK(s.n == 2, "append-only count %llu", (unsigned long long)s.n); |
| 85 | CHECK(cx_verify(&s, 1) == CX_OK && cx_verify_chain(&s) == CX_OK, "digests"); |
| 86 | cx_tamper(&s, 1, 0, 99); |
| 87 | CHECK(cx_verify(&s, 1) == CX_ERR_DIGEST && cx_verify_chain(&s) == CX_ERR_DIGEST, |
| 88 | "tamper not detected"); |
| 89 | cx_free(&s); |
| 90 | } |
| 91 | |
| 92 | /* 2. provenance relation ---------------------------------------------------- */ |
| 93 | static void test_provenance(void) { |
| 94 | g_test = "provenance relation"; |
| 95 | printf("[*] %s\n", g_test); |
| 96 | CxStore s; |
| 97 | cx_init(&s, 4); |
| 98 | uint64_t obs = 0, ev = 0, cand = 0, prom = 0, w = 1; |
| 99 | CxHeader h = hdr(CX_OBSERVATION, CX_K_EVIDENCE_REF, 1, 1); |
| 100 | cx_append(&s, &h, &w, 1, &obs); |
| 101 | h = hdr(CX_EVIDENCE, CX_K_EVIDENCE_REF, 1, 2); |
| 102 | h.links[0] = obs; |
| 103 | cx_append(&s, &h, &w, 1, &ev); |
| 104 | h = hdr(CX_CLAIM, CX_K_CANDIDATE, 1, 3); |
| 105 | cx_append(&s, &h, &w, 1, &cand); |
| 106 | CHECK(cx_promote(&s, 0, ev, cand, 4, NULL) == CX_ERR_ARG, "promoted evidence as candidate"); |
| 107 | CHECK(cx_promote(&s, 0, cand, obs, 4, NULL) == CX_ERR_ARG, "observation accepted as evidence"); |
| 108 | CHECK(cx_promote(&s, 0, cand, ev, 4, &prom) == CX_OK && prom == 4, "promote"); |
| 109 | const CxObject *p = cx_get(&s, prom); |
| 110 | CHECK(p && p->cls == CX_EVIDENCE && p->kind == CX_K_PROMOTION && p->links[0] == cand && |
| 111 | p->links[1] == ev && (p->protect & CX_PROT_VERIFY_EVIDENCE), "promotion record"); |
| 112 | uint64_t anc[8]; |
| 113 | uint32_t n = cx_provenance(&s, prom, anc, 8); |
| 114 | CHECK(n == 3 && anc[0] == cand && anc[1] == ev && anc[2] == obs, |
| 115 | "provenance walk n=%u", n); |
| 116 | cx_free(&s); |
| 117 | } |
| 118 | |
| 119 | /* 3. typed recall ----------------------------------------------------------- */ |
| 120 | static void test_recall(void) { |
| 121 | g_test = "typed recall"; |
| 122 | printf("[*] %s\n", g_test); |
| 123 | CxStore s; |
| 124 | cx_init(&s, 4); |
| 125 | for (uint64_t t = 1; t <= 6; t++) { |
| 126 | CxHeader h = hdr(t % 2 ? CX_CLAIM : CX_OBSERVATION, t % 2 ? CX_K_CLAIM : CX_K_EVIDENCE_REF, |
| 127 | t <= 4 ? 1 : 2, t); |
| 128 | uint64_t pay[2] = { t, t * 10 }; |
| 129 | cx_append(&s, &h, pay, 2, NULL); |
| 130 | } |
| 131 | CxRecord r[8]; |
| 132 | CxFilter f = { CX_CLAIM, CX_K_CLAIM, 0, 0 }; |
| 133 | uint32_t n = cx_recall(&s, 1, 0, UINT64_MAX, &f, r, 8); |
| 134 | CHECK(n == 2 && r[0].hdr.t == 1 && r[1].hdr.t == 3, "typed filter n=%u", n); |
| 135 | CHECK(n == 2 && r[0].verified && r[0].payload[1] == 10 && r[1].payload[1] == 30, "payload"); |
| 136 | n = cx_recall(&s, 1, 2, 3, NULL, r, 8); |
| 137 | CHECK(n == 2 && r[0].hdr.t == 2 && r[1].hdr.t == 3, "time range"); |
| 138 | n = cx_recall(&s, 1, 0, UINT64_MAX, NULL, r, 1); |
| 139 | CHECK(n == 1, "max bound"); |
| 140 | cx_tamper(&s, 3, 0, 0); |
| 141 | n = cx_recall(&s, 1, 3, 3, NULL, r, 8); |
| 142 | CHECK(n == 1 && !r[0].verified, "tampered record reported verified"); |
| 143 | CxWorldRecord wr; |
| 144 | CHECK(cx_recall_id(&s, 1, &r[0]) == CX_OK && cx_world_decode(&r[0], &wr) == CX_ERR_ARG, |
| 145 | "non-World record decoded as World record"); |
| 146 | cx_free(&s); |
| 147 | } |
| 148 | |
| 149 | /* 4. restart persistence ---------------------------------------------------- */ |
| 150 | static void test_persistence(void) { |
| 151 | g_test = "restart persistence"; |
| 152 | printf("[*] %s\n", g_test); |
| 153 | char path[512]; |
| 154 | path_of(path, sizeof path, "persist.cx"); |
| 155 | CxStore s; |
| 156 | CHECK(cx_open(&s, path, 16, CX_OPEN_SYNC) == CX_OK, "create journal"); |
| 157 | for (uint64_t t = 1; t <= 5; t++) { |
| 158 | CxHeader h = hdr(CX_CLAIM, CX_K_CLAIM, t % 3, t); |
| 159 | h.links[0] = t > 1 ? t - 1 : 0; |
| 160 | uint64_t pay[3] = { t, t + 100, t + 200 }; |
| 161 | CHECK(cx_append(&s, &h, pay, 3, NULL) == CX_OK, "append %llu", (unsigned long long)t); |
| 162 | } |
| 163 | uint8_t chain[32]; |
| 164 | memcpy(chain, s.chain, 32); |
| 165 | cx_close(&s); |
| 166 | |
| 167 | CHECK(cx_open(&s, path, 16, 0) == CX_OK, "reopen"); |
| 168 | CHECK(s.n == 5 && memcmp(s.chain, chain, 32) == 0 && cx_verify_chain(&s) == CX_OK, |
| 169 | "replayed state differs"); |
| 170 | CxRecord r; |
| 171 | CHECK(cx_recall_id(&s, 4, &r) == CX_OK && r.payload[2] == 204 && r.hdr.links[0] == 3, |
| 172 | "recalled record differs"); |
| 173 | cx_close(&s); |
| 174 | CxStore t; |
| 175 | CHECK(cx_open(&t, path, 17, 0) == CX_ERR_FORMAT, "subject count mismatch accepted"); |
| 176 | |
| 177 | /* A torn trailing record: refused, or dropped only on request. */ |
| 178 | struct stat st; |
| 179 | stat(path, &st); |
| 180 | CHECK(truncate(path, st.st_size - 5) == 0, "truncate"); |
| 181 | CHECK(cx_open(&t, path, 16, 0) == CX_ERR_TORN, "torn tail accepted silently"); |
| 182 | CHECK(cx_open(&t, path, 16, CX_OPEN_REPAIR_TAIL) == CX_OK && t.n == 4, "repair tail"); |
| 183 | cx_close(&t); |
| 184 | |
| 185 | /* A complete record with a changed byte is never repaired. */ |
| 186 | int fd = open(path, O_RDWR); |
| 187 | uint8_t b = 0; |
| 188 | off_t at = 32 + 8 * 14; /* first record, payload word 1 */ |
| 189 | CHECK(pread(fd, &b, 1, at) == 1, "read"); |
| 190 | b ^= 1; |
| 191 | CHECK(pwrite(fd, &b, 1, at) == 1, "write"); |
| 192 | close(fd); |
| 193 | CHECK(cx_open(&t, path, 16, CX_OPEN_REPAIR_TAIL) == CX_ERR_DIGEST, "tampered journal opened"); |
| 194 | } |
| 195 | |
| 196 | /* 4b. the chain is derived; opening is the integrity seam --------------------- */ |
| 197 | static void test_chain_derived(void) { |
| 198 | g_test = "chain derived at open"; |
| 199 | printf("[*] %s\n", g_test); |
| 200 | char path[512]; |
| 201 | path_of(path, sizeof path, "derived.cx"); |
| 202 | CxStore s; |
| 203 | CHECK(cx_open(&s, path, 16, 0) == CX_OK, "create journal"); |
| 204 | for (uint64_t t = 1; t <= 4; t++) { |
| 205 | CxHeader h = hdr(CX_CLAIM, CX_K_CLAIM, t % 3, t); |
| 206 | uint64_t pay[2] = { t, t + 50 }; |
| 207 | CHECK(cx_append(&s, &h, pay, 2, NULL) == CX_OK, "append %llu", (unsigned long long)t); |
| 208 | } |
| 209 | uint8_t chain[32]; |
| 210 | memcpy(chain, s.chain, 32); |
| 211 | CHECK(cx_verify_chain(&s) == CX_OK, "chain verifies before corruption"); |
| 212 | memset(s.chain, 0xA5, 32); |
| 213 | CHECK(cx_verify_chain(&s) == CX_ERR_DIGEST, "corrupt chain not detected"); |
| 214 | for (uint64_t id = 1; id <= s.n; id++) |
| 215 | CHECK(cx_verify(&s, id) == CX_OK, "record %llu digest broke with intact records", |
| 216 | (unsigned long long)id); |
| 217 | cx_close(&s); |
| 218 | |
| 219 | CHECK(cx_open(&s, path, 16, 0) == CX_OK, "reopen from disk"); |
| 220 | CHECK(s.n == 4 && memcmp(s.chain, chain, 32) == 0 && cx_verify_chain(&s) == CX_OK, |
| 221 | "reopened chain differs from the original"); |
| 222 | cx_close(&s); |
| 223 | } |
| 224 | |
| 225 | /* 5. execution -> Cortex through the real World ------------------------------ */ |
| 226 | |
| 227 | enum { SUBJ_WORKER = 1, SUBJ_EXTERNAL = 100, ISSUER = 3, RES_IN = 0x10, RES_OUT = 0x20 }; |
| 228 | |
| 229 | typedef struct { |
| 230 | RxCapRoot root; |
| 231 | RxCapAdmin admin; |
| 232 | RxWorld w; |
| 233 | RxObjRef in, out; |
| 234 | RxCapRef c_ext; |
| 235 | } Env; |
| 236 | |
| 237 | static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) { |
| 238 | RxCapMint m; |
| 239 | memset(&m, 0, sizeof m); |
| 240 | m.issuer = ISSUER; |
| 241 | m.subject = subject; |
| 242 | m.resource = resource; |
| 243 | m.rights = rights; |
| 244 | m.parent = (RxCapRef){ UINT32_MAX, 0 }; |
| 245 | m.authority = rx_capadmin_office(&e->admin); |
| 246 | RxCapRef r = { UINT32_MAX, 0 }; |
| 247 | rx_capadmin_mint(&e->admin, &m, &r); |
| 248 | return r; |
| 249 | } |
| 250 | |
| 251 | /* The work: out.field0 = 2 * in.field0. */ |
| 252 | static int fn_double(RxCtx *c) { |
| 253 | const RxObjRef *out = c->user; |
| 254 | c->out[c->n_out++] = (RxMutation){ *out, 0, c->in[0].field[0] * 2 }; |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int env_start(Env *e) { |
| 259 | memset(e, 0, sizeof *e); |
| 260 | if (rx_caproot_start(&e->root, &e->admin) != RX_CAP_OK) return -1; |
| 261 | if (rx_world_init(&e->w, &e->root, 2, 1u << 12) != RX_OK) return -1; |
| 262 | e->w.external_subject = SUBJ_EXTERNAL; |
| 263 | uint64_t init[RX_MAX_FIELDS] = { 0 }; |
| 264 | rx_world_create(&e->w, 1, RX_PERSIST_DURABLE, RES_IN, init, &e->in); |
| 265 | rx_world_create(&e->w, 1, RX_PERSIST_DURABLE, RES_OUT, init, &e->out); |
| 266 | e->c_ext = mint(e, SUBJ_EXTERNAL, RES_IN, RX_RIGHT_WRITE); |
| 267 | RxReactionDesc d; |
| 268 | memset(&d, 0, sizeof d); |
| 269 | d.name = "work.double"; |
| 270 | d.faculty = RX_FACULTY_OMEGA; |
| 271 | d.subject = SUBJ_WORKER; |
| 272 | d.priority = RX_PRIO_FOREGROUND; |
| 273 | d.fn = fn_double; |
| 274 | d.user = &e->out; |
| 275 | d.triggers[d.n_triggers++] = (RxDep){ e->in, RX_FIELD(0) }; |
| 276 | d.writes[d.n_writes++] = (RxDep){ e->out, RX_FIELD(0) }; |
| 277 | d.caps[d.n_caps++] = (RxCapNeed){ mint(e, SUBJ_WORKER, RES_IN, RX_RIGHT_READ), RES_IN, |
| 278 | RX_RIGHT_READ }; |
| 279 | d.caps[d.n_caps++] = (RxCapNeed){ mint(e, SUBJ_WORKER, RES_OUT, RX_RIGHT_WRITE), RES_OUT, |
| 280 | RX_RIGHT_WRITE }; |
| 281 | uint32_t rid; |
| 282 | return rx_world_add_reaction(&e->w, &d, &rid) == RX_OK ? 0 : -1; |
| 283 | } |
| 284 | |
| 285 | static void env_stop(Env *e) { |
| 286 | rx_world_destroy(&e->w); |
| 287 | rx_caproot_stop(&e->root, &e->admin); |
| 288 | } |
| 289 | |
| 290 | static int64_t submit(Env *e, uint64_t v) { |
| 291 | RxMutation m = { e->in, 0, v }; |
| 292 | return rx_world_publish_external(&e->w, e->c_ext, &m, 1); |
| 293 | } |
| 294 | |
| 295 | static void test_execution(void) { |
| 296 | g_test = "execution to Cortex"; |
| 297 | printf("[*] %s\n", g_test); |
| 298 | char path[512]; |
| 299 | path_of(path, sizeof path, "world.cx"); |
| 300 | CxStore s; |
| 301 | CHECK(cx_open(&s, path, RX_CORTEX_SUBJECTS, 0) == CX_OK, "open journal"); |
| 302 | Env e; |
| 303 | CHECK(env_start(&e) == 0, "world start"); |
| 304 | CHECK(rx_cortex_attach(&e.w, &s, 1) == RX_OK, "attach"); |
| 305 | int64_t ext = submit(&e, 21); |
| 306 | CHECK(ext > 0, "submit"); |
| 307 | CHECK(rx_world_wait_quiescent(&e.w, 10000) == RX_OK, "quiesce"); |
| 308 | uint64_t recs = 0, errs = 0; |
| 309 | rx_cortex_status(&e.w, &recs, &errs); |
| 310 | CHECK(errs == 0 && recs == e.w.n_crumbs, "records %llu crumbs %llu errors %llu", |
| 311 | (unsigned long long)recs, (unsigned long long)e.w.n_crumbs, (unsigned long long)errs); |
| 312 | |
| 313 | /* Accepted work. */ |
| 314 | uint64_t acc_id = rx_cortex_record_of(&e.w, (uint64_t)ext); |
| 315 | CxRecord r; |
| 316 | CxWorldRecord acc, res; |
| 317 | CHECK(acc_id && cx_recall_id(&s, acc_id, &r) == CX_OK && cx_world_decode(&r, &acc) == CX_OK && |
| 318 | acc.kind == CX_K_WORK_ACCEPTED && r.hdr.cls == CX_OBSERVATION && |
| 319 | acc.obj == e.in.id && acc.field[0] == 21, "accepted-work record"); |
| 320 | |
| 321 | /* Result, with World evidence and provenance back to the accepted work. */ |
| 322 | uint64_t res_id = rx_cortex_recall_result(&s, e.out.id, &res); |
| 323 | CHECK(res_id && res.field[0] == 42 && res.reaction != UINT32_MAX && |
| 324 | res.faculty == RX_FACULTY_OMEGA && res.session == 1, "result record"); |
| 325 | const RxCrumb *k = rx_world_crumb(&e.w, res.crumb); |
| 326 | CHECK(k && k->kind == RX_CRUMB_COMMIT && memcmp(k->digest, res.crumb_digest, 32) == 0, |
| 327 | "result does not reference the World commit crumb"); |
| 328 | CHECK(res.cause == acc_id && res.episode == (uint64_t)ext, "provenance to accepted work"); |
| 329 | uint64_t anc[8]; |
| 330 | uint32_t n = cx_provenance(&s, res_id, anc, 8); |
| 331 | int found = 0; |
| 332 | for (uint32_t i = 0; i < n; i++) found |= anc[i] == acc_id; |
| 333 | CHECK(found, "accepted work not in provenance"); |
| 334 | CHECK(cx_get(&s, res_id)->protect & CX_PROT_COMMIT_RECEIPT, "commit receipt not protected"); |
| 335 | env_stop(&e); /* releases the writer claim */ |
| 336 | uint64_t n_first = s.n; |
| 337 | cx_close(&s); |
| 338 | |
| 339 | /* Restart: a new World session on the same journal keeps the old |
| 340 | * evidence recallable and keeps appending in order. */ |
| 341 | CHECK(cx_open(&s, path, 0, 0) == CX_OK && s.n == n_first, "reopen after restart"); |
| 342 | CHECK(rx_cortex_recall_result(&s, e.out.id, &res) == res_id && res.field[0] == 42, |
| 343 | "result lost across restart"); |
| 344 | CHECK(env_start(&e) == 0 && rx_cortex_attach(&e.w, &s, 2) == RX_OK, "second session"); |
| 345 | CHECK(submit(&e, 5) > 0 && rx_world_wait_quiescent(&e.w, 10000) == RX_OK, "second submit"); |
| 346 | rx_cortex_status(&e.w, &recs, &errs); |
| 347 | CHECK(errs == 0, "second session append errors %llu", (unsigned long long)errs); |
| 348 | CHECK(rx_cortex_recall_result(&s, e.out.id, &res) > res_id && res.field[0] == 10 && |
| 349 | res.session == 2, "second session result"); |
| 350 | env_stop(&e); |
| 351 | CHECK(cx_verify_chain(&s) == CX_OK, "chain"); |
| 352 | cx_close(&s); |
| 353 | } |
| 354 | |
| 355 | /* 6. duplicate-writer prevention ------------------------------------------- */ |
| 356 | static void test_single_writer(void) { |
| 357 | g_test = "duplicate-writer prevention"; |
| 358 | printf("[*] %s\n", g_test); |
| 359 | char path[512]; |
| 360 | path_of(path, sizeof path, "single.cx"); |
| 361 | CxStore a, b; |
| 362 | CHECK(cx_open(&a, path, RX_CORTEX_SUBJECTS, 0) == CX_OK, "first writer"); |
| 363 | CHECK(cx_open(&b, path, RX_CORTEX_SUBJECTS, 0) == CX_ERR_WRITER, "second open in process"); |
| 364 | pid_t pid = fork(); |
| 365 | if (pid == 0) { |
| 366 | CxStore c; |
| 367 | _exit(cx_open(&c, path, RX_CORTEX_SUBJECTS, 0) == CX_ERR_WRITER ? 0 : 1); |
| 368 | } |
| 369 | int st = 0; |
| 370 | waitpid(pid, &st, 0); |
| 371 | CHECK(WIFEXITED(st) && WEXITSTATUS(st) == 0, "second process opened the journal for write"); |
| 372 | CHECK(cx_open(&b, path, 0, CX_OPEN_READONLY) == CX_OK, "read-only inspection"); |
| 373 | CxHeader h = hdr(CX_CLAIM, CX_K_CLAIM, 1, 1); |
| 374 | CHECK(cx_append(&b, &h, NULL, 0, NULL) == CX_ERR_WRITER, "read-only store appended"); |
| 375 | cx_close(&b); |
| 376 | |
| 377 | Env e1, e2; |
| 378 | CHECK(env_start(&e1) == 0 && env_start(&e2) == 0, "worlds"); |
| 379 | CHECK(rx_cortex_attach(&e1.w, &a, 1) == RX_OK, "attach one"); |
| 380 | CHECK(rx_cortex_attach(&e2.w, &a, 2) == RX_ERR_IDENTITY, "second World attached"); |
| 381 | CHECK(cx_append(&a, &h, NULL, 0, NULL) == CX_ERR_WRITER, "bypass write while attached"); |
| 382 | CHECK(cx_claim_writer(&a, 12345) == CX_ERR_WRITER, "second claim"); |
| 383 | CHECK(rx_cortex_attach(&e1.w, &a, 3) == RX_ERR_EXISTS, "double attach"); |
| 384 | CHECK(rx_cortex_detach(&e1.w) == RX_OK, "detach"); |
| 385 | CHECK(rx_cortex_attach(&e2.w, &a, 2) == RX_OK, "attach after detach"); |
| 386 | env_stop(&e1); |
| 387 | env_stop(&e2); /* destroy releases */ |
| 388 | CHECK(a.writer == 0, "claim not released by destroy"); |
| 389 | h.t = a.n + 1; |
| 390 | CHECK(cx_append(&a, &h, NULL, 0, NULL) == CX_OK, "append after release"); |
| 391 | cx_close(&a); |
| 392 | } |
| 393 | |
| 394 | int main(void) { |
| 395 | snprintf(g_dir, sizeof g_dir, "/tmp/rx_cortex_canon.XXXXXX"); |
| 396 | if (!mkdtemp(g_dir)) { perror("mkdtemp"); return 2; } |
| 397 | test_append(); |
| 398 | test_provenance(); |
| 399 | test_recall(); |
| 400 | test_persistence(); |
| 401 | test_chain_derived(); |
| 402 | test_execution(); |
| 403 | test_single_writer(); |
| 404 | char cmd[300]; |
| 405 | snprintf(cmd, sizeof cmd, "rm -rf '%s'", g_dir); |
| 406 | if (system(cmd) != 0) fprintf(stderr, "cleanup of %s failed\n", g_dir); |
| 407 | printf("%d checks, %d failures\n", g_checks, g_fail); |
| 408 | printf(g_fail ? "M20 CORTEX TESTS: FAIL\n" : "M20 CORTEX TESTS: PASS\n"); |
| 409 | return g_fail ? 1 : 0; |
| 410 | } |