aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tests / test_continuity_subject_provision.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 | /* test_continuity_subject_provision.c -- host tests of |
| 18 | * svc/continuity_subject_provision.c (ALLEN genesis at identity provisioning; |
| 19 | * ARCH-0035 / OS-0018, PROPOSED) over the REAL sealed Store in a file with the |
| 20 | * real ck_rng over a mocked RNDR. |
| 21 | * |
| 22 | * genesis cs_provision writes identity + subject in ONE transaction of |
| 23 | * four objects; reopened, both resolve; the subject is |
| 24 | * sequence 1, bound to this root and agent, lineage and |
| 25 | * provenance as requested |
| 26 | * exactly once a second cs_provision is CR_ALREADY_PROVISIONED and writes |
| 27 | * nothing; a standing intent then advances the chain (seq 2) |
| 28 | * refusals zero provenance / lineage, bad origin, subject objects |
| 29 | * without a root, a failing Store: nothing written |
| 30 | * no minting a Store provisioned WITHOUT a subject (cr_provision alone) |
| 31 | * resolves ABSENT and nothing here writes one |
| 32 | * power cuts a cut at every Store checkpoint and at every block boundary |
| 33 | * of the provisioning write leaves exactly the old state |
| 34 | * (unprovisioned, no subject object) or exactly the new one |
| 35 | * (identity WITH its genesis subject); never one without the |
| 36 | * other |
| 37 | * Mutant (Makefile continuity-subject-provision-mutants): |
| 38 | * CS_MUTANT_PROVISION_SPLIT_TXN subject in a second transaction */ |
| 39 | #include <stdint.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <unistd.h> |
| 44 | |
| 45 | #include "ck_test.h" |
| 46 | #include "continuity_resolve_sealed.h" |
| 47 | #include "continuity_subject_provision.h" |
| 48 | #include "disk_file.h" |
| 49 | #include "../argus/sha256.h" |
| 50 | |
| 51 | #define CK_(c, ...) \ |
| 52 | do { \ |
| 53 | ck_t_run++; \ |
| 54 | if (!(c)) { \ |
| 55 | ck_t_fail++; \ |
| 56 | printf(" FAIL %s:%d: %s: ", __FILE__, __LINE__, #c); \ |
| 57 | printf(__VA_ARGS__); \ |
| 58 | printf("\n"); \ |
| 59 | } \ |
| 60 | } while (0) |
| 61 | |
| 62 | #define A_UNITS 4u |
| 63 | #define S_UNITS 512u |
| 64 | #define IMG_BYTES ((size_t)(A_UNITS + S_UNITS) * 4096u) |
| 65 | |
| 66 | static ss_workspace g_ws; |
| 67 | static ss_store g_s; |
| 68 | static disk_file g_f; |
| 69 | static disk_dev g_d; |
| 70 | static st_disk g_sd; |
| 71 | static st_dev g_sdev; |
| 72 | static ts_device g_tdev; |
| 73 | static ss_keys g_k; |
| 74 | static uint32_t g_bs; |
| 75 | static char g_path[256]; |
| 76 | static struct cr_work g_w; |
| 77 | static struct cr_txwork g_tw; |
| 78 | static struct cr_view g_v, g_v2; |
| 79 | static struct cr_source g_src; |
| 80 | static struct cr_sink g_snk; |
| 81 | static struct cr_sealed_sink g_sk; |
| 82 | static struct cs_subject g_subj, g_subj2; |
| 83 | static uint8_t g_sid[32], g_sid2[32]; |
| 84 | static const uint8_t RUUID[16] = {0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, |
| 85 | 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a}; |
| 86 | static uint8_t base_img[IMG_BYTES], snap_img[IMG_BYTES]; |
| 87 | static struct cs_genesis G; |
| 88 | |
| 89 | static int g_open; |
| 90 | static void dev_close(void) |
| 91 | { |
| 92 | if (g_open) disk_file_close(&g_f); |
| 93 | g_open = 0; |
| 94 | } |
| 95 | static int dev_open(int create) |
| 96 | { |
| 97 | dev_close(); |
| 98 | uint32_t bpu = 4096u / g_bs; |
| 99 | if (create) unlink(g_path); |
| 100 | memset(&g_f, 0, sizeof g_f); |
| 101 | if (disk_file_open(&g_f, &g_d, g_path, g_bs, (uint64_t)(A_UNITS + S_UNITS) * bpu, create) != 0) return -1; |
| 102 | g_open = 1; |
| 103 | if (st_disk_bind(&g_sd, &g_d, (uint64_t)A_UNITS * bpu, S_UNITS, &g_sdev) != 0) return -2; |
| 104 | ss_ts_device(&g_d, &g_tdev); |
| 105 | return 0; |
| 106 | } |
| 107 | static void make_keys(void) |
| 108 | { |
| 109 | uint8_t kvol[32]; |
| 110 | memset(kvol, 0x77, 32); |
| 111 | m5_subkeys sk; |
| 112 | m5_derive_subkeys(kvol, M5_ID_TEST, 0, &sk); |
| 113 | memset(&g_k, 0, sizeof g_k); |
| 114 | g_k.identity_class = M5_ID_TEST; |
| 115 | g_k.key_generation = 1; |
| 116 | memcpy(g_k.k_root_auth, sk.k_root_auth, 32); |
| 117 | memcpy(g_k.k_domain, sk.k_artifact, 32); |
| 118 | m5_subkeys_wipe(&sk); |
| 119 | } |
| 120 | |
| 121 | /* Counting sink in front of the sealed sink: how many Store transactions |
| 122 | * the operation made, and how many objects the last one carried. */ |
| 123 | static int g_tx, g_tx_objs, g_tx_kind24, g_fail_store; |
| 124 | static int count_transact(void *ctx, const struct cr_wobj *objs, size_t n) |
| 125 | { |
| 126 | struct cr_sink *inner = ctx; |
| 127 | g_tx++; |
| 128 | g_tx_objs = (int)n; |
| 129 | g_tx_kind24 = 0; |
| 130 | for (size_t i = 0; i < n; i++) g_tx_kind24 += objs[i].kind == CC_KIND_SUBJECT; |
| 131 | if (g_fail_store) return -42; |
| 132 | return inner->transact(inner->ctx, objs, n); |
| 133 | } |
| 134 | static struct cr_sink g_sealed, g_count; |
| 135 | static void bind(st_hook hook) |
| 136 | { |
| 137 | g_sk.s = &g_s; |
| 138 | g_sk.hook = hook; |
| 139 | g_sk.hook_arg = NULL; |
| 140 | cr_bind_sealed(&g_src, &g_s); |
| 141 | cr_bind_sealed_sink(&g_sealed, &g_sk); |
| 142 | g_count.ctx = &g_sealed; |
| 143 | g_count.transact = count_transact; |
| 144 | g_snk = g_count; |
| 145 | g_tx = g_tx_objs = g_tx_kind24 = 0; |
| 146 | } |
| 147 | static void fresh(void) |
| 148 | { |
| 149 | CK_(dev_open(1) == 0, "rig create"); |
| 150 | CK_(ss_format(&g_sdev, &g_tdev, 0, RUUID, &g_k, &g_ws) == 0, "format"); |
| 151 | CK_(ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) == 0, "open genesis"); |
| 152 | bind(NULL); |
| 153 | } |
| 154 | static int reopen(void) |
| 155 | { |
| 156 | if (dev_open(0) != 0) return -9999; |
| 157 | memset(&g_s, 0, sizeof g_s); |
| 158 | int rc = ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws); |
| 159 | bind(NULL); |
| 160 | return rc; |
| 161 | } |
| 162 | static int file_load(uint8_t *dst) |
| 163 | { |
| 164 | FILE *f = fopen(g_path, "rb"); |
| 165 | if (!f) return -1; |
| 166 | size_t n = fread(dst, 1, IMG_BYTES, f); |
| 167 | fclose(f); |
| 168 | return n == IMG_BYTES ? 0 : -2; |
| 169 | } |
| 170 | static int file_store(const uint8_t *src) |
| 171 | { |
| 172 | FILE *f = fopen(g_path, "wb"); |
| 173 | if (!f) return -1; |
| 174 | size_t n = fwrite(src, 1, IMG_BYTES, f); |
| 175 | fclose(f); |
| 176 | return n == IMG_BYTES ? 0 : -2; |
| 177 | } |
| 178 | static void snapshot(void) |
| 179 | { |
| 180 | dev_close(); |
| 181 | CK_(file_load(snap_img) == 0, "snapshot"); |
| 182 | CK_(reopen() == 0, "reopen after snapshot"); |
| 183 | } |
| 184 | static int unchanged(void) |
| 185 | { |
| 186 | static uint8_t now[IMG_BYTES]; |
| 187 | dev_close(); |
| 188 | int same = file_load(now) == 0 && memcmp(now, snap_img, IMG_BYTES) == 0; |
| 189 | (void)reopen(); |
| 190 | return same; |
| 191 | } |
| 192 | |
| 193 | /* ---- mocked RNDR ---- */ |
| 194 | struct mock { |
| 195 | int present; |
| 196 | uint64_t ctr; |
| 197 | }; |
| 198 | static struct mock g_m; |
| 199 | static struct ck_rng_ops g_ops; |
| 200 | static struct ck_rng g_rng; |
| 201 | static int m_present(void *c) { return ((struct mock *)c)->present; } |
| 202 | static int m_read(void *c, uint64_t *out) |
| 203 | { |
| 204 | struct mock *m = c; |
| 205 | uint64_t z = (m->ctr += 0x9e3779b97f4a7c15ull); |
| 206 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; |
| 207 | z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; |
| 208 | *out = z ^ (z >> 31); |
| 209 | return 1; |
| 210 | } |
| 211 | static void rng_init(uint64_t seed) |
| 212 | { |
| 213 | g_m.present = 1; |
| 214 | g_m.ctr = seed; |
| 215 | g_ops.present = m_present; |
| 216 | g_ops.read = m_read; |
| 217 | g_ops.ctx = &g_m; |
| 218 | memset(&g_rng, 0, sizeof g_rng); |
| 219 | CK_(ck_rng_probe(&g_rng, &g_ops) == CK_RNG_OK, "rng probe"); |
| 220 | } |
| 221 | |
| 222 | static int do_cs_provision(const char **why, int *rc) |
| 223 | { |
| 224 | return cs_provision(&g_src, &g_snk, &g_w, &g_tw, &g_rng, RUUID, CC_SOURCE_QUALIFICATION, &G, &g_v, |
| 225 | &g_subj, g_sid, why, rc); |
| 226 | } |
| 227 | static uint32_t count_kind24(void) |
| 228 | { |
| 229 | uint32_t n = g_src.count(g_src.ctx), k = 0; |
| 230 | for (uint32_t i = 0; i < n; i++) { |
| 231 | uint16_t kind = 0, ver = 0; |
| 232 | if (g_src.entry(g_src.ctx, i, &kind, &ver) == 0 && kind == CC_KIND_SUBJECT) k++; |
| 233 | } |
| 234 | return k; |
| 235 | } |
| 236 | |
| 237 | /* ---- 1. genesis ---- */ |
| 238 | static void t_genesis(void) |
| 239 | { |
| 240 | const char *why = NULL; |
| 241 | int rc = 0; |
| 242 | fresh(); |
| 243 | rng_init(11); |
| 244 | int o = do_cs_provision(&why, &rc); |
| 245 | CK_(o == CR_RESOLVED, "cs_provision: %s (%s rc=%d)", cr_outcome_name(o), why ? why : "-", rc); |
| 246 | CK_(g_tx == 1, "exactly one Store transaction (%d)", g_tx); |
| 247 | CK_(g_tx_objs == 4 && g_tx_kind24 == 1, "the transaction carries AgentRoot, AgentState, Manifest and the subject (%d objects, %d subject)", |
| 248 | g_tx_objs, g_tx_kind24); |
| 249 | CK_(g_subj.sequence == 1 && sv1_all_zero(g_subj.previous, 32), "genesis is sequence 1 with no previous"); |
| 250 | CK_(memcmp(g_subj.root, g_v.root_id, 32) == 0 && memcmp(g_subj.agent, g_v.root.agent_id, 32) == 0, |
| 251 | "subject bound to this root and agent"); |
| 252 | CK_(memcmp(g_subj.cortex, G.cortex, 32) == 0, "memory lineage as requested"); |
| 253 | CK_(memcmp(g_subj.provenance, G.provenance, 32) == 0 && g_subj.origin == CS_ORIGIN_OPERATOR, "provenance and origin"); |
| 254 | CK_(g_subj.n_intents == 0 && g_subj.n_knowledge == 0, "no intents, no knowledge at genesis"); |
| 255 | |
| 256 | /* Another process: reopen the image and resolve both chains. */ |
| 257 | dev_close(); |
| 258 | CK_(reopen() == 0, "reopen"); |
| 259 | CK_(cr_resolve(&g_src, &g_w, &g_v2, &why, &rc) == CR_RESOLVED, "identity resolves after reopen"); |
| 260 | CK_(memcmp(g_v2.root.agent_id, g_v.root.agent_id, 32) == 0, "same agent"); |
| 261 | CK_(cs_resolve(&g_src, &g_w, &g_v2, &g_subj2, g_sid2, &why, &rc) == CS_RESOLVED, "subject resolves: %s", why ? why : "-"); |
| 262 | CK_(memcmp(g_sid2, g_sid, 32) == 0 && g_subj2.sequence == 1, "same subject object, sequence 1"); |
| 263 | CK_(count_kind24() == 1, "exactly one subject object in the Store"); |
| 264 | } |
| 265 | |
| 266 | /* ---- 2. exactly once, then a standing intent ---- */ |
| 267 | static void t_exactly_once(void) |
| 268 | { |
| 269 | const char *why = NULL; |
| 270 | int rc = 0; |
| 271 | fresh(); |
| 272 | rng_init(12); |
| 273 | CK_(do_cs_provision(&why, &rc) == CR_RESOLVED, "provision"); |
| 274 | snapshot(); |
| 275 | rng_init(13); |
| 276 | int o = do_cs_provision(&why, &rc); |
| 277 | CK_(o == CR_ALREADY_PROVISIONED, "second provisioning refused: %s", cr_outcome_name(o)); |
| 278 | CK_(g_tx == 0, "nothing sent to the Store"); |
| 279 | CK_(unchanged(), "image unchanged"); |
| 280 | CK_(count_kind24() == 1, "still one subject object"); |
| 281 | |
| 282 | /* The standing intent through the ALLEN commit path. */ |
| 283 | CK_(cr_resolve(&g_src, &g_w, &g_v2, &why, &rc) == CR_RESOLVED, "resolve"); |
| 284 | CK_(cs_resolve(&g_src, &g_w, &g_v2, &g_subj2, g_sid2, &why, &rc) == CS_RESOLVED, "subject"); |
| 285 | uint8_t prov[32], iid[32]; |
| 286 | memset(prov, 0x31, 32); |
| 287 | cs_subject_advance(&g_subj2, g_sid2, prov, CS_ORIGIN_OPERATOR); |
| 288 | const uint64_t p[2] = {7, 1000}; |
| 289 | CK_(cs_subject_intend(&g_subj2, CS_INTENT_GOAL_LATENCY, p, iid, &why) == CC_OK, "intend"); |
| 290 | CK_(cs_commit(&g_src, &g_snk, &g_w, &g_v2, &g_subj2, g_sid, &why, &rc) == CS_RESOLVED, "commit seq 2: %s", why ? why : "-"); |
| 291 | dev_close(); |
| 292 | CK_(reopen() == 0, "reopen"); |
| 293 | CK_(cr_resolve(&g_src, &g_w, &g_v2, &why, &rc) == CR_RESOLVED, "resolve after reopen"); |
| 294 | CK_(cs_resolve(&g_src, &g_w, &g_v2, &g_subj, g_sid2, &why, &rc) == CS_RESOLVED, "subject after reopen"); |
| 295 | CK_(g_subj.sequence == 2 && memcmp(g_sid2, g_sid, 32) == 0, "head is the sequence-2 object"); |
| 296 | const struct cs_intent *a = cs_subject_active(&g_subj, CS_INTENT_GOAL_LATENCY, 7); |
| 297 | CK_(a && memcmp(a->id, iid, 32) == 0 && a->payload[1] == 1000, "standing intent survives reopen"); |
| 298 | CK_(count_kind24() == 2, "two subject objects: genesis and one successor"); |
| 299 | } |
| 300 | |
| 301 | /* ---- 3. refusals: nothing written ---- */ |
| 302 | static void t_refusals(void) |
| 303 | { |
| 304 | const char *why = NULL; |
| 305 | int rc = 0; |
| 306 | struct cs_genesis keep = G; |
| 307 | fresh(); |
| 308 | snapshot(); |
| 309 | rng_init(21); |
| 310 | memset(G.provenance, 0, 32); |
| 311 | CK_(do_cs_provision(&why, &rc) == CR_E_ARG && why && !strcmp(why, "subject genesis needs a provenance"), "zero provenance"); |
| 312 | G = keep; |
| 313 | memset(G.cortex, 0, 32); |
| 314 | CK_(do_cs_provision(&why, &rc) == CR_E_ARG && why && !strcmp(why, "subject genesis needs a memory lineage reference"), "zero lineage"); |
| 315 | G = keep; |
| 316 | G.origin = 9; |
| 317 | CK_(do_cs_provision(&why, &rc) == CR_E_ARG, "bad origin"); |
| 318 | G = keep; |
| 319 | CK_(cs_provision(&g_src, &g_snk, &g_w, &g_tw, &g_rng, RUUID, CC_SOURCE_QUALIFICATION, NULL, &g_v, &g_subj, g_sid, &why, &rc) == CR_E_ARG, |
| 320 | "null genesis"); |
| 321 | G = keep; |
| 322 | fresh(); |
| 323 | snapshot(); |
| 324 | rng_init(22); |
| 325 | g_fail_store = 1; |
| 326 | int o = do_cs_provision(&why, &rc); |
| 327 | g_fail_store = 0; |
| 328 | CK_(o == CR_STORE, "failing Store: %s", cr_outcome_name(o)); |
| 329 | CK_(unchanged(), "failing Store: image unchanged"); |
| 330 | CK_(cr_resolve(&g_src, &g_w, &g_v2, &why, &rc) == CR_UNPROVISIONED && count_kind24() == 0, "still unprovisioned, no subject"); |
| 331 | |
| 332 | /* A subject object with no agent root is never provisioned over. */ |
| 333 | fresh(); |
| 334 | { |
| 335 | static struct cs_subject f; |
| 336 | static uint8_t b[CS_MAX_BYTES]; |
| 337 | size_t len = 0; |
| 338 | memset(&f, 0, sizeof f); |
| 339 | memset(f.root, 0x44, 32); |
| 340 | memset(f.agent, 0x45, 32); |
| 341 | f.sequence = 1; |
| 342 | memset(f.provenance, 0x46, 32); |
| 343 | f.origin = CS_ORIGIN_OPERATOR; |
| 344 | memset(f.cortex, 0x47, 32); |
| 345 | CK_(cs_subject_encode(&f, b, sizeof b, &len, &why) == CC_OK, "encode orphan"); |
| 346 | struct cr_wobj ob = {CC_KIND_SUBJECT, CC_STORE_OBJECT_VERSION, b, len}; |
| 347 | CK_(g_sealed.transact(g_sealed.ctx, &ob, 1) == 0, "plant orphan subject"); |
| 348 | } |
| 349 | snapshot(); |
| 350 | rng_init(23); |
| 351 | o = do_cs_provision(&why, &rc); |
| 352 | CK_(o == CR_CORRUPT && why && !strcmp(why, "subject objects without an agent root"), "orphan subject: %s (%s)", |
| 353 | cr_outcome_name(o), why ? why : "-"); |
| 354 | CK_(g_tx == 0 && unchanged(), "orphan subject: nothing written"); |
| 355 | } |
| 356 | |
| 357 | /* ---- 4. a Store provisioned without a subject resolves ABSENT ---- */ |
| 358 | static void t_absent_never_minted(void) |
| 359 | { |
| 360 | const char *why = NULL; |
| 361 | int rc = 0; |
| 362 | fresh(); |
| 363 | rng_init(31); |
| 364 | CK_(cr_provision(&g_src, &g_snk, &g_w, &g_tw, &g_rng, RUUID, CC_SOURCE_QUALIFICATION, &g_v, &why, &rc) == CR_RESOLVED, |
| 365 | "identity-only provisioning"); |
| 366 | snapshot(); |
| 367 | CK_(cs_resolve(&g_src, &g_w, &g_v, &g_subj, g_sid, &why, &rc) == CS_ABSENT, "subject ABSENT"); |
| 368 | rng_init(32); |
| 369 | CK_(do_cs_provision(&why, &rc) == CR_ALREADY_PROVISIONED, "cs_provision does not add a subject to a provisioned identity"); |
| 370 | CK_(unchanged() && count_kind24() == 0, "nothing written; still no subject object"); |
| 371 | } |
| 372 | |
| 373 | /* ---- 5. power cuts during the provisioning write ---- */ |
| 374 | static int g_cp_target, g_cp_fired; |
| 375 | static void cp_hook(void *arg, int cp) |
| 376 | { |
| 377 | (void)arg; |
| 378 | if (cp == g_cp_target && !g_cp_fired) { |
| 379 | g_cp_fired = 1; |
| 380 | disk_file_arm_cut(&g_f, 0); |
| 381 | } |
| 382 | } |
| 383 | enum { CUT_CHECKPOINTS = 9 }; |
| 384 | static const int CPS[CUT_CHECKPOINTS] = {ST_CP_BEFORE_FIRST_WRITE, ST_CP_AFTER_PAYLOAD_OBJECTS, ST_CP_AFTER_CATALOG, |
| 385 | ST_CP_AFTER_COMMIT_RECORD, ST_CP_AFTER_FIRST_FLUSH, |
| 386 | ST_CP_AFTER_INACTIVE_SUPERBLOCK, ST_CP_AFTER_FINAL_FLUSH, |
| 387 | SS_CP_BEFORE_ANCHOR, SS_CP_AFTER_ANCHOR}; |
| 388 | static const char *cp_name(int cp) |
| 389 | { |
| 390 | return cp >= 100 ? (cp == SS_CP_BEFORE_ANCHOR ? "before_anchor" : "after_anchor") : st_checkpoint_name(cp); |
| 391 | } |
| 392 | static uint8_t g_agent_new[32], g_sid_new[32]; |
| 393 | |
| 394 | /* 1 = old (unprovisioned, no subject object), 2 = new (identity + its |
| 395 | * genesis subject, the same objects as the uncut run), 0 = anything else. */ |
| 396 | static int classify(const char *what) |
| 397 | { |
| 398 | const char *why = NULL; |
| 399 | int rc = reopen(); |
| 400 | CK_(rc == 0, "%s: reopen after the cut: %d", what, rc); |
| 401 | if (rc) return 0; |
| 402 | int o = cr_resolve(&g_src, &g_w, &g_v2, &why, &rc); |
| 403 | if (o == CR_UNPROVISIONED) { |
| 404 | uint32_t k = count_kind24(); |
| 405 | CK_(k == 0, "%s: unprovisioned but %u subject objects", what, k); |
| 406 | return k == 0 ? 1 : 0; |
| 407 | } |
| 408 | if (o != CR_RESOLVED) { |
| 409 | CK_(0, "%s: identity %s (%s)", what, cr_outcome_name(o), why ? why : "-"); |
| 410 | return 0; |
| 411 | } |
| 412 | int s = cs_resolve(&g_src, &g_w, &g_v2, &g_subj2, g_sid2, &why, &rc); |
| 413 | int ok = s == CS_RESOLVED && g_subj2.sequence == 1 && memcmp(g_sid2, g_sid_new, 32) == 0 && |
| 414 | memcmp(g_v2.root.agent_id, g_agent_new, 32) == 0 && count_kind24() == 1; |
| 415 | CK_(ok, "%s: identity present but subject %s (%s)", what, s == CS_ABSENT ? "ABSENT" : cr_outcome_name(s), |
| 416 | why ? why : "-"); |
| 417 | return ok ? 2 : 0; |
| 418 | } |
| 419 | |
| 420 | static void t_power_cuts(void) |
| 421 | { |
| 422 | const char *why = NULL; |
| 423 | int rc = 0, old = 0, neu = 0, bad = 0; |
| 424 | fresh(); |
| 425 | dev_close(); |
| 426 | CK_(file_load(base_img) == 0, "save base"); |
| 427 | CK_(reopen() == 0, "reopen base"); |
| 428 | uint64_t w0 = g_f.blocks_written; |
| 429 | rng_init(777); |
| 430 | CK_(do_cs_provision(&why, &rc) == CR_RESOLVED, "uncut provisioning"); |
| 431 | uint64_t W = g_f.blocks_written - w0; |
| 432 | memcpy(g_agent_new, g_v.root.agent_id, 32); |
| 433 | memcpy(g_sid_new, g_sid, 32); |
| 434 | dev_close(); |
| 435 | for (int i = 0; i < CUT_CHECKPOINTS; i++) { |
| 436 | char what[96]; |
| 437 | snprintf(what, sizeof what, "bs=%u cp %s", g_bs, cp_name(CPS[i])); |
| 438 | CK_(file_store(base_img) == 0, "restore"); |
| 439 | CK_(reopen() == 0, "reopen base"); |
| 440 | g_cp_target = CPS[i]; |
| 441 | g_cp_fired = 0; |
| 442 | bind(cp_hook); |
| 443 | rng_init(777); |
| 444 | (void)do_cs_provision(&why, &rc); |
| 445 | CK_(g_cp_fired == 1, "%s: checkpoint never reached", what); |
| 446 | disk_file_disarm(&g_f); |
| 447 | int v = classify(what); |
| 448 | if (CPS[i] <= ST_CP_AFTER_FIRST_FLUSH) CK_(v == 1, "%s: expected unprovisioned", what); |
| 449 | if (CPS[i] == ST_CP_AFTER_FINAL_FLUSH || CPS[i] >= SS_CP_BEFORE_ANCHOR) CK_(v == 2, "%s: expected identity + subject", what); |
| 450 | old += v == 1; |
| 451 | neu += v == 2; |
| 452 | bad += v == 0; |
| 453 | } |
| 454 | for (uint64_t k = 0; k <= W; k++) { |
| 455 | char what[96]; |
| 456 | snprintf(what, sizeof what, "bs=%u block cut %llu/%llu", g_bs, (unsigned long long)k, (unsigned long long)W); |
| 457 | CK_(file_store(base_img) == 0, "restore"); |
| 458 | CK_(reopen() == 0, "reopen base"); |
| 459 | disk_file_arm_cut(&g_f, k); |
| 460 | rng_init(777); |
| 461 | (void)do_cs_provision(&why, &rc); |
| 462 | disk_file_disarm(&g_f); |
| 463 | int v = classify(what); |
| 464 | if (k == 0) CK_(v == 1, "%s: cut 0 must be unprovisioned", what); |
| 465 | if (k == W) CK_(v == 2, "%s: full write must be identity + subject", what); |
| 466 | old += v == 1; |
| 467 | neu += v == 2; |
| 468 | bad += v == 0; |
| 469 | } |
| 470 | printf("power cut, ALLEN genesis, %4u-byte blocks: %d checkpoints + %llu block boundaries: %d unprovisioned, " |
| 471 | "%d identity+subject, %d other\n", |
| 472 | g_bs, CUT_CHECKPOINTS, (unsigned long long)W + 1, old, neu, bad); |
| 473 | CK_(bad == 0, "never an identity without its subject, never a stray subject"); |
| 474 | } |
| 475 | |
| 476 | int main(void) |
| 477 | { |
| 478 | make_keys(); |
| 479 | memset(G.provenance, 0xa5, 32); |
| 480 | memset(G.cortex, 0xc0, 32); |
| 481 | G.origin = CS_ORIGIN_OPERATOR; |
| 482 | snprintf(g_path, sizeof g_path, "/tmp/ck_csp_test_%d.img", (int)getpid()); |
| 483 | static const uint32_t geos[2] = {4096u, 512u}; |
| 484 | for (int g = 0; g < 2; g++) { |
| 485 | g_bs = geos[g]; |
| 486 | t_genesis(); |
| 487 | t_exactly_once(); |
| 488 | t_refusals(); |
| 489 | t_absent_never_minted(); |
| 490 | t_power_cuts(); |
| 491 | } |
| 492 | dev_close(); |
| 493 | unlink(g_path); |
| 494 | return ck_t_verdict("test_continuity_subject_provision"); |
| 495 | } |