okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Daehuncore: add guarded shared expert arena backing80f83268d

main
2.0 KiB61 linesraw
1// Linux selftest for the optional MAP_SHARED backing of PinnedArena.
2#include "strata/core/pinned.hpp"
3
4#include <cstdio>
5#include <cstdlib>
6#include <string>
7#include <vector>
8
9#include <unistd.h>
10
11int main() {
12    char path[] = "/tmp/strata-shared-arena-XXXXXX";
13    const int fd = mkstemp(path);
14    if (fd < 0) {
15        std::perror("mkstemp");
16        return 2;
17    }
18    close(fd);
19
20    int fail = 0;
21    const uint64_t bytes = 64u << 10;
22    const uint64_t pack_hash = 0x0123456789abcdefull;
23    const std::vector<uint64_t> no_bounds;
24    {
25        strata::core::PinnedArena first(bytes, no_bounds, 0, path, pack_hash);
26        if (!first.valid()) {
27            std::fprintf(stderr, "first shared arena failed: %s\n", first.note.c_str());
28            fail = 1;
29        } else {
30            first.data()[123] = 0x5a;
31
32            strata::core::PinnedArena second(bytes, no_bounds, 0, path, pack_hash);
33            if (!second.valid()) {
34                std::fprintf(stderr, "second shared arena failed: %s\n", second.note.c_str());
35                fail = 1;
36            } else if (second.data()[123] != 0x5a) {
37                std::fprintf(stderr, "shared mapping did not expose the same bytes\n");
38                fail = 1;
39            }
40
41            strata::core::PinnedArena wrong_pack(bytes, no_bounds, 0, path, pack_hash + 1);
42            if (wrong_pack.valid() || wrong_pack.note.find("pack hash") == std::string::npos) {
43                std::fprintf(stderr, "pack mismatch was not refused: %s\n", wrong_pack.note.c_str());
44                fail = 1;
45            }
46
47            strata::core::PinnedArena wrong_size(bytes * 2, no_bounds, 0, path, pack_hash);
48            if (wrong_size.valid() || wrong_size.note.find("expected") == std::string::npos) {
49                std::fprintf(stderr, "size mismatch was not refused: %s\n", wrong_size.note.c_str());
50                fail = 1;
51            }
52        }
53    }
54
55    if (unlink(path) != 0) {
56        std::perror("unlink");
57        fail = 1;
58    }
59    std::printf("pinned_shared_test: %s\n", fail ? "FAILED" : "OK");
60    return fail;
61}