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/disk/tests/disk_test.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 / disk / tests / disk_test.c` 292 lines ยท 11341 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 /* 18 * disk_test.c -- hosted tests for the disk core (disk.c) and the file backend 19 * (disk_file.c). Usage: disk_test 20 */ 21 #include 22 #include 23 #include 24 #include 25 26 #include "../disk.h" 27 #include "../disk_file.h" 28 29 static int failures, checks; 30 #define CHECK(cond) \ 31 do { \ 32 checks++; \ 33 if (!(cond)) { \ 34 failures++; \ 35 fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ 36 } \ 37 } while (0) 38 39 static char dir[512]; 40 41 /* ---- recording fake backend ---- */ 42 #define LOG_MAX 128 43 typedef struct { 44 char op; 45 uint64_t lba; 46 uint32_t count; 47 uint8_t *buf; 48 } rec; 49 typedef struct { 50 rec log[LOG_MAX]; 51 int n; 52 int fail_at; /* fail the call with this index (0-based), -1 = never */ 53 int fail_rc; 54 } fake; 55 56 static int fk_note(fake *f, char op, uint64_t lba, uint32_t count, const uint8_t *buf) 57 { 58 int i = f->n; 59 if (f->n < LOG_MAX) { 60 f->log[f->n].op = op; 61 f->log[f->n].lba = lba; 62 f->log[f->n].count = count; 63 f->log[f->n].buf = (uint8_t *)buf; 64 } 65 f->n++; 66 return (i == f->fail_at) ? f->fail_rc : 0; 67 } 68 static int fk_read(void *c, uint64_t lba, uint32_t n, uint8_t *b) { return fk_note(c, 'R', lba, n, b); } 69 static int fk_write(void *c, uint64_t lba, uint32_t n, const uint8_t *b) { return fk_note(c, 'W', lba, n, b); } 70 static int fk_flush(void *c) { return fk_note(c, 'F', 0, 0, NULL); } 71 72 static disk_dev fake_dev(fake *f, uint32_t bs, uint64_t blocks, uint32_t maxio) 73 { 74 memset(f, 0, sizeof *f); 75 f->fail_at = -1; 76 f->fail_rc = DISK_EIO; 77 disk_dev d = {f, bs, blocks, fk_read, fk_write, fk_flush, maxio}; 78 return d; 79 } 80 81 static void test_geometry(void) 82 { 83 fake f; 84 disk_dev d = fake_dev(&f, 512, 100, 8); 85 CHECK(disk_check(&d) == DISK_OK); 86 d.block_size = 4096; 87 CHECK(disk_check(&d) == DISK_OK); 88 uint32_t bad[] = {0, 1, 256, 1024, 2048, 8192, 513}; 89 for (unsigned i = 0; i < sizeof bad / sizeof bad[0]; i++) { 90 d.block_size = bad[i]; 91 CHECK(disk_check(&d) == DISK_EGEOMETRY); 92 } 93 uint8_t buf[512]; 94 d.block_size = 1024; 95 CHECK(disk_read(&d, 0, 1, buf) == DISK_EGEOMETRY); 96 CHECK(disk_write(&d, 0, 1, buf) == DISK_EGEOMETRY); 97 CHECK(disk_flush(&d) == DISK_EGEOMETRY); 98 d = fake_dev(&f, 512, 0, 8); 99 CHECK(disk_check(&d) == DISK_EGEOMETRY); 100 d = fake_dev(&f, 512, 10, 0); 101 CHECK(disk_check(&d) == DISK_EGEOMETRY); 102 d = fake_dev(&f, 512, 10, 1); 103 d.read = NULL; 104 CHECK(disk_check(&d) == DISK_EARG); 105 CHECK(disk_check(NULL) == DISK_EARG); 106 CHECK(f.n == 0); /* no backend call on refusal */ 107 } 108 109 static void test_bounds(void) 110 { 111 fake f; 112 disk_dev d = fake_dev(&f, 512, 100, 8); 113 uint8_t buf[512 * 8]; 114 CHECK(disk_read(&d, 0, 0, buf) == DISK_EARG); /* zero count */ 115 CHECK(disk_write(&d, 0, 0, buf) == DISK_EARG); 116 CHECK(disk_read(&d, 0, 1, NULL) == DISK_EARG); 117 CHECK(disk_read(&d, 100, 1, buf) == DISK_ERANGE); /* first block past end */ 118 CHECK(disk_read(&d, 105, 1, buf) == DISK_ERANGE); /* lba well past end */ 119 CHECK(disk_write(&d, 105, 1, buf) == DISK_ERANGE); 120 CHECK(disk_read(&d, 99, 2, buf) == DISK_ERANGE); /* end past device */ 121 CHECK(disk_read(&d, UINT64_MAX - 1, 4, buf) == DISK_ERANGE); /* overflow */ 122 CHECK(disk_write(&d, UINT64_MAX, 1, buf) == DISK_ERANGE); 123 CHECK(f.n == 0); 124 CHECK(disk_read(&d, 99, 1, buf) == DISK_OK); /* last block ok */ 125 CHECK(disk_read(&d, 96, 4, buf) == DISK_OK); 126 CHECK(f.n == 2); 127 } 128 129 static void test_split(void) 130 { 131 fake f; 132 disk_dev d = fake_dev(&f, 512, 1000, 4); 133 static uint8_t buf[512 * 10]; 134 CHECK(disk_read(&d, 20, 10, buf) == DISK_OK); 135 CHECK(f.n == 3); 136 CHECK(f.log[0].lba == 20 && f.log[0].count == 4 && f.log[0].buf == buf); 137 CHECK(f.log[1].lba == 24 && f.log[1].count == 4 && f.log[1].buf == buf + 4 * 512); 138 CHECK(f.log[2].lba == 28 && f.log[2].count == 2 && f.log[2].buf == buf + 8 * 512); 139 d = fake_dev(&f, 4096, 1000, 3); 140 static uint8_t wbuf[4096 * 7]; 141 CHECK(disk_write(&d, 0, 7, wbuf) == DISK_OK); 142 CHECK(f.n == 3 && f.log[2].lba == 6 && f.log[2].count == 1 && 143 f.log[2].buf == wbuf + 6 * 4096); 144 /* backend error on the second chunk stops the transfer */ 145 d = fake_dev(&f, 512, 1000, 4); 146 f.fail_at = 1; 147 CHECK(disk_write(&d, 0, 10, buf) == DISK_EIO); 148 CHECK(f.n == 2); 149 } 150 151 static void test_queue(void) 152 { 153 fake f; 154 disk_dev d = fake_dev(&f, 512, 1000, 8); 155 disk_queue q; 156 static uint8_t buf[512 * 4]; 157 disk_queue_init(&q, &d); 158 for (unsigned i = 0; i < DISK_QUEUE_DEPTH; i++) 159 CHECK(disk_queue_push(&q, DISK_OP_WRITE, i, 1, buf) == DISK_OK); 160 CHECK(disk_queue_push(&q, DISK_OP_WRITE, 0, 1, buf) == DISK_EFULL); 161 CHECK(disk_queue_push(&q, DISK_OP_FLUSH, 0, 0, NULL) == DISK_EFULL); 162 CHECK(q.len == DISK_QUEUE_DEPTH); 163 CHECK(disk_queue_drain(&q) == DISK_OK); 164 CHECK(f.n == (int)DISK_QUEUE_DEPTH); 165 for (int i = 0; i < f.n; i++) 166 CHECK(f.log[i].op == 'W' && f.log[i].lba == (uint64_t)i); 167 168 /* refusals at push: bad op, out of range, zero count, NULL buf */ 169 CHECK(disk_queue_push(&q, (disk_op)9, 0, 1, buf) == DISK_EARG); 170 CHECK(disk_queue_push(&q, DISK_OP_READ, 1000, 1, buf) == DISK_ERANGE); 171 CHECK(disk_queue_push(&q, DISK_OP_READ, 0, 0, buf) == DISK_EARG); 172 CHECK(disk_queue_push(&q, DISK_OP_READ, 0, 1, NULL) == DISK_EARG); 173 174 /* flush barrier: everything before the flush is issued before it, in order */ 175 d = fake_dev(&f, 512, 1000, 8); 176 disk_queue_init(&q, &d); 177 CHECK(disk_queue_push(&q, DISK_OP_WRITE, 10, 2, buf) == DISK_OK); 178 CHECK(disk_queue_push(&q, DISK_OP_WRITE, 5, 1, buf) == DISK_OK); 179 CHECK(disk_queue_push(&q, DISK_OP_FLUSH, 0, 0, NULL) == DISK_OK); 180 CHECK(disk_queue_push(&q, DISK_OP_WRITE, 7, 1, buf) == DISK_OK); 181 CHECK(disk_queue_push(&q, DISK_OP_READ, 10, 1, buf) == DISK_OK); 182 CHECK(disk_queue_push(&q, DISK_OP_FLUSH, 0, 0, NULL) == DISK_OK); 183 CHECK(disk_queue_drain(&q) == DISK_OK); 184 const char *want = "WWFWRF"; 185 CHECK(f.n == 6); 186 for (int i = 0; i < 6 && i < f.n; i++) 187 CHECK(f.log[i].op == want[i]); 188 CHECK(f.log[0].lba == 10 && f.log[1].lba == 5 && f.log[3].lba == 7); 189 CHECK(q.req[2].status == DISK_OK); 190 191 /* wrap-around of the ring */ 192 for (int round = 0; round < 3; round++) { 193 for (int i = 0; i < 20; i++) 194 CHECK(disk_queue_push(&q, DISK_OP_READ, (uint64_t)i, 1, buf) == DISK_OK); 195 CHECK(disk_queue_drain(&q) == DISK_OK); 196 } 197 198 /* fail-stop: the first failing request stops the drain; queue refuses 199 * until reset */ 200 d = fake_dev(&f, 512, 1000, 8); 201 disk_queue_init(&q, &d); 202 f.fail_at = 1; 203 CHECK(disk_queue_push(&q, DISK_OP_WRITE, 1, 1, buf) == DISK_OK); 204 CHECK(disk_queue_push(&q, DISK_OP_WRITE, 2, 1, buf) == DISK_OK); 205 CHECK(disk_queue_push(&q, DISK_OP_FLUSH, 0, 0, NULL) == DISK_OK); 206 CHECK(disk_queue_push(&q, DISK_OP_WRITE, 3, 1, buf) == DISK_OK); 207 CHECK(disk_queue_drain(&q) == DISK_EIO); 208 CHECK(f.n == 2); /* the flush and later write never reached the device */ 209 CHECK(q.failed == 1); 210 CHECK(disk_queue_push(&q, DISK_OP_WRITE, 4, 1, buf) == DISK_ESTATE); 211 CHECK(disk_queue_drain(&q) == DISK_ESTATE); 212 CHECK(f.n == 2); 213 disk_queue_reset(&q); 214 CHECK(disk_queue_push(&q, DISK_OP_WRITE, 4, 1, buf) == DISK_OK); 215 CHECK(disk_queue_drain(&q) == DISK_OK); 216 CHECK(f.n == 3 && f.log[2].lba == 4); 217 disk_queue_reset(NULL); 218 CHECK(disk_queue_push(NULL, DISK_OP_FLUSH, 0, 0, NULL) == DISK_EARG); 219 } 220 221 static void fill(uint8_t *b, size_t n, unsigned seed) 222 { 223 for (size_t i = 0; i < n; i++) 224 b[i] = (uint8_t)(seed * 131u + i * 7u + (i >> 9)); 225 } 226 227 static void test_file_backend(uint32_t bs) 228 { 229 char path[600]; 230 snprintf(path, sizeof path, "%s/disk_test_%u.img", dir, bs); 231 disk_file f; 232 disk_dev d; 233 CHECK(disk_file_open(&f, &d, path, 1024, 64, 1) == DISK_EGEOMETRY); 234 CHECK(disk_file_open(&f, &d, path, bs, 64, 1) == DISK_OK); 235 CHECK(d.block_size == bs && d.block_count == 64); 236 uint8_t *w = malloc((size_t)bs * 64), *r = malloc((size_t)bs * 64); 237 fill(w, (size_t)bs * 64, 3); 238 CHECK(disk_write(&d, 0, 64, w) == DISK_OK); 239 CHECK(disk_flush(&d) == DISK_OK); 240 CHECK(f.flushes == 1 && f.blocks_written == 64); 241 disk_file_close(&f); 242 /* reopen: size must match the file */ 243 CHECK(disk_file_open(&f, &d, path, bs, 63, 0) == DISK_EGEOMETRY); 244 CHECK(disk_file_open(&f, &d, path, bs, 0, 0) == DISK_OK); 245 CHECK(d.block_count == 64); 246 memset(r, 0, (size_t)bs * 64); 247 CHECK(disk_read(&d, 0, 64, r) == DISK_OK); 248 CHECK(memcmp(r, w, (size_t)bs * 64) == 0); 249 CHECK(disk_read(&d, 64, 1, r) == DISK_ERANGE); 250 251 /* power cut at every block of a 12-block write (split in chunks of 5): 252 * exactly the prefix of k blocks lands, nothing after it */ 253 const uint32_t N = 12; 254 d.max_blocks_per_io = 5; 255 for (uint32_t k = 0; k <= N + 1; k++) { 256 fill(w, (size_t)bs * 64, 100 + k); /* old content */ 257 disk_file_disarm(&f); 258 CHECK(disk_write(&d, 0, 64, w) == DISK_OK); 259 uint8_t *nw = malloc((size_t)bs * N); 260 fill(nw, (size_t)bs * N, 200 + k); 261 disk_file_arm_cut(&f, k); 262 int rc = disk_write(&d, 20, N, nw); 263 CHECK(k >= N ? rc == DISK_OK : rc == DISK_EPOWER); 264 if (k < N) 265 CHECK(disk_flush(&d) == DISK_EPOWER); 266 disk_file_disarm(&f); /* reboot */ 267 CHECK(disk_read(&d, 0, 64, r) == DISK_OK); 268 uint32_t landed = k < N ? k : N; 269 CHECK(memcmp(r, w, (size_t)bs * 20) == 0); 270 CHECK(memcmp(r + (size_t)bs * 20, nw, (size_t)bs * landed) == 0); 271 CHECK(memcmp(r + (size_t)bs * (20 + landed), w + (size_t)bs * (20 + landed), 272 (size_t)bs * (64 - 20 - landed)) == 0); 273 free(nw); 274 } 275 disk_file_close(&f); 276 unlink(path); 277 free(w); 278 free(r); 279 } 280 281 int main(int argc, char **argv) 282 { 283 snprintf(dir, sizeof dir, "%s", argc > 1 ? argv[1] : "."); 284 test_geometry(); 285 test_bounds(); 286 test_split(); 287 test_queue(); 288 test_file_backend(512); 289 test_file_backend(4096); 290 printf("disk_test: %d checks, %d failures\n", checks, failures); 291 return failures ? 1 : 0; 292 }