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_codec.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_codec.c` 882 lines ยท 39026 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_codec.c -- host tests of svc/continuity_codec.c (C 18 * continuity port, cut 1; contract native/kernel/CONTINUITY_RECOVERY_CONTRACT.md, 19 * aienos#222). Oracle: crates/aienos-kernel/src/continuity.rs at `4a116e5` 20 * ("rs:" below). 21 * 22 * 1. Known answers. Expected bytes are written out by hand from the Rust 23 * encoders, each piece citing the Rust line that emits it. Hash values 24 * (root/child branch ids, logical ObjectIds) were computed outside this 25 * code with coreutils sha256sum over printf-built inputs, not with the C 26 * code under test. Cut 2 adds Rust-emitted golden vectors (D-1, section 6). 27 * 2. Round trips (twin of rs tests continuity_tests.rs:286, 87h). 28 * 3. One refusal per rule, with the Rust error class and reason text. 29 * 4. Branch-table lineage (twin of continuity_tests.rs:328, 87i). 30 * 5. K-1 (PROPOSED): 16384-byte cap on encode and decode. 31 * Mutants (make continuity-mutants) must each turn this test FAIL. */ 32 #include 33 #include 34 #include 35 #include 36 #include "ck_test.h" 37 #include "continuity_codec.h" 38 #include "store_v1.h" /* sv1_object_id: independent C twin of ObjectId::calculate */ 39 40 static const char *why; 41 static uint8_t buf[CC_MAX_OBJECT_BYTES + 1024]; /* decoders read from here */ 42 static uint8_t out[CC_MAX_OBJECT_BYTES + 1024]; 43 static struct cc_state st, st2; 44 static struct cc_wal wal, wal2; 45 static struct cc_manifest man, man2; 46 static uint8_t stmt[CC_MAX_WAL_RECORDS][CC_MAX_STATEMENT_BYTES]; 47 48 /* ---- known-answer builder ---- */ 49 static uint8_t kat[4096]; 50 static size_t kn; 51 static void k_str(const char *s) { size_t n = strlen(s); memcpy(kat + kn, s, n); kn += n; } 52 static void k_rep(uint8_t b, size_t n) { memset(kat + kn, b, n); kn += n; } 53 static void k_le(uint64_t v, int n) { for (int i = 0; i < n; i++) kat[kn++] = (uint8_t)(v >> (8 * i)); } 54 static int hexv(char c) { return c <= '9' ? c - '0' : c - 'a' + 10; } 55 static void unhex(const char *h, uint8_t *o) { for (size_t i = 0; h[2 * i]; i++) o[i] = (uint8_t)(hexv(h[2 * i]) << 4 | hexv(h[2 * i + 1])); } 56 static void k_hex(const char *h) { unhex(h, kat + kn); kn += strlen(h) / 2; } 57 58 /* sha256sum("AIENOS_ROOT_BRANCH_v1:" || 0x11 x 32) */ 59 #define RB_HEX "`89f5fd9d69301a1ab4cce49d57b3f711fb9c36524807ef53fada8119ac24f803`" 60 /* sha256sum("AIENOS_CHILD_BRANCH_v1:" || RB || index u64 BE) for index 0, 1, 2^32, 2^64-1 */ 61 #define C0_HEX "`0be1954eac96e977c2eabee2a079600ad249b7c46b4f07910089098041f55785`" 62 #define C1_HEX "`909874bd0bd38ea9b278982665b3f33ed29b5c76340719bccf030bc59bebcc55`" 63 #define C32_HEX "`35b2e2d23d315594bfdb01277cdce8ad5aedf79641e11b7a82c9d08a6c5e669a`" 64 #define CMAX_HEX "`d9716d516fb3c31b21dc4e9a9e748a928a5a53d60642c0251094e47aeb8d22fd`" 65 /* sha256sum("AIENOS-STORE-OBJECT-V1\0" || kind u16 LE || 1 u16 LE || len u64 LE || bytes) 66 * over the four known-answer objects below (store/v1.rs:63-78). */ 67 #define ID_ROOT_HEX "`d231e0aaba16e227a5330c590d60f0367d42930593bca2092df9fe3ae23f6bf7`" 68 #define ID_MANI_HEX "`e8ba64304b22ed1f65c810a6735420dca321b42b6136caddc5214c5e0a509287`" 69 #define ID_BRAN_HEX "`561ec891c466d831b187ad19016c2800e56aa9947dfb356792c127e003682b20`" 70 #define ID_WAL_HEX "`1e6f48f3e32c1dbc13b200f112deae41668f0b5048e17578d99fe1583d344faf`" 71 72 static uint8_t AGENT[32]; /* 0x11 x 32, as continuity_tests.rs:8 */ 73 74 static int is(int got, int cls, const char *w) 75 { 76 return got == cls && why && strcmp(why, w) == 0; 77 } 78 79 static int id_matches(uint16_t kind, const uint8_t *b, size_t n, const char *hex) 80 { 81 uint8_t a[32], c[32], e[32]; 82 unhex(hex, e); 83 if (cc_object_id(kind, b, n, a) != CC_OK || sv1_object_id(kind, 1, b, n, c) != 0) 84 return 0; 85 return memcmp(a, e, 32) == 0 && memcmp(c, e, 32) == 0; 86 } 87 88 /* ---- 1. derivations ---- */ 89 static void test_branch_ids(void) 90 { 91 uint8_t rb[32], c[32], e[32]; 92 cc_root_branch_id(AGENT, rb); 93 unhex(RB_HEX, e); 94 CHECK(memcmp(rb, e, 32) == 0); 95 static const struct { uint64_t ix; const char *hex; } v[] = { 96 {0, C0_HEX}, {1, C1_HEX}, {0x100000000ull, C32_HEX}, {UINT64_MAX, CMAX_HEX}}; 97 for (size_t i = 0; i < sizeof v / sizeof v[0]; i++) { 98 cc_child_branch_id(rb, v[i].ix, c); 99 unhex(v[i].hex, e); 100 CHECK(memcmp(c, e, 32) == 0); /* index big-endian, rs:82 (MC-6) */ 101 } 102 CHECK(cc_object_id(0, AGENT, 32, c) == CC_E_ARG); /* v1.rs:64 */ 103 CHECK(cc_object_id(CC_KIND_AGENT_ROOT, AGENT, 0, c) == CC_E_ARG); 104 } 105 106 /* ---- AgentRoot ---- */ 107 static void root_kat(void) /* rs:205-215, total 112 */ 108 { 109 kn = 0; 110 k_str("AIENROOT"); /* rs:91 MAGIC_ROOT (:34) */ 111 k_le(0, 2); /* rs:92 FORMAT_VERSION 0 */ 112 k_le(0, 2); /* rs:93 reserved */ 113 k_le(96, 4); /* rs:94 body_len = ROOT_BODY (:202) */ 114 k_rep(0x11, 32); /* rs:208 agent_id */ 115 k_rep(0x5a, 16); /* rs:209 store_uuid */ 116 k_hex(RB_HEX); /* rs:210 root_branch */ 117 k_le(0x0102030405060708ull, 8); /* rs:211 provisioned_generation LE */ 118 k_le(2, 1); /* rs:212 source Qualification (:190) */ 119 k_rep(0, 7); /* rs:213 reserved */ 120 } 121 static void root_val(struct cc_root *r) 122 { 123 memset(r, 0, sizeof *r); 124 memcpy(r->agent_id, AGENT, 32); 125 memset(r->store_uuid, 0x5a, 16); 126 unhex(RB_HEX, r->root_branch); 127 r->provisioned_generation = 0x0102030405060708ull; 128 r->source = CC_SOURCE_QUALIFICATION; 129 } 130 static int root_eq(const struct cc_root *a, const struct cc_root *b) 131 { 132 return !memcmp(a->agent_id, b->agent_id, 32) && !memcmp(a->store_uuid, b->store_uuid, 16) && 133 !memcmp(a->root_branch, b->root_branch, 32) && 134 a->provisioned_generation == b->provisioned_generation && a->source == b->source; 135 } 136 /* Decode n bytes copied to buf (buf is larger, so a broken bound check reads 137 * inside the array and the test reports instead of crashing). */ 138 static int dec_root(const uint8_t *b, size_t n, struct cc_root *r) 139 { 140 memmove(buf, b, n); 141 why = 0; 142 return cc_root_decode(buf, n, r, &why); 143 } 144 145 static void test_root(void) 146 { 147 struct cc_root v, d; 148 size_t n = 0; 149 root_val(&v); 150 root_kat(); 151 CHECK(kn == CC_ROOT_BYTES); 152 CHECK(cc_root_encode(&v, out, sizeof out, &n, &why) == CC_OK && n == kn && !memcmp(out, kat, kn)); 153 CHECK(id_matches(CC_KIND_AGENT_ROOT, kat, kn, ID_ROOT_HEX)); 154 CHECK(dec_root(kat, kn, &d) == CC_OK && root_eq(&d, &v)); 155 156 /* 87h: every single-bit flip is refused or changes the decoded value. */ 157 int unnoticed = 0; 158 for (size_t i = 0; i < kn * 8; i++) { 159 uint8_t t[CC_ROOT_BYTES]; 160 memcpy(t, kat, kn); 161 t[i / 8] ^= (uint8_t)(1u << (i % 8)); 162 if (dec_root(t, kn, &d) == CC_OK && root_eq(&d, &v)) 163 unnoticed++; 164 } 165 CHECK(unnoticed == 0); 166 167 uint8_t t[CC_ROOT_BYTES + 8]; 168 /* truncated: last byte cut, body_len says 95 so the header agrees (rs:123-132) */ 169 memcpy(t, kat, kn); t[12] = 95; 170 CHECK(is(dec_root(t, kn - 1, &d), CC_E_CORRUPT, "truncated object")); 171 /* truncated inside the header (rs:107-118) */ 172 CHECK(is(dec_root(kat, 10, &d), CC_E_CORRUPT, "truncated object")); 173 CHECK(is(dec_root(kat, 0, &d), CC_E_CORRUPT, "truncated object")); 174 /* trailing: one extra byte, body_len says 97 (rs:160-165) */ 175 memcpy(t, kat, kn); t[kn] = 0; t[12] = 97; 176 CHECK(is(dec_root(t, kn + 1, &d), CC_E_CORRUPT, "trailing bytes")); 177 /* plain extra byte: body length mismatch (rs:118) */ 178 memcpy(t, kat, kn); t[kn] = 0; 179 CHECK(is(dec_root(t, kn + 1, &d), CC_E_CORRUPT, "body length mismatch")); 180 CHECK(is(dec_root(kat, kn - 1, &d), CC_E_CORRUPT, "body length mismatch")); 181 memcpy(t, kat, kn); t[0] ^= 0x20; 182 CHECK(is(dec_root(t, kn, &d), CC_E_CORRUPT, "bad magic")); /* rs:107 */ 183 memcpy(t, kat, kn); t[8] = 1; 184 CHECK(is(dec_root(t, kn, &d), CC_E_CORRUPT, "unsupported continuity format version")); /* rs:110 */ 185 memcpy(t, kat, kn); t[9] = 1; 186 CHECK(is(dec_root(t, kn, &d), CC_E_CORRUPT, "unsupported continuity format version")); 187 memcpy(t, kat, kn); t[11] = 0x80; 188 CHECK(is(dec_root(t, kn, &d), CC_E_CORRUPT, "nonzero header reserved")); /* rs:115 */ 189 memcpy(t, kat, kn); t[111] = 1; 190 CHECK(is(dec_root(t, kn, &d), CC_E_CORRUPT, "nonzero reserved")); /* rs:229 */ 191 memcpy(t, kat, kn); t[104] = 3; 192 CHECK(is(dec_root(t, kn, &d), CC_E_CORRUPT, "unknown provisioning source")); /* rs:227 */ 193 memcpy(t, kat, kn); t[104] = 0; 194 CHECK(is(dec_root(t, kn, &d), CC_E_CORRUPT, "unknown provisioning source")); 195 /* zero agent: checked before the derivation (rs:231 before :234) */ 196 struct cc_root z = v; 197 memset(z.agent_id, 0, 32); 198 cc_root_branch_id(z.agent_id, z.root_branch); 199 CHECK(cc_root_encode(&z, out, sizeof out, &n, &why) == CC_OK); 200 CHECK(is(dec_root(out, n, &d), CC_E_CORRUPT, "zero agent id")); 201 z = v; 202 memset(z.root_branch, 0x99, 32); /* as continuity_tests.rs:313 */ 203 CHECK(cc_root_encode(&z, out, sizeof out, &n, &why) == CC_OK); 204 CHECK(is(dec_root(out, n, &d), CC_E_CORRUPT, "root branch does not derive from agent")); 205 /* the right magic for another kind is refused (kind check by magic) */ 206 memcpy(t, kat, kn); memcpy(t, "AIENMANI", 8); 207 CHECK(is(dec_root(t, kn, &d), CC_E_CORRUPT, "bad magic")); 208 209 /* encoder: caller bugs, nothing truncated */ 210 z = v; z.source = 0; 211 CHECK(cc_root_encode(&z, out, sizeof out, &n, &why) == CC_E_ARG && n == 0); 212 memset(out, 0xee, 16); 213 CHECK(cc_root_encode(&v, out, CC_ROOT_BYTES - 1, &n, &why) == CC_E_ARG && n == 0 && out[0] == 0xee); 214 } 215 216 /* ---- ContinuityManifest ---- */ 217 static void mani_kat(void) /* rs:264-282, total 168 */ 218 { 219 kn = 0; 220 k_str("AIENMANI"); k_le(0, 2); k_le(0, 2); /* rs:91-93, MAGIC_MANIFEST (:35) */ 221 k_le(120 + 32, 4); /* rs:94 body (:268) */ 222 k_rep(0xa1, 32); /* rs:271 root */ 223 k_rep(0x00, 32); /* rs:272 previous None = zero */ 224 k_le(1, 8); /* rs:273 sequence */ 225 k_le(1, 8); /* rs:274 incarnation */ 226 k_rep(0xb2, 32); /* rs:275 agent_state */ 227 k_le(1, 2); /* rs:276 cortex_wal count */ 228 k_rep(0, 6); /* rs:277 reserved */ 229 k_rep(0xc3, 32); /* rs:279 cortex_wal[0] */ 230 } 231 static void mani_val(struct cc_manifest *m) 232 { 233 memset(m, 0, sizeof *m); 234 memset(m->root, 0xa1, 32); 235 m->sequence = 1; 236 m->incarnation = 1; 237 memset(m->agent_state, 0xb2, 32); 238 m->n_wal = 1; 239 memset(m->wal[0], 0xc3, 32); 240 } 241 static int mani_eq(const struct cc_manifest *a, const struct cc_manifest *b) 242 { 243 return !memcmp(a->root, b->root, 32) && !memcmp(a->previous, b->previous, 32) && 244 a->sequence == b->sequence && a->incarnation == b->incarnation && 245 !memcmp(a->agent_state, b->agent_state, 32) && a->n_wal == b->n_wal && 246 !memcmp(a->wal, b->wal, 32 * (size_t)a->n_wal); 247 } 248 static int dec_mani(const uint8_t *b, size_t n) 249 { 250 memmove(buf, b, n); 251 why = 0; 252 return cc_manifest_decode(buf, n, &man2, &why); 253 } 254 255 static void test_manifest(void) 256 { 257 size_t n = 0; 258 mani_val(&man); 259 mani_kat(); 260 CHECK(kn == 168); 261 CHECK(cc_manifest_encode(&man, out, sizeof out, &n, &why) == CC_OK && n == kn && !memcmp(out, kat, kn)); 262 CHECK(id_matches(CC_KIND_MANIFEST, kat, kn, ID_MANI_HEX)); 263 CHECK(dec_mani(kat, kn) == CC_OK && mani_eq(&man2, &man)); 264 265 uint8_t t[256]; 266 memcpy(t, kat, kn); t[130] = 1; 267 CHECK(is(dec_mani(t, kn), CC_E_CORRUPT, "nonzero reserved")); /* rs:292 */ 268 /* count 2 with only one id present, body_len unchanged: truncated (rs:298) */ 269 memcpy(t, kat, kn); t[128] = 2; 270 CHECK(is(dec_mani(t, kn), CC_E_CORRUPT, "truncated object")); 271 memcpy(t, kat, kn); t[128] = 65; 272 CHECK(is(dec_mani(t, kn), CC_E_CORRUPT, "too many cortex WAL segments")); /* rs:293-295 */ 273 memcpy(t, kat, kn); t[80] = 0; 274 CHECK(is(dec_mani(t, kn), CC_E_CORRUPT, "manifest sequence/previous mismatch")); /* seq 0, rs:301 */ 275 memcpy(t, kat, kn); t[80] = 2; 276 CHECK(is(dec_mani(t, kn), CC_E_CORRUPT, "manifest sequence/previous mismatch")); /* seq 2, no previous */ 277 memcpy(t, kat, kn); t[48] = 1; 278 CHECK(is(dec_mani(t, kn), CC_E_CORRUPT, "manifest sequence/previous mismatch")); /* seq 1 with previous */ 279 memcpy(t, kat, kn); t[48] = 1; t[80] = 2; 280 CHECK(dec_mani(t, kn) == CC_OK && man2.sequence == 2 && man2.previous[0] == 1); 281 282 /* 64 WAL ids is the largest manifest (2184 bytes); 65 is Limit (rs:265-267) */ 283 man.n_wal = CC_MAX_WAL_SEGMENTS; 284 CHECK(cc_manifest_encode(&man, out, sizeof out, &n, &why) == CC_OK && n == 2184); 285 CHECK(dec_mani(out, n) == CC_OK && man2.n_wal == 64); 286 man.n_wal = CC_MAX_WAL_SEGMENTS + 1; 287 CHECK(is(cc_manifest_encode(&man, out, sizeof out, &n, &why), CC_E_LIMIT, "too many cortex WAL segments") && n == 0); 288 } 289 290 /* ---- AgentState ---- */ 291 static void bran_kat(void) /* genesis(AGENT, 1) + fork(root): rs:392-409, total 224 */ 292 { 293 kn = 0; 294 k_str("AIENBRAN"); k_le(0, 2); k_le(0, 2); /* rs:91-93, MAGIC_BRANCHES (:36) */ 295 k_le(48 + 2 * 80, 4); /* rs:94 body (:394) */ 296 k_rep(0x11, 32); /* rs:397 agent_id */ 297 k_le(1, 8); /* rs:398 written_at */ 298 k_le(2, 4); /* rs:399 branch count */ 299 k_rep(0, 4); /* rs:400 reserved */ 300 /* sorted by id (rs:387-388): C0 (0x0b..) < RB (0x89..) */ 301 k_hex(C0_HEX); /* rs:402 id = child_branch_id(RB, 0) */ 302 k_hex(RB_HEX); /* rs:403 parent */ 303 k_le(1, 4); /* rs:404 depth */ 304 k_rep(0, 4); /* rs:405 reserved */ 305 k_le(0, 8); /* rs:406 forks */ 306 k_hex(RB_HEX); /* rs:402 root id */ 307 k_rep(0, 32); /* rs:403 parent None = zero */ 308 k_le(0, 4); /* rs:404 depth 0 */ 309 k_rep(0, 4); /* rs:405 */ 310 k_le(1, 8); /* rs:406 forks 1 (rs:382) */ 311 } 312 static int dec_state(const uint8_t *b, size_t n) 313 { 314 memmove(buf, b, n); 315 why = 0; 316 return cc_state_decode(buf, n, &st2, &why); 317 } 318 static int state_eq(const struct cc_state *a, const struct cc_state *b) 319 { 320 if (memcmp(a->agent_id, b->agent_id, 32) || a->written_at != b->written_at || a->n != b->n) 321 return 0; 322 for (uint32_t i = 0; i < a->n; i++) 323 if (memcmp(a->br[i].id, b->br[i].id, 32) || memcmp(a->br[i].parent, b->br[i].parent, 32) || 324 a->br[i].depth != b->br[i].depth || a->br[i].forks != b->br[i].forks) 325 return 0; 326 return 1; 327 } 328 static long idx_of(const struct cc_state *s, const uint8_t id[32]) 329 { 330 for (uint32_t i = 0; i < s->n; i++) 331 if (!memcmp(s->br[i].id, id, 32)) 332 return (long)i; 333 return -1; 334 } 335 336 static void test_state(void) 337 { 338 size_t n = 0; 339 uint8_t rb[32], child[32], gchild[32], e[32]; 340 cc_root_branch_id(AGENT, rb); 341 cc_state_genesis(&st, AGENT, 1); 342 CHECK(st.n == 1 && cc_state_validate(&st, &why) == CC_OK); 343 CHECK(cc_state_fork(&st, rb, child, &why) == CC_OK); 344 unhex(C0_HEX, e); 345 CHECK(memcmp(child, e, 32) == 0); 346 bran_kat(); 347 CHECK(kn == 224); 348 CHECK(cc_state_encode(&st, out, sizeof out, &n, &why) == CC_OK && n == kn && !memcmp(out, kat, kn)); 349 CHECK(id_matches(CC_KIND_AGENT_STATE, kat, kn, ID_BRAN_HEX)); 350 CHECK(dec_state(kat, kn) == CC_OK && state_eq(&st2, &st)); 351 352 /* second fork of the root takes index 1 (rs:373, :382) */ 353 st2 = st; 354 CHECK(cc_state_fork(&st2, rb, gchild, &why) == CC_OK); 355 unhex(C1_HEX, e); 356 CHECK(memcmp(gchild, e, 32) == 0 && st2.n == 3); 357 /* 87h tail: fork of a fork (depth 2) round-trips */ 358 CHECK(cc_state_fork(&st2, child, gchild, &why) == CC_OK); 359 CHECK(st2.br[idx_of(&st2, gchild)].depth == 2); 360 CHECK(cc_state_encode(&st2, out, sizeof out, &n, &why) == CC_OK); 361 static struct cc_state keep; 362 keep = st2; 363 CHECK(dec_state(out, n) == CC_OK && state_eq(&st2, &keep)); 364 365 uint8_t t[256]; 366 memcpy(t, kat, kn); t[60] = 1; 367 CHECK(is(dec_state(t, kn), CC_E_CORRUPT, "nonzero reserved")); /* rs:416 */ 368 memcpy(t, kat, kn); t[64 + 68] = 1; 369 CHECK(is(dec_state(t, kn), CC_E_CORRUPT, "nonzero reserved")); /* rs:425 */ 370 memcpy(t, kat, kn); t[56] = 0; 371 CHECK(is(dec_state(t, kn), CC_E_CORRUPT, "branch count out of range")); /* rs:417 */ 372 memcpy(t, kat, kn); t[56] = 3; 373 CHECK(is(dec_state(t, kn), CC_E_CORRUPT, "truncated object")); /* rs:422 */ 374 memcpy(t, kat, kn); t[56] = 0x01; t[57] = 0x01; /* 257 */ 375 CHECK(is(dec_state(t, kn), CC_E_CORRUPT, "branch count out of range")); 376 memcpy(t, kat, kn); t[56] = 1; t[12] = 48 + 80; /* header length disagrees with the input length */ 377 CHECK(is(dec_state(t, kn), CC_E_CORRUPT, "body length mismatch")); 378 memcpy(t, kat, kn); t[56] = 1; 379 CHECK(is(dec_state(t, kn), CC_E_CORRUPT, "trailing bytes")); 380 /* swap the two branches: not strictly sorted (rs:452-454) */ 381 memcpy(t, kat, kn); memcpy(t + 64, kat + 144, 80); memcpy(t + 144, kat + 64, 80); 382 CHECK(is(dec_state(t, kn), CC_E_CORRUPT, "branches not strictly sorted")); 383 384 /* 87i twins (continuity_tests.rs:328-346) through validate */ 385 long ci = idx_of(&st, child), ri = idx_of(&st, rb); 386 st2 = st; st2.br[ci].depth = 5; 387 CHECK(is(cc_state_validate(&st2, &why), CC_E_CORRUPT, "branch lineage is inconsistent")); 388 st2 = st; st2.br[ri].forks = 2; 389 CHECK(is(cc_state_validate(&st2, &why), CC_E_CORRUPT, "fork indexes are not contiguous")); 390 st2 = st; st2.br[ri] = st2.br[ci]; st2.n = 1; /* root removed */ 391 CHECK(is(cc_state_validate(&st2, &why), CC_E_CORRUPT, "parent branch is absent")); 392 cc_state_genesis(&st2, AGENT, 1); st2.br[0].depth = 1; 393 CHECK(is(cc_state_validate(&st2, &why), CC_E_CORRUPT, "unexpected root branch")); 394 st2 = st; st2.agent_id[0] ^= 1; /* root id no longer derives from the agent */ 395 CHECK(is(cc_state_validate(&st2, &why), CC_E_CORRUPT, "unexpected root branch")); 396 st2 = st; st2.br[ci].id[31] ^= 1; /* child id not derived from its parent */ 397 CHECK(is(cc_state_validate(&st2, &why), CC_E_CORRUPT, "branch lineage is inconsistent")); 398 st2 = st; st2.br[ci].depth = UINT32_MAX; 399 CHECK(is(cc_state_validate(&st2, &why), CC_E_CORRUPT, "branch lineage is inconsistent")); 400 /* encode runs validate first (rs:393) */ 401 st2 = st; st2.br[ri].forks = 2; 402 CHECK(is(cc_state_encode(&st2, out, sizeof out, &n, &why), CC_E_CORRUPT, "fork indexes are not contiguous") && n == 0); 403 /* Hostile fork counts: terminates, refused. Child index 0 under a parent 404 * claiming 2^64-1 forks with a sum that would wrap to the child count. */ 405 st2 = st; st2.br[ri].forks = UINT64_MAX; st2.br[ci].forks = 2; 406 CHECK(is(cc_state_validate(&st2, &why), CC_E_CORRUPT, "fork indexes are not contiguous")); 407 st2 = st; st2.br[ri].forks = UINT64_MAX; st2.br[ci].id[31] ^= 1; 408 CHECK(cc_state_validate(&st2, &why) == CC_E_CORRUPT); 409 410 /* fork refusals (rs:366-387) */ 411 st2 = st; memset(e, 0x77, 32); 412 CHECK(is(cc_state_fork(&st2, e, gchild, &why), CC_E_CORRUPT, "fork parent is absent")); 413 st2 = st; st2.n = CC_MAX_BRANCHES; 414 CHECK(is(cc_state_fork(&st2, rb, gchild, &why), CC_E_LIMIT, "too many branches")); 415 st2 = st; st2.br[ci].depth = UINT32_MAX; 416 CHECK(is(cc_state_fork(&st2, child, gchild, &why), CC_E_LIMIT, "branch depth")); 417 /* collision: parent forks rewound to 0 re-derives the existing child; the 418 * parent's count still advances, as in Rust (rs:382 before :383-387) */ 419 st2 = st; st2.br[ri].forks = 0; 420 CHECK(is(cc_state_fork(&st2, rb, gchild, &why), CC_E_CORRUPT, "child branch id collision")); 421 CHECK(st2.n == 2 && st2.br[ri].forks == 1); 422 423 /* K-1 (PROPOSED): 204 branches = exactly 16384 bytes; 205 is Limit. */ 424 cc_state_genesis(&st2, AGENT, 1); 425 int ok = 1; 426 while (st2.n < CC_MAX_BRANCHES_IN_CAP) 427 ok &= cc_state_fork(&st2, rb, gchild, &why) == CC_OK; 428 CHECK(ok && st2.n == 204); 429 CHECK(cc_state_encode(&st2, out, sizeof out, &n, &why) == CC_OK && n == CC_MAX_OBJECT_BYTES); 430 CHECK(dec_state(out, n) == CC_OK && st2.n == 204); 431 CHECK(cc_state_fork(&st2, rb, gchild, &why) == CC_OK && st2.n == 205); /* Rust bound is 256 */ 432 memset(out, 0xee, 64); 433 CHECK(cc_state_encode(&st2, out, sizeof out, &n, &why) == CC_E_LIMIT && n == 0 && out[0] == 0xee); 434 } 435 436 /* ---- CortexWalSegment ---- */ 437 static void wal_kat(void) /* rs:528-552, total 107 */ 438 { 439 kn = 0; 440 k_str("AIENCWAL"); k_le(0, 2); k_le(0, 2); /* rs:91-93, MAGIC_WAL (:37) */ 441 k_le(16 + 37 + 38, 4); /* rs:94 body (:532, :537) */ 442 k_le(3, 8); /* rs:541 sequence */ 443 k_le(2, 4); /* rs:542 record count */ 444 k_rep(0, 4); /* rs:543 reserved */ 445 k_le(6, 1); k_le(0, 1); k_le(1, 2); /* rs:545-547 OperatorDecision, reserved, len */ 446 k_rep(0x42, 32); k_str("a"); /* rs:548-549 */ 447 k_le(2, 1); k_le(0, 1); k_le(2, 2); /* rs:545-547 VerifiedFact */ 448 k_rep(0x42, 32); k_str("bb"); /* rs:548-549 */ 449 } 450 static int dec_wal(const uint8_t *b, size_t n) 451 { 452 memmove(buf, b, n); 453 why = 0; 454 return cc_wal_decode(buf, n, &wal2, &why); 455 } 456 457 static void test_wal(void) 458 { 459 size_t n = 0; 460 memset(&wal, 0, sizeof wal); 461 wal.sequence = 3; 462 wal.n = 2; 463 wal.rec[0].status = 6; memset(wal.rec[0].evidence_hash, 0x42, 32); wal.rec[0].len = 1; wal.rec[0].statement = (const uint8_t *)"a"; 464 wal.rec[1].status = 2; memset(wal.rec[1].evidence_hash, 0x42, 32); wal.rec[1].len = 2; wal.rec[1].statement = (const uint8_t *)"bb"; 465 wal_kat(); 466 CHECK(kn == 107); 467 CHECK(cc_wal_encode(&wal, out, sizeof out, &n, &why) == CC_OK && n == kn && !memcmp(out, kat, kn)); 468 CHECK(id_matches(CC_KIND_CORTEX_WAL, kat, kn, ID_WAL_HEX)); 469 CHECK(dec_wal(kat, kn) == CC_OK && wal2.sequence == 3 && wal2.n == 2); 470 CHECK(wal2.rec[0].status == 6 && wal2.rec[0].len == 1 && wal2.rec[0].statement == buf + 68 && 471 wal2.rec[0].statement[0] == 'a' && wal2.rec[0].evidence_hash[31] == 0x42); 472 CHECK(wal2.rec[1].status == 2 && wal2.rec[1].len == 2 && wal2.rec[1].statement == buf + 105 && 473 !memcmp(wal2.rec[1].statement, "bb", 2)); 474 475 uint8_t t[256]; 476 memcpy(t, kat, kn); t[28] = 1; 477 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "nonzero reserved")); /* rs:558 */ 478 memcpy(t, kat, kn); t[33] = 1; 479 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "nonzero reserved")); /* rs:566 */ 480 memcpy(t, kat, kn); t[24] = 0; 481 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "WAL segment record count")); /* rs:559 */ 482 memcpy(t, kat, kn); t[24] = 65; 483 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "WAL segment record count")); 484 memcpy(t, kat, kn); t[32] = 7; 485 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "unknown epistemic status")); /* rs:564-565 */ 486 memcpy(t, kat, kn); t[32] = 0; 487 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "unknown epistemic status")); 488 memcpy(t, kat, kn); t[34] = 0; 489 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "statement length")); /* rs:568 */ 490 memcpy(t, kat, kn); t[34] = 0x01; t[35] = 0x04; /* 1025 */ 491 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "statement length")); 492 /* last statement claims 3 bytes, header agrees with the real length: 493 * the statement read runs past the end (rs:572) */ 494 memcpy(t, kat, kn); t[71] = 3; 495 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "truncated object")); 496 /* count 3, only two records present */ 497 memcpy(t, kat, kn); t[24] = 3; 498 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "truncated object")); 499 memcpy(t, kat, kn); t[71] = 1; 500 CHECK(is(dec_wal(t, kn), CC_E_CORRUPT, "trailing bytes")); 501 502 /* encoder bounds (rs:529-536) */ 503 struct cc_wal w = wal; 504 w.n = 0; 505 CHECK(is(cc_wal_encode(&w, out, sizeof out, &n, &why), CC_E_LIMIT, "WAL segment record count") && n == 0); 506 w = wal; w.rec[1].len = 0; 507 CHECK(is(cc_wal_encode(&w, out, sizeof out, &n, &why), CC_E_LIMIT, "statement length")); 508 w = wal; w.rec[1].len = CC_MAX_STATEMENT_BYTES + 1; w.rec[1].statement = stmt[0]; 509 CHECK(is(cc_wal_encode(&w, out, sizeof out, &n, &why), CC_E_LIMIT, "statement length")); 510 w = wal; w.rec[0].status = 9; 511 CHECK(cc_wal_encode(&w, out, sizeof out, &n, &why) == CC_E_ARG); 512 513 /* every status 1..6 and statement lengths 1 and 1024 round-trip */ 514 memset(&w, 0, sizeof w); 515 w.sequence = 9; 516 w.n = 7; 517 for (uint32_t i = 0; i < 7; i++) { 518 w.rec[i].status = (uint8_t)(i % 6 + 1); 519 memset(w.rec[i].evidence_hash, (int)i, 32); 520 memset(stmt[i], 'A' + (int)i, CC_MAX_STATEMENT_BYTES); 521 w.rec[i].len = i == 6 ? CC_MAX_STATEMENT_BYTES : 1; 522 w.rec[i].statement = stmt[i]; 523 } 524 CHECK(cc_wal_encode(&w, out, sizeof out, &n, &why) == CC_OK && n == 16 + 16 + 7 * 36 + 6 + 1024); 525 CHECK(dec_wal(out, n) == CC_OK && wal2.n == 7 && wal2.rec[6].len == 1024 && wal2.rec[5].status == 6 && 526 wal2.rec[6].statement[1023] == 'G'); 527 528 /* K-1 (PROPOSED, MC-11): a Rust-legal 64 x 1024 segment (67872 bytes) 529 * is Limit, never truncated, nothing written. */ 530 memset(&w, 0, sizeof w); 531 w.sequence = 1; 532 w.n = CC_MAX_WAL_RECORDS; 533 for (uint32_t i = 0; i < CC_MAX_WAL_RECORDS; i++) { 534 w.rec[i].status = 1; 535 w.rec[i].len = CC_MAX_STATEMENT_BYTES; 536 w.rec[i].statement = stmt[i]; 537 } 538 memset(out, 0xee, 64); 539 n = 123; 540 CHECK(is(cc_wal_encode(&w, out, sizeof out, &n, &why), CC_E_LIMIT, 541 "continuity object exceeds the 16384-byte sealed Store cap") && n == 0 && out[0] == 0xee); 542 } 543 544 /* K-1 on decode (PROPOSED): more than 16384 bytes is Limit before parsing. */ 545 static void test_decode_cap(void) 546 { 547 memset(buf, 0, sizeof buf); 548 memcpy(buf, "AIENCWAL", 8); 549 CHECK(cc_wal_decode(buf, CC_MAX_OBJECT_BYTES + 1, &wal2, &why) == CC_E_LIMIT); 550 CHECK(cc_state_decode(buf, CC_MAX_OBJECT_BYTES + 1, &st2, &why) == CC_E_LIMIT); 551 struct cc_root r; 552 CHECK(cc_root_decode(buf, CC_MAX_OBJECT_BYTES + 1, &r, &why) == CC_E_LIMIT); 553 CHECK(cc_manifest_decode(buf, CC_MAX_OBJECT_BYTES + 1, &man2, &why) == CC_E_LIMIT); 554 /* exactly 16384 is parsed (refused here only as Corrupt) */ 555 CHECK(cc_wal_decode(buf, CC_MAX_OBJECT_BYTES, &wal2, &why) == CC_E_CORRUPT); 556 } 557 558 /* ---- D-1 golden vectors and D-2 decode agreement (cut 2) ---- 559 * Fixtures are emitted by the Rust oracle (crates/aienos-kernel/src/ 560 * continuity_vectors.rs) and read here at run time: the directory is argv[1], 561 * else the CC_FIXTURE_DIR compile-time define (set by the Makefile). 562 * continuity_vectors.txt bid / vec / deferred lines (D-1) 563 * continuity_verdicts.txt reason table + one verdict string per vector (D-2) */ 564 #define V_MAX 64 565 #define V_BYTES_MAX 2300 /* largest vector: manifest with 64 WAL ids, 2184 */ 566 #define R_MAX 32 567 static char fbuf_v[1 << 17], fbuf_d[1 << 18]; 568 static struct vec { 569 char name[48]; 570 uint16_t kind; 571 uint8_t oid[32]; 572 uint8_t bytes[V_BYTES_MAX]; 573 size_t n; 574 const char *verdict; /* into fbuf_d, 8n+1 chars, not NUL terminated */ 575 size_t verdict_len; 576 } vecs[V_MAX]; 577 static int n_vecs, n_deferred, n_bids; 578 static struct { char c; char cls[8]; char why[64]; } reasons[R_MAX]; 579 static int n_reasons; 580 /* Comparison counters: the gate asserts the work was actually done, so a 581 * skipped comparison is a FAIL, not a silent pass (mutants SKIP_D1/SKIP_D2). */ 582 static unsigned long d1_compared, d2_compared, d2_expected; 583 static int quiet; /* silence expected divergences in the tamper test */ 584 585 static int read_file(const char *dir, const char *name, char *dst, size_t cap) 586 { 587 char path[512]; 588 if (snprintf(path, sizeof path, "%s/%s", dir, name) >= (int)sizeof path) 589 return 0; 590 FILE *f = fopen(path, "rb"); 591 if (!f) { 592 printf(" cannot open %s\n", path); 593 return 0; 594 } 595 size_t n = fread(dst, 1, cap - 1, f); 596 int big = fgetc(f) != EOF; 597 fclose(f); 598 dst[n] = 0; 599 return n > 0 && !big; 600 } 601 /* Strict hex: exactly 2*n lowercase digits, then end of token. */ 602 static int hex_n(const char *s, size_t n, uint8_t *o) 603 { 604 for (size_t i = 0; i < 2 * n; i++) 605 if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 606 return 0; 607 for (size_t i = 0; i < n; i++) 608 o[i] = (uint8_t)(hexv(s[2 * i]) << 4 | hexv(s[2 * i + 1])); 609 return 1; 610 } 611 /* Next line of a NUL-terminated buffer: returns its start, NUL-terminates it, 612 * advances *p. NULL at end. */ 613 static char *next_line(char **p) 614 { 615 char *s = *p, *e; 616 if (!*s) 617 return 0; 618 for (e = s; *e && *e != '\n'; e++) 619 ; 620 if (*e) 621 *e++ = 0; 622 *p = e; 623 return s; 624 } 625 /* Split off the next space-separated token of *l (in place). */ 626 static char *tok(char **l) 627 { 628 char *s = *l, *e; 629 if (!s || !*s) 630 return 0; 631 for (e = s; *e && *e != ' '; e++) 632 ; 633 if (*e) 634 *e++ = 0; 635 *l = e; 636 return s; 637 } 638 639 static int load_fixtures(const char *dir) 640 { 641 char *p, *l; 642 if (!read_file(dir, "continuity_vectors.txt", fbuf_v, sizeof fbuf_v) || 643 !read_file(dir, "continuity_verdicts.txt", fbuf_d, sizeof fbuf_d)) 644 return 0; 645 for (p = fbuf_v; (l = next_line(&p));) { 646 char *kw = tok(&l), *name = tok(&l); 647 if (!kw || kw[0] == '#') 648 continue; 649 if (!name) 650 return 0; 651 if (!strcmp(kw, "bid")) { 652 n_bids++; 653 } else if (!strcmp(kw, "deferred")) { 654 n_deferred++; 655 } else if (!strcmp(kw, "vec")) { 656 char *kind = tok(&l), *oid = tok(&l), *bytes = l; 657 if (!kind || !oid || !bytes || n_vecs >= V_MAX || strlen(name) >= sizeof vecs[0].name) 658 return 0; 659 struct vec *v = &vecs[n_vecs++]; 660 strcpy(v->name, name); 661 v->kind = (uint16_t)strtoul(kind, 0, 10); 662 size_t hl = strlen(bytes); 663 if (!hex_n(oid, 32, v->oid) || hl % 2 || hl / 2 > V_BYTES_MAX || strlen(oid) != 64) 664 return 0; 665 v->n = hl / 2; 666 if (!hex_n(bytes, v->n, v->bytes)) 667 return 0; 668 } else { 669 return 0; 670 } 671 } 672 for (p = fbuf_d; (l = next_line(&p));) { 673 char *kw = tok(&l), *name = tok(&l); 674 if (!kw || kw[0] == '#') 675 continue; 676 if (!name) 677 return 0; 678 if (!strcmp(kw, "reason")) { 679 char *cls = tok(&l); 680 if (!cls || n_reasons >= R_MAX || strlen(name) != 1 || strlen(cls) >= 8 || strlen(l) >= 64) 681 return 0; 682 reasons[n_reasons].c = name[0]; 683 strcpy(reasons[n_reasons].cls, cls); 684 strcpy(reasons[n_reasons].why, l); 685 n_reasons++; 686 } else if (!strcmp(kw, "verdict")) { 687 int found = 0; 688 for (int i = 0; i < n_vecs; i++) 689 if (!strcmp(vecs[i].name, name) && !vecs[i].verdict) { 690 vecs[i].verdict = l; 691 vecs[i].verdict_len = strlen(l); 692 found = 1; 693 } 694 if (!found) 695 return 0; 696 } else { 697 return 0; 698 } 699 } 700 return 1; 701 } 702 703 /* C verdict of one byte string, in the fixture alphabet: '.' accept, else the 704 * reason code whose (class, why) the C decoder returned; '?' if C returned a 705 * class or text the Rust oracle never does (itself a divergence). */ 706 static char c_verdict(uint16_t kind, const uint8_t *b, size_t n) 707 { 708 struct cc_root r; 709 int e; 710 memmove(buf, b, n); 711 why = 0; 712 switch (kind) { 713 case CC_KIND_AGENT_ROOT: e = cc_root_decode(buf, n, &r, &why); break; 714 case CC_KIND_MANIFEST: e = cc_manifest_decode(buf, n, &man2, &why); break; 715 case CC_KIND_AGENT_STATE: e = cc_state_decode(buf, n, &st2, &why); break; 716 case CC_KIND_CORTEX_WAL: e = cc_wal_decode(buf, n, &wal2, &why); break; 717 default: return '?'; 718 } 719 if (e == CC_OK) 720 return '.'; 721 const char *cls = e == CC_E_CORRUPT ? "Corrupt" : e == CC_E_LIMIT ? "Limit" : 0; 722 for (int i = 0; cls && why && i < n_reasons; i++) 723 if (!strcmp(reasons[i].cls, cls) && !strcmp(reasons[i].why, why)) 724 return reasons[i].c; 725 return '?'; 726 } 727 728 /* D-1: ObjectId (two independent C implementations) and, when the vector is 729 * accepted, decode then encode reproduces the bytes exactly. */ 730 static int d1_ok(uint16_t kind, const uint8_t *b, size_t n, const uint8_t oid[32], int accepted) 731 { 732 uint8_t a[32], c[32], re[V_BYTES_MAX + 64]; 733 size_t rn = 0; 734 int ok = 1; 735 #ifndef CC_MUTANT_SKIP_D1 736 d1_compared++; 737 ok = cc_object_id(kind, b, n, a) == CC_OK && sv1_object_id(kind, 1, b, n, c) == 0 && 738 !memcmp(a, oid, 32) && !memcmp(c, oid, 32); 739 if (accepted) { 740 struct cc_root r; 741 memmove(buf, b, n); 742 why = 0; 743 int e = CC_E_ARG; 744 switch (kind) { 745 case CC_KIND_AGENT_ROOT: 746 e = cc_root_decode(buf, n, &r, &why) || cc_root_encode(&r, re, sizeof re, &rn, &why); 747 break; 748 case CC_KIND_MANIFEST: 749 e = cc_manifest_decode(buf, n, &man2, &why) || cc_manifest_encode(&man2, re, sizeof re, &rn, &why); 750 break; 751 case CC_KIND_AGENT_STATE: 752 e = cc_state_decode(buf, n, &st2, &why) || cc_state_encode(&st2, re, sizeof re, &rn, &why); 753 break; 754 case CC_KIND_CORTEX_WAL: 755 e = cc_wal_decode(buf, n, &wal2, &why) || cc_wal_encode(&wal2, re, sizeof re, &rn, &why); 756 break; 757 } 758 ok = ok && e == CC_OK && rn == n && !memcmp(re, b, n); 759 } 760 #else 761 (void)kind; (void)b; (void)n; (void)oid; (void)accepted; (void)a; (void)c; (void)re; (void)rn; 762 #endif 763 return ok; 764 } 765 766 /* D-2: verdict of the untouched vector and of every single-bit flip must equal 767 * the Rust verdict string. Returns the number of disagreements; prints the 768 * first few. */ 769 static unsigned long d2_mismatches(const char *name, uint16_t kind, const uint8_t *b, size_t n, 770 const char *verdict, size_t vlen) 771 { 772 uint8_t t[V_BYTES_MAX]; 773 unsigned long bad = 0; 774 if (vlen != 8 * n + 1) 775 return 1; 776 memcpy(t, b, n); 777 #ifndef CC_MUTANT_SKIP_D2 778 if (c_verdict(kind, t, n) != verdict[0]) 779 bad++; 780 d2_compared++; 781 for (size_t i = 0; i < n * 8; i++) { 782 t[i / 8] ^= (uint8_t)(1u << (i % 8)); 783 char cv = c_verdict(kind, t, n); 784 t[i / 8] ^= (uint8_t)(1u << (i % 8)); 785 d2_compared++; 786 if (cv != verdict[i + 1]) { 787 if (bad++ < 3 && !quiet) 788 printf(" D-2 divergence %s bit %zu: C '%c' Rust '%c'\n", name, i, cv, verdict[i + 1]); 789 } 790 } 791 #else 792 (void)name; (void)kind; (void)verdict; 793 #endif 794 return bad; 795 } 796 797 static void test_golden(void) 798 { 799 CHECK(n_vecs == 22 && n_bids == 5 && n_deferred == 4 && n_reasons == 21); 800 /* Branch ids named in the fixture equal the C derivation (D-1). */ 801 { 802 uint8_t rb[32], e[32]; 803 cc_root_branch_id(AGENT, rb); 804 CHECK(hex_n(RB_HEX, 32, e) && !memcmp(rb, e, 32)); 805 } 806 for (int i = 0; i < n_vecs; i++) { 807 struct vec *v = &vecs[i]; 808 int expect_ok = strcmp(v->name, "state_forksum_overflow") != 0; 809 CHECK(v->verdict != 0); 810 if (!v->verdict) 811 continue; 812 d2_expected += 8 * v->n + 1; 813 /* the Rust verdict of the untouched vector: accept, except the hostile one */ 814 CHECK((v->verdict[0] == '.') == expect_ok); 815 int ok = d1_ok(v->kind, v->bytes, v->n, v->oid, v->verdict[0] == '.'); 816 if (!ok) 817 printf(" D-1 mismatch: %s\n", v->name); 818 CHECK(ok); 819 unsigned long bad = d2_mismatches(v->name, v->kind, v->bytes, v->n, v->verdict, v->verdict_len); 820 if (bad) 821 printf(" D-2: %s: %lu disagreements with Rust\n", v->name, bad); 822 CHECK(bad == 0); 823 } 824 /* The work was done: every vector, every bit (not skipped, not truncated). */ 825 CHECK(d1_compared == (unsigned long)n_vecs); 826 CHECK(d2_compared == d2_expected && d2_expected > 80000); 827 printf(" golden: %d vectors, %lu D-2 verdicts compared (C vs Rust)\n", n_vecs, d2_compared); 828 } 829 830 /* The comparisons refuse tampered vectors: a flipped byte, a flipped ObjectId 831 * bit and an altered verdict character are each detected. */ 832 static void test_tampered(void) 833 { 834 struct vec *v = &vecs[0]; /* root_operator, accepted */ 835 uint8_t t[V_BYTES_MAX], o[32]; 836 char vs[1024]; 837 quiet = 1; 838 CHECK(v->verdict && d1_ok(v->kind, v->bytes, v->n, v->oid, 1)); 839 memcpy(t, v->bytes, v->n); 840 t[100] ^= 0x01; /* inside the provisioned generation: still decodes, bytes differ */ 841 CHECK(!d1_ok(v->kind, t, v->n, v->oid, 1)); 842 memcpy(o, v->oid, 32); 843 o[31] ^= 0x80; 844 CHECK(!d1_ok(v->kind, v->bytes, v->n, o, 1)); 845 CHECK(v->verdict_len < sizeof vs); 846 memcpy(vs, v->verdict, v->verdict_len); 847 CHECK(d2_mismatches(v->name, v->kind, v->bytes, v->n, vs, v->verdict_len) == 0); 848 vs[0] = 'a'; /* untouched vector claimed refused */ 849 CHECK(d2_mismatches(v->name, v->kind, v->bytes, v->n, vs, v->verdict_len) == 1); 850 memcpy(vs, v->verdict, v->verdict_len); 851 vs[1 + 8 * 20] = vs[1 + 8 * 20] == 'f' ? 'a' : 'f'; /* a flip verdict altered */ 852 CHECK(d2_mismatches(v->name, v->kind, v->bytes, v->n, vs, v->verdict_len) == 1); 853 /* wrong length of verdict string is itself a disagreement */ 854 CHECK(d2_mismatches(v->name, v->kind, v->bytes, v->n, vs, v->verdict_len - 1) == 1); 855 /* a bit-flipped vector decodes to the verdict Rust gives for that flip */ 856 memcpy(t, v->bytes, v->n); 857 t[0] ^= 0x01; /* magic */ 858 CHECK(c_verdict(v->kind, t, v->n) == 'a'); 859 } 860 861 int main(int argc, char **argv) 862 { 863 memset(AGENT, 0x11, 32); 864 test_branch_ids(); 865 test_root(); 866 test_manifest(); 867 test_state(); 868 test_wal(); 869 test_decode_cap(); 870 #ifdef CC_FIXTURE_DIR 871 const char *dir = argc > 1 ? argv[1] : CC_FIXTURE_DIR; 872 #else 873 const char *dir = argc > 1 ? argv[1] : 0; 874 #endif 875 int loaded = dir && load_fixtures(dir); 876 CHECK(loaded); 877 if (loaded) { 878 test_golden(); 879 test_tampered(); 880 } 881 return ck_t_verdict("test_continuity_codec"); 882 }