178 lines
5.1 KiB
C++
178 lines
5.1 KiB
C++
#include "distributed_host/single_node.hpp"
|
|
|
|
#include <Void/Net/Http.hpp>
|
|
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
#include <cassert>
|
|
#include <cstdio>
|
|
#include <string>
|
|
|
|
namespace
|
|
{
|
|
using namespace DistributedHost;
|
|
|
|
SingleNodeConfig config()
|
|
{
|
|
SingleNodeConfig cfg;
|
|
cfg.peer_address = "127.0.0.1:0";
|
|
cfg.peer_token = "peer-secret";
|
|
cfg.shutdown_grace = std::chrono::seconds{2};
|
|
return cfg;
|
|
}
|
|
|
|
Void::Net::Header auth()
|
|
{
|
|
return Void::Net::Header{"authorization", "Bearer peer-secret"};
|
|
}
|
|
|
|
void config_fails_closed_without_a_token()
|
|
{
|
|
SingleNodeConfig no_token;
|
|
no_token.peer_token.clear();
|
|
assert(!SingleNodeHost::create(no_token).has_value());
|
|
|
|
SingleNodeConfig bad_address;
|
|
bad_address.peer_token = "x";
|
|
bad_address.peer_address = "not-an-address";
|
|
assert(!SingleNodeHost::create(bad_address).has_value());
|
|
|
|
SingleNodeConfig zero_entries = config();
|
|
zero_entries.max_log_entries = 0;
|
|
assert(!SingleNodeHost::create(zero_entries).has_value());
|
|
}
|
|
|
|
void peer_fixture_requires_authentication_and_serves_bounded_state()
|
|
{
|
|
auto host = SingleNodeHost::create(config());
|
|
assert(host.has_value());
|
|
// Construction opens no listener.
|
|
assert(host->peer_port() == 0);
|
|
assert(host->history().empty());
|
|
|
|
assert(host->start().has_value());
|
|
assert(host->health().readiness == Readiness::WriteReady);
|
|
const std::uint16_t port = host->peer_port();
|
|
assert(port != 0);
|
|
|
|
Void::Net::HttpClient client;
|
|
const std::string base =
|
|
"http://127.0.0.1:" + std::to_string(port);
|
|
|
|
// Both peer endpoints reject missing and wrong credentials.
|
|
auto anonymous_health = client.get(base + "/peer/health");
|
|
assert(anonymous_health.has_value());
|
|
assert(anonymous_health->status == 401);
|
|
auto wrong = client.get(
|
|
base + "/peer/health",
|
|
{Void::Net::Header{"authorization", "Bearer wrong"}});
|
|
assert(wrong.has_value());
|
|
assert(wrong->status == 401);
|
|
|
|
auto health = client.get(base + "/peer/health", {auth()});
|
|
assert(health.has_value());
|
|
assert(health->status == 200);
|
|
assert(health->body.find("role=leader") != std::string::npos);
|
|
assert(health->body.find("entries=0") != std::string::npos);
|
|
// The credential never appears in a response.
|
|
assert(health->body.find("peer-secret") == std::string::npos);
|
|
|
|
auto append = [&](std::string body) {
|
|
Void::Net::Request request;
|
|
request.method = Void::Net::Method::Post;
|
|
request.url = base + "/peer/append";
|
|
request.body = std::move(body);
|
|
request.headers.push_back(auth());
|
|
return client.send(request);
|
|
};
|
|
|
|
auto anonymous = client.send(Void::Net::Request{
|
|
.method = Void::Net::Method::Post,
|
|
.url = base + "/peer/append",
|
|
.body = "entry"});
|
|
assert(anonymous.has_value());
|
|
assert(anonymous->status == 401);
|
|
|
|
auto first = append("entry-one");
|
|
assert(first.has_value());
|
|
assert(first->status == 200);
|
|
assert(first->body == "index=1");
|
|
|
|
auto second = append("entry-two");
|
|
assert(second.has_value());
|
|
assert(second->body == "index=2");
|
|
|
|
auto after = client.get(base + "/peer/health", {auth()});
|
|
assert(after.has_value());
|
|
assert(after->body.find("entries=2") != std::string::npos);
|
|
|
|
// Auth is required for the mutation even with a valid-looking path.
|
|
auto wrong_method = client.get(base + "/peer/append", {auth()});
|
|
assert(wrong_method.has_value());
|
|
assert(wrong_method->status == 405);
|
|
|
|
host->stop();
|
|
assert(host->peer_port() == 0);
|
|
const auto closed = client.get(base + "/peer/health", {auth()});
|
|
assert(!closed.has_value());
|
|
|
|
// Idempotent.
|
|
host->stop();
|
|
}
|
|
|
|
void bind_failure_rolls_back_in_reverse_order()
|
|
{
|
|
// Occupy a loopback port with a raw listener.
|
|
const int fd = ::socket(AF_INET, SOCK_STREAM, 0);
|
|
assert(fd >= 0);
|
|
sockaddr_in addr{};
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
addr.sin_port = 0;
|
|
assert(::bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == 0);
|
|
assert(::listen(fd, 4) == 0);
|
|
socklen_t length = sizeof(addr);
|
|
assert(::getsockname(fd, reinterpret_cast<sockaddr*>(&addr), &length) ==
|
|
0);
|
|
const std::uint16_t busy_port = ntohs(addr.sin_port);
|
|
|
|
SingleNodeConfig cfg = config();
|
|
cfg.peer_address = "127.0.0.1:" + std::to_string(busy_port);
|
|
auto host = SingleNodeHost::create(cfg);
|
|
assert(host.has_value());
|
|
|
|
auto started = host->start();
|
|
assert(!started.has_value());
|
|
assert(host->health().readiness == Readiness::Failed);
|
|
const std::vector<std::string> expected{
|
|
"storage.open", "storage.recover", "storage.close"};
|
|
assert(host->history() == expected);
|
|
assert(host->peer_port() == 0);
|
|
(void)::close(fd);
|
|
}
|
|
|
|
void log_cap_is_bounded()
|
|
{
|
|
PeerLog log{2};
|
|
assert(log.append("a").has_value());
|
|
assert(log.append("b").has_value());
|
|
auto rejected = log.append("c");
|
|
assert(!rejected.has_value());
|
|
assert(log.size() == 2);
|
|
assert(log.last_index() == 2);
|
|
}
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
config_fails_closed_without_a_token();
|
|
peer_fixture_requires_authentication_and_serves_bounded_state();
|
|
bind_failure_rolls_back_in_reverse_order();
|
|
log_cap_is_bounded();
|
|
std::puts("distributed single-node host ok");
|
|
return 0;
|
|
}
|