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 / svc / tests / allen_memory_test.c
| 1 | /* allen_memory_test.c -- host tests for Allen's lasting memory: |
| 2 | * 1. one copy encodes and decodes; every kind of damage is refused; |
| 3 | * 2. the two-copy store: first start empty, saves alternate, a power cut |
| 4 | * in the middle of a save keeps the previous memory, a spoiled newest |
| 5 | * copy falls back to the older one; |
| 6 | * 3. the AIENOS partition added in unused space (dev/gpt_add.c) next to an |
| 7 | * existing partition full of sentinel bytes: only the GPT blocks change, |
| 8 | * a second run finds it, no space / no GPT / a torn add write nothing; |
| 9 | * 4. the store on that partition view: nothing outside the partition moves; |
| 10 | * 5. Allen taught, "restarted" from the bytes on disk, and asked. |
| 11 | * Hosted test code only (libc). Run by scripts/ck_allen_memory_test.sh. */ |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include "allen_memory.h" |
| 16 | #include "allen_reply.h" |
| 17 | #include "disk_file.h" |
| 18 | #include "sha256.h" |
| 19 | #include "disk_part.h" |
| 20 | #include "gpt_add.h" |
| 21 | #include "gpt_write.h" |
| 22 | |
| 23 | static int checks, failures; |
| 24 | #define CHECK(c) \ |
| 25 | do { \ |
| 26 | checks++; \ |
| 27 | if (!(c)) { \ |
| 28 | failures++; \ |
| 29 | printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \ |
| 30 | } \ |
| 31 | } while (0) |
| 32 | |
| 33 | static char g_path[512]; |
| 34 | #define MIB(bs) ((1024u * 1024u) / (bs)) |
| 35 | |
| 36 | static uint8_t *slurp(size_t *len) |
| 37 | { |
| 38 | FILE *fp = fopen(g_path, "rb"); |
| 39 | if (!fp) |
| 40 | return NULL; |
| 41 | fseek(fp, 0, SEEK_END); |
| 42 | long n = ftell(fp); |
| 43 | fseek(fp, 0, SEEK_SET); |
| 44 | uint8_t *b = malloc((size_t)n); |
| 45 | if (b && fread(b, 1, (size_t)n, fp) != (size_t)n) { |
| 46 | free(b); |
| 47 | b = NULL; |
| 48 | } |
| 49 | fclose(fp); |
| 50 | *len = (size_t)n; |
| 51 | return b; |
| 52 | } |
| 53 | |
| 54 | /* Every block of a and b that differs lies in [lo, hi] (or in [lo2, hi2]). */ |
| 55 | static int diff_only_in(const uint8_t *a, const uint8_t *b, size_t len, uint32_t bs, uint64_t lo, uint64_t hi, |
| 56 | uint64_t lo2, uint64_t hi2) |
| 57 | { |
| 58 | for (size_t blk = 0; blk < len / bs; blk++) |
| 59 | if (memcmp(a + blk * bs, b + blk * bs, bs) && !(blk >= lo && blk <= hi) && !(blk >= lo2 && blk <= hi2)) { |
| 60 | printf(" block %zu changed outside the allowed ranges\n", blk); |
| 61 | return 0; |
| 62 | } |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | static void memory_sample(ck_allen_memory *m) |
| 67 | { |
| 68 | memset(m, 0, sizeof *m); |
| 69 | m->starts = 3; |
| 70 | m->lines = 41; |
| 71 | strcpy(m->name, "Drake"); |
| 72 | m->nfacts = 2; |
| 73 | strcpy(m->fact[0].key, "my favorite color"); |
| 74 | strcpy(m->fact[0].val, "blue"); |
| 75 | strcpy(m->fact[1].key, "the dogs"); |
| 76 | strcpy(m->fact[1].val, "rex and max"); |
| 77 | m->fact[1].are = 1; |
| 78 | } |
| 79 | |
| 80 | static void test_codec(void) |
| 81 | { |
| 82 | static uint8_t u[CK_ALLEN_MEM_UNIT], v[CK_ALLEN_MEM_UNIT]; |
| 83 | ck_allen_memory a, b; |
| 84 | uint64_t gen = 0; |
| 85 | memory_sample(&a); |
| 86 | ck_allen_mem_encode(&a, 7, u); |
| 87 | CHECK(ck_allen_mem_decode(u, &b, &gen) == 0); |
| 88 | CHECK(gen == 7 && !memcmp(&a, &b, sizeof a)); |
| 89 | /* any flipped byte in the header or payload is refused */ |
| 90 | size_t spots[] = {0, 8, 12, 16, 25, 40, 64, 70, 72 + 5, 100, 200, 300, 4000}; |
| 91 | for (size_t i = 0; i < sizeof spots / sizeof spots[0]; i++) { |
| 92 | memcpy(v, u, sizeof u); |
| 93 | v[spots[i]] ^= 0x20; |
| 94 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); |
| 95 | } |
| 96 | /* well sealed but bad text, count or generation: still refused */ |
| 97 | ck_allen_memory bad = a; |
| 98 | strcpy(bad.fact[0].key, "My Color"); |
| 99 | ck_allen_mem_encode(&bad, 1, v); |
| 100 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); |
| 101 | bad = a; |
| 102 | strcpy(bad.fact[0].val, "two spaces"); |
| 103 | ck_allen_mem_encode(&bad, 1, v); |
| 104 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); |
| 105 | bad = a; |
| 106 | strcpy(bad.name, "drake"); |
| 107 | ck_allen_mem_encode(&bad, 1, v); |
| 108 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); |
| 109 | bad = a; |
| 110 | bad.fact[1].key[0] = 0; |
| 111 | ck_allen_mem_encode(&bad, 1, v); |
| 112 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); |
| 113 | ck_allen_mem_encode(&a, 0, v); |
| 114 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); |
| 115 | memset(v, 0, sizeof v); |
| 116 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); |
| 117 | /* a full memory fits and round-trips */ |
| 118 | ck_allen_memory full; |
| 119 | memset(&full, 0, sizeof full); |
| 120 | full.nfacts = CK_ALLEN_FACTS_MAX; |
| 121 | for (unsigned i = 0; i < CK_ALLEN_FACTS_MAX; i++) { |
| 122 | memset(full.fact[i].key, 'k', CK_ALLEN_KEY_MAX - 1); |
| 123 | full.fact[i].key[0] = (char)('a' + i % 26); |
| 124 | memset(full.fact[i].val, 'v', CK_ALLEN_VAL_MAX - 1); |
| 125 | } |
| 126 | ck_allen_mem_encode(&full, 9, u); |
| 127 | CHECK(ck_allen_mem_decode(u, &b, &gen) == 0 && !memcmp(&full, &b, sizeof full)); |
| 128 | /* Allen's own name rides along (version 3), and a version 2 unit written |
| 129 | * before he had one still reads, with no name. */ |
| 130 | ck_allen_memory named = a; |
| 131 | strcpy(named.self, "Nova"); |
| 132 | ck_allen_mem_encode(&named, 11, u); |
| 133 | CHECK(ck_allen_mem_decode(u, &b, &gen) == 0 && gen == 11 && !strcmp(b.self, "Nova") && !strcmp(b.name, "Drake")); |
| 134 | strcpy(named.self, "nova"); |
| 135 | ck_allen_mem_encode(&named, 11, v); |
| 136 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); /* a name starts with a capital, like the person's */ |
| 137 | ck_allen_mem_encode(&a, 5, v); |
| 138 | { |
| 139 | /* the same memory as version 2: no self field, shorter payload, resealed */ |
| 140 | uint32_t plen = 0; |
| 141 | for (int i = 3; i >= 0; i--) |
| 142 | plen = plen << 8 | v[12 + i]; |
| 143 | plen -= CK_ALLEN_NAME_MAX + 12u; /* v2 has neither self (24B) nor sky fields (12B) */ |
| 144 | v[8] = 2, v[9] = v[10] = v[11] = 0; |
| 145 | for (int i = 0; i < 4; i++) |
| 146 | v[12 + i] = (uint8_t)(plen >> (8 * i)); |
| 147 | sha256_ctx h; |
| 148 | sha256_init(&h); |
| 149 | sha256_update(&h, v, 32); |
| 150 | sha256_update(&h, v + 64, plen); |
| 151 | sha256_final(&h, v + 32); |
| 152 | } |
| 153 | CHECK(ck_allen_mem_decode(v, &b, &gen) == 0 && gen == 5 && !b.self[0] && !memcmp(&a, &b, sizeof a)); |
| 154 | v[8] = 5; /* a version from the future is refused */ |
| 155 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); |
| 156 | /* the sky fields (version 4) round-trip; out-of-range ones are refused like any other bad field */ |
| 157 | ck_allen_memory sky = a; |
| 158 | sky.sky_mode = 1, sky.sky_region = 11, sky.sky_have_loc = 1; |
| 159 | sky.sky_lat_udeg = -90000000, sky.sky_lon_udeg = 180000000; |
| 160 | ck_allen_mem_encode(&sky, 12, u); |
| 161 | CHECK(ck_allen_mem_decode(u, &b, &gen) == 0 && gen == 12 && !memcmp(&sky, &b, sizeof sky)); |
| 162 | const struct { |
| 163 | uint8_t mode, region, have; |
| 164 | int32_t lat, lon; |
| 165 | } bad_sky[] = { |
| 166 | {3, 0, 0, 0, 0}, /* sky_mode 0..2 */ |
| 167 | {255, 0, 0, 0, 0}, |
| 168 | {2, 12, 0, 0, 0}, /* 12 regions */ |
| 169 | {1, 0, 2, 0, 0}, /* sky_have_loc 0 or 1 */ |
| 170 | {1, 0, 1, 90000001, 0}, /* latitude within +-90 degrees */ |
| 171 | {1, 0, 1, -90000001, 0}, |
| 172 | {1, 0, 1, 0, 180000001}, /* longitude within +-180 degrees */ |
| 173 | {1, 0, 1, 0, INT32_MIN}, |
| 174 | }; |
| 175 | for (size_t i = 0; i < sizeof bad_sky / sizeof bad_sky[0]; i++) { |
| 176 | ck_allen_memory s = a; |
| 177 | s.sky_mode = bad_sky[i].mode, s.sky_region = bad_sky[i].region, s.sky_have_loc = bad_sky[i].have; |
| 178 | s.sky_lat_udeg = bad_sky[i].lat, s.sky_lon_udeg = bad_sky[i].lon; |
| 179 | ck_allen_mem_encode(&s, 12, v); |
| 180 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); |
| 181 | } |
| 182 | { |
| 183 | /* the reserved byte after sky_have_loc must be zero (allen_reply.h); set it and reseal */ |
| 184 | ck_allen_mem_encode(&sky, 12, v); |
| 185 | uint32_t plen = 0; |
| 186 | for (int i = 3; i >= 0; i--) |
| 187 | plen = plen << 8 | v[12 + i]; |
| 188 | v[64 + plen - 12 + 3] = 1; |
| 189 | sha256_ctx h; |
| 190 | sha256_init(&h); |
| 191 | sha256_update(&h, v, 32); |
| 192 | sha256_update(&h, v + 64, plen); |
| 193 | sha256_final(&h, v + 32); |
| 194 | CHECK(ck_allen_mem_decode(v, &b, &gen) != 0); |
| 195 | v[64 + plen - 12 + 3] = 0; /* the same unit with the byte back to zero reads */ |
| 196 | sha256_init(&h); |
| 197 | sha256_update(&h, v, 32); |
| 198 | sha256_update(&h, v + 64, plen); |
| 199 | sha256_final(&h, v + 32); |
| 200 | CHECK(ck_allen_mem_decode(v, &b, &gen) == 0 && !memcmp(&sky, &b, sizeof sky)); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* A memory large enough that one copy spans all 8 blocks of its unit, so a |
| 205 | * power cut part way really tears it. */ |
| 206 | static void memory_big(ck_allen_memory *m) |
| 207 | { |
| 208 | memory_sample(m); |
| 209 | m->nfacts = 30; |
| 210 | for (unsigned i = 2; i < 30; i++) { |
| 211 | snprintf(m->fact[i].key, sizeof m->fact[i].key, "fact number %u", i); |
| 212 | snprintf(m->fact[i].val, sizeof m->fact[i].val, "value %u", i * 7); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static void test_store(void) |
| 217 | { |
| 218 | disk_file f; |
| 219 | disk_dev d; |
| 220 | ck_allen_store st; |
| 221 | ck_allen_memory m, got; |
| 222 | CHECK(disk_file_open(&f, &d, g_path, 512, 64, 1) == 0); |
| 223 | CHECK(ck_allen_store_load(&st, &d, &got) == CK_ALLEN_MEM_EMPTY && st.valid == 0 && got.nfacts == 0); |
| 224 | memory_big(&m); |
| 225 | CHECK(ck_allen_store_save(&st, &m) == CK_ALLEN_MEM_OK && st.gen == 1 && st.newest == 0); |
| 226 | m.lines = 50; |
| 227 | CHECK(ck_allen_store_save(&st, &m) == CK_ALLEN_MEM_OK && st.gen == 2 && st.newest == 1); |
| 228 | CHECK(ck_allen_store_load(&st, &d, &got) == CK_ALLEN_MEM_OK && st.gen == 2 && st.valid == 2 && |
| 229 | got.lines == 50); |
| 230 | /* power cut after 3 of the 8 blocks of the next save */ |
| 231 | m.lines = 60; |
| 232 | strcpy(m.fact[29].val, "changed at the end of the copy"); |
| 233 | disk_file_arm_cut(&f, 3); |
| 234 | CHECK(ck_allen_store_save(&st, &m) != CK_ALLEN_MEM_OK); |
| 235 | disk_file_disarm(&f); |
| 236 | CHECK(ck_allen_store_load(&st, &d, &got) == CK_ALLEN_MEM_OK && st.gen == 2 && st.valid == 1 && |
| 237 | got.lines == 50); |
| 238 | /* power cut before the flush: the copy is whole but the flush failed; |
| 239 | * the save reports failure, and loading takes whichever copy is valid */ |
| 240 | disk_file_arm_cut(&f, 8); |
| 241 | CHECK(ck_allen_store_save(&st, &m) != CK_ALLEN_MEM_OK); |
| 242 | disk_file_disarm(&f); |
| 243 | CHECK(ck_allen_store_load(&st, &d, &got) == CK_ALLEN_MEM_OK && (got.lines == 50 || got.lines == 60)); |
| 244 | CHECK(ck_allen_store_save(&st, &m) == CK_ALLEN_MEM_OK); |
| 245 | uint64_t newest = st.newest, gen = st.gen; |
| 246 | /* spoil the newest copy on disk: the older one is used */ |
| 247 | disk_file_close(&f); |
| 248 | FILE *fp = fopen(g_path, "r+b"); |
| 249 | CHECK(fp && fseek(fp, (long)(newest * 4096 + 100), SEEK_SET) == 0 && fputc(0x55, fp) != EOF); |
| 250 | if (fp) |
| 251 | fclose(fp); |
| 252 | CHECK(disk_file_open(&f, &d, g_path, 512, 0, 0) == 0); |
| 253 | CHECK(ck_allen_store_load(&st, &d, &got) == CK_ALLEN_MEM_OK && st.gen == gen - 1 && st.valid == 1); |
| 254 | /* the next save goes over the spoiled copy, never over the good one */ |
| 255 | CHECK(ck_allen_store_save(&st, &m) == CK_ALLEN_MEM_OK && st.newest == newest && st.gen == gen); |
| 256 | CHECK(ck_allen_store_load(&st, &d, &got) == CK_ALLEN_MEM_OK && st.valid == 2 && got.lines == 60); |
| 257 | disk_file_close(&f); |
| 258 | /* too small a device is refused */ |
| 259 | CHECK(disk_file_open(&f, &d, g_path, 512, 15, 1) == 0); |
| 260 | CHECK(ck_allen_store_load(&st, &d, &got) == CK_ALLEN_MEM_EGEOM); |
| 261 | disk_file_close(&f); |
| 262 | } |
| 263 | |
| 264 | static int g_listed; |
| 265 | static char g_names[4][40]; |
| 266 | static void on_entry(void *ctx, const ck_gpt_entry *e) |
| 267 | { |
| 268 | (void)ctx; |
| 269 | if (g_listed < 4) |
| 270 | strcpy(g_names[g_listed], e->name); |
| 271 | g_listed++; |
| 272 | } |
| 273 | |
| 274 | static void test_gpt_add(uint32_t bs) |
| 275 | { |
| 276 | const uint64_t per = MIB(bs), disk = 64 * per; |
| 277 | /* one existing partition, 1..9 MiB, full of sentinel bytes; the rest free */ |
| 278 | gw_part sentinel[1] = {{gw_linux_type, 1 * per, 9 * per - 1u, "old macOS", 0xa5}}; |
| 279 | CHECK(gw_write(g_path, bs, disk, sentinel, 1, 0) == 0); |
| 280 | size_t n0 = 0, n1 = 0; |
| 281 | uint8_t *before = slurp(&n0); |
| 282 | disk_file f; |
| 283 | disk_dev d; |
| 284 | ck_part p; |
| 285 | ck_gpt_info gi; |
| 286 | CHECK(disk_file_open(&f, &d, g_path, bs, 0, 0) == 0); |
| 287 | CHECK(ck_gpt_find_aienos(&d, &p, &gi) == CK_GPT_ENOPART && gi.first_usable && gi.entries == 128); |
| 288 | g_listed = 0; |
| 289 | CHECK(ck_gpt_list(&d, on_entry, 0) == 1 && g_listed == 1 && !strcmp(g_names[0], "old macOS")); |
| 290 | ck_gpt_added a; |
| 291 | /* 16 MiB on 1 MiB boundaries: the gap before the sentinel is too small, |
| 292 | * so it lands right after it, at 9 MiB */ |
| 293 | CHECK(ck_gpt_add_aienos(&d, 16 * per, per, &a) == CK_GPT_OK); |
| 294 | CHECK(a.created == 1 && a.first == 9 * per && a.last == 25 * per - 1u && a.index == 1); |
| 295 | uint8_t *after = slurp(&n1); |
| 296 | CHECK(before && after && n0 == n1); |
| 297 | if (before && after && n0 == n1) { |
| 298 | /* only the GPT blocks changed: LBA 1 .. first usable - 1 and last usable + 1 .. last */ |
| 299 | CHECK(diff_only_in(before, after, n0, bs, 1, gi.first_usable - 1u, gi.last_usable + 1u, disk - 1u)); |
| 300 | CHECK(!memcmp(before, after, bs)); /* the protective MBR untouched */ |
| 301 | CHECK(!memcmp(before + per * bs, after + per * bs, 8 * per * bs)); /* every sentinel byte */ |
| 302 | } |
| 303 | CHECK(ck_gpt_find_aienos(&d, &p, &gi) == CK_GPT_OK && p.first_lba == 9 * per && p.blocks == 16 * per); |
| 304 | g_listed = 0; |
| 305 | CHECK(ck_gpt_list(&d, on_entry, 0) == 2 && !strcmp(g_names[1], "AIENOS Allen memory")); |
| 306 | /* a second run finds it and writes nothing */ |
| 307 | free(before); |
| 308 | before = slurp(&n0); |
| 309 | CHECK(ck_gpt_add_aienos(&d, 16 * per, per, &a) == CK_GPT_OK && a.created == 0 && a.first == 9 * per); |
| 310 | free(after); |
| 311 | after = slurp(&n1); |
| 312 | CHECK(before && after && n0 == n1 && !memcmp(before, after, n0)); |
| 313 | free(before); |
| 314 | free(after); |
| 315 | |
| 316 | /* 4. the store on the partition view: nothing outside it moves */ |
| 317 | before = slurp(&n0); |
| 318 | ck_allen_store st; |
| 319 | ck_allen_memory m, got; |
| 320 | memory_sample(&m); |
| 321 | CHECK(ck_allen_store_load(&st, &p.dev, &got) == CK_ALLEN_MEM_EMPTY); |
| 322 | CHECK(ck_allen_store_save(&st, &m) == CK_ALLEN_MEM_OK && ck_allen_store_save(&st, &m) == CK_ALLEN_MEM_OK); |
| 323 | after = slurp(&n1); |
| 324 | if (before && after && n0 == n1) |
| 325 | CHECK(diff_only_in(before, after, n0, bs, 9 * per, 9 * per + 2 * 4096 / bs - 1u, 1, 0)); |
| 326 | CHECK(ck_allen_store_load(&st, &p.dev, &got) == CK_ALLEN_MEM_OK && st.valid == 2 && !memcmp(&got, &m, sizeof m)); |
| 327 | free(before); |
| 328 | free(after); |
| 329 | disk_file_close(&f); |
| 330 | |
| 331 | /* no unused gap big enough: refused, nothing written */ |
| 332 | gw_part whole[1] = {{gw_linux_type, 34, disk - 34, "whole disk", 0}}; |
| 333 | CHECK(gw_write(g_path, bs, disk, whole, 1, 0) == 0); |
| 334 | before = slurp(&n0); |
| 335 | CHECK(disk_file_open(&f, &d, g_path, bs, 0, 0) == 0); |
| 336 | int rc = ck_gpt_add_aienos(&d, 16 * per, per, &a); |
| 337 | CHECK(rc == CK_GPT_ENOSPACE || (bs == 4096 && rc == CK_GPT_EHDR)); /* 4096: 34 is inside the array */ |
| 338 | disk_file_close(&f); |
| 339 | after = slurp(&n1); |
| 340 | CHECK(before && after && n0 == n1 && !memcmp(before, after, n0)); |
| 341 | free(before); |
| 342 | free(after); |
| 343 | |
| 344 | /* no GPT at all: refused, nothing written */ |
| 345 | CHECK(gw_write(g_path, bs, disk, sentinel, 1, GW_NO_GPT) == 0); |
| 346 | before = slurp(&n0); |
| 347 | CHECK(disk_file_open(&f, &d, g_path, bs, 0, 0) == 0); |
| 348 | CHECK(ck_gpt_add_aienos(&d, 16 * per, per, &a) == CK_GPT_ENOMBR); |
| 349 | CHECK(ck_gpt_list(&d, on_entry, 0) == CK_GPT_ENOMBR); |
| 350 | disk_file_close(&f); |
| 351 | after = slurp(&n1); |
| 352 | CHECK(before && after && n0 == n1 && !memcmp(before, after, n0)); |
| 353 | free(before); |
| 354 | free(after); |
| 355 | |
| 356 | /* torn add: power lost after the backup copy was written. The primary |
| 357 | * (what the firmware reads) is still the old, valid one; the strict |
| 358 | * check refuses the pair, and a second attempt writes nothing. */ |
| 359 | CHECK(gw_write(g_path, bs, disk, sentinel, 1, 0) == 0); |
| 360 | CHECK(disk_file_open(&f, &d, g_path, bs, 0, 0) == 0); |
| 361 | uint64_t eblocks = (128u * 128u + bs - 1u) / bs; |
| 362 | disk_file_arm_cut(&f, eblocks + 1u); |
| 363 | CHECK(ck_gpt_add_aienos(&d, 16 * per, per, &a) == CK_GPT_EWRITE); |
| 364 | disk_file_disarm(&f); |
| 365 | CHECK(ck_gpt_find_aienos(&d, &p, &gi) == CK_GPT_EBACKUP); |
| 366 | before = slurp(&n0); |
| 367 | CHECK(ck_gpt_add_aienos(&d, 16 * per, per, &a) == CK_GPT_EBACKUP); |
| 368 | after = slurp(&n1); |
| 369 | CHECK(before && after && n0 == n1 && !memcmp(before, after, n0)); |
| 370 | free(before); |
| 371 | free(after); |
| 372 | disk_file_close(&f); |
| 373 | } |
| 374 | |
| 375 | static int says(const char *got, const char *want) |
| 376 | { |
| 377 | if (strstr(got, want)) |
| 378 | return 1; |
| 379 | printf(" answer \"%s\" lacks \"%s\"\n", got, want); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static void test_teach_restart(void) |
| 384 | { |
| 385 | static ck_allen_mind m, m2; |
| 386 | static uint8_t u[CK_ALLEN_MEM_UNIT]; |
| 387 | ck_allen_mind_init(&m, 0); |
| 388 | m.lasting = 1; |
| 389 | CHECK(says(ck_allen_welcome(&m), "What will you call me?")); |
| 390 | CHECK(says(ck_allen_answer(&m, "My name is Drake", 1), "Nice to meet you, Drake. I'll remember that.")); |
| 391 | CHECK(m.changed == 1); |
| 392 | m.changed = 0; |
| 393 | CHECK(says(ck_allen_answer(&m, "Remember my favorite color is blue.", 2), |
| 394 | "Got it: your favorite color is blue. I'll remember that.")); |
| 395 | CHECK(m.changed == 1 && m.mem.nfacts == 1); |
| 396 | CHECK(says(ck_allen_answer(&m, "remember that the dogs are rex and max", 3), "the dogs are rex and max")); |
| 397 | CHECK(says(ck_allen_answer(&m, "what is my favorite color", 4), "Your favorite color is blue.")); |
| 398 | CHECK(says(ck_allen_answer(&m, "What are the dogs", 4), "The dogs are rex and max.")); |
| 399 | CHECK(says(ck_allen_answer(&m, "what is my favorite food", 4), "You haven't told me your favorite food yet.")); |
| 400 | CHECK(says(ck_allen_answer(&m, "remember my favorite color is green", 5), "your favorite color is green")); |
| 401 | CHECK(m.mem.nfacts == 2); |
| 402 | CHECK(says(ck_allen_answer(&m, "what do you remember", 6), |
| 403 | "I remember 3 things: your name is Drake; your favorite color is green; the dogs are rex and max.")); |
| 404 | CHECK(says(ck_allen_answer(&m, "remember", 6), "I keep it on this computer's own disk.")); |
| 405 | CHECK(says(ck_allen_answer(&m, "remember blue", 6), "Teach me like this")); |
| 406 | CHECK(says(ck_allen_answer(&m, "what is 2 plus 2", 6), "2 plus 2 is 4.")); |
| 407 | |
| 408 | /* number(): nine digits are the limit; longer digit runs are refused before they can overflow */ |
| 409 | CHECK(says(ck_allen_answer(&m, "what is 999999999 plus 1", 6), "999999999 plus 1 is 1000000000.")); |
| 410 | CHECK(!strstr(ck_allen_answer(&m, "what is 1000000000 plus 1", 6), " is 1000000001")); |
| 411 | CHECK(!strstr(ck_allen_answer(&m, "what is 1 plus 1000000000", 6), " is 1000000001")); |
| 412 | CHECK(!strstr(ck_allen_answer(&m, "what is 999999999999999999999999 plus 1", 6), " plus ")); |
| 413 | CHECK(!strstr(ck_allen_answer(&m, "what is 1 times 99999999999999999999999999999", 6), " times ")); |
| 414 | |
| 415 | /* "restart": the memory goes to disk bytes and comes back in a new mind */ |
| 416 | m.mem.starts = 1; |
| 417 | ck_allen_mem_encode(&m.mem, 5, u); |
| 418 | uint64_t gen = 0; |
| 419 | ck_allen_mind_init(&m2, 0); |
| 420 | m2.lasting = 1; |
| 421 | CHECK(ck_allen_mem_decode(u, &m2.mem, &gen) == 0 && gen == 5); |
| 422 | m2.mem.starts++; |
| 423 | CHECK(says(ck_allen_welcome(&m2), "Welcome back, Drake. I still remember 2 things you taught me.")); |
| 424 | CHECK(says(ck_allen_answer(&m2, "what is my favorite color", 1), "Your favorite color is green.")); |
| 425 | CHECK(says(ck_allen_answer(&m2, "what is my name", 1), "You're Drake.")); |
| 426 | CHECK(says(ck_allen_answer(&m2, "how many times have you restarted", 1), "This is start number 2 for me")); |
| 427 | CHECK(m2.changed == 0); |
| 428 | CHECK(says(ck_allen_answer(&m2, "forget the dogs", 1), "Done. I've forgotten the dogs.")); |
| 429 | CHECK(m2.changed == 1 && m2.mem.nfacts == 1); |
| 430 | CHECK(says(ck_allen_answer(&m2, "forget the cat", 1), "I don't remember anything about the cat.")); |
| 431 | CHECK(says(ck_allen_answer(&m2, "forget everything", 1), "forgotten everything")); |
| 432 | CHECK(m2.mem.nfacts == 0 && !m2.mem.name[0] && says(ck_allen_welcome(&m2), "What will you call me?")); |
| 433 | CHECK(says(ck_allen_answer(&m2, "what do you remember", 1), "Nothing yet.")); |
| 434 | |
| 435 | /* without a disk Allen says it only lasts until power off */ |
| 436 | ck_allen_mind_init(&m, 0); |
| 437 | CHECK(says(ck_allen_answer(&m, "remember my car is red", 1), "until this computer turns off")); |
| 438 | /* a full memory says so and keeps what it has */ |
| 439 | for (unsigned i = 0; i < CK_ALLEN_FACTS_MAX + 2; i++) { |
| 440 | char line[64]; |
| 441 | snprintf(line, sizeof line, "remember thing %u is %u", i, i); |
| 442 | ck_allen_answer(&m, line, 1); |
| 443 | } |
| 444 | CHECK(m.mem.nfacts == CK_ALLEN_FACTS_MAX); |
| 445 | CHECK(says(ck_allen_answer(&m, "remember one more is here", 1), "My memory is full.")); |
| 446 | /* everything Allen can be taught encodes and decodes */ |
| 447 | ck_allen_mem_encode(&m.mem, 1, u); |
| 448 | CHECK(ck_allen_mem_decode(u, &m2.mem, &gen) == 0); |
| 449 | /* too long is refused, not cut */ |
| 450 | ck_allen_mind_init(&m, 0); |
| 451 | CHECK(says(ck_allen_answer(&m, "remember the name of the very first computer i ever owned back then is x", 1), |
| 452 | "longer than I can hold")); |
| 453 | CHECK(m.mem.nfacts == 0); |
| 454 | } |
| 455 | |
| 456 | |
| 457 | /* Notes: saved as typed, handed back when asked, linked, deleted, and kept |
| 458 | * on disk one sealed unit each. */ |
| 459 | static void test_notes(void) |
| 460 | { |
| 461 | static ck_allen_mind m, m2; |
| 462 | static ck_allen_notes ns, back; |
| 463 | static uint8_t u[CK_ALLEN_MEM_UNIT]; |
| 464 | ck_allen_mind_init(&m, 0); |
| 465 | memset(&ns, 0, sizeof ns); |
| 466 | m.notes = &ns; |
| 467 | m.lasting = 1; |
| 468 | CHECK(says(ck_allen_answer(&m, "save the gate code is 4471", 1), "Saved. It's your first note.")); |
| 469 | CHECK(m.changed == 1 && m.note_dirty == 1 && m.lit == 1 && m.mem.nnotes == 1 && ns.n == 1); |
| 470 | m.changed = m.note_dirty = 0; |
| 471 | CHECK(says(ck_allen_answer(&m, "Note: the wifi password is Blue-Fox-88", 1), |
| 472 | "Saved. It doesn't link to any other note yet.")); |
| 473 | CHECK(strcmp(ns.note[1].text, "the wifi password is Blue-Fox-88") == 0 && m.note_dirty == 2); |
| 474 | CHECK(says(ck_allen_answer(&m, "save the garage codes are 9012", 1), "Saved. It links to 1 other note.")); |
| 475 | CHECK(says(ck_allen_answer(&m, "What's the gate code?", 1), "From your notes: the gate code is 4471.")); |
| 476 | CHECK(m.lit == 1 && m.map_changed == 1); |
| 477 | CHECK(says(ck_allen_answer(&m, "what is the wifi password", 1), |
| 478 | "From your notes: the wifi password is Blue-Fox-88.")); |
| 479 | CHECK(says(ck_allen_answer(&m, "find code", 1), |
| 480 | "From your notes: the garage codes are 9012. I have 1 more on that.")); |
| 481 | CHECK(says(ck_allen_answer(&m, "find the electrician", 1), "I have nothing saved about the electrician.")); |
| 482 | m.changed = m.note_dirty = 0; |
| 483 | CHECK(says(ck_allen_answer(&m, "save the gate code is 4471.", 1), "I already have that saved.")); |
| 484 | CHECK(ns.n == 3 && m.changed == 0 && m.note_dirty == 0); |
| 485 | CHECK(says(ck_allen_answer(&m, "how many notes", 1), "You have 3 notes. The newest: the garage codes are 9012.")); |
| 486 | CHECK(says(ck_allen_answer(&m, "show me my notes", 1), "You have 3 notes.")); |
| 487 | CHECK(says(ck_allen_answer(&m, "save", 1), "Tell me what to save")); |
| 488 | CHECK(says(ck_allen_answer(&m, "help", 1), "keep notes")); |
| 489 | CHECK(says(ck_allen_answer(&m, "delete the note about the garage", 1), |
| 490 | "Deleted the note: the garage codes are 9012.")); |
| 491 | CHECK(ns.note[2].gone && m.note_dirty == 3 && ns.n == 3); |
| 492 | CHECK(strstr(ck_allen_answer(&m, "what's the garage code", 1), "From your notes") == NULL); |
| 493 | CHECK(says(ck_allen_answer(&m, "delete the note about the boat", 1), "I have no note about the boat.")); |
| 494 | /* taught facts still answer their own questions; machine questions too */ |
| 495 | CHECK(says(ck_allen_answer(&m, "remember my car is red", 1), "Got it: your car is red.")); |
| 496 | CHECK(says(ck_allen_answer(&m, "what is my car", 1), "Your car is red.")); |
| 497 | CHECK(says(ck_allen_answer(&m, "what is your name", 1), "Allen")); |
| 498 | CHECK(ck_allen_notes_live(&ns) == 2 && ck_allen_note_degree(&ns, 0) == 0); |
| 499 | |
| 500 | /* on disk: three notes, a reload, a damaged unit, a cut write */ |
| 501 | disk_file f; |
| 502 | disk_dev d; |
| 503 | uint32_t damaged = 9; |
| 504 | CHECK(disk_file_open(&f, &d, g_path, 512, (CK_ALLEN_DISK_UNITS + 2) * 8, 1) == 0); |
| 505 | for (uint32_t i = 0; i < ns.n; i++) |
| 506 | CHECK(ck_allen_note_write(&d, &ns, i) == CK_ALLEN_MEM_OK); |
| 507 | CHECK(ck_allen_notes_load(&d, 3, &back, &damaged) == CK_ALLEN_MEM_OK && damaged == 0 && back.n == 3); |
| 508 | CHECK(strcmp(back.note[1].text, ns.note[1].text) == 0 && back.note[2].gone == 1 && back.note[0].gone == 0); |
| 509 | CHECK(back.note[0].nword == ns.note[0].nword && back.note[0].word[0] == ns.note[0].word[0]); |
| 510 | /* a cut while writing note 3 leaves the three saved notes as they were */ |
| 511 | CHECK(ck_allen_note_set(&ns.note[3], "the boat is in slip 12", 1) == 0); |
| 512 | disk_file_arm_cut(&f, 3); |
| 513 | CHECK(ck_allen_note_write(&d, &ns, 3) != CK_ALLEN_MEM_OK); |
| 514 | disk_file_disarm(&f); |
| 515 | CHECK(ck_allen_notes_load(&d, 3, &back, &damaged) == CK_ALLEN_MEM_OK && damaged == 0); |
| 516 | /* one spoiled byte: that note loads as gone and is counted */ |
| 517 | disk_file_close(&f); |
| 518 | FILE *fp = fopen(g_path, "r+b"); |
| 519 | CHECK(fp && fseek(fp, (long)((CK_ALLEN_NOTES_FIRST + 1) * 4096 + 70), SEEK_SET) == 0 && fputc('X', fp) != EOF); |
| 520 | if (fp) |
| 521 | fclose(fp); |
| 522 | CHECK(disk_file_open(&f, &d, g_path, 512, 0, 0) == 0); |
| 523 | CHECK(ck_allen_notes_load(&d, 3, &back, &damaged) == CK_ALLEN_MEM_OK && damaged == 1 && back.note[1].gone); |
| 524 | CHECK(ck_allen_notes_load(&d, CK_ALLEN_NOTES_MAX + 1, &back, &damaged) == CK_ALLEN_MEM_EGEOM); |
| 525 | disk_file_close(&f); |
| 526 | /* a unit is valid only in its own slot, and only printable text */ |
| 527 | ck_allen_note_encode(&ns.note[0], 0, u); |
| 528 | CHECK(ck_allen_note_decode(u, 0, &back.note[0]) == 0 && ck_allen_note_decode(u, 1, &back.note[0]) != 0); |
| 529 | u[64] = 0x07; |
| 530 | CHECK(ck_allen_note_decode(u, 0, &back.note[0]) != 0); |
| 531 | CHECK(ck_allen_note_set(&back.note[0], "bad \x01 byte", 1) != 0 && ck_allen_note_set(&back.note[0], " ", 1) != 0); |
| 532 | |
| 533 | /* the note count travels in the memory copies; a restart says so */ |
| 534 | uint64_t gen = 0; |
| 535 | m.mem.starts = 1; |
| 536 | ck_allen_mem_encode(&m.mem, 7, u); |
| 537 | ck_allen_mind_init(&m2, 0); |
| 538 | CHECK(ck_allen_mem_decode(u, &m2.mem, &gen) == 0 && m2.mem.nnotes == 3); |
| 539 | m2.notes = &ns; |
| 540 | ns.n = 3; |
| 541 | CHECK(says(ck_allen_welcome(&m2), "Welcome back. I still remember 1 thing you taught me, and your 2 notes.")); |
| 542 | m2.mem.nfacts = 0; |
| 543 | CHECK(says(ck_allen_welcome(&m2), "Welcome back. I still have your 2 notes.")); |
| 544 | m.mem.nnotes = CK_ALLEN_NOTES_MAX + 1; |
| 545 | ck_allen_mem_encode(&m.mem, 8, u); |
| 546 | CHECK(ck_allen_mem_decode(u, &m2.mem, &gen) != 0); |
| 547 | /* a full notebook says so */ |
| 548 | ck_allen_mind_init(&m, 0); |
| 549 | memset(&ns, 0, sizeof ns); |
| 550 | m.notes = &ns; |
| 551 | ns.n = CK_ALLEN_NOTES_MAX; |
| 552 | CHECK(says(ck_allen_answer(&m, "save one more thing", 1), "My notebook is full.")); |
| 553 | /* without a disk, notes last until power off and Allen says so */ |
| 554 | ns.n = 0; |
| 555 | CHECK(says(ck_allen_answer(&m, "save the dentist is on friday", 1), "Only until this computer turns off")); |
| 556 | } |
| 557 | |
| 558 | int main(int argc, char **argv) |
| 559 | { |
| 560 | snprintf(g_path, sizeof g_path, "%s/disk.img", argc > 1 ? argv[1] : "."); |
| 561 | test_codec(); |
| 562 | test_store(); |
| 563 | test_gpt_add(512); |
| 564 | test_gpt_add(4096); |
| 565 | test_teach_restart(); |
| 566 | test_notes(); |
| 567 | printf("allen memory: %d checks, %d failures\n", checks, failures); |
| 568 | return failures ? 1 : 0; |
| 569 | } |