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

66 lines
2.2 KiB
C

#include "naut/event.h"
#include "naut/rpc.h"
#include "test.h"
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
static json_t *echo(void *context, const json_t *params, naut_err *error) {
(void)context;
*error = NAUT_OK;
return json_deep_copy(params);
}
int main(void) {
naut_rpc_registry *registry = naut_rpc_registry_create();
CHECK(registry != NULL);
CHECK(naut_rpc_register(registry, "echo", echo, NULL) == NAUT_OK);
CHECK(naut_rpc_register(registry, "echo", echo, NULL) == NAUT_ERR_INVAL);
json_t *params = json_pack("{s:i}", "value", 42);
naut_err error;
json_t *response =
naut_rpc_dispatch(registry, "echo", params, &error);
CHECK(error == NAUT_OK && response != NULL);
CHECK(json_integer_value(json_object_get(response, "value")) == 42);
json_decref(response);
CHECK(naut_rpc_dispatch(registry, "missing", params, &error) == NULL);
CHECK(error == NAUT_ERR_INVAL);
json_decref(params);
int sockets[2];
CHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);
naut_event event = {
.type = NAUT_EVENT_TORRENT_FINISHED,
.torrent_id = 7,
};
json_t *event_json = naut_rpc_event_json(&event);
CHECK(event_json != NULL);
naut_err send_error =
naut_rpc_send_json(sockets[0], NAUT_RPC_EVENT, event_json);
CHECK_EQ(send_error, NAUT_OK);
json_decref(event_json);
if (send_error != NAUT_OK) {
close(sockets[0]);
close(sockets[1]);
naut_rpc_registry_destroy(registry);
TEST_MAIN_END();
}
naut_rpc_frame_type frame_type;
json_t *payload = NULL;
CHECK(naut_rpc_recv_json(sockets[1], &frame_type, &payload) == NAUT_OK);
CHECK(frame_type == NAUT_RPC_EVENT);
CHECK(json_integer_value(json_object_get(payload, "torrent_id")) == 7);
CHECK(strcmp(json_string_value(json_object_get(payload, "event")),
"torrent_finished") == 0);
json_decref(payload);
close(sockets[0]);
close(sockets[1]);
naut_event_type type;
CHECK(naut_event_type_parse("file_complete", &type));
CHECK(type == NAUT_EVENT_FILE_COMPLETE);
CHECK(!naut_event_type_parse("bad", &type));
naut_rpc_registry_destroy(registry);
TEST_MAIN_END();
}