daemon: rpc/ — Unix-socket transport + generated dispatch wiring (build step 1)
First real code in daemon/. veloxd now listens on
$XDG_RUNTIME_DIR/velox/velox.sock (0600, SO_PEERCRED same-UID check),
frames NDJSON, and routes every method through the generated
velox::proto::dispatch(). The CLI and GUI have a server to talk to.
Modules:
- rpc/ndjson.hpp — newline-delimited framing, 8 MiB frame cap, CRLF-
tolerant, partial-tail buffering. Header-only, tested.
- rpc/event_loop — single-threaded poll(2) reactor; never blocks the
loop. stop()/wake() are async-signal-safe (eventfd).
- rpc/runtime_dir — $XDG_RUNTIME_DIR/velox resolution, 0700, owner-checked;
refuses an insecure fallback rather than using /tmp.
- rpc/uds_server — listener + non-blocking per-conn read/write with
backpressure; handles session.hello (protocol-major
check -> -32001, sessionId, transport=uds) and
session.subscribe in the server layer; routes the
rest through dispatch().
- rpc/dispatcher — VeloxDispatcher : proto::Dispatcher, all 39 methods.
download.list answers an empty table; the rest return
"not implemented" (-> -32603) until the store lands.
- main.cpp — abstract-namespace single-instance lock, signal ->
clean shutdown, socket unlinked on exit.
Tests (ASan+UBSan and TSan clean):
- veloxd.ndjson — framing edge cases
- veloxd.uds_roundtrip — real socket: hello ok / version mismatch / empty
list / -32601 / -32700 / pipelined requests, and a
guard on the -32603 collapse documented in P1.
Known gap, filed not worked around: daemon/docs/proto-requests-m1.md P1 —
the generated Dispatcher has no error channel below -32603, so handlers
cannot yet return -32010/-32011/-32013 with their data payloads. The
server layer handles -32001/-32002/-32003 around dispatch(); genuine
in-handler errors collapse to -32603 until PROTO gives handlers a real
error return. Three error fixtures are non-conformant until then.
Not in this drop: rpc/ws_server (next; needs the store for hashed pairing
tokens), store/, sched/, cli/. WS reuses this event loop.
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Upd9WhG9oppieig5nRDLig
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
# daemon unit + integration tests. Registered with ctest; run via `ctest --preset dev`.
|
||||
# No external test framework — each file is a small self-checking binary (matches the
|
||||
# lightweight style core/ uses, without depending on core's private test support).
|
||||
|
||||
add_executable(veloxd_ndjson_test ndjson_test.cpp)
|
||||
target_link_libraries(veloxd_ndjson_test PRIVATE veloxd_rpc)
|
||||
target_compile_options(veloxd_ndjson_test PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
||||
add_test(NAME veloxd.ndjson COMMAND veloxd_ndjson_test)
|
||||
|
||||
add_executable(veloxd_uds_roundtrip_test uds_roundtrip_test.cpp)
|
||||
target_link_libraries(veloxd_uds_roundtrip_test PRIVATE veloxd_rpc)
|
||||
target_compile_options(veloxd_uds_roundtrip_test PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
||||
add_test(NAME veloxd.uds_roundtrip COMMAND veloxd_uds_roundtrip_test)
|
||||
set_tests_properties(veloxd.uds_roundtrip PROPERTIES TIMEOUT 30)
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
// Minimal test harness: CHECK accumulates failures, TEST_MAIN reports and sets the exit
|
||||
// code. Deliberately tiny — the daemon does not pull in a test framework for this.
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace veloxd_test {
|
||||
|
||||
inline std::vector<std::string>& failures() {
|
||||
static std::vector<std::string> f;
|
||||
return f;
|
||||
}
|
||||
inline int& checks() {
|
||||
static int n = 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
} // namespace veloxd_test
|
||||
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
++::veloxd_test::checks(); \
|
||||
if (!(cond)) { \
|
||||
::veloxd_test::failures().push_back(std::string(__FILE__) + ":" + \
|
||||
std::to_string(__LINE__) + ": " + #cond); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_EQ(a, b) \
|
||||
do { \
|
||||
++::veloxd_test::checks(); \
|
||||
auto _va = (a); \
|
||||
auto _vb = (b); \
|
||||
if (!(_va == _vb)) { \
|
||||
::veloxd_test::failures().push_back(std::string(__FILE__) + ":" + \
|
||||
std::to_string(__LINE__) + ": " + #a + \
|
||||
" == " + #b); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define TEST_MAIN() \
|
||||
int main() { \
|
||||
run(); \
|
||||
for (const auto& f : ::veloxd_test::failures()) std::printf("FAIL %s\n", f.c_str()); \
|
||||
std::printf("%d/%d checks passed\n", \
|
||||
::veloxd_test::checks() - \
|
||||
static_cast<int>(::veloxd_test::failures().size()), \
|
||||
::veloxd_test::checks()); \
|
||||
return ::veloxd_test::failures().empty() ? 0 : 1; \
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "rpc/ndjson.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "check.hpp"
|
||||
|
||||
using velox::daemon::rpc::FrameReader;
|
||||
using velox::daemon::rpc::frame;
|
||||
|
||||
void run() {
|
||||
// One chunk, one frame.
|
||||
{
|
||||
FrameReader r;
|
||||
auto f = r.feed("{\"a\":1}\n");
|
||||
CHECK_EQ(f.size(), 1u);
|
||||
CHECK_EQ(f.at(0), std::string("{\"a\":1}"));
|
||||
CHECK_EQ(r.buffered(), 0u);
|
||||
}
|
||||
|
||||
// A frame split across two feeds is delivered only when the newline arrives.
|
||||
{
|
||||
FrameReader r;
|
||||
CHECK_EQ(r.feed("{\"a\":").size(), 0u);
|
||||
auto f = r.feed("1}\n");
|
||||
CHECK_EQ(f.size(), 1u);
|
||||
CHECK_EQ(f.at(0), std::string("{\"a\":1}"));
|
||||
}
|
||||
|
||||
// Several frames in one chunk, plus a partial tail held back.
|
||||
{
|
||||
FrameReader r;
|
||||
auto f = r.feed("1\n2\n3\n4");
|
||||
CHECK_EQ(f.size(), 3u);
|
||||
CHECK_EQ(f.at(0), std::string("1"));
|
||||
CHECK_EQ(f.at(2), std::string("3"));
|
||||
CHECK_EQ(r.buffered(), 1u);
|
||||
auto g = r.feed("\n");
|
||||
CHECK_EQ(g.size(), 1u);
|
||||
CHECK_EQ(g.at(0), std::string("4"));
|
||||
}
|
||||
|
||||
// CRLF terminator is trimmed; blank lines are skipped.
|
||||
{
|
||||
FrameReader r;
|
||||
auto f = r.feed("x\r\n\r\n\ny\r\n");
|
||||
CHECK_EQ(f.size(), 2u);
|
||||
CHECK_EQ(f.at(0), std::string("x"));
|
||||
CHECK_EQ(f.at(1), std::string("y"));
|
||||
}
|
||||
|
||||
// Overflow latches once the unframed tail passes the cap.
|
||||
{
|
||||
FrameReader r;
|
||||
CHECK(!r.overflowed());
|
||||
std::string big(9u * 1024 * 1024, 'a'); // no newline
|
||||
r.feed(big);
|
||||
CHECK(r.overflowed());
|
||||
}
|
||||
|
||||
// frame() appends exactly one newline.
|
||||
CHECK_EQ(frame("hello"), std::string("hello\n"));
|
||||
}
|
||||
|
||||
TEST_MAIN()
|
||||
@@ -0,0 +1,195 @@
|
||||
// Integration: a real UdsServer on a temp socket, a real client socket, NDJSON round trips.
|
||||
// Proves the transport, the generated dispatch() wiring, and the session-layer handling.
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "check.hpp"
|
||||
#include "rpc/dispatcher.hpp"
|
||||
#include "rpc/event_loop.hpp"
|
||||
#include "rpc/ndjson.hpp"
|
||||
#include "rpc/uds_server.hpp"
|
||||
#include "velox_proto.hpp"
|
||||
|
||||
using nlohmann::json;
|
||||
namespace rpc = velox::daemon::rpc;
|
||||
|
||||
namespace {
|
||||
|
||||
std::string make_temp_socket_path() {
|
||||
char tmpl[] = "/tmp/veloxd-test-XXXXXX";
|
||||
const char* dir = ::mkdtemp(tmpl);
|
||||
return std::string(dir ? dir : "/tmp") + "/velox.sock";
|
||||
}
|
||||
|
||||
int connect_client(const std::string& path) {
|
||||
const int fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
sockaddr_un addr{};
|
||||
addr.sun_family = AF_UNIX;
|
||||
std::memcpy(addr.sun_path, path.c_str(), path.size());
|
||||
if (::connect(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0) {
|
||||
::close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Send one framed request, read one framed reply (blocking client; the server is async).
|
||||
json call(int fd, const json& request) {
|
||||
const std::string out = rpc::frame(request.dump());
|
||||
if (::write(fd, out.data(), out.size()) != static_cast<ssize_t>(out.size())) return {};
|
||||
|
||||
std::string buf;
|
||||
char chunk[4096];
|
||||
for (;;) {
|
||||
const ssize_t n = ::read(fd, chunk, sizeof(chunk));
|
||||
if (n <= 0) return {};
|
||||
buf.append(chunk, static_cast<std::size_t>(n));
|
||||
if (const auto nl = buf.find('\n'); nl != std::string::npos)
|
||||
return json::parse(buf.substr(0, nl), nullptr, false);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void run() {
|
||||
const std::string sock = make_temp_socket_path();
|
||||
|
||||
rpc::EventLoop loop;
|
||||
rpc::VeloxDispatcher dispatcher;
|
||||
rpc::UdsServer server(loop, dispatcher, sock);
|
||||
const auto ec = server.start();
|
||||
CHECK(!ec);
|
||||
if (ec) return;
|
||||
|
||||
std::thread loop_thread([&loop] { loop.run(); });
|
||||
|
||||
// --- session.hello, matching major -> a real SessionHelloResult -------------------
|
||||
{
|
||||
const int c = connect_client(sock);
|
||||
CHECK(c >= 0);
|
||||
const json reply = call(c, {{"jsonrpc", "2.0"},
|
||||
{"id", 1},
|
||||
{"method", "session.hello"},
|
||||
{"params",
|
||||
{{"clientType", "test"},
|
||||
{"clientName", "roundtrip"},
|
||||
{"protocolVersion", std::string(velox::proto::kProtocolVersion)}}}});
|
||||
CHECK(reply.contains("result"));
|
||||
CHECK_EQ(reply["id"].get<int>(), 1);
|
||||
CHECK_EQ(reply["result"]["protocolVersion"].get<std::string>(),
|
||||
std::string(velox::proto::kProtocolVersion));
|
||||
CHECK_EQ(reply["result"]["transport"].get<std::string>(), std::string("uds"));
|
||||
CHECK(!reply["result"]["sessionId"].get<std::string>().empty());
|
||||
::close(c);
|
||||
}
|
||||
|
||||
// --- session.hello, wrong major -> -32001, connection closed after the reply ------
|
||||
{
|
||||
const int c = connect_client(sock);
|
||||
const json reply = call(c, {{"jsonrpc", "2.0"},
|
||||
{"id", 2},
|
||||
{"method", "session.hello"},
|
||||
{"params",
|
||||
{{"clientType", "gui"},
|
||||
{"clientName", "from the future"},
|
||||
{"protocolVersion", "2.0.0"}}}});
|
||||
CHECK(reply.contains("error"));
|
||||
CHECK_EQ(reply["error"]["code"].get<int>(), -32001);
|
||||
CHECK_EQ(reply["error"]["data"]["actual"].get<std::string>(), std::string("2.0.0"));
|
||||
::close(c);
|
||||
}
|
||||
|
||||
// --- download.list -> an empty table (dispatcher answers this one for real) -------
|
||||
{
|
||||
const int c = connect_client(sock);
|
||||
const json reply =
|
||||
call(c, {{"jsonrpc", "2.0"}, {"id", 3}, {"method", "download.list"}, {"params", json::object()}});
|
||||
CHECK(reply.contains("result"));
|
||||
CHECK_EQ(reply["result"]["total"].get<int>(), 0);
|
||||
CHECK(reply["result"]["items"].is_array());
|
||||
CHECK_EQ(reply["result"]["items"].size(), 0u);
|
||||
::close(c);
|
||||
}
|
||||
|
||||
// --- an unknown method -> -32601 ------------------------------------------------
|
||||
{
|
||||
const int c = connect_client(sock);
|
||||
const json reply =
|
||||
call(c, {{"jsonrpc", "2.0"}, {"id", 4}, {"method", "no.such.method"}, {"params", json::object()}});
|
||||
CHECK(reply.contains("error"));
|
||||
CHECK_EQ(reply["error"]["code"].get<int>(), -32601);
|
||||
::close(c);
|
||||
}
|
||||
|
||||
// --- malformed JSON -> -32700, id null ----------------------------------------
|
||||
{
|
||||
const int c = connect_client(sock);
|
||||
const std::string bad = "{ this is not json )\n";
|
||||
CHECK(::write(c, bad.data(), bad.size()) == static_cast<ssize_t>(bad.size()));
|
||||
std::string buf;
|
||||
char chunk[1024];
|
||||
const ssize_t n = ::read(c, chunk, sizeof(chunk));
|
||||
CHECK(n > 0);
|
||||
if (n > 0) {
|
||||
buf.assign(chunk, static_cast<std::size_t>(n));
|
||||
const json reply = json::parse(buf.substr(0, buf.find('\n')), nullptr, false);
|
||||
CHECK_EQ(reply["error"]["code"].get<int>(), -32700);
|
||||
CHECK(reply["id"].is_null());
|
||||
}
|
||||
::close(c);
|
||||
}
|
||||
|
||||
// --- download.get -> -32603 for now: documents the P1 codegen gap ---------------
|
||||
// (proto-requests-m1.md P1: handlers cannot yet return -32010. When P1 lands this
|
||||
// check flips to -32010 and is the regression guard for it.)
|
||||
{
|
||||
const int c = connect_client(sock);
|
||||
const json reply = call(c, {{"jsonrpc", "2.0"},
|
||||
{"id", 6},
|
||||
{"method", "download.get"},
|
||||
{"params", {{"taskId", "00000000-0000-4000-8000-000000000000"}}}});
|
||||
CHECK(reply.contains("error"));
|
||||
CHECK_EQ(reply["error"]["code"].get<int>(), -32603);
|
||||
::close(c);
|
||||
}
|
||||
|
||||
// --- two requests in one write, pipelined on one connection --------------------
|
||||
{
|
||||
const int c = connect_client(sock);
|
||||
std::string out = rpc::frame(json({{"jsonrpc", "2.0"}, {"id", 7}, {"method", "download.list"}, {"params", json::object()}}).dump());
|
||||
out += rpc::frame(json({{"jsonrpc", "2.0"}, {"id", 8}, {"method", "download.list"}, {"params", json::object()}}).dump());
|
||||
CHECK(::write(c, out.data(), out.size()) == static_cast<ssize_t>(out.size()));
|
||||
std::string buf;
|
||||
char chunk[4096];
|
||||
int seen = 0;
|
||||
while (seen < 2) {
|
||||
const ssize_t n = ::read(c, chunk, sizeof(chunk));
|
||||
if (n <= 0) break;
|
||||
buf.append(chunk, static_cast<std::size_t>(n));
|
||||
std::size_t nl;
|
||||
while ((nl = buf.find('\n')) != std::string::npos) {
|
||||
const json reply = json::parse(buf.substr(0, nl), nullptr, false);
|
||||
CHECK(reply.contains("result"));
|
||||
++seen;
|
||||
buf.erase(0, nl + 1);
|
||||
}
|
||||
}
|
||||
CHECK_EQ(seen, 2);
|
||||
::close(c);
|
||||
}
|
||||
|
||||
loop.stop();
|
||||
loop_thread.join();
|
||||
::unlink(sock.c_str());
|
||||
}
|
||||
|
||||
TEST_MAIN()
|
||||
Reference in New Issue
Block a user