Files

83 lines
3.9 KiB
Nix

# `nix flake check` (Linux, KVM): two machines. `server` runs the module; `client` talks to it
# through Caddy with TLS, does everything the protocol allows and some things it refuses, fills a
# mailbox cap to its 507, and then the test fails if the client's address appears anywhere in the
# server's journal or under /var/log, or in the relay's database (.agents/features/home-relay.md).
{ self, pkgs }:
pkgs.testers.runNixOSTest {
name = "tabellarius-relay-keeps-no-address";
nodes.server = { lib, ... }: {
imports = [ self.nixosModules.default ];
services.tabellarius = {
enable = true;
domain = "relay.test";
maxMailboxes = 3;
};
# No Let's Encrypt inside a VM: Caddy's own CA instead.
services.caddy.virtualHosts."relay.test".extraConfig = lib.mkBefore "tls internal";
};
nodes.client = { pkgs, ... }: {
environment.systemPackages = [ pkgs.curl pkgs.python3 ];
};
testScript = ''
import base64, hashlib, json, os
def b64(raw):
return base64.urlsafe_b64encode(raw).rstrip(b"=").decode()
start_all()
server.wait_for_unit("tabellarius.service")
server.wait_for_unit("caddy.service")
server.wait_for_open_port(443)
client.wait_for_unit("multi-user.target")
client_ip = client.succeed("ip -4 -o addr show dev eth1 | awk '{print $4}' | cut -d/ -f1").strip()
server_ip = server.succeed("ip -4 -o addr show dev eth1 | awk '{print $4}' | cut -d/ -f1").strip()
assert client_ip and server_ip and client_ip != server_ip, (client_ip, server_ip)
def call(method, path, token=None, body=None, json_body=False):
cmd = ["curl", "-sk", "-o", "/dev/null", "-w", "%{http_code}",
"--resolve", "relay.test:443:" + server_ip, "-X", method]
if token:
cmd += ["-H", "Authorization: Bearer " + token]
if body is not None:
client.succeed("cat > /tmp/body <<'EOF'\n" + body + "\nEOF")
client.succeed("truncate -s -1 /tmp/body")
cmd += ["--data-binary", "@/tmp/body"]
if json_body:
cmd += ["-H", "Content-Type: application/json"]
cmd.append("https://relay.test" + path)
return client.succeed(" ".join("'" + c + "'" for c in cmd)).strip()
def mailbox():
mid, post, fetch = os.urandom(16), os.urandom(32), os.urandom(32)
body = json.dumps({"id": b64(mid), "postTokenHash": b64(hashlib.sha256(post).digest()),
"fetchTokenHash": b64(hashlib.sha256(fetch).digest())})
return b64(mid), b64(post), b64(fetch), body
assert call("GET", "/version") == "200"
mid, post, fetch, body = mailbox()
assert call("POST", "/v0/mailboxes", body=body, json_body=True) == "201"
blob = "sealed-envelope-for-the-test"
assert call("POST", "/v0/mailboxes/" + mid + "/blobs", token=post, body=blob) == "201"
assert call("GET", "/v0/mailboxes/" + mid + "/blobs", token=fetch) == "200"
digest = b64(hashlib.sha256(blob.encode()).digest())
assert call("DELETE", "/v0/mailboxes/" + mid + "/blobs/" + digest, token=fetch) == "204"
# Refusals: a wrong token, a malformed id, a body that is not JSON.
assert call("GET", "/v0/mailboxes/" + mid + "/blobs", token=b64(os.urandom(32))) == "404"
assert call("GET", "/v0/mailboxes/nonsense/blobs", token=fetch) == "404"
assert call("POST", "/v0/mailboxes", body="not json", json_body=True) == "400"
# The cap: three mailboxes, then 507.
for _ in range(2):
assert call("POST", "/v0/mailboxes", body=mailbox()[3], json_body=True) == "201"
assert call("POST", "/v0/mailboxes", body=mailbox()[3], json_body=True) == "507"
# Nowhere on the server: not in the journal, not under /var/log, not in the relay's data.
server.succeed("journalctl --sync")
server.fail("journalctl --no-pager -o verbose | grep -F '" + client_ip + "'")
server.fail("grep -rF '" + client_ip + "' /var/log /var/lib/private/tabellarius")
'';
}