Naut/tests/unit/test_storage.c
ookami125 2178d6a70c Initial commit: Naut-Torrent — from-scratch 10 GbE BitTorrent client
A maintainable, extensible BitTorrent client (C11, Linux/io_uring) targeting
10 GbE saturation. All torrent functionality is built from scratch; liburing
is the only linked third-party dependency on the data path.

Implements Phases 1-7 of the roadmap:
- core: page-aligned buffer pool, MPMC/Treiber queues, bitfields, worker pool
- crypto: SHA-1/256 (SHA-NI + scalar), Merkle (BEP-52), RC4 (MSE)
- bencode/metainfo: zero-copy parser, v1/v2/hybrid .torrent + magnet
- peer: sans-IO wire codec, MSE/PE handshake state machine, BEP-10, ut_metadata, PEX
- piece/storage: block-level multi-peer engine, rarest-first + endgame,
  per-file completion events + single-file relocate (move-as-you-finish)
- tracker/dht: HTTP + UDP (BEP-15) trackers, BEP-5 KRPC iterative lookup
- platform: io_uring reactor (SQPOLL, registered buffers, SEND_ZC)
- surface: versioned RPC, native plugin ABI, sandboxed Lua scripting, nautd/nautctl

Verified against libtorrent (single/multi/hybrid, MSE, magnet-via-DHT, swarm);
unit + interop tests green; ASan/UBSan/TSan clean. Scripting reference in
docs/scripting.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-15 12:12:00 -04:00

79 lines
2.8 KiB
C

#include "naut/storage.h"
#include "test.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void) {
char tmpl[] = "/tmp/naut_stor_XXXXXX";
char *root = mkdtemp(tmpl);
CHECK(root != NULL);
/* multi-file torrent: a write/read can straddle the file boundary */
naut_file files[3] = {
{ (char *)"a.bin", 100 },
{ (char *)"d/b.bin", 50 },
{ (char *)"d/e/c.bin", 30 },
};
naut_err err;
naut_storage *s = naut_storage_open(files, 3, root, &err);
CHECK(s && err == NAUT_OK);
CHECK_EQ(naut_storage_total(s), 180);
CHECK(!naut_storage_direct_enabled(s));
/* fill the whole space with a known pattern in one straddling write */
uint8_t pattern[180];
for (int i = 0; i < 180; i++) pattern[i] = (uint8_t)(i * 3 + 1);
CHECK(naut_storage_write(s, 0, pattern, 180) == NAUT_OK);
/* read back across boundaries at an awkward offset */
uint8_t rb[120];
CHECK(naut_storage_read(s, 40, rb, 120) == NAUT_OK); /* spans all 3 files */
CHECK(memcmp(rb, pattern + 40, 120) == 0);
/* out-of-range rejected */
CHECK(naut_storage_write(s, 170, pattern, 20) == NAUT_ERR_RANGE);
CHECK(naut_storage_sync(s) == NAUT_OK);
naut_storage_close(s);
/* reopen and confirm persistence + nested files exist on disk */
s = naut_storage_open(files, 3, root, &err);
CHECK(s && err == NAUT_OK);
uint8_t all[180];
CHECK(naut_storage_read(s, 0, all, 180) == NAUT_OK);
CHECK(memcmp(all, pattern, 180) == 0);
naut_storage_close(s);
/* Optional O_DIRECT uses aligned bulk I/O and buffered edge fallback. */
{
char direct_tmpl[] = "/tmp/naut_direct_XXXXXX";
char *direct_root = mkdtemp(direct_tmpl);
naut_file direct_file = { (char *)"direct.bin", 8192 };
naut_storage_opts opts = {
.direct_io = true,
.preallocate = true,
};
naut_storage *direct = naut_storage_open_opts(
&direct_file, 1, direct_root, &opts, &err);
CHECK(direct && err == NAUT_OK);
uint8_t *write_buf = aligned_alloc(NAUT_PAGE, 8192);
uint8_t *read_buf = aligned_alloc(NAUT_PAGE, 8192);
CHECK(write_buf && read_buf);
memset(write_buf, 0x5a, 8192);
CHECK(naut_storage_write(direct, 0, write_buf, 8192) == NAUT_OK);
CHECK(naut_storage_read(direct, 0, read_buf, 8192) == NAUT_OK);
CHECK(memcmp(write_buf, read_buf, 8192) == 0);
free(write_buf);
free(read_buf);
naut_storage_close(direct);
char direct_cmd[256];
snprintf(direct_cmd, sizeof direct_cmd, "rm -rf '%s'", direct_root);
if (system(direct_cmd) != 0) {}
}
/* cleanup */
char cmd[256]; snprintf(cmd, sizeof cmd, "rm -rf '%s'", root);
if (system(cmd) != 0) { /* best effort */ }
TEST_MAIN_END();
}