3a0944ebff
ci / check (pull_request) Has been cancelled
The relay stores files (id, upload and fetch token hashes, chunk count, uploaded hour) and chunks (file id, index, ciphertext): every chunk is 256 000 bytes, so it learns the size rounded up to 250 KiB and nothing finer, and it keeps no fetch count. Only the uploader's token puts or deletes, only the fetch token gets, a chunk never changes; per-file and relay-wide quotas answer 507; files go after 30 days, incomplete ones after 24 hours. The client pads a file to whole chunks and seals each with ChaCha20-Poly1305 in the aead crate's STREAM, under a key made for that file; the key, name, type, size and thumbnail travel only in the new `file` payload, which shares `seq` with text. Also: every route now answers a bare 413 (axum's own carried a text). PROTOCOL.md §1, §4, §5, §7, §8.
301 lines
8.9 KiB
Rust
301 lines
8.9 KiB
Rust
//! Quotas (`.agents/features/home-relay.md` §4, `PROTOCOL.md` §8): over a limit the relay answers a
|
|
//! bare 507; the per-mailbox caps only after the post token. Small limits, so the tests are quick.
|
|
|
|
use axum::body::Body;
|
|
use axum::http::{header, Method, Request, StatusCode};
|
|
use http_body_util::BodyExt;
|
|
use std::sync::Arc;
|
|
use tabellarius_protocol::*;
|
|
use tabellarius_relay::http::{router, AppState, Clock};
|
|
use tabellarius_relay::parse_args;
|
|
use tabellarius_relay::store::{Limits, Store, MAILBOX_BLOBS, MAILBOX_BYTES};
|
|
use tower::ServiceExt;
|
|
|
|
const T0: i64 = 1_794_000_000;
|
|
|
|
fn random<const N: usize>() -> [u8; N] {
|
|
let mut b = [0u8; N];
|
|
getrandom::fill(&mut b).expect("system randomness");
|
|
b
|
|
}
|
|
|
|
struct Mailbox {
|
|
id: MailboxId,
|
|
post: Token,
|
|
fetch: Token,
|
|
}
|
|
|
|
impl Mailbox {
|
|
fn new() -> Self {
|
|
Self {
|
|
id: MailboxId::from_bytes(random()),
|
|
post: Token::from_bytes(random()),
|
|
fetch: Token::from_bytes(random()),
|
|
}
|
|
}
|
|
fn path(&self) -> String {
|
|
format!("/v0/mailboxes/{}/blobs", self.id.to_wire())
|
|
}
|
|
}
|
|
|
|
struct Relay(axum::Router);
|
|
|
|
impl Relay {
|
|
fn with(limits: Limits) -> Self {
|
|
let store = Arc::new(Store::open_in_memory_with(limits).expect("store"));
|
|
let clock: Clock = Arc::new(|| T0);
|
|
Relay(router(AppState::new(store, clock)))
|
|
}
|
|
async fn call(
|
|
&self,
|
|
method: Method,
|
|
path: &str,
|
|
auth: Option<&Token>,
|
|
body: Vec<u8>,
|
|
) -> StatusCode {
|
|
let mut req = Request::builder().method(method).uri(path);
|
|
if let Some(t) = auth {
|
|
req = req.header(header::AUTHORIZATION, t.to_bearer());
|
|
}
|
|
let resp = self
|
|
.0
|
|
.clone()
|
|
.oneshot(req.body(Body::from(body)).expect("request"))
|
|
.await
|
|
.expect("infallible");
|
|
let status = resp.status();
|
|
let body = resp.into_body().collect().await.expect("body").to_bytes();
|
|
if !status.is_success() {
|
|
assert!(body.is_empty(), "a refusal has no body");
|
|
}
|
|
status
|
|
}
|
|
async fn create(&self, m: &Mailbox) -> StatusCode {
|
|
let body = serde_json::to_vec(&CreateMailbox {
|
|
id: m.id,
|
|
post_token_hash: m.post.hash(),
|
|
fetch_token_hash: m.fetch.hash(),
|
|
})
|
|
.expect("json");
|
|
self.call(Method::POST, "/v0/mailboxes", None, body).await
|
|
}
|
|
async fn post(&self, m: &Mailbox, token: &Token, blob: &[u8]) -> StatusCode {
|
|
self.call(Method::POST, &m.path(), Some(token), blob.to_vec())
|
|
.await
|
|
}
|
|
async fn ack(&self, m: &Mailbox, blob: &[u8]) -> StatusCode {
|
|
let path = format!("{}/{}", m.path(), blob_hash(blob).to_wire());
|
|
self.call(Method::DELETE, &path, Some(&m.fetch), vec![])
|
|
.await
|
|
}
|
|
async fn invite(&self) -> (StatusCode, InviteId, Token) {
|
|
let id = InviteId::from_bytes(random());
|
|
let secret = Token::from_bytes(random());
|
|
let body = serde_json::to_vec(&CreateInvite {
|
|
id,
|
|
secret_hash: secret.hash(),
|
|
key_package: KeyPackageBytes::new(b"kp".to_vec()).expect("small"),
|
|
})
|
|
.expect("json");
|
|
(
|
|
self.call(Method::POST, "/v0/invites", None, body).await,
|
|
id,
|
|
secret,
|
|
)
|
|
}
|
|
}
|
|
|
|
fn small(f: impl FnOnce(&mut Limits)) -> Limits {
|
|
let mut l = Limits::default();
|
|
f(&mut l);
|
|
l
|
|
}
|
|
|
|
#[test]
|
|
fn the_protocol_caps_are_the_documented_ones() {
|
|
assert_eq!(MAILBOX_BLOBS, 1_000);
|
|
assert_eq!(MAILBOX_BYTES, 32 * 1024 * 1024);
|
|
let l = Limits::default();
|
|
assert_eq!(
|
|
(l.total_bytes, l.mailboxes, l.invites),
|
|
(1 << 30, 100_000, 10_000)
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_mailbox_holds_a_thousand_blobs_then_says_full_until_one_is_acknowledged() {
|
|
let relay = Relay::with(Limits::default());
|
|
let m = Mailbox::new();
|
|
assert_eq!(relay.create(&m).await, StatusCode::CREATED);
|
|
for i in 0..MAILBOX_BLOBS {
|
|
assert_eq!(
|
|
relay.post(&m, &m.post, &i.to_le_bytes()).await,
|
|
StatusCode::CREATED
|
|
);
|
|
}
|
|
let one_more = b"one more";
|
|
assert_eq!(
|
|
relay.post(&m, &m.post, one_more).await,
|
|
StatusCode::INSUFFICIENT_STORAGE
|
|
);
|
|
// A stranger without the token learns nothing: 404, as for any wrong token.
|
|
let stranger = Token::from_bytes(random());
|
|
assert_eq!(
|
|
relay.post(&m, &stranger, one_more).await,
|
|
StatusCode::NOT_FOUND
|
|
);
|
|
assert_eq!(
|
|
relay.ack(&m, &0i64.to_le_bytes()).await,
|
|
StatusCode::NO_CONTENT
|
|
);
|
|
assert_eq!(relay.post(&m, &m.post, one_more).await, StatusCode::CREATED);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_mailbox_holds_at_most_its_bytes() {
|
|
let relay = Relay::with(small(|l| l.mailbox_bytes = 1000));
|
|
let m = Mailbox::new();
|
|
relay.create(&m).await;
|
|
assert_eq!(
|
|
relay.post(&m, &m.post, &[1u8; 600]).await,
|
|
StatusCode::CREATED
|
|
);
|
|
assert_eq!(
|
|
relay.post(&m, &m.post, &[2u8; 400]).await,
|
|
StatusCode::CREATED,
|
|
"exactly at the cap"
|
|
);
|
|
assert_eq!(
|
|
relay.post(&m, &m.post, &[3u8; 1]).await,
|
|
StatusCode::INSUFFICIENT_STORAGE
|
|
);
|
|
// Another mailbox is not affected.
|
|
let other = Mailbox::new();
|
|
relay.create(&other).await;
|
|
assert_eq!(
|
|
relay.post(&other, &other.post, &[4u8; 600]).await,
|
|
StatusCode::CREATED
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_relay_holds_at_most_its_bytes_across_mailboxes() {
|
|
let relay = Relay::with(small(|l| l.total_bytes = 1000));
|
|
let (a, b) = (Mailbox::new(), Mailbox::new());
|
|
relay.create(&a).await;
|
|
relay.create(&b).await;
|
|
assert_eq!(
|
|
relay.post(&a, &a.post, &[1u8; 700]).await,
|
|
StatusCode::CREATED
|
|
);
|
|
assert_eq!(
|
|
relay.post(&b, &b.post, &[2u8; 301]).await,
|
|
StatusCode::INSUFFICIENT_STORAGE
|
|
);
|
|
assert_eq!(
|
|
relay.post(&b, &b.post, &[2u8; 300]).await,
|
|
StatusCode::CREATED
|
|
);
|
|
assert_eq!(relay.ack(&a, &[1u8; 700]).await, StatusCode::NO_CONTENT);
|
|
assert_eq!(
|
|
relay.post(&b, &b.post, &[3u8; 700]).await,
|
|
StatusCode::CREATED,
|
|
"room again after an acknowledgement"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_relay_holds_at_most_its_mailboxes() {
|
|
let relay = Relay::with(small(|l| l.mailboxes = 3));
|
|
for _ in 0..3 {
|
|
assert_eq!(relay.create(&Mailbox::new()).await, StatusCode::CREATED);
|
|
}
|
|
assert_eq!(
|
|
relay.create(&Mailbox::new()).await,
|
|
StatusCode::INSUFFICIENT_STORAGE
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_relay_holds_at_most_its_unclaimed_invites() {
|
|
let relay = Relay::with(small(|l| l.invites = 2));
|
|
let (s, id, secret) = relay.invite().await;
|
|
assert_eq!(s, StatusCode::CREATED);
|
|
assert_eq!(relay.invite().await.0, StatusCode::CREATED);
|
|
assert_eq!(relay.invite().await.0, StatusCode::INSUFFICIENT_STORAGE);
|
|
// A claimed invite is gone and makes room.
|
|
let path = format!("/v0/invites/{}", id.to_wire());
|
|
assert_eq!(
|
|
relay.call(Method::GET, &path, Some(&secret), vec![]).await,
|
|
StatusCode::OK
|
|
);
|
|
assert_eq!(relay.invite().await.0, StatusCode::CREATED);
|
|
}
|
|
|
|
#[test]
|
|
fn the_operator_sets_the_relay_wide_caps() {
|
|
let args = parse_args(
|
|
[
|
|
"--max-bytes",
|
|
"5000",
|
|
"--max-mailboxes",
|
|
"7",
|
|
"--max-invites",
|
|
"9",
|
|
"--max-files",
|
|
"11",
|
|
"--max-file-bytes",
|
|
"13",
|
|
]
|
|
.map(String::from),
|
|
)
|
|
.expect("args");
|
|
assert_eq!(
|
|
(
|
|
args.limits.total_bytes,
|
|
args.limits.mailboxes,
|
|
args.limits.invites,
|
|
args.limits.files,
|
|
args.limits.file_bytes
|
|
),
|
|
(5000, 7, 9, 11, 13)
|
|
);
|
|
assert_eq!(
|
|
args.limits.mailbox_blobs, MAILBOX_BLOBS,
|
|
"not the operator's"
|
|
);
|
|
for bad in [
|
|
vec!["--max-bytes"],
|
|
vec!["--max-bytes", "0"],
|
|
vec!["--max-bytes", "-5"],
|
|
vec!["--max-mailboxes", "many"],
|
|
vec!["--max-invites", "1.5"],
|
|
vec!["--max-files", "0"],
|
|
vec!["--max-file-bytes", "-1"],
|
|
] {
|
|
assert!(
|
|
parse_args(bad.iter().map(|s| s.to_string())).is_err(),
|
|
"{bad:?}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn an_expired_invite_does_not_count_against_new_ones() {
|
|
let store = Store::open_in_memory_with(small(|l| l.invites = 1)).expect("store");
|
|
let invite = || CreateInvite {
|
|
id: InviteId::from_bytes(random()),
|
|
secret_hash: Token::from_bytes(random()).hash(),
|
|
key_package: KeyPackageBytes::new(b"kp".to_vec()).expect("small"),
|
|
};
|
|
store.create_invite(&invite(), T0).expect("first");
|
|
assert_eq!(
|
|
store.create_invite(&invite(), T0 + 3600),
|
|
Err(tabellarius_relay::store::Refused::Full)
|
|
);
|
|
// Eight days on, the first has expired, though the hourly purge has not run yet.
|
|
store
|
|
.create_invite(&invite(), T0 + 8 * 24 * 3600)
|
|
.expect("room after the expiry");
|
|
}
|