Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/tests/test_continuity_resolve.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / tests / test_continuity_resolve.c` 897 lines ยท 34561 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_continuity_resolve.c -- host tests of svc/continuity_resolve.c (C 18 * continuity port, cut 3; contract native/kernel/CONTINUITY_RECOVERY_CONTRACT.md, 19 * aienos#222). Oracle: crates/aienos-kernel/src/continuity.rs `resolve` 20 * (:637-710), recovery_core.rs (:94-133), recovery.rs (:103, :124-140). 21 * 22 * Storage: the REAL sealed Store (native/store, M5 envelopes, anchor) in a 23 * file, at 512 and 4096 byte blocks; a fake cr_source only for the cases the 24 * real Store cannot produce (an oversize object, a read error). 25 * 26 * 1. Challenge, operator response, deferred D-1 fixture lines (byte for byte). 27 * 2. One resolve outcome per case, including the hostile ones. 28 * 3. K-5: a degraded mount through ss_open, answered by running it. 29 * Mutants (make continuity-mutants) must each turn this test FAIL. */ 30 #include 31 #include 32 #include 33 #include 34 #include 35 36 #include "ck_test.h" 37 #include "continuity_resolve_sealed.h" 38 #include "disk_file.h" 39 #include "aienos_crypto.h" 40 #include "../argus/sha256.h" 41 42 #define CK_(c, ...) \ 43 do { \ 44 ck_t_run++; \ 45 if (!(c)) { \ 46 ck_t_fail++; \ 47 printf(" FAIL %s:%d: %s: ", __FILE__, __LINE__, #c); \ 48 printf(__VA_ARGS__); \ 49 printf("\n"); \ 50 } \ 51 } while (0) 52 53 static int hexv(char c) { return c <= '9' ? c - '0' : c - 'a' + 10; } 54 static void unhex(const char *h, uint8_t *o) 55 { 56 for (size_t i = 0; h[2 * i]; i++) o[i] = (uint8_t)(hexv(h[2 * i]) << 4 | hexv(h[2 * i + 1])); 57 } 58 59 /* ------------------------------------------------------------------ */ 60 /* 1. challenge, response, fixtures */ 61 /* ------------------------------------------------------------------ */ 62 63 static uint8_t UUID[16], DIGEST[32], KEY0F[32]; 64 65 static void test_challenge_kat(void) 66 { 67 /* Rust-emitted values (fixture lines of cut 2); also recomputed with sha256sum 68 * over printf-built input (domain, uuid 5a x16, generation 7 LE, action, digest 33 x32). */ 69 uint8_t c[32], e[32]; 70 cr_challenge(UUID, 7, CR_ACTION_REPAIR_DEGRADED_PEER, DIGEST, c); 71 unhex("`7dfd4366095705ca21c81268752c14bf8961d000ed1a276d9dcf0753c0f2f4b5`", e); 72 CK_(memcmp(c, e, 32) == 0, "repair challenge KAT"); 73 cr_challenge(UUID, 7, CR_ACTION_PROVISION_IDENTITY, DIGEST, c); 74 unhex("`91fe9cff4a2afe23d343ae77c5530bed2c3115bea3df648562b6c19004b184fc`", e); 75 CK_(memcmp(c, e, 32) == 0, "provision challenge KAT"); 76 } 77 78 /* recovery.rs:206-235: KEY 00..1f, CHALLENGE a0..bf, openssl-computed answer. */ 79 static void test_operator_kat(void) 80 { 81 uint8_t key[32], ch[32], r[32], want[32]; 82 for (int i = 0; i < 32; i++) { 83 key[i] = (uint8_t)i; 84 ch[i] = (uint8_t)(0xa0 + i); 85 } 86 unhex("`f6162da5f879e9bd4defc7108c463b5d164f4db68ae991781bf798fc14907aaa`", want); 87 cr_operator_response(key, ch, r); 88 CK_(memcmp(r, want, 32) == 0, "operator response KAT (rs recovery.rs:231)"); 89 CK_(cr_operator_verify(key, ch, want) == 1, "verify accepts the KAT"); 90 91 /* rs :269: any single-bit flip of response, challenge or key is refused. */ 92 int bad = 0; 93 for (int byte = 0; byte < 32; byte++) 94 for (int bit = 0; bit < 8; bit++) { 95 uint8_t m = (uint8_t)(1u << bit), x[32], k2[32], c2[32]; 96 memcpy(x, want, 32); 97 x[byte] ^= m; 98 bad += cr_operator_verify(key, ch, x); 99 memcpy(c2, ch, 32); 100 c2[byte] ^= m; 101 bad += cr_operator_verify(key, c2, want); 102 memcpy(k2, key, 32); 103 k2[byte] ^= m; 104 bad += cr_operator_verify(k2, ch, want); 105 } 106 CK_(bad == 0, "%d bit flips accepted (MR-4: bytes 16-31 of the response)", bad); 107 108 /* rs :299, :311: bare SHA-256, un-domained HMAC and zero are refused. */ 109 uint8_t legacy[32], undom[32], z[32] = {0}; 110 sha256_ctx h; 111 sha256_init(&h); 112 sha256_update(&h, key, 32); 113 sha256_update(&h, ch, 32); 114 sha256_final(&h, legacy); 115 CK_(cr_operator_verify(key, ch, legacy) == 0, "legacy bare SHA-256 refused (MR-8)"); 116 aienos_hmac_sha256(key, ch, 32, undom); 117 CK_(cr_operator_verify(key, ch, undom) == 0, "undomained HMAC refused (MR-8)"); 118 CK_(cr_operator_verify(key, ch, z) == 0, "zero response refused"); 119 uint8_t zk[32] = {0}; 120 CK_(cr_operator_verify(zk, zk, z) == 0, "all-zero key and challenge still need the MAC"); 121 /* flipping only byte 16..31 of the response must fail on its own (MR-4). */ 122 uint8_t hi[32]; 123 memcpy(hi, want, 32); 124 hi[31] ^= 1; 125 CK_(cr_operator_verify(key, ch, hi) == 0, "flip in the last byte refused"); 126 } 127 128 static void test_challenge_binding(void) /* rs recovery_core_tests.rs:300-315 (91k) */ 129 { 130 uint8_t base[32], c[32], d2[32]; 131 cr_challenge(UUID, 7, 1, DIGEST, base); 132 cr_challenge(UUID, 7, 2, DIGEST, c); 133 CK_(memcmp(base, c, 32) != 0, "challenge binds the action (MR-3)"); 134 memcpy(d2, DIGEST, 32); 135 d2[0] ^= 1; 136 cr_challenge(UUID, 7, 1, d2, c); 137 CK_(memcmp(base, c, 32) != 0, "challenge binds the state digest (MR-2)"); 138 cr_challenge(UUID, 8, 1, DIGEST, c); 139 CK_(memcmp(base, c, 32) != 0, "challenge binds the generation"); 140 uint8_t u2[16]; 141 memcpy(u2, UUID, 16); 142 u2[15] ^= 1; 143 cr_challenge(u2, 7, 1, DIGEST, c); 144 CK_(memcmp(base, c, 32) != 0, "challenge binds the store uuid"); 145 /* An authorisation for one action does not verify for the other. */ 146 uint8_t ch1[32], ch2[32], resp1[32]; 147 cr_challenge(UUID, 7, 1, DIGEST, ch1); 148 cr_challenge(UUID, 7, 2, DIGEST, ch2); 149 cr_operator_response(KEY0F, ch1, resp1); 150 CK_(cr_operator_verify(KEY0F, ch1, resp1) == 1, "own challenge verifies"); 151 CK_(cr_operator_verify(KEY0F, ch2, resp1) == 0, "response replayed on the other action refused"); 152 CK_(cr_operator_verify(KEY0F, ch1, resp1) == 1, "and still verifies on its own"); 153 } 154 155 static void test_state_digest(void) 156 { 157 static uint8_t u0[4096], u1[4096]; 158 uint8_t a[32], b[32]; 159 memset(u0, 0xa5, sizeof u0); 160 memset(u1, 0x5a, sizeof u1); 161 cr_state_digest(u0, u1, a); 162 uint8_t both[8192]; 163 memcpy(both, u0, 4096); 164 memcpy(both + 4096, u1, 4096); 165 sha256_hash(both, sizeof both, b); 166 CK_(memcmp(a, b, 32) == 0, "state digest = SHA-256(unit0 || unit1)"); 167 cr_state_digest(u1, u0, b); 168 CK_(memcmp(a, b, 32) != 0, "unit order matters"); 169 } 170 171 /* The deferred lines of cut 2: Rust-emitted challenges and responses for the 172 * system record {uuid 5a x16, generation 7, digest 33 x32, key 0f x32}. */ 173 static int fx_checked; 174 static int test_fixture(const char *dir) 175 { 176 char path[512], line[1024]; 177 snprintf(path, sizeof path, "%s/continuity_vectors.txt", dir); 178 FILE *f = fopen(path, "r"); 179 if (!f) return 0; 180 uint8_t ch[2][32], rs[2][32]; 181 int seen = 0; 182 while (fgets(line, sizeof line, f)) { 183 char kw[32], name[64], hex[128]; 184 if (sscanf(line, "%31s %63s %127s", kw, name, hex) != 3 || strcmp(kw, "deferred")) continue; 185 uint8_t want[32], got[32]; 186 unhex(hex, want); 187 int act = strstr(name, "repair_degraded_peer") ? 1 : strstr(name, "provision_identity") ? 2 : 0; 188 if (!act) continue; 189 cr_challenge(UUID, 7, (uint8_t)act, DIGEST, ch[act - 1]); 190 cr_operator_response(KEY0F, ch[act - 1], rs[act - 1]); 191 memcpy(got, !strncmp(name, "challenge_", 10) ? ch[act - 1] : rs[act - 1], 32); 192 CK_(memcmp(got, want, 32) == 0, "fixture %s differs from the Rust oracle", name); 193 seen++; 194 fx_checked++; 195 } 196 fclose(f); 197 CK_(seen == 4, "fixture has %d deferred lines, want 4", seen); 198 return 1; 199 } 200 201 /* ------------------------------------------------------------------ */ 202 /* 2. a real sealed Store in a file */ 203 /* ------------------------------------------------------------------ */ 204 205 #define A_UNITS 4u 206 #define S_UNITS 512u 207 #define T_UNITS (A_UNITS + S_UNITS) 208 209 static ss_workspace g_ws; 210 static ss_store g_s; 211 static disk_file g_f; 212 static disk_dev g_d; 213 static st_disk g_sd; 214 static st_dev g_sdev; 215 static ts_device g_tdev; 216 static ss_keys g_k; 217 static uint32_t g_bs; 218 static char g_path[256]; 219 static struct cr_work g_w; 220 static struct cr_view g_v; 221 222 static const uint8_t RUUID[16] = {0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 223 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a}; 224 225 static int g_open; 226 static void dev_close(void) 227 { 228 if (g_open) disk_file_close(&g_f); 229 g_open = 0; 230 } 231 static int dev_open(int create) 232 { 233 dev_close(); 234 uint32_t bpu = 4096u / g_bs; 235 if (create) unlink(g_path); 236 memset(&g_f, 0, sizeof g_f); 237 if (disk_file_open(&g_f, &g_d, g_path, g_bs, (uint64_t)T_UNITS * bpu, create) != 0) return -1; 238 g_open = 1; 239 if (st_disk_bind(&g_sd, &g_d, (uint64_t)A_UNITS * bpu, S_UNITS, &g_sdev) != 0) return -2; 240 ss_ts_device(&g_d, &g_tdev); 241 return 0; 242 } 243 244 static void make_keys(void) 245 { 246 uint8_t kvol[32]; 247 memset(kvol, 0x77, 32); 248 m5_subkeys sk; 249 m5_derive_subkeys(kvol, M5_ID_TEST, 0, &sk); 250 memset(&g_k, 0, sizeof g_k); 251 g_k.identity_class = M5_ID_TEST; 252 g_k.key_generation = 1; 253 memcpy(g_k.k_root_auth, sk.k_root_auth, 32); 254 memcpy(g_k.k_domain, sk.k_artifact, 32); 255 m5_subkeys_wipe(&sk); 256 } 257 258 /* New formatted, opened Store. */ 259 static void fresh(void) 260 { 261 CK_(dev_open(1) == 0, "rig create"); 262 CK_(ss_format(&g_sdev, &g_tdev, 0, RUUID, &g_k, &g_ws) == 0, "format"); 263 CK_(ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) == 0, "open genesis"); 264 } 265 static int reopen(void) 266 { 267 if (dev_open(0) != 0) return -9999; 268 memset(&g_s, 0, sizeof g_s); 269 return ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws); 270 } 271 272 struct obj { 273 uint16_t kind, ver; 274 uint8_t b[CC_MAX_OBJECT_BYTES]; 275 size_t n; 276 uint8_t id[32]; 277 }; 278 static struct obj O[8]; 279 280 static uint8_t AG[32], AG2[32]; 281 282 static void fin(struct obj *o, uint16_t kind) 283 { 284 o->kind = kind; 285 o->ver = 1; 286 CK_(cc_object_id(kind, o->b, o->n, o->id) == CC_OK, "object id"); 287 } 288 static void mk_root(struct obj *o, const uint8_t agent[32], uint8_t source) 289 { 290 struct cc_root r; 291 memset(&r, 0, sizeof r); 292 memcpy(r.agent_id, agent, 32); 293 memcpy(r.store_uuid, RUUID, 16); 294 cc_root_branch_id(agent, r.root_branch); 295 r.provisioned_generation = 2; 296 r.source = source; 297 CK_(cc_root_encode(&r, o->b, sizeof o->b, &o->n, NULL) == CC_OK, "root encode"); 298 fin(o, CC_KIND_AGENT_ROOT); 299 } 300 static void mk_state(struct obj *o, const uint8_t agent[32], uint64_t written_at) 301 { 302 static struct cc_state s; 303 cc_state_genesis(&s, agent, written_at); 304 CK_(cc_state_encode(&s, o->b, sizeof o->b, &o->n, NULL) == CC_OK, "state encode"); 305 fin(o, CC_KIND_AGENT_STATE); 306 } 307 static void mk_man(struct obj *o, const uint8_t root[32], const uint8_t *prev, uint64_t seq, 308 uint64_t inc, const uint8_t *state, const uint8_t (*wal)[32], uint32_t nwal) 309 { 310 static struct cc_manifest m; 311 memset(&m, 0, sizeof m); 312 memcpy(m.root, root, 32); 313 if (prev) memcpy(m.previous, prev, 32); 314 m.sequence = seq; 315 m.incarnation = inc; 316 if (state) memcpy(m.agent_state, state, 32); 317 m.n_wal = nwal; 318 for (uint32_t i = 0; i < nwal; i++) memcpy(m.wal[i], wal[i], 32); 319 int rc = cc_manifest_encode(&m, o->b, sizeof o->b, &o->n, NULL); 320 CK_(rc == CC_OK, "manifest encode rc=%d", rc); 321 fin(o, CC_KIND_MANIFEST); 322 } 323 static void mk_wal(struct obj *o, uint64_t seq, const char *const *stmts, uint32_t n) 324 { 325 static struct cc_wal w; 326 memset(&w, 0, sizeof w); 327 w.sequence = seq; 328 w.n = n; 329 for (uint32_t i = 0; i < n; i++) { 330 w.rec[i].status = CC_EPI_DIRECT_OBSERVATION; 331 memset(w.rec[i].evidence_hash, (int)(0x40 + i), 32); 332 w.rec[i].len = (uint16_t)strlen(stmts[i]); 333 w.rec[i].statement = (const uint8_t *)stmts[i]; 334 } 335 CK_(cc_wal_encode(&w, o->b, sizeof o->b, &o->n, NULL) == CC_OK, "wal encode"); 336 fin(o, CC_KIND_CORTEX_WAL); 337 } 338 339 static int commit(struct obj **os, size_t n) 340 { 341 ss_object so[SS_MAX_OBJECTS]; 342 for (size_t i = 0; i < n; i++) { 343 so[i].kind = os[i]->kind; 344 so[i].version = os[i]->ver; 345 so[i].bytes = os[i]->b; 346 so[i].len = os[i]->n; 347 } 348 return ss_transact(&g_s, so, n, NULL, NULL, NULL); 349 } 350 static int commit1(struct obj *o) 351 { 352 struct obj *a[1] = {o}; 353 return commit(a, 1); 354 } 355 356 static int do_resolve(const char **why, int *rc) 357 { 358 struct cr_source src; 359 cr_bind_sealed(&src, &g_s); 360 return cr_resolve(&src, &g_w, &g_v, why, rc); 361 } 362 363 #define EXPECT(outcome, reason) \ 364 do { \ 365 const char *w_ = NULL, *r_ = (reason); \ 366 int rc_ = 0, o_ = do_resolve(&w_, &rc_); \ 367 CK_(o_ == (outcome) && (!r_ || (w_ && !strcmp(w_, r_))), \ 368 "bs=%u %s: got %s (%s), want %s (%s)", g_bs, __func__, cr_outcome_name(o_), \ 369 w_ ? w_ : "-", cr_outcome_name(outcome), r_ ? r_ : "-"); \ 370 } while (0) 371 372 /* Genesis, as provision would write it (INV-8): root, state(written_at 1), 373 * manifest 1 in one transaction. ids in O[0..3]. */ 374 static void genesis(void) 375 { 376 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 377 mk_state(&O[1], AG, 1); 378 mk_man(&O[2], O[0].id, NULL, 1, 1, O[1].id, NULL, 0); 379 struct obj *a[3] = {&O[0], &O[1], &O[2]}; 380 CK_(commit(a, 3) == 0, "commit genesis"); 381 } 382 383 static void t_unprovisioned(void) 384 { 385 fresh(); 386 CK_(reopen() == 0, "reopen"); 387 EXPECT(CR_UNPROVISIONED, NULL); 388 /* kind 19 (CortexCheckpoint) is ignored by Rust (no code reads it): still Unprovisioned. */ 389 memset(&O[7], 0, sizeof O[7]); 390 memcpy(O[7].b, "stray", 5); 391 O[7].n = 5; 392 O[7].kind = 19; 393 O[7].ver = 1; 394 CK_(commit1(&O[7]) == 0, "kind 19 write"); 395 CK_(reopen() == 0, "reopen"); 396 EXPECT(CR_UNPROVISIONED, NULL); 397 /* an unrelated application kind is ignored as well */ 398 O[7].kind = 0x0B00; 399 CK_(commit1(&O[7]) == 0, "boot kind write"); 400 CK_(reopen() == 0, "reopen"); 401 EXPECT(CR_UNPROVISIONED, NULL); 402 } 403 404 /* The memory digest of {alpha, bravo!}, computed with sha256sum outside C. */ 405 #define MEM_AB "`d6ac0e5f36b30cbac775b60eb8beccb8260390e26373cbec5613ef8f157e922c`" 406 #define MEM_EMPTY "`e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`" 407 408 static void t_resolved(void) 409 { 410 fresh(); 411 genesis(); 412 CK_(reopen() == 0, "reopen"); 413 const char *w = NULL; 414 int rc = 0, o = do_resolve(&w, &rc); 415 CK_(o == CR_RESOLVED, "genesis resolves: %s %s", cr_outcome_name(o), w ? w : "-"); 416 uint8_t want[32]; 417 CK_(memcmp(g_v.root.agent_id, AG, 32) == 0, "agent id"); 418 CK_(g_v.manifest.sequence == 1 && g_v.manifest.incarnation == 1, "sequence/incarnation"); 419 CK_(g_v.state.n == 1 && g_v.cortex_count == 0, "branches/cortex"); 420 unhex(MEM_EMPTY, want); 421 CK_(memcmp(g_v.memory, want, 32) == 0, "empty memory digest"); 422 CK_(memcmp(g_v.root_id, O[0].id, 32) == 0 && memcmp(g_v.manifest_id, O[2].id, 32) == 0, "ids"); 423 424 /* manifest 2 (+WAL seq 2, 2 records), manifest 3 (+WAL seq 3, 1 record, state rewritten) */ 425 const char *s1[] = {"alpha", "bravo!"}; 426 mk_wal(&O[3], 2, s1, 2); 427 uint8_t wl[2][32]; 428 memcpy(wl[0], O[3].id, 32); 429 mk_man(&O[4], O[0].id, O[2].id, 2, 2, O[1].id, wl, 1); 430 struct obj *a1[2] = {&O[3], &O[4]}; 431 CK_(commit(a1, 2) == 0, "commit 2"); 432 CK_(reopen() == 0, "reopen"); 433 o = do_resolve(&w, &rc); 434 CK_(o == CR_RESOLVED, "chain of 2 resolves: %s %s", cr_outcome_name(o), w ? w : "-"); 435 CK_(g_v.manifest.sequence == 2 && g_v.manifest.incarnation == 2 && g_v.cortex_count == 2, "view 2"); 436 unhex(MEM_AB, want); 437 CK_(memcmp(g_v.memory, want, 32) == 0, "memory digest of {alpha, bravo!}"); 438 439 const char *s2[] = {"charlie"}; 440 mk_wal(&O[5], 3, s2, 1); 441 memcpy(wl[1], O[5].id, 32); 442 mk_man(&O[6], O[0].id, O[4].id, 3, 3, O[1].id, wl, 2); 443 struct obj *a2[2] = {&O[5], &O[6]}; 444 CK_(commit(a2, 2) == 0, "commit 3"); 445 CK_(reopen() == 0, "reopen"); 446 o = do_resolve(&w, &rc); 447 CK_(o == CR_RESOLVED, "chain of 3 resolves: %s %s", cr_outcome_name(o), w ? w : "-"); 448 CK_(g_v.manifest.sequence == 3 && g_v.cortex_count == 3 && g_v.manifest.n_wal == 2, "view 3"); 449 CK_(memcmp(g_v.manifest_id, O[6].id, 32) == 0, "current manifest is the highest sequence"); 450 /* memory digest over all three records, streamed in commit order */ 451 uint8_t m3[32]; 452 { 453 static const char *all[] = {"alpha", "bravo!", "charlie"}; 454 sha256_ctx h; 455 sha256_init(&h); 456 for (int i = 0; i < 3; i++) { 457 uint8_t l4[4] = {(uint8_t)strlen(all[i]), 0, 0, 0}; 458 sha256_update(&h, l4, 4); 459 sha256_update(&h, (const uint8_t *)all[i], strlen(all[i])); 460 } 461 sha256_final(&h, m3); 462 } 463 CK_(memcmp(g_v.memory, m3, 32) == 0, "memory digest over three records"); 464 } 465 466 static void t_resolve_never_writes(void) 467 { 468 fresh(); 469 genesis(); 470 CK_(reopen() == 0, "reopen"); 471 static uint8_t a[T_UNITS * 4096u], b[T_UNITS * 4096u]; 472 FILE *f = fopen(g_path, "rb"); 473 CK_(f && fread(a, 1, sizeof a, f) == sizeof a, "read image"); 474 if (f) fclose(f); 475 uint64_t w0 = g_f.blocks_written, f0 = g_f.flushes; 476 EXPECT(CR_RESOLVED, NULL); 477 CK_(g_f.blocks_written == w0 && g_f.flushes == f0, "no block written, no flush"); 478 f = fopen(g_path, "rb"); 479 CK_(f && fread(b, 1, sizeof b, f) == sizeof b, "reread image"); 480 if (f) fclose(f); 481 CK_(memcmp(a, b, sizeof a) == 0, "image unchanged after resolve"); 482 } 483 484 /* INV-5 / MC-2: continuity objects with no root are Corrupt, never Unprovisioned. */ 485 static void t_orphans(void) 486 { 487 static const char *WHY = "continuity objects without an agent root"; 488 uint8_t rootid[32]; 489 memset(rootid, 0xee, 32); 490 /* orphan manifest */ 491 fresh(); 492 mk_state(&O[1], AG, 1); 493 mk_man(&O[2], rootid, NULL, 1, 1, O[1].id, NULL, 0); 494 CK_(commit1(&O[2]) == 0, "write orphan manifest"); 495 CK_(reopen() == 0, "reopen"); 496 EXPECT(CR_CORRUPT, WHY); 497 /* orphan agent state */ 498 fresh(); 499 CK_(commit1(&O[1]) == 0, "write orphan state"); 500 CK_(reopen() == 0, "reopen"); 501 EXPECT(CR_CORRUPT, WHY); 502 /* orphan WAL */ 503 fresh(); 504 const char *s[] = {"x"}; 505 mk_wal(&O[3], 1, s, 1); 506 CK_(commit1(&O[3]) == 0, "write orphan wal"); 507 CK_(reopen() == 0, "reopen"); 508 EXPECT(CR_CORRUPT, WHY); 509 } 510 511 static void t_conflict(void) /* INV-5, MC-12: two roots */ 512 { 513 fresh(); 514 genesis(); 515 mk_root(&O[5], AG2, CC_SOURCE_QUALIFICATION); 516 CK_(commit1(&O[5]) == 0, "second root"); 517 CK_(reopen() == 0, "reopen"); 518 EXPECT(CR_CONFLICT, NULL); 519 } 520 521 static void t_duplicate_id(void) /* P-3 */ 522 { 523 fresh(); 524 genesis(); 525 CK_(commit1(&O[0]) == 0, "same root bytes written twice (sealed store allows it)"); 526 CK_(reopen() == 0, "reopen"); 527 EXPECT(CR_CORRUPT, "duplicate logical object id"); 528 /* the duplicate is detected before the root count: it is not a Conflict */ 529 } 530 531 static void t_chain(void) /* INV-6, MC-4 */ 532 { 533 /* previous names a wrong id (an object that exists, but is not manifest 1) */ 534 fresh(); 535 genesis(); 536 mk_man(&O[3], O[0].id, O[1].id, 2, 2, O[1].id, NULL, 0); 537 CK_(commit1(&O[3]) == 0, "manifest 2 with wrong previous"); 538 CK_(reopen() == 0, "reopen"); 539 EXPECT(CR_CORRUPT, "manifest chain is broken"); 540 /* previous = an id that exists nowhere */ 541 fresh(); 542 genesis(); 543 uint8_t nowhere[32]; 544 memset(nowhere, 0xcd, 32); 545 mk_man(&O[3], O[0].id, nowhere, 2, 2, O[1].id, NULL, 0); 546 CK_(commit1(&O[3]) == 0, "manifest 2 with absent previous"); 547 CK_(reopen() == 0, "reopen"); 548 EXPECT(CR_CORRUPT, "manifest chain is broken"); 549 /* gap: sequence 3 after 1 */ 550 fresh(); 551 genesis(); 552 mk_man(&O[3], O[0].id, O[2].id, 3, 2, O[1].id, NULL, 0); 553 CK_(commit1(&O[3]) == 0, "manifest with sequence 3"); 554 CK_(reopen() == 0, "reopen"); 555 EXPECT(CR_CORRUPT, "manifest sequence gap or fork"); 556 /* fork: two manifests with sequence 2 */ 557 fresh(); 558 genesis(); 559 mk_man(&O[3], O[0].id, O[2].id, 2, 2, O[1].id, NULL, 0); 560 mk_man(&O[4], O[0].id, O[2].id, 2, 3, O[1].id, NULL, 0); 561 struct obj *a[2] = {&O[3], &O[4]}; 562 CK_(commit(a, 2) == 0, "two manifests with sequence 2"); 563 CK_(reopen() == 0, "reopen"); 564 EXPECT(CR_CORRUPT, "manifest sequence gap or fork"); 565 /* foreign root */ 566 fresh(); 567 genesis(); 568 uint8_t other[32]; 569 memset(other, 0x99, 32); 570 mk_man(&O[3], other, O[2].id, 2, 2, O[1].id, NULL, 0); 571 CK_(commit1(&O[3]) == 0, "manifest naming a foreign root"); 572 CK_(reopen() == 0, "reopen"); 573 EXPECT(CR_CORRUPT, "manifest names a foreign root"); 574 /* root and state but no manifest */ 575 fresh(); 576 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 577 mk_state(&O[1], AG, 1); 578 struct obj *b[2] = {&O[0], &O[1]}; 579 CK_(commit(b, 2) == 0, "root + state only"); 580 CK_(reopen() == 0, "reopen"); 581 EXPECT(CR_CORRUPT, "agent root has no manifest"); 582 } 583 584 static void t_references(void) /* INV-7: kinds, absence, order */ 585 { 586 /* manifest names a WAL object as its agent state (wrong kind) */ 587 const char *s[] = {"w"}; 588 fresh(); 589 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 590 mk_wal(&O[3], 1, s, 1); 591 mk_man(&O[2], O[0].id, NULL, 1, 1, O[3].id, NULL, 0); 592 struct obj *a[3] = {&O[0], &O[3], &O[2]}; 593 CK_(commit(a, 3) == 0, "wrong-kind state reference"); 594 CK_(reopen() == 0, "reopen"); 595 EXPECT(CR_CORRUPT, "referenced object has the wrong kind"); 596 /* WAL list names a state object (wrong kind) */ 597 fresh(); 598 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 599 mk_state(&O[1], AG, 1); 600 uint8_t wl[2][32]; 601 memcpy(wl[0], O[1].id, 32); 602 mk_man(&O[2], O[0].id, NULL, 1, 1, O[1].id, wl, 1); 603 struct obj *b[3] = {&O[0], &O[1], &O[2]}; 604 CK_(commit(b, 3) == 0, "state named as WAL"); 605 CK_(reopen() == 0, "reopen"); 606 EXPECT(CR_CORRUPT, "referenced object has the wrong kind"); 607 /* absent state */ 608 fresh(); 609 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 610 uint8_t gone[32]; 611 memset(gone, 0xab, 32); 612 mk_man(&O[2], O[0].id, NULL, 1, 1, gone, NULL, 0); 613 struct obj *c[2] = {&O[0], &O[2]}; 614 CK_(commit(c, 2) == 0, "absent state reference"); 615 CK_(reopen() == 0, "reopen"); 616 EXPECT(CR_CORRUPT, "referenced object is absent"); 617 /* zero state id */ 618 fresh(); 619 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 620 mk_man(&O[2], O[0].id, NULL, 1, 1, NULL, NULL, 0); 621 struct obj *d[2] = {&O[0], &O[2]}; 622 CK_(commit(d, 2) == 0, "no agent state"); 623 CK_(reopen() == 0, "reopen"); 624 EXPECT(CR_CORRUPT, "manifest has no agent state"); 625 /* state of another agent */ 626 fresh(); 627 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 628 mk_state(&O[1], AG2, 1); 629 mk_man(&O[2], O[0].id, NULL, 1, 1, O[1].id, NULL, 0); 630 struct obj *e[3] = {&O[0], &O[1], &O[2]}; 631 CK_(commit(e, 3) == 0, "state of another agent"); 632 CK_(reopen() == 0, "reopen"); 633 EXPECT(CR_CORRUPT, "agent state belongs to another agent"); 634 /* WAL segments out of order (sequence 2 then 1) and beyond the manifest */ 635 fresh(); 636 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 637 mk_state(&O[1], AG, 1); 638 mk_man(&O[2], O[0].id, NULL, 1, 1, O[1].id, NULL, 0); 639 mk_wal(&O[3], 2, s, 1); 640 mk_wal(&O[4], 1, s, 1); 641 memcpy(wl[0], O[3].id, 32); 642 memcpy(wl[1], O[4].id, 32); 643 mk_man(&O[5], O[0].id, O[2].id, 2, 2, O[1].id, wl, 2); 644 struct obj *f[5] = {&O[0], &O[1], &O[2], &O[3], &O[4]}; 645 CK_(commit(f, 5) == 0, "genesis + two wal"); 646 CK_(commit1(&O[5]) == 0, "manifest 2 with WAL order 2,1"); 647 CK_(reopen() == 0, "reopen"); 648 EXPECT(CR_CORRUPT, "WAL segments out of order"); 649 /* WAL sequence beyond the manifest sequence */ 650 fresh(); 651 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 652 mk_state(&O[1], AG, 1); 653 mk_wal(&O[3], 5, s, 1); 654 memcpy(wl[0], O[3].id, 32); 655 mk_man(&O[2], O[0].id, NULL, 1, 1, O[1].id, wl, 1); 656 struct obj *g[4] = {&O[0], &O[1], &O[3], &O[2]}; 657 CK_(commit(g, 4) == 0, "WAL from the future"); 658 CK_(reopen() == 0, "reopen"); 659 EXPECT(CR_CORRUPT, "WAL segments out of order"); 660 } 661 662 static void t_versions_and_decode(void) 663 { 664 /* a root stored with Store version 2 counts as a root but cannot be read as one */ 665 fresh(); 666 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 667 O[0].ver = 2; 668 CK_(commit1(&O[0]) == 0, "root v2"); 669 CK_(reopen() == 0, "reopen"); 670 EXPECT(CR_CORRUPT, "referenced object has the wrong kind"); 671 /* bad magic root */ 672 fresh(); 673 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 674 O[0].b[0] ^= 1; 675 CK_(commit1(&O[0]) == 0, "root with bad magic"); 676 CK_(reopen() == 0, "reopen"); 677 EXPECT(CR_CORRUPT, "bad magic"); 678 /* empty object: a continuity kind with zero bytes cannot have a logical id */ 679 fresh(); 680 memset(&O[6], 0, sizeof O[6]); 681 O[6].kind = CC_KIND_MANIFEST; 682 O[6].ver = 1; 683 O[6].n = 0; 684 ss_object so = {CC_KIND_MANIFEST, 1, O[6].b, 0}; 685 int rc = ss_transact(&g_s, &so, 1, NULL, NULL, NULL); 686 if (rc == 0) { 687 CK_(reopen() == 0, "reopen"); 688 EXPECT(CR_CORRUPT, "continuity object has no valid id"); 689 } else { 690 printf("note: sealed store refuses a zero-length object (rc=%d)\n", rc); 691 } 692 } 693 694 /* K-1 (contract 5.3): 16384 bytes is the sealed Store bound. The largest legal 695 * branch table (204 branches) goes through the whole pipeline; one object past 696 * the bound cannot be written to the sealed Store at all. */ 697 static void t_size_bound(void) 698 { 699 static struct cc_state s; 700 fresh(); 701 mk_root(&O[0], AG, CC_SOURCE_OPERATOR); 702 cc_state_genesis(&s, AG, 1); 703 uint8_t child[32]; 704 for (int i = 1; i < (int)CC_MAX_BRANCHES_IN_CAP; i++) 705 CK_(cc_state_fork(&s, s.br[0].id, child, NULL) == CC_OK, "fork %d", i); 706 CK_(cc_state_encode(&s, O[1].b, sizeof O[1].b, &O[1].n, NULL) == CC_OK, "encode 204 branches"); 707 CK_(O[1].n == CC_MAX_OBJECT_BYTES, "204 branches are exactly %u bytes (got %zu)", CC_MAX_OBJECT_BYTES, O[1].n); 708 fin(&O[1], CC_KIND_AGENT_STATE); 709 mk_man(&O[2], O[0].id, NULL, 1, 1, O[1].id, NULL, 0); 710 struct obj *a[3] = {&O[0], &O[1], &O[2]}; 711 CK_(commit(a, 3) == 0, "commit 16384-byte state"); 712 CK_(reopen() == 0, "reopen"); 713 EXPECT(CR_RESOLVED, NULL); 714 CK_(g_v.state.n == CC_MAX_BRANCHES_IN_CAP, "204 branches read back"); 715 716 /* one byte past the bound: the sealed Store refuses to write it */ 717 fresh(); 718 uint64_t gen0 = ss_generation(&g_s); 719 static uint8_t big[SS_MAX_PLAINTEXT + 1]; 720 memset(big, 0x41, sizeof big); 721 ss_object so = {CC_KIND_AGENT_STATE, 1, big, sizeof big}; 722 int rc = ss_transact(&g_s, &so, 1, NULL, NULL, NULL); 723 CK_(rc != 0, "sealed Store accepted %zu bytes", sizeof big); 724 CK_(ss_generation(&g_s) == gen0, "nothing committed"); 725 CK_(reopen() == 0, "reopen"); 726 CK_(ss_generation(&g_s) == gen0, "generation unchanged after reopen"); 727 EXPECT(CR_UNPROVISIONED, NULL); 728 } 729 730 /* K-5: does ss_open return 0 on a DegradedRecovery mount with a malformed 731 * peer? Answered here by running it; the result is printed and checked. */ 732 static void t_degraded(void) 733 { 734 fresh(); 735 genesis(); 736 const char *s[] = {"alpha", "bravo!"}; 737 mk_wal(&O[3], 2, s, 2); 738 uint8_t wl[1][32]; 739 memcpy(wl[0], O[3].id, 32); 740 mk_man(&O[4], O[0].id, O[2].id, 2, 2, O[1].id, wl, 1); 741 struct obj *a[2] = {&O[3], &O[4]}; 742 CK_(commit(a, 2) == 0, "commit 2"); 743 CK_(reopen() == 0, "reopen valid"); 744 EXPECT(CR_RESOLVED, NULL); 745 uint8_t agent[32], mem[32]; 746 memcpy(agent, g_v.root.agent_id, 32); 747 memcpy(mem, g_v.memory, 32); 748 struct cr_source src; 749 cr_bind_sealed(&src, &g_s); 750 CK_(g_s.st.state == ST_VALID, "valid mount"); 751 CK_(cr_writable(&src) == CR_RESOLVED, "valid mount is writable"); 752 uint32_t active = st_active_slot(&g_s.st); 753 uint64_t gen = ss_generation(&g_s); 754 755 /* Fill the inactive superblock slot (Store-region unit 1 - active) with garbage. */ 756 dev_close(); 757 FILE *f = fopen(g_path, "r+b"); 758 CK_(f != NULL, "open image"); 759 static uint8_t junk[4096]; 760 for (size_t i = 0; i < sizeof junk; i++) junk[i] = (uint8_t)(i * 37u + 11u); 761 if (f) { 762 fseek(f, (long)((A_UNITS + (1u - active)) * 4096u), SEEK_SET); 763 CK_(fwrite(junk, 1, sizeof junk, f) == sizeof junk, "write junk"); 764 fclose(f); 765 } 766 int orc = reopen(); 767 printf("K-5 answer: ss_open rc=%d, state=%d (1 = DegradedRecovery), peer=%d (2 = Malformed), " 768 "generation %llu -> %llu\n", 769 orc, g_s.st.state, g_s.st.peer, (unsigned long long)gen, 770 (unsigned long long)ss_generation(&g_s)); 771 CK_(orc == 0, "K-5: ss_open returns 0 on a degraded mount with a malformed peer"); 772 CK_(g_s.st.state == ST_DEGRADED_RECOVERY && g_s.st.peer == ST_PEER_MALFORMED, "degraded, malformed peer"); 773 CK_(ss_generation(&g_s) == gen, "same generation"); 774 cr_bind_sealed(&src, &g_s); 775 CK_(cr_writable(&src) == CR_READ_ONLY, "degraded mount is not writable (INV-11)"); 776 const char *w = NULL; 777 int rc = 0, o = do_resolve(&w, &rc); 778 CK_(o == CR_RESOLVED, "degraded mount still resolves the verified view: %s %s", cr_outcome_name(o), w ? w : "-"); 779 CK_(memcmp(g_v.root.agent_id, agent, 32) == 0 && memcmp(g_v.memory, mem, 32) == 0, "same agent and memory"); 780 CK_(g_v.manifest.incarnation == 2 && g_v.cortex_count == 2, "same view"); 781 /* The raw state digest reads the junk unit as it is (not repaired, not decoded). */ 782 uint8_t dg[32], expect[32]; 783 cr_state_digest_dev(&g_sdev, dg); 784 static uint8_t raw[2 * 4096]; 785 f = fopen(g_path, "rb"); 786 CK_(f && fseek(f, (long)(A_UNITS * 4096u), SEEK_SET) == 0 && fread(raw, 1, sizeof raw, f) == sizeof raw, 787 "read raw units"); 788 if (f) fclose(f); 789 sha256_hash(raw, sizeof raw, expect); 790 CK_(memcmp(dg, expect, 32) == 0, "cr_state_digest_dev = SHA-256 of the two raw Store units"); 791 } 792 793 /* ---- fake source: the cases the real Store cannot produce ---- */ 794 struct fake { 795 uint16_t kind[4]; 796 size_t len[4]; 797 int rc[4]; 798 uint32_t n; 799 int mount; 800 }; 801 static uint32_t fk_count(void *c) { return ((struct fake *)c)->n; } 802 static int fk_entry(void *c, uint32_t i, uint16_t *k, uint16_t *v) 803 { 804 struct fake *f = c; 805 *k = f->kind[i]; 806 *v = 1; 807 return 0; 808 } 809 static int fk_read(void *c, uint32_t i, uint8_t *out, size_t cap, size_t *len) 810 { 811 struct fake *f = c; 812 if (f->rc[i]) return f->rc[i]; 813 *len = f->len[i]; 814 memset(out, 0x55, *len < cap ? *len : cap); 815 return 0; 816 } 817 static int fk_mount(void *c) { return ((struct fake *)c)->mount; } 818 819 static void t_fake(void) 820 { 821 struct fake f; 822 memset(&f, 0, sizeof f); 823 struct cr_source src = {&f, fk_count, fk_entry, fk_read, fk_mount, NULL}; 824 const char *w = NULL; 825 int rc = 0; 826 /* oversize (K-1): Limit, never decoded, never truncated */ 827 f.n = 1; 828 f.kind[0] = CC_KIND_AGENT_STATE; 829 f.len[0] = CC_MAX_OBJECT_BYTES + 1; 830 int o = cr_resolve(&src, &g_w, &g_v, &w, &rc); 831 CK_(o == CR_LIMIT && w && !strcmp(w, "continuity object exceeds the size bound"), "oversize: %s", cr_outcome_name(o)); 832 f.len[0] = CC_MAX_OBJECT_BYTES; /* at the bound: read, then refused by the decoder as Corrupt */ 833 o = cr_resolve(&src, &g_w, &g_v, &w, &rc); 834 CK_(o == CR_CORRUPT, "bound-sized garbage is Corrupt, not Limit: %s", cr_outcome_name(o)); 835 /* read error passes through as Store */ 836 f.rc[0] = -4242; 837 o = cr_resolve(&src, &g_w, &g_v, &w, &rc); 838 CK_(o == CR_STORE && rc == -4242, "store error passes through: %s %d", cr_outcome_name(o), rc); 839 /* untracked kinds are never read */ 840 f.kind[0] = 0x0B00; 841 f.rc[0] = -1; 842 o = cr_resolve(&src, &g_w, &g_v, &w, &rc); 843 CK_(o == CR_UNPROVISIONED, "unrelated kind never read: %s", cr_outcome_name(o)); 844 /* mount state */ 845 f.mount = CR_MOUNT_DEGRADED; 846 CK_(cr_writable(&src) == CR_READ_ONLY, "degraded fake is read only"); 847 f.mount = CR_MOUNT_VALID; 848 CK_(cr_writable(&src) == CR_RESOLVED, "valid fake is writable"); 849 struct cr_source bad = src; 850 bad.mount_state = NULL; 851 CK_(cr_writable(&bad) == CR_READ_ONLY, "no mount_state fails closed"); 852 CK_(cr_resolve(NULL, &g_w, &g_v, &w, &rc) == CR_E_ARG, "NULL source"); 853 CK_(cr_resolve(&src, NULL, &g_v, &w, &rc) == CR_E_ARG, "NULL work"); 854 } 855 856 static const char *fixture_dir; 857 858 int main(int argc, char **argv) 859 { 860 memset(UUID, 0x5a, 16); 861 memset(DIGEST, 0x33, 32); 862 memset(KEY0F, 0x0f, 32); 863 memset(AG, 0x11, 32); 864 memset(AG2, 0x22, 32); 865 #ifdef CR_FIXTURE_DIR 866 fixture_dir = argc > 1 ? argv[1] : CR_FIXTURE_DIR; 867 #else 868 fixture_dir = argc > 1 ? argv[1] : NULL; 869 #endif 870 test_challenge_kat(); 871 test_operator_kat(); 872 test_challenge_binding(); 873 test_state_digest(); 874 CK_(fixture_dir && test_fixture(fixture_dir) && fx_checked == 4, "deferred fixture lines compared: %d", fx_checked); 875 876 make_keys(); 877 snprintf(g_path, sizeof g_path, "/tmp/ck_cr_test_%d.img", (int)getpid()); 878 t_fake(); 879 static const uint32_t geos[2] = {4096u, 512u}; 880 for (int g = 0; g < 2; g++) { 881 g_bs = geos[g]; 882 t_unprovisioned(); 883 t_resolved(); 884 t_resolve_never_writes(); 885 t_orphans(); 886 t_conflict(); 887 t_duplicate_id(); 888 t_chain(); 889 t_references(); 890 t_versions_and_decode(); 891 t_size_bound(); 892 t_degraded(); 893 } 894 dev_close(); 895 unlink(g_path); 896 return ck_t_verdict("test_continuity_resolve"); 897 }