|
| 1 | +/* |
| 2 | + * test_store_pragmas.c — Tests for SQLite pragma resolution. |
| 3 | + * |
| 4 | + * Validates that the CBM_SQLITE_MMAP_SIZE env var controls the mmap_size |
| 5 | + * pragma applied to on-disk stores. Default behavior (env unset) must |
| 6 | + * remain 64 MB. Setting the env to 0 disables memory-mapped I/O so |
| 7 | + * concurrent processes that truncate the DB file under a sibling's live |
| 8 | + * mapping return SQLITE_IOERR instead of crashing the process with SIGBUS. |
| 9 | + */ |
| 10 | +#include "../src/foundation/compat.h" |
| 11 | +#include "test_framework.h" |
| 12 | +#include "test_helpers.h" |
| 13 | +#include <store/store.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <string.h> |
| 17 | +#include <unistd.h> |
| 18 | + |
| 19 | +static void clear_mmap_env(void) { |
| 20 | + unsetenv("CBM_SQLITE_MMAP_SIZE"); |
| 21 | +} |
| 22 | + |
| 23 | +TEST(mmap_size_default_when_unset) { |
| 24 | + clear_mmap_env(); |
| 25 | + ASSERT_EQ(cbm_store_resolve_mmap_size(), 67108864LL); |
| 26 | + PASS(); |
| 27 | +} |
| 28 | + |
| 29 | +TEST(mmap_size_zero_disables_mmap) { |
| 30 | + setenv("CBM_SQLITE_MMAP_SIZE", "0", 1); |
| 31 | + ASSERT_EQ(cbm_store_resolve_mmap_size(), 0LL); |
| 32 | + clear_mmap_env(); |
| 33 | + PASS(); |
| 34 | +} |
| 35 | + |
| 36 | +TEST(mmap_size_explicit_value) { |
| 37 | + setenv("CBM_SQLITE_MMAP_SIZE", "1048576", 1); |
| 38 | + ASSERT_EQ(cbm_store_resolve_mmap_size(), 1048576LL); |
| 39 | + clear_mmap_env(); |
| 40 | + PASS(); |
| 41 | +} |
| 42 | + |
| 43 | +TEST(mmap_size_negative_clamped_to_zero) { |
| 44 | + setenv("CBM_SQLITE_MMAP_SIZE", "-1", 1); |
| 45 | + ASSERT_EQ(cbm_store_resolve_mmap_size(), 0LL); |
| 46 | + clear_mmap_env(); |
| 47 | + PASS(); |
| 48 | +} |
| 49 | + |
| 50 | +TEST(mmap_size_garbage_falls_back_to_default) { |
| 51 | + setenv("CBM_SQLITE_MMAP_SIZE", "not-a-number", 1); |
| 52 | + ASSERT_EQ(cbm_store_resolve_mmap_size(), 67108864LL); |
| 53 | + clear_mmap_env(); |
| 54 | + PASS(); |
| 55 | +} |
| 56 | + |
| 57 | +TEST(mmap_size_partial_garbage_falls_back_to_default) { |
| 58 | + setenv("CBM_SQLITE_MMAP_SIZE", "123abc", 1); |
| 59 | + ASSERT_EQ(cbm_store_resolve_mmap_size(), 67108864LL); |
| 60 | + clear_mmap_env(); |
| 61 | + PASS(); |
| 62 | +} |
| 63 | + |
| 64 | +/* Integration smoke: opening a file-backed store with mmap_size=0 must |
| 65 | + * succeed. Proves the resolver is wired through configure_pragmas(). */ |
| 66 | +TEST(store_open_with_mmap_disabled) { |
| 67 | + setenv("CBM_SQLITE_MMAP_SIZE", "0", 1); |
| 68 | + char tmp_path[256]; |
| 69 | + snprintf(tmp_path, sizeof(tmp_path), "/tmp/cbm_test_pragmas_%d.db", (int)getpid()); |
| 70 | + unlink(tmp_path); |
| 71 | + |
| 72 | + cbm_store_t *s = cbm_store_open_path(tmp_path); |
| 73 | + ASSERT(s != NULL); |
| 74 | + cbm_store_close(s); |
| 75 | + |
| 76 | + unlink(tmp_path); |
| 77 | + /* WAL/SHM siblings created by the open */ |
| 78 | + char tmp_wal[300]; |
| 79 | + char tmp_shm[300]; |
| 80 | + snprintf(tmp_wal, sizeof(tmp_wal), "%s-wal", tmp_path); |
| 81 | + snprintf(tmp_shm, sizeof(tmp_shm), "%s-shm", tmp_path); |
| 82 | + unlink(tmp_wal); |
| 83 | + unlink(tmp_shm); |
| 84 | + |
| 85 | + clear_mmap_env(); |
| 86 | + PASS(); |
| 87 | +} |
| 88 | + |
| 89 | +SUITE(store_pragmas) { |
| 90 | + RUN_TEST(mmap_size_default_when_unset); |
| 91 | + RUN_TEST(mmap_size_zero_disables_mmap); |
| 92 | + RUN_TEST(mmap_size_explicit_value); |
| 93 | + RUN_TEST(mmap_size_negative_clamped_to_zero); |
| 94 | + RUN_TEST(mmap_size_garbage_falls_back_to_default); |
| 95 | + RUN_TEST(mmap_size_partial_garbage_falls_back_to_default); |
| 96 | + RUN_TEST(store_open_with_mmap_disabled); |
| 97 | +} |
0 commit comments