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 / store / tests / store_v1_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * store_v1_test.c -- byte identity of the C Store v1 format against |
| 19 | * docs/adr/0015-golden-vectors.json, plus a port of every Rust negative test |
| 20 | * (crates/aienos-kernel/tests/store_v1_negative.rs and the malformed-vector |
| 21 | * unit tests in crates/aienos-kernel/src/store/v1.rs). |
| 22 | * |
| 23 | * Golden rule (same as the Rust test): every row of the frozen JSON must be |
| 24 | * produced by the C encoders and match exactly, and every value this test |
| 25 | * produces must be a row in the JSON. A new vector without a C assertion |
| 26 | * fails the test. |
| 27 | */ |
| 28 | #include "store_test_util.h" |
| 29 | #include "golden_vectors.h" |
| 30 | |
| 31 | /* ---- computed golden rows ---- */ |
| 32 | #define MAX_COMPUTED 128 |
| 33 | static struct { |
| 34 | const char *section, *key; |
| 35 | char *value; |
| 36 | int matched; |
| 37 | } comp[MAX_COMPUTED]; |
| 38 | static int ncomp; |
| 39 | |
| 40 | static void put_text(const char *section, const char *key, const char *text) |
| 41 | { |
| 42 | if (ncomp >= MAX_COMPUTED) { |
| 43 | fprintf(stderr, "too many computed rows\n"); |
| 44 | exit(2); |
| 45 | } |
| 46 | comp[ncomp].section = section; |
| 47 | comp[ncomp].key = key; |
| 48 | comp[ncomp].value = strdup(text); |
| 49 | ncomp++; |
| 50 | } |
| 51 | static void put_hex(const char *section, const char *key, const void *b, size_t n) |
| 52 | { |
| 53 | char *h = malloc(2 * n + 1); |
| 54 | to_hex(b, n, h); |
| 55 | put_text(section, key, h); |
| 56 | free(h); |
| 57 | } |
| 58 | static void put_sha(const char *section, const char *key, const void *b, size_t n) |
| 59 | { |
| 60 | uint8_t d[32]; |
| 61 | sha256_hash(b, n, d); |
| 62 | put_hex(section, key, d, 32); |
| 63 | } |
| 64 | static void put_num(const char *section, const char *key, uint64_t v) |
| 65 | { |
| 66 | char t[32]; |
| 67 | snprintf(t, sizeof t, "%llu", (unsigned long long)v); |
| 68 | put_text(section, key, t); |
| 69 | } |
| 70 | static void put_le16(const char *s, const char *k, uint16_t v) |
| 71 | { |
| 72 | uint8_t b[2]; |
| 73 | sv1_put16(b, v); |
| 74 | put_hex(s, k, b, 2); |
| 75 | } |
| 76 | static void put_le64(const char *s, const char *k, uint64_t v) |
| 77 | { |
| 78 | uint8_t b[8]; |
| 79 | sv1_put64(b, v); |
| 80 | put_hex(s, k, b, 8); |
| 81 | } |
| 82 | |
| 83 | static const uint8_t UUID[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; |
| 84 | |
| 85 | static size_t preimage(uint16_t kind, uint16_t ver, const uint8_t *sem, size_t n, uint8_t *out) |
| 86 | { |
| 87 | static const char dom[] = "AIENOS-STORE-OBJECT-V1"; /* + NUL */ |
| 88 | size_t o = sizeof dom; /* includes the NUL */ |
| 89 | memcpy(out, dom, sizeof dom); |
| 90 | sv1_put16(out + o, kind); |
| 91 | sv1_put16(out + o + 2, ver); |
| 92 | sv1_put64(out + o + 4, n); |
| 93 | memcpy(out + o + 12, sem, n); |
| 94 | return o + 12 + n; |
| 95 | } |
| 96 | |
| 97 | static void unit_of(const uint8_t *sem, size_t n, uint8_t u[SV1_UNIT]) |
| 98 | { |
| 99 | memset(u, 0, SV1_UNIT); |
| 100 | memcpy(u, sem, n); |
| 101 | } |
| 102 | |
| 103 | static void make_sb(uint32_t slot, const sv1_commit *c, uint64_t commit_unit, sv1_superblock *sb) |
| 104 | { |
| 105 | memset(sb, 0, sizeof *sb); |
| 106 | memcpy(sb->store_uuid, c->store_uuid, 16); |
| 107 | sb->slot_id = slot; |
| 108 | sb->region_units = c->region_units; |
| 109 | sb->generation = c->generation; |
| 110 | sv1_commit_object_id(c, sb->commit_record_id); |
| 111 | sb->commit_record_unit = commit_unit; |
| 112 | memcpy(sb->catalog_id, c->catalog_id, 32); |
| 113 | sb->catalog_first_unit = c->catalog_first_unit; |
| 114 | sb->catalog_byte_length = c->catalog_byte_length; |
| 115 | sb->catalog_unit_count = c->catalog_unit_count; |
| 116 | sb->catalog_entry_count = c->catalog_entry_count; |
| 117 | sb->committed_high_water = c->committed_high_water; |
| 118 | } |
| 119 | |
| 120 | /* encode + round trip + slot binding + CRC domain (Rust encode_superblock) */ |
| 121 | static void encode_sb(const sv1_superblock *sb, uint8_t out[SV1_UNIT]) |
| 122 | { |
| 123 | sv1_superblock back; |
| 124 | CHECK_EQ(sv1_superblock_encode(sb, out), 0, "superblock encode"); |
| 125 | CHECK_EQ(sv1_superblock_decode(out, SV1_UNIT, sb->slot_id, &back), 0, "superblock decode"); |
| 126 | CHECK(memcmp(&back, sb, sizeof back) == 0 || (sv1_superblock_equivalent(&back, sb) && |
| 127 | back.slot_id == sb->slot_id), |
| 128 | "superblock round trip"); |
| 129 | CHECK(sv1_superblock_decode(out, SV1_UNIT, 1 - sb->slot_id, &back) != 0, "slot bound"); |
| 130 | uint8_t z[SV1_UNIT]; |
| 131 | memcpy(z, out, SV1_UNIT); |
| 132 | memset(z + SV1_SB_CRC_OFFSET, 0, 4); |
| 133 | CHECK(sv1_crc32c(z, SV1_UNIT) == sv1_get32(out + SV1_SB_CRC_OFFSET), "superblock CRC domain"); |
| 134 | } |
| 135 | |
| 136 | typedef struct { |
| 137 | uint8_t empty_cat[16]; |
| 138 | uint8_t empty_cat_id[32]; |
| 139 | sv1_entry one_entry; |
| 140 | uint8_t one_cat[80]; |
| 141 | uint8_t one_cat_id[32]; |
| 142 | uint8_t app[16]; |
| 143 | uint8_t app_id[32]; |
| 144 | sv1_commit g1, g2; |
| 145 | } graph_t; |
| 146 | |
| 147 | static void build_graph(graph_t *g) |
| 148 | { |
| 149 | size_t len; |
| 150 | memset(g, 0, sizeof *g); |
| 151 | CHECK_EQ(sv1_catalog_encode(NULL, 0, g->empty_cat, sizeof g->empty_cat, &len), 0, "empty cat"); |
| 152 | CHECK_EQ(len, 16, "empty catalog length"); |
| 153 | sv1_object_id(1, 1, g->empty_cat, 16, g->empty_cat_id); |
| 154 | for (int i = 0; i < 16; i++) |
| 155 | g->app[i] = (uint8_t)i; |
| 156 | sv1_object_id(3, 1, g->app, 16, g->app_id); |
| 157 | memcpy(g->one_entry.object_id, g->app_id, 32); |
| 158 | g->one_entry.kind = 3; |
| 159 | g->one_entry.version = 1; |
| 160 | g->one_entry.first_unit = 4; |
| 161 | g->one_entry.byte_length = 16; |
| 162 | g->one_entry.unit_count = 1; |
| 163 | CHECK_EQ(sv1_catalog_encode(&g->one_entry, 1, g->one_cat, sizeof g->one_cat, &len), 0, "one cat"); |
| 164 | CHECK_EQ(len, 80, "one-entry catalog length"); |
| 165 | sv1_object_id(1, 1, g->one_cat, 80, g->one_cat_id); |
| 166 | sv1_commit *c = &g->g1; |
| 167 | memcpy(c->store_uuid, UUID, 16); |
| 168 | c->region_units = 16; |
| 169 | c->generation = 1; |
| 170 | memcpy(c->catalog_id, g->empty_cat_id, 32); |
| 171 | c->catalog_first_unit = 2; |
| 172 | c->catalog_byte_length = 16; |
| 173 | c->catalog_unit_count = 1; |
| 174 | c->committed_high_water = 4; |
| 175 | sv1_commit *d = &g->g2; |
| 176 | *d = *c; |
| 177 | d->generation = 2; |
| 178 | d->previous_generation = 1; |
| 179 | sv1_commit_object_id(c, d->previous_commit_id); |
| 180 | memcpy(d->previous_catalog_id, g->empty_cat_id, 32); |
| 181 | memcpy(d->catalog_id, g->one_cat_id, 32); |
| 182 | d->catalog_first_unit = 5; |
| 183 | d->catalog_byte_length = 80; |
| 184 | d->catalog_entry_count = 1; |
| 185 | d->committed_high_water = 7; |
| 186 | } |
| 187 | |
| 188 | static void golden(void) |
| 189 | { |
| 190 | graph_t g; |
| 191 | build_graph(&g); |
| 192 | uint8_t u[SV1_UNIT], pre[512], sem[SV1_COMMIT_BYTES], id[32]; |
| 193 | size_t pn; |
| 194 | |
| 195 | put_text("", "schema", "AIENOS-STORE-V1-GOLDEN-VECTORS-1"); |
| 196 | put_text("", "source_contract", "ADR 0015, format layout tables"); |
| 197 | |
| 198 | /* object_id */ |
| 199 | { |
| 200 | const char *S = "object_id"; |
| 201 | uint8_t semantic[16]; |
| 202 | for (int i = 0; i < 16; i++) |
| 203 | semantic[i] = (uint8_t)i; |
| 204 | sv1_object_id(0x1234, 0x5678, semantic, 16, id); |
| 205 | pn = preimage(0x1234, 0x5678, semantic, 16, pre); |
| 206 | put_le16(S, "kind_u16le", 0x1234); |
| 207 | put_le16(S, "version_u16le", 0x5678); |
| 208 | put_hex(S, "semantic_bytes_hex", semantic, 16); |
| 209 | put_hex(S, "preimage_hex", pre, pn); |
| 210 | put_hex(S, "object_id_hex", id, 32); |
| 211 | put_sha(S, "preimage_sha256", pre, pn); |
| 212 | put_sha(S, "semantic_sha256", semantic, 16); |
| 213 | uint8_t d[32]; |
| 214 | sha256_hash(pre, pn, d); |
| 215 | CHECK(memcmp(d, id, 32) == 0, "ObjectId is SHA-256 of the preimage"); |
| 216 | } |
| 217 | /* empty_catalog */ |
| 218 | { |
| 219 | const char *S = "empty_catalog"; |
| 220 | sv1_entry tmp[1]; |
| 221 | uint32_t n = 99; |
| 222 | put_hex(S, "semantic_bytes_hex", g.empty_cat, 16); |
| 223 | put_hex(S, "object_id_hex", g.empty_cat_id, 32); |
| 224 | unit_of(g.empty_cat, 16, u); |
| 225 | put_hex(S, "unit_hex", u, SV1_UNIT); |
| 226 | put_sha(S, "unit_sha256", u, SV1_UNIT); |
| 227 | put_sha(S, "semantic_sha256", g.empty_cat, 16); |
| 228 | CHECK(sv1_catalog_decode(g.empty_cat, 16, tmp, 1, &n) == 0 && n == 0, "empty decode"); |
| 229 | } |
| 230 | /* one_entry_catalog */ |
| 231 | { |
| 232 | const char *S = "one_entry_catalog"; |
| 233 | uint8_t e[64]; |
| 234 | sv1_entry back[2]; |
| 235 | uint32_t n = 0; |
| 236 | CHECK_EQ(sv1_entry_encode(&g.one_entry, e), 0, "entry encode"); |
| 237 | CHECK(memcmp(g.one_cat + 16, e, 64) == 0, "catalog body is the entry"); |
| 238 | put_hex(S, "entry_object_id_hex", e, 32); |
| 239 | put_hex(S, "entry_kind_u16le", e + 32, 2); |
| 240 | put_hex(S, "entry_version_u16le", e + 34, 2); |
| 241 | put_hex(S, "entry_first_unit_u64le", e + 36, 8); |
| 242 | put_hex(S, "entry_byte_length_u64le", e + 44, 8); |
| 243 | put_hex(S, "entry_unit_count_u32le", e + 52, 4); |
| 244 | put_hex(S, "entry_flags_u16le", e + 56, 2); |
| 245 | put_hex(S, "entry_reserved_hex", e + 58, 6); |
| 246 | CHECK(memcmp(e, g.app_id, 32) == 0, "entry id is the app id"); |
| 247 | put_hex(S, "semantic_bytes_hex", g.one_cat, 80); |
| 248 | put_hex(S, "object_id_hex", g.one_cat_id, 32); |
| 249 | unit_of(g.one_cat, 80, u); |
| 250 | put_hex(S, "unit_hex", u, SV1_UNIT); |
| 251 | put_sha(S, "unit_sha256", u, SV1_UNIT); |
| 252 | put_sha(S, "semantic_sha256", g.one_cat, 80); |
| 253 | CHECK(sv1_entry_decode(e, 64, &back[0]) == 0 && sv1_entry_equal(&back[0], &g.one_entry), |
| 254 | "entry decode"); |
| 255 | CHECK(sv1_catalog_decode(g.one_cat, 80, back, 2, &n) == 0 && n == 1 && |
| 256 | sv1_entry_equal(&back[0], &g.one_entry), |
| 257 | "catalog decode"); |
| 258 | } |
| 259 | /* crc32c */ |
| 260 | { |
| 261 | const char *S = "crc32c"; |
| 262 | uint32_t crc = sv1_crc32c((const uint8_t *)"123456789", 9); |
| 263 | char t[16]; |
| 264 | uint8_t b[4]; |
| 265 | put_text(S, "check_input_ascii", "123456789"); |
| 266 | snprintf(t, sizeof t, "0x%08x", crc); |
| 267 | put_text(S, "check_output_numeric", t); |
| 268 | CHECK(crc == 0xe3069283u, "crc32c check value"); |
| 269 | sv1_put32(b, crc); |
| 270 | put_hex(S, "check_output_u32le", b, 4); |
| 271 | put_text(S, "algorithm", "reflected Castagnoli polynomial 0x82f63b78, init/xorout 0xffffffff"); |
| 272 | } |
| 273 | /* genesis */ |
| 274 | uint8_t ga[SV1_UNIT], gb[SV1_UNIT]; |
| 275 | { |
| 276 | const char *S = "genesis"; |
| 277 | sv1_commit back; |
| 278 | sv1_superblock sa, sbb, da, db; |
| 279 | put_hex(S, "store_uuid_hex", UUID, 16); |
| 280 | put_le64(S, "region_units_u64le", g.g1.region_units); |
| 281 | put_num(S, "catalog_first_unit", g.g1.catalog_first_unit); |
| 282 | put_num(S, "commit_record_unit_number", 3); |
| 283 | put_text(S, "unit_map.0", "superblock A"); |
| 284 | put_text(S, "unit_map.1", "superblock B"); |
| 285 | put_text(S, "unit_map.2", "genesis catalog"); |
| 286 | put_text(S, "unit_map.3", "genesis CommitRecord"); |
| 287 | put_hex(S, "catalog_semantic_hex", g.empty_cat, 16); |
| 288 | unit_of(g.empty_cat, 16, u); |
| 289 | put_hex(S, "catalog_unit_hex", u, SV1_UNIT); |
| 290 | put_hex(S, "catalog_object_id_hex", g.empty_cat_id, 32); |
| 291 | CHECK_EQ(sv1_commit_encode(&g.g1, sem), 0, "genesis commit encode"); |
| 292 | sv1_commit_object_id(&g.g1, id); |
| 293 | pn = preimage(2, 1, sem, SV1_COMMIT_BYTES, pre); |
| 294 | put_hex(S, "commit_record_semantic_hex", sem, SV1_COMMIT_BYTES); |
| 295 | put_hex(S, "commit_record_object_id_hex", id, 32); |
| 296 | unit_of(sem, SV1_COMMIT_BYTES, u); |
| 297 | put_hex(S, "commit_record_unit_hex", u, SV1_UNIT); |
| 298 | put_sha(S, "commit_record_unit_sha256", u, SV1_UNIT); |
| 299 | put_hex(S, "commit_record_crc_domain_preimage_hex", pre, pn); |
| 300 | uint8_t d[32]; |
| 301 | sha256_hash(pre, pn, d); |
| 302 | CHECK(memcmp(d, id, 32) == 0, "commit id is SHA-256 of preimage"); |
| 303 | CHECK(sv1_commit_decode(sem, SV1_COMMIT_BYTES, &back) == 0 && |
| 304 | memcmp(&back, &g.g1, sizeof back) == 0, |
| 305 | "commit round trip"); |
| 306 | make_sb(0, &g.g1, 3, &sa); |
| 307 | make_sb(1, &g.g1, 3, &sbb); |
| 308 | encode_sb(&sa, ga); |
| 309 | encode_sb(&sbb, gb); |
| 310 | put_hex(S, "superblock_a_unit_hex", ga, SV1_UNIT); |
| 311 | put_sha(S, "superblock_a_unit_sha256", ga, SV1_UNIT); |
| 312 | put_hex(S, "superblock_a_crc_u32le", ga + SV1_SB_CRC_OFFSET, 4); |
| 313 | put_hex(S, "superblock_b_unit_hex", gb, SV1_UNIT); |
| 314 | put_sha(S, "superblock_b_unit_sha256", gb, SV1_UNIT); |
| 315 | put_hex(S, "superblock_b_crc_u32le", gb + SV1_SB_CRC_OFFSET, 4); |
| 316 | CHECK(memcmp(ga, gb, SV1_UNIT) != 0, "A != B"); |
| 317 | CHECK(memcmp(ga + 168, gb + 168, 4) != 0, "CRC A != CRC B"); |
| 318 | put_text(S, "expected_root_classification", "redundant valid copies; equivalent logical roots"); |
| 319 | CHECK(sv1_superblock_decode(ga, SV1_UNIT, 0, &da) == 0 && |
| 320 | sv1_superblock_decode(gb, SV1_UNIT, 1, &db) == 0 && |
| 321 | sv1_superblock_equivalent(&da, &db), |
| 322 | "genesis A/B equivalent"); |
| 323 | CHECK(sv1_superblock_validate_commit(&da, id, &g.g1) == 0, "A binds genesis"); |
| 324 | CHECK(sv1_superblock_validate_commit(&db, id, &g.g1) == 0, "B binds genesis"); |
| 325 | |
| 326 | /* the C genesis writer produces exactly the vector's units 0, 2, 3 */ |
| 327 | static uint8_t gu[4][SV1_UNIT]; |
| 328 | CHECK_EQ(sv1_genesis_units(UUID, 16, gu), 0, "genesis units"); |
| 329 | CHECK(memcmp(gu[0], ga, SV1_UNIT) == 0, "genesis unit 0 = vector superblock A"); |
| 330 | CHECK(sv1_all_zero(gu[1], SV1_UNIT), "genesis unit 1 is zero (B written later)"); |
| 331 | unit_of(g.empty_cat, 16, u); |
| 332 | CHECK(memcmp(gu[2], u, SV1_UNIT) == 0, "genesis unit 2 = vector catalog unit"); |
| 333 | unit_of(sem, SV1_COMMIT_BYTES, u); |
| 334 | CHECK(memcmp(gu[3], u, SV1_UNIT) == 0, "genesis unit 3 = vector commit unit"); |
| 335 | |
| 336 | /* the engine classifies the vector image as the vector says */ |
| 337 | memdev m; |
| 338 | st_store s; |
| 339 | static st_workspace ws; |
| 340 | mem_init(&m, 16); |
| 341 | memcpy(m.units[0], ga, SV1_UNIT); |
| 342 | memcpy(m.units[1], gb, SV1_UNIT); |
| 343 | memcpy(m.units[2], gu[2], SV1_UNIT); |
| 344 | memcpy(m.units[3], gu[3], SV1_UNIT); |
| 345 | st_dev dv = mem_dev(&m); |
| 346 | CHECK_EQ(st_open(&s, &dv, &ws), 0, "open genesis vector image"); |
| 347 | CHECK(s.state == ST_VALID && s.peer == ST_PEER_VALID && st_generation(&s) == 1, |
| 348 | "genesis vector: equivalent roots mount Valid"); |
| 349 | mem_free(&m); |
| 350 | } |
| 351 | /* adjacent_generations */ |
| 352 | { |
| 353 | const char *S = "adjacent_generations"; |
| 354 | uint8_t g1id[32], g2id[32], sb2[SV1_UNIT]; |
| 355 | sv1_commit back; |
| 356 | sv1_superblock s2; |
| 357 | sv1_commit_object_id(&g.g1, g1id); |
| 358 | put_hex(S, "generation_1_commit_record_object_id_hex", g1id, 32); |
| 359 | put_hex(S, "generation_1_catalog_object_id_hex", g.empty_cat_id, 32); |
| 360 | put_num(S, "generation_1_committed_high_water", g.g1.committed_high_water); |
| 361 | put_text(S, "unit_map.0", "generation 2 superblock A"); |
| 362 | put_text(S, "unit_map.1", "generation 1 superblock B"); |
| 363 | put_text(S, "unit_map.2", "generation 1 catalog"); |
| 364 | put_text(S, "unit_map.3", "generation 1 CommitRecord"); |
| 365 | put_text(S, "unit_map.4", "generation 2 application object"); |
| 366 | put_text(S, "unit_map.5", "generation 2 catalog"); |
| 367 | put_text(S, "unit_map.6", "generation 2 CommitRecord"); |
| 368 | put_hex(S, "application_object_semantic_hex", g.app, 16); |
| 369 | put_hex(S, "application_object_id_hex", g.app_id, 32); |
| 370 | unit_of(g.app, 16, u); |
| 371 | put_hex(S, "application_object_unit_hex", u, SV1_UNIT); |
| 372 | put_sha(S, "application_object_unit_sha256", u, SV1_UNIT); |
| 373 | put_hex(S, "generation_2_catalog_semantic_hex", g.one_cat, 80); |
| 374 | put_hex(S, "generation_2_catalog_object_id_hex", g.one_cat_id, 32); |
| 375 | unit_of(g.one_cat, 80, u); |
| 376 | put_hex(S, "generation_2_catalog_unit_hex", u, SV1_UNIT); |
| 377 | put_sha(S, "generation_2_catalog_unit_sha256", u, SV1_UNIT); |
| 378 | CHECK_EQ(sv1_commit_encode(&g.g2, sem), 0, "gen2 commit encode"); |
| 379 | sv1_commit_object_id(&g.g2, g2id); |
| 380 | pn = preimage(2, 1, sem, SV1_COMMIT_BYTES, pre); |
| 381 | put_hex(S, "generation_2_commit_record_semantic_hex", sem, SV1_COMMIT_BYTES); |
| 382 | put_hex(S, "generation_2_commit_record_object_id_hex", g2id, 32); |
| 383 | unit_of(sem, SV1_COMMIT_BYTES, u); |
| 384 | put_hex(S, "generation_2_commit_record_unit_hex", u, SV1_UNIT); |
| 385 | put_sha(S, "generation_2_commit_record_unit_sha256", u, SV1_UNIT); |
| 386 | put_hex(S, "generation_2_commit_record_preimage_hex", pre, pn); |
| 387 | uint8_t d[32]; |
| 388 | sha256_hash(pre, pn, d); |
| 389 | CHECK(memcmp(d, g2id, 32) == 0, "gen2 id is SHA-256 of preimage"); |
| 390 | CHECK(sv1_commit_decode(sem, SV1_COMMIT_BYTES, &back) == 0 && |
| 391 | memcmp(&back, &g.g2, sizeof back) == 0, |
| 392 | "gen2 commit round trip"); |
| 393 | make_sb(0, &g.g2, 6, &s2); |
| 394 | CHECK(sv1_superblock_validate_commit(&s2, g2id, &g.g2) == 0, "gen2 sb binds commit"); |
| 395 | encode_sb(&s2, sb2); |
| 396 | put_hex(S, "generation_2_superblock_a_unit_hex", sb2, SV1_UNIT); |
| 397 | put_sha(S, "generation_2_superblock_a_unit_sha256", sb2, SV1_UNIT); |
| 398 | put_hex(S, "generation_2_superblock_a_crc_u32le", sb2 + SV1_SB_CRC_OFFSET, 4); |
| 399 | put_num(S, "generation_2_committed_high_water", g.g2.committed_high_water); |
| 400 | put_text(S, "expected_root_classification", |
| 401 | "normal adjacent history; generation 2 follows generation 1 exactly"); |
| 402 | CHECK(sv1_commit_identifies_predecessor(&g.g2, &g.g1, g1id), "gen2 identifies gen1"); |
| 403 | CHECK(memcmp(sem + 72, g1id, 32) == 0, "previous_commit_id links genesis"); |
| 404 | CHECK(memcmp(sem + 104, g.empty_cat_id, 32) == 0, "previous_catalog_id links genesis"); |
| 405 | |
| 406 | /* engine: the vector's 7-unit map mounts as generation 2, peer valid; |
| 407 | * and a C transaction from genesis writes exactly these units. */ |
| 408 | memdev m, m2; |
| 409 | st_store s; |
| 410 | static st_workspace ws; |
| 411 | mem_init(&m, 16); |
| 412 | memcpy(m.units[0], sb2, SV1_UNIT); |
| 413 | memcpy(m.units[1], gb, SV1_UNIT); |
| 414 | unit_of(g.empty_cat, 16, m.units[2]); |
| 415 | static uint8_t gu[4][SV1_UNIT]; |
| 416 | sv1_genesis_units(UUID, 16, gu); |
| 417 | memcpy(m.units[3], gu[3], SV1_UNIT); |
| 418 | unit_of(g.app, 16, m.units[4]); |
| 419 | unit_of(g.one_cat, 80, m.units[5]); |
| 420 | unit_of(sem, SV1_COMMIT_BYTES, m.units[6]); |
| 421 | st_dev dv = mem_dev(&m); |
| 422 | CHECK_EQ(st_open(&s, &dv, &ws), 0, "open adjacent vector image"); |
| 423 | CHECK(s.state == ST_VALID && s.peer == ST_PEER_VALID && st_generation(&s) == 2, |
| 424 | "adjacent vector: generation 2 selected, peer valid"); |
| 425 | |
| 426 | /* from a genesis whose active slot is B (so the new root lands in A) */ |
| 427 | mem_init(&m2, 16); |
| 428 | memcpy(m2.units[1], gb, SV1_UNIT); |
| 429 | memcpy(m2.units[2], gu[2], SV1_UNIT); |
| 430 | memcpy(m2.units[3], gu[3], SV1_UNIT); |
| 431 | st_dev dv2 = mem_dev(&m2); |
| 432 | CHECK_EQ(st_open(&s, &dv2, &ws), 0, "open genesis(B)"); |
| 433 | st_object o = {3, 1, g.app, 16}; |
| 434 | CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), 0, "transact app object"); |
| 435 | int same = 1; |
| 436 | for (int i = 0; i < 16; i++) |
| 437 | if (i != 1 && memcmp(m.units[i], m2.units[i], SV1_UNIT)) |
| 438 | same = 0; |
| 439 | CHECK(same && memcmp(m2.units[1], gb, SV1_UNIT) == 0, |
| 440 | "C transaction writes the adjacent_generations vector byte for byte"); |
| 441 | mem_free(&m); |
| 442 | mem_free(&m2); |
| 443 | } |
| 444 | /* equivalent_roots */ |
| 445 | { |
| 446 | const char *S = "equivalent_roots"; |
| 447 | put_hex(S, "superblock_a_unit_hex", ga, SV1_UNIT); |
| 448 | put_sha(S, "superblock_a_unit_sha256", ga, SV1_UNIT); |
| 449 | put_hex(S, "superblock_b_unit_hex", gb, SV1_UNIT); |
| 450 | put_sha(S, "superblock_b_unit_sha256", gb, SV1_UNIT); |
| 451 | put_hex(S, "superblock_a_crc_u32le", ga + SV1_SB_CRC_OFFSET, 4); |
| 452 | put_hex(S, "superblock_b_crc_u32le", gb + SV1_SB_CRC_OFFSET, 4); |
| 453 | put_text(S, "expected_root_classification", |
| 454 | "same generation and equivalent logical root; redundant valid copies"); |
| 455 | CHECK(memcmp(ga, gb, SV1_UNIT) != 0, "redundant copies differ in slot and CRC"); |
| 456 | } |
| 457 | |
| 458 | /* bidirectional match */ |
| 459 | int matched_rows = 0; |
| 460 | for (size_t r = 0; r < GOLDEN_ROWS; r++) { |
| 461 | int found = 0; |
| 462 | for (int i = 0; i < ncomp; i++) { |
| 463 | if (strcmp(comp[i].section, GOLDEN[r].section) || strcmp(comp[i].key, GOLDEN[r].key)) |
| 464 | continue; |
| 465 | found = 1; |
| 466 | comp[i].matched++; |
| 467 | CHECK(strcmp(comp[i].value, GOLDEN[r].value) == 0, "golden %s.%s differs", |
| 468 | GOLDEN[r].section, GOLDEN[r].key); |
| 469 | } |
| 470 | CHECK(found, "golden %s.%s not asserted by the C test", GOLDEN[r].section, GOLDEN[r].key); |
| 471 | matched_rows += found; |
| 472 | } |
| 473 | for (int i = 0; i < ncomp; i++) { |
| 474 | CHECK(comp[i].matched == 1, "computed %s.%s missing from JSON (or duplicated)", |
| 475 | comp[i].section, comp[i].key); |
| 476 | free(comp[i].value); |
| 477 | } |
| 478 | printf("golden vectors: %d of %zu JSON rows covered\n", matched_rows, |
| 479 | (size_t)GOLDEN_ROWS); |
| 480 | } |
| 481 | |
| 482 | /* ---- negative tests (store_v1_negative.rs) ---- */ |
| 483 | static void ent(uint64_t first, const char *data, sv1_entry *e) |
| 484 | { |
| 485 | size_t n = strlen(data); |
| 486 | memset(e, 0, sizeof *e); |
| 487 | sv1_object_id(3, 1, (const uint8_t *)data, n, e->object_id); |
| 488 | e->kind = 3; |
| 489 | e->version = 1; |
| 490 | e->first_unit = first; |
| 491 | e->byte_length = n; |
| 492 | e->unit_count = 1; |
| 493 | } |
| 494 | static void neg_genesis(sv1_commit *c) |
| 495 | { |
| 496 | uint8_t cat[16]; |
| 497 | size_t len; |
| 498 | memset(c, 0, sizeof *c); |
| 499 | sv1_catalog_encode(NULL, 0, cat, 16, &len); |
| 500 | memset(c->store_uuid, 0x11, 16); |
| 501 | c->region_units = 8; |
| 502 | c->generation = 1; |
| 503 | sv1_object_id(1, 1, cat, 16, c->catalog_id); |
| 504 | c->catalog_first_unit = 2; |
| 505 | c->catalog_byte_length = 16; |
| 506 | c->catalog_unit_count = 1; |
| 507 | c->committed_high_water = 4; |
| 508 | } |
| 509 | |
| 510 | static void neg_catalog_header(void) |
| 511 | { |
| 512 | uint8_t v[17]; |
| 513 | size_t len; |
| 514 | sv1_entry e[2]; |
| 515 | uint32_t n; |
| 516 | sv1_catalog_encode(NULL, 0, v, 16, &len); |
| 517 | const struct { int off; uint8_t val; } cases[] = {{0, 'X'}, {8, 2}, {10, 63}}; |
| 518 | for (size_t i = 0; i < 3; i++) { |
| 519 | uint8_t m[16]; |
| 520 | memcpy(m, v, 16); |
| 521 | m[cases[i].off] = cases[i].val; |
| 522 | CHECK(sv1_catalog_decode(m, 16, e, 2, &n) != 0, "catalog header offset %d", cases[i].off); |
| 523 | } |
| 524 | uint8_t m[17]; |
| 525 | memcpy(m, v, 16); |
| 526 | sv1_put32(m + 12, 1); |
| 527 | CHECK_EQ(sv1_catalog_decode(m, 16, e, 2, &n), SV1_E_INVALID_LENGTH, "wrong count"); |
| 528 | CHECK_EQ(sv1_catalog_decode(v, 15, e, 2, &n), SV1_E_INVALID_LENGTH, "short catalog"); |
| 529 | memcpy(m, v, 16); |
| 530 | m[16] = 0; |
| 531 | CHECK_EQ(sv1_catalog_decode(m, 17, e, 2, &n), SV1_E_INVALID_LENGTH, "trailing byte"); |
| 532 | memcpy(m, v, 16); |
| 533 | sv1_put32(m + 12, SV1_MAX_CATALOG_ENTRIES + 1); |
| 534 | CHECK_EQ(sv1_catalog_decode(m, 16, e, 2, &n), SV1_E_CATALOG_TOO_LARGE, "over limit"); |
| 535 | /* v1.rs: bad magic is BadCatalogMagic exactly */ |
| 536 | memcpy(m, v, 16); |
| 537 | m[0] ^= 1; |
| 538 | CHECK_EQ(sv1_catalog_decode(m, 16, e, 2, &n), SV1_E_BAD_CATALOG_MAGIC, "bad catalog magic"); |
| 539 | } |
| 540 | |
| 541 | static void neg_entry_reserved(void) |
| 542 | { |
| 543 | sv1_entry e, back; |
| 544 | uint8_t enc[64], m[64]; |
| 545 | ent(2, "x", &e); |
| 546 | sv1_entry_encode(&e, enc); |
| 547 | for (int off = 58; off < 64; off++) { |
| 548 | memcpy(m, enc, 64); |
| 549 | m[off] = 1; |
| 550 | CHECK_EQ(sv1_entry_decode(m, 64, &back), SV1_E_NONZERO_RESERVED, "entry reserved"); |
| 551 | } |
| 552 | memcpy(m, enc, 64); |
| 553 | m[56] = 1; |
| 554 | CHECK_EQ(sv1_entry_decode(m, 64, &back), SV1_E_MALFORMED_DESCRIPTOR, "entry flags"); |
| 555 | const struct { int off; uint64_t val; } cases[] = { |
| 556 | {32, 0}, {34, 0}, {44, 0}, {44, 67108865}, {52, 0}, {52, 2}}; |
| 557 | for (size_t i = 0; i < 6; i++) { |
| 558 | memcpy(m, enc, 64); |
| 559 | int w = cases[i].off == 44 ? 8 : cases[i].off == 52 ? 4 : 2; |
| 560 | uint8_t b[8]; |
| 561 | sv1_put64(b, cases[i].val); |
| 562 | memcpy(m + cases[i].off, b, (size_t)w); |
| 563 | CHECK(sv1_entry_decode(m, 64, &back) != 0, "entry offset %d value %llu", cases[i].off, |
| 564 | (unsigned long long)cases[i].val); |
| 565 | } |
| 566 | uint8_t id[32]; |
| 567 | CHECK_EQ(sv1_object_id(3, 1, (const uint8_t *)"", 0, id), SV1_E_INVALID_OBJECT, "empty object"); |
| 568 | CHECK_EQ(sv1_object_id(0, 1, (const uint8_t *)"xxxxxxxxxxxxxxxx", 16, id), SV1_E_INVALID_OBJECT, |
| 569 | "kind zero object"); |
| 570 | e.flags = 1; |
| 571 | CHECK_EQ(sv1_entry_encode(&e, enc), SV1_E_MALFORMED_DESCRIPTOR, "encode bad flags"); |
| 572 | } |
| 573 | |
| 574 | static void neg_catalog_order(void) |
| 575 | { |
| 576 | sv1_entry a, b, low, high, list[2], back[2]; |
| 577 | uint8_t buf[160]; |
| 578 | size_t len; |
| 579 | uint32_t n; |
| 580 | ent(2, "a", &a); |
| 581 | ent(3, "b", &b); |
| 582 | if (sv1_cmp_id(a.object_id, b.object_id) < 0) { |
| 583 | low = a; |
| 584 | high = b; |
| 585 | } else { |
| 586 | low = b; |
| 587 | high = a; |
| 588 | } |
| 589 | list[0] = low; |
| 590 | list[1] = high; |
| 591 | CHECK_EQ(sv1_catalog_encode(list, 2, buf, sizeof buf, &len), 0, "sorted encode"); |
| 592 | CHECK_EQ(sv1_catalog_decode(buf, len, back, 2, &n), 0, "sorted decode"); |
| 593 | list[0] = high; |
| 594 | list[1] = low; |
| 595 | CHECK_EQ(sv1_catalog_encode(list, 2, buf, sizeof buf, &len), SV1_E_CATALOG_ORDER, "descending"); |
| 596 | list[0] = low; |
| 597 | list[1] = low; |
| 598 | CHECK_EQ(sv1_catalog_encode(list, 2, buf, sizeof buf, &len), SV1_E_DUPLICATE_OBJECT, "duplicate"); |
| 599 | /* decoder side: hand-built descending bytes */ |
| 600 | list[0] = low; |
| 601 | list[1] = high; |
| 602 | sv1_catalog_encode(list, 2, buf, sizeof buf, &len); |
| 603 | uint8_t t[64]; |
| 604 | memcpy(t, buf + 16, 64); |
| 605 | memcpy(buf + 16, buf + 80, 64); |
| 606 | memcpy(buf + 80, t, 64); |
| 607 | CHECK_EQ(sv1_catalog_decode(buf, len, back, 2, &n), SV1_E_CATALOG_ORDER, "decode descending"); |
| 608 | memcpy(buf + 16, buf + 80, 64); |
| 609 | CHECK_EQ(sv1_catalog_decode(buf, len, back, 2, &n), SV1_E_DUPLICATE_OBJECT, "decode duplicate"); |
| 610 | } |
| 611 | |
| 612 | static void neg_bounds(void) |
| 613 | { |
| 614 | sv1_entry e; |
| 615 | ent(2, "x", &e); |
| 616 | CHECK_EQ(sv1_entry_validate_bounds(&e, 8, 4, 5), 0, "valid bounds"); |
| 617 | ent(0, "x", &e); |
| 618 | CHECK_EQ(sv1_entry_validate_bounds(&e, 8, 4, 5), SV1_E_OUT_OF_BOUNDS, "unit 0"); |
| 619 | ent(1, "x", &e); |
| 620 | CHECK_EQ(sv1_entry_validate_bounds(&e, 8, 4, 5), SV1_E_OUT_OF_BOUNDS, "unit 1"); |
| 621 | ent(4, "x", &e); |
| 622 | CHECK_EQ(sv1_entry_validate_bounds(&e, 8, 4, 5), SV1_E_OUT_OF_BOUNDS, "into catalog"); |
| 623 | ent(2, "x", &e); |
| 624 | CHECK_EQ(sv1_entry_validate_bounds(&e, 8, 4, 2), SV1_E_OUT_OF_BOUNDS, "past high water"); |
| 625 | ent(3, "x", &e); |
| 626 | CHECK_EQ(sv1_entry_validate_bounds(&e, 2, 8, 8), SV1_E_OUT_OF_BOUNDS, "past region"); |
| 627 | ent(2, "x", &e); |
| 628 | e.first_unit = UINT64_MAX; |
| 629 | CHECK_EQ(sv1_entry_validate_bounds(&e, 8, 8, 8), SV1_E_LENGTH_OVERFLOW, "overflow"); |
| 630 | /* v1.rs: */ |
| 631 | ent(2, "x", &e); |
| 632 | CHECK_EQ(sv1_entry_validate_bounds(&e, 8, 2, 8), SV1_E_OUT_OF_BOUNDS, "at catalog first"); |
| 633 | } |
| 634 | |
| 635 | static void neg_extents(void) |
| 636 | { |
| 637 | sv1_entry l[2]; |
| 638 | sv1_extent scratch[2]; |
| 639 | ent(2, "first", &l[0]); |
| 640 | ent(2, "second", &l[1]); |
| 641 | sv1_sort_entries(l, 2); |
| 642 | CHECK_EQ(sv1_catalog_validate_extents(l, 2, 16, 4, 8, scratch), SV1_E_OVERLAP, "overlap"); |
| 643 | ent(3, "third", &l[0]); |
| 644 | CHECK_EQ(sv1_catalog_validate_extents(l, 1, 16, 3, 8, scratch), SV1_E_OUT_OF_BOUNDS, |
| 645 | "metadata overlap"); |
| 646 | } |
| 647 | |
| 648 | static void neg_extent_identity(void) |
| 649 | { |
| 650 | const char *sem = "payload"; |
| 651 | size_t n = strlen(sem); |
| 652 | uint8_t id[32], x[SV1_UNIT], y[SV1_UNIT]; |
| 653 | sv1_object_id(3, 1, (const uint8_t *)sem, n, id); |
| 654 | unit_of((const uint8_t *)sem, n, x); |
| 655 | CHECK_EQ(sv1_validate_extent(id, 3, 1, n, x, SV1_UNIT), 0, "valid extent"); |
| 656 | memcpy(y, x, SV1_UNIT); |
| 657 | y[0] ^= 1; |
| 658 | CHECK_EQ(sv1_validate_extent(id, 3, 1, n, y, SV1_UNIT), SV1_E_INTEGRITY, "semantic mutation"); |
| 659 | memcpy(y, x, SV1_UNIT); |
| 660 | y[SV1_UNIT - 1] = 1; |
| 661 | CHECK_EQ(sv1_validate_extent(id, 3, 1, n, y, SV1_UNIT), SV1_E_NONZERO_RESERVED, "padding"); |
| 662 | CHECK_EQ(sv1_validate_extent(id, 4, 1, n, x, SV1_UNIT), SV1_E_INTEGRITY, "wrong kind"); |
| 663 | CHECK_EQ(sv1_validate_extent(id, 3, 2, n, x, SV1_UNIT), SV1_E_INTEGRITY, "wrong version"); |
| 664 | CHECK_EQ(sv1_validate_extent(id, 3, 1, n + 1, x, SV1_UNIT), SV1_E_INTEGRITY, "wrong length"); |
| 665 | CHECK_EQ(sv1_validate_extent(id, 3, 1, n, x, SV1_UNIT - 1), SV1_E_INVALID_LENGTH, "short extent"); |
| 666 | } |
| 667 | |
| 668 | static void neg_superblock(void) |
| 669 | { |
| 670 | sv1_commit c; |
| 671 | sv1_superblock sb, out; |
| 672 | uint8_t v[SV1_UNIT], m[SV1_UNIT]; |
| 673 | CHECK(sv1_crc32c((const uint8_t *)"123456789", 9) == 0xe3069283u, "crc kat"); |
| 674 | neg_genesis(&c); |
| 675 | make_sb(0, &c, 3, &sb); |
| 676 | CHECK_EQ(sv1_superblock_encode(&sb, v), 0, "sb encode"); |
| 677 | CHECK_EQ(sv1_superblock_decode(v, SV1_UNIT, 0, &out), 0, "sb decode"); |
| 678 | memcpy(m, v, SV1_UNIT); |
| 679 | m[56] ^= 1; |
| 680 | CHECK_EQ(sv1_superblock_decode(m, SV1_UNIT, 0, &out), SV1_E_BAD_SUPERBLOCK_CRC, "field corrupt"); |
| 681 | memcpy(m, v, SV1_UNIT); |
| 682 | m[168] ^= 1; |
| 683 | CHECK_EQ(sv1_superblock_decode(m, SV1_UNIT, 0, &out), SV1_E_BAD_SUPERBLOCK_CRC, "crc corrupt"); |
| 684 | int bad = 0; |
| 685 | for (int off = 172; off < (int)SV1_UNIT; off++) { |
| 686 | memcpy(m, v, SV1_UNIT); |
| 687 | m[off] = 1; |
| 688 | rechecksum(m); |
| 689 | if (sv1_superblock_decode(m, SV1_UNIT, 0, &out) != SV1_E_NONZERO_RESERVED) |
| 690 | bad++; |
| 691 | } |
| 692 | CHECK(bad == 0, "reserved bytes 172..4096: %d not refused as NonzeroReserved", bad); |
| 693 | g_checks += (int)SV1_UNIT - 172 - 1; |
| 694 | CHECK_EQ(sv1_superblock_decode(v, SV1_UNIT, 1, &out), SV1_E_WRONG_SUPERBLOCK_SLOT, "wrong slot"); |
| 695 | const struct { int off; uint64_t val; } cases[] = {{48, 3}, {56, 0}, {160, 5}}; |
| 696 | for (int i = 0; i < 3; i++) { |
| 697 | memcpy(m, v, SV1_UNIT); |
| 698 | sv1_put64(m + cases[i].off, cases[i].val); |
| 699 | rechecksum(m); |
| 700 | CHECK_EQ(sv1_superblock_decode(m, SV1_UNIT, 0, &out), SV1_E_MALFORMED_SUPERBLOCK, "sb shape"); |
| 701 | } |
| 702 | memcpy(m, v, SV1_UNIT); |
| 703 | m[12] = 1; |
| 704 | rechecksum(m); |
| 705 | CHECK_EQ(sv1_superblock_decode(m, SV1_UNIT, 0, &out), SV1_E_UNSUPPORTED_FEATURES, "sb features"); |
| 706 | memcpy(m, v, SV1_UNIT); |
| 707 | m[0] ^= 1; |
| 708 | rechecksum(m); |
| 709 | CHECK_EQ(sv1_superblock_decode(m, SV1_UNIT, 0, &out), SV1_E_BAD_SUPERBLOCK_MAGIC, "sb magic"); |
| 710 | } |
| 711 | |
| 712 | static void neg_commit(void) |
| 713 | { |
| 714 | sv1_commit good, back, t; |
| 715 | uint8_t enc[SV1_COMMIT_BYTES], m[SV1_COMMIT_BYTES + 1]; |
| 716 | neg_genesis(&good); |
| 717 | CHECK_EQ(sv1_commit_encode(&good, enc), 0, "commit encode"); |
| 718 | CHECK(sv1_commit_decode(enc, SV1_COMMIT_BYTES, &back) == 0 && |
| 719 | memcmp(&back, &good, sizeof back) == 0, |
| 720 | "commit round trip"); |
| 721 | memset(m, 0, sizeof m); |
| 722 | CHECK(sv1_commit_decode(m, SV1_COMMIT_BYTES - 1, &back) != 0, "short commit"); |
| 723 | CHECK(sv1_commit_decode(m, SV1_COMMIT_BYTES + 1, &back) != 0, "long commit"); |
| 724 | const struct { int off; uint64_t val; } cases[] = {{56, 0}, {64, 1}, {176, 15}, {184, 2}, |
| 725 | {188, 1}, {192, 3}, {192, 5}}; |
| 726 | for (int i = 0; i < 7; i++) { |
| 727 | memcpy(m, enc, SV1_COMMIT_BYTES); |
| 728 | sv1_put64(m + cases[i].off, cases[i].val); |
| 729 | CHECK(sv1_commit_decode(m, SV1_COMMIT_BYTES, &back) != 0, "commit offset %d value %llu", |
| 730 | cases[i].off, (unsigned long long)cases[i].val); |
| 731 | } |
| 732 | memcpy(m, enc, SV1_COMMIT_BYTES); |
| 733 | sv1_put64(m + 48, UINT64_C(4294967297)); |
| 734 | CHECK(sv1_commit_decode(m, SV1_COMMIT_BYTES, &back) != 0, "oversized region"); |
| 735 | memcpy(m, enc, SV1_COMMIT_BYTES); |
| 736 | m[16] = 1; |
| 737 | CHECK_EQ(sv1_commit_decode(m, SV1_COMMIT_BYTES, &back), SV1_E_UNSUPPORTED_FEATURES, "features"); |
| 738 | uint8_t id[32], pad[SV1_UNIT]; |
| 739 | sv1_commit_object_id(&good, id); |
| 740 | unit_of(enc, SV1_COMMIT_BYTES, pad); |
| 741 | CHECK_EQ(sv1_validate_extent(id, 2, 1, SV1_COMMIT_BYTES, pad, SV1_UNIT), 0, "padded commit"); |
| 742 | pad[SV1_UNIT - 1] = 1; |
| 743 | CHECK_EQ(sv1_validate_extent(id, 2, 1, SV1_COMMIT_BYTES, pad, SV1_UNIT), SV1_E_NONZERO_RESERVED, |
| 744 | "commit padding"); |
| 745 | t = good; |
| 746 | t.previous_generation = 1; |
| 747 | CHECK_EQ(sv1_commit_encode(&t, enc), SV1_E_INVALID_GENERATION, "genesis prev gen"); |
| 748 | t = good; |
| 749 | t.committed_high_water = 5; |
| 750 | CHECK_EQ(sv1_commit_encode(&t, enc), SV1_E_MALFORMED_COMMIT, "genesis high water"); |
| 751 | } |
| 752 | |
| 753 | static void neg_generation_link(void) |
| 754 | { |
| 755 | sv1_commit prev, next, ov; |
| 756 | uint8_t pid[32], enc[SV1_COMMIT_BYTES]; |
| 757 | neg_genesis(&prev); |
| 758 | sv1_commit_object_id(&prev, pid); |
| 759 | next = prev; |
| 760 | next.generation = 2; |
| 761 | next.previous_generation = 1; |
| 762 | memcpy(next.previous_commit_id, pid, 32); |
| 763 | memcpy(next.previous_catalog_id, prev.catalog_id, 32); |
| 764 | next.catalog_first_unit = 4; |
| 765 | next.committed_high_water = 6; |
| 766 | CHECK_EQ(sv1_commit_encode(&next, enc), 0, "next encode"); |
| 767 | CHECK(sv1_commit_identifies_predecessor(&next, &prev, pid), "identifies predecessor"); |
| 768 | next.previous_catalog_id[0] ^= 1; |
| 769 | CHECK(!sv1_commit_identifies_predecessor(&next, &prev, pid), "wrong prev catalog"); |
| 770 | memcpy(next.previous_catalog_id, prev.catalog_id, 32); |
| 771 | next.previous_commit_id[0] ^= 1; |
| 772 | CHECK(!sv1_commit_identifies_predecessor(&next, &prev, pid), "wrong prev commit"); |
| 773 | memcpy(next.previous_commit_id, pid, 32); |
| 774 | next.previous_generation = 0; |
| 775 | CHECK_EQ(sv1_commit_encode(&next, enc), SV1_E_INVALID_GENERATION, "non-adjacent"); |
| 776 | ov = next; |
| 777 | ov.generation = UINT64_MAX; |
| 778 | ov.previous_generation = UINT64_MAX - 1; |
| 779 | memset(ov.previous_commit_id, 1, 32); |
| 780 | memset(ov.previous_catalog_id, 1, 32); |
| 781 | CHECK_EQ(sv1_commit_encode(&ov, enc), 0, "max generation"); |
| 782 | ov.previous_generation = UINT64_MAX; |
| 783 | CHECK_EQ(sv1_commit_encode(&ov, enc), SV1_E_INVALID_GENERATION, "generation overflow"); |
| 784 | } |
| 785 | |
| 786 | static void neg_sb_binding(void) |
| 787 | { |
| 788 | sv1_commit c; |
| 789 | sv1_superblock sb, w; |
| 790 | uint8_t id[32]; |
| 791 | neg_genesis(&c); |
| 792 | make_sb(0, &c, 3, &sb); |
| 793 | sv1_commit_object_id(&c, id); |
| 794 | CHECK_EQ(sv1_superblock_validate_commit(&sb, id, &c), 0, "sb binds commit"); |
| 795 | w = sb; |
| 796 | w.commit_record_unit = 2; |
| 797 | CHECK_EQ(sv1_superblock_validate_commit(&w, id, &c), SV1_E_MALFORMED_SUPERBLOCK, "commit unit"); |
| 798 | w = sb; |
| 799 | w.catalog_first_unit = 1; |
| 800 | CHECK_EQ(sv1_superblock_validate_commit(&w, id, &c), SV1_E_MALFORMED_SUPERBLOCK, "catalog"); |
| 801 | w = sb; |
| 802 | w.committed_high_water = 3; |
| 803 | CHECK_EQ(sv1_superblock_validate_commit(&w, id, &c), SV1_E_MALFORMED_SUPERBLOCK, "high water"); |
| 804 | CHECK_EQ(sv1_commit_validates_catalog(&c, c.catalog_id, NULL, 0), 0, "validates empty catalog"); |
| 805 | uint8_t other[32]; |
| 806 | memset(other, 7, 32); |
| 807 | CHECK(sv1_commit_validates_catalog(&c, other, NULL, 0) != 0, "wrong catalog id refused"); |
| 808 | } |
| 809 | |
| 810 | /* C only: each refusal reached on its own, with its exact code (mutation |
| 811 | * coverage for checks that a combined negative case reaches first). */ |
| 812 | static void neg_exact_refusals(void) |
| 813 | { |
| 814 | sv1_commit good, back, c; |
| 815 | sv1_superblock sb, out; |
| 816 | uint8_t enc[SV1_COMMIT_BYTES], v[SV1_UNIT], cat[SV1_UNIT], real[32], bogus[32]; |
| 817 | neg_genesis(&good); |
| 818 | CHECK_EQ(sv1_commit_encode(&good, enc), 0, "commit encode"); |
| 819 | enc[0] ^= 1; |
| 820 | CHECK_EQ(sv1_commit_decode(enc, SV1_COMMIT_BYTES, &back), SV1_E_MALFORMED_COMMIT, "commit magic"); |
| 821 | make_sb(0, &good, 3, &sb); |
| 822 | CHECK_EQ(sv1_superblock_encode(&sb, v), 0, "sb encode"); |
| 823 | v[8] = 2; |
| 824 | rechecksum(v); |
| 825 | CHECK_EQ(sv1_superblock_decode(v, SV1_UNIT, 0, &out), SV1_E_UNSUPPORTED_VERSION, "sb version"); |
| 826 | /* empty catalog: commit and caller must name the catalog the entries hash to */ |
| 827 | size_t len; |
| 828 | memset(cat, 0, sizeof cat); |
| 829 | CHECK_EQ(sv1_catalog_encode(NULL, 0, cat, sizeof cat, &len), 0, "empty catalog"); |
| 830 | CHECK_EQ(sv1_object_id(SV1_KIND_CATALOG, SV1_VERSION, cat, len, real), 0, "catalog id"); |
| 831 | memset(bogus, 0x3c, 32); |
| 832 | c = good; |
| 833 | memcpy(c.catalog_id, real, 32); |
| 834 | c.catalog_entry_count = 0; |
| 835 | c.catalog_byte_length = sv1_catalog_len(0); |
| 836 | c.catalog_unit_count = 1; |
| 837 | CHECK_EQ(sv1_commit_validates_catalog(&c, real, NULL, 0), 0, "catalog matches"); |
| 838 | memcpy(c.catalog_id, bogus, 32); |
| 839 | CHECK_EQ(sv1_commit_validates_catalog(&c, real, NULL, 0), SV1_E_INTEGRITY, "commit names another catalog"); |
| 840 | CHECK_EQ(sv1_commit_validates_catalog(&c, bogus, NULL, 0), SV1_E_INTEGRITY, "entries hash to another catalog"); |
| 841 | } |
| 842 | |
| 843 | int main(int argc, char **argv) |
| 844 | { |
| 845 | int rules_only = argc > 1 && strcmp(argv[1], "--rules-only") == 0; |
| 846 | (void)rules_only; |
| 847 | golden(); |
| 848 | neg_catalog_header(); |
| 849 | neg_entry_reserved(); |
| 850 | neg_catalog_order(); |
| 851 | neg_bounds(); |
| 852 | neg_extents(); |
| 853 | neg_extent_identity(); |
| 854 | neg_superblock(); |
| 855 | neg_commit(); |
| 856 | neg_exact_refusals(); |
| 857 | neg_generation_link(); |
| 858 | neg_sb_binding(); |
| 859 | printf("store_v1_test: %d checks, %d failed\n", g_checks, g_failed); |
| 860 | printf("STORE_V1_FORMAT: %s\n", g_failed ? "FAIL" : "PASS"); |
| 861 | return g_failed ? 1 : 0; |
| 862 | } |