Naut/tests/unit/test_filemove.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

116 lines
4.3 KiB
C

/* Proves the "move files as they finish" feature at the engine level:
* - per-file completion fires the moment a file's last piece verifies,
* BEFORE the whole torrent is done;
* - a completed file can be relocated mid-download while later pieces (for
* other files) keep arriving, with no corruption. */
#include "naut/piece.h"
#include "naut/metainfo.h"
#include "naut/storage.h"
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef NAUT_FIXTURES
#define NAUT_FIXTURES "tests/fixtures"
#endif
static uint8_t *slurp(const char *path, size_t *len) {
FILE *f = fopen(path, "rb"); if (!f) return NULL;
fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET);
uint8_t *b = malloc(n);
if (fread(b, 1, n, f) != (size_t)n) { fclose(f); free(b); return NULL; }
fclose(f); *len = (size_t)n; return b;
}
struct cbctx {
naut_download *d;
naut_storage *st;
char destdir[256];
int order[8], norder;
bool moved0;
bool file0_done_while_incomplete;
};
static void on_file(void *ctx, uint32_t fidx, const char *path) {
struct cbctx *c = ctx;
c->order[c->norder++] = (int)fidx;
if (fidx == 0 && !c->moved0) {
c->file0_done_while_incomplete = !naut_download_complete(c->d);
char dest[512]; snprintf(dest, sizeof dest, "%s/done_a.txt", c->destdir);
/* relocate file 0 RIGHT NOW, mid-download */
CHECK(naut_storage_relocate(c->st, 0, dest) == NAUT_OK);
c->moved0 = true;
(void)path;
}
}
int main(void) {
size_t tlen;
uint8_t *tor = slurp(NAUT_FIXTURES "/multi_v1.torrent", &tlen);
CHECK(tor != NULL); if (!tor) return 1;
naut_metainfo mi;
CHECK(naut_metainfo_parse(tor, tlen, &mi) == NAUT_OK);
CHECK_EQ(mi.num_files, 2);
/* build the flat byte space from the original files, in torrent order */
uint8_t *global = malloc(mi.total_length);
uint64_t goff = 0;
for (size_t i = 0; i < mi.num_files; i++) {
char p[512]; snprintf(p, sizeof p, "%s/data/%s", NAUT_FIXTURES, mi.files[i].path);
size_t fl; uint8_t *fb = slurp(p, &fl);
CHECK(fb && fl == (size_t)mi.files[i].length);
memcpy(global + goff, fb, fl); goff += fl; free(fb);
}
char tmpl[] = "/tmp/naut_fm_XXXXXX"; char *root = mkdtemp(tmpl);
char destdir[256]; snprintf(destdir, sizeof destdir, "%s_moved", root);
naut_err err;
naut_storage *st = naut_storage_open(mi.files, mi.num_files, root, &err);
CHECK(st && err == NAUT_OK);
naut_download *d = naut_download_create(&mi, st);
CHECK(d != NULL);
struct cbctx ctx; memset(&ctx, 0, sizeof ctx);
ctx.d = d; ctx.st = st; snprintf(ctx.destdir, sizeof ctx.destdir, "%s", destdir);
naut_download_set_file_cb(d, on_file, &ctx);
uint32_t idx, begin, len;
while (naut_download_next_request(d, &idx, &begin, &len)) {
uint64_t g = (uint64_t)idx * (uint64_t)mi.piece_length + begin;
bool done = false;
CHECK(naut_download_on_block(d, idx, begin, global + g, len, &done) == NAUT_OK);
}
CHECK(naut_download_complete(d));
/* file 0 finished and was moved while the torrent was still incomplete */
CHECK_EQ(ctx.norder, 2);
CHECK_EQ(ctx.order[0], 0); /* file 0 completed first */
CHECK_EQ(ctx.order[1], 1);
CHECK(ctx.file0_done_while_incomplete); /* fired before whole-torrent done */
CHECK(ctx.moved0);
CHECK(naut_download_file_complete(d, 0) && naut_download_file_complete(d, 1));
naut_storage_sync(st);
naut_storage_close(st);
/* relocated file 0 is at the destination, byte-correct */
char dest[512]; snprintf(dest, sizeof dest, "%s/done_a.txt", destdir);
size_t dl; uint8_t *got0 = slurp(dest, &dl);
CHECK(got0 && dl == (size_t)mi.files[0].length && memcmp(got0, global, dl) == 0);
free(got0);
/* file 1 stayed put and is byte-correct (pieces after the move landed fine) */
char p1[512]; snprintf(p1, sizeof p1, "%s/%s", root, mi.files[1].path);
size_t l1; uint8_t *got1 = slurp(p1, &l1);
CHECK(got1 && l1 == (size_t)mi.files[1].length &&
memcmp(got1, global + mi.files[0].length, l1) == 0);
free(got1);
naut_download_destroy(d);
free(global); free(tor); naut_metainfo_free(&mi);
char cmd[600]; snprintf(cmd, sizeof cmd, "rm -rf '%s' '%s'", root, destdir);
if (system(cmd)) {}
TEST_MAIN_END();
}