Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
593 changes: 98 additions & 495 deletions README.md

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions crates/mq-http/src/h1.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! HTTP/1.1 request head parser (spec §2.1), port of `src/gateway/mq_http1.c`.
//! HTTP/1.1 request head parser (spec §2.1).

pub const HEAD_MAX: usize = 16 * 1024;
pub const MAX_HEADERS: usize = 64;
/// Longest method (bytes) of the fetch listener's own request line only
/// (C `char[16]` minus NUL); the shared method bound is `limits::METHOD_MAX`.
/// Longest method (bytes) of the fetch listener's own request line only;
/// the shared method bound is `limits::METHOD_MAX`.
pub const METHOD_MAX: usize = 15;
/// Longest request-target of the fetch listener's own request line only
/// (C `char[1024]` minus NUL); the shared bound is `limits::TARGET_PATH_MAX`.
/// Longest request-target of the fetch listener's own request line only;
/// the shared bound is `limits::TARGET_PATH_MAX`.
pub const PATH_MAX: usize = 1023;

/// One header line; `value` is OWS-trimmed.
Expand Down Expand Up @@ -61,7 +61,7 @@ fn trim_ows(mut v: &[u8]) -> &[u8] {
v
}

/// Strict decimal in `0..=i64::MAX` (leading zeros accepted, as C).
/// Strict decimal in `0..=i64::MAX` (leading zeros accepted).
pub fn parse_content_length(v: &[u8]) -> Option<u64> {
if v.is_empty() {
return None;
Expand All @@ -86,7 +86,7 @@ fn te_lists_chunked(v: &[u8]) -> bool {
}

/// Parse a request head from the start of `buf` (spec §2.1). A head whose
/// terminator lies beyond `HEAD_MAX` is `TooLarge` (C answered Bad).
/// terminator lies beyond `HEAD_MAX` is `TooLarge`.
pub fn parse_head(buf: &[u8]) -> Progress<'_> {
let Some(p) = find(buf, b"\r\n\r\n") else {
return if buf.len() >= HEAD_MAX {
Expand Down Expand Up @@ -122,7 +122,7 @@ fn parse_complete(head: &[u8]) -> Option<Head<'_>> {
let rest = &line[m_end + 1..];
let t_end = rest.iter().position(|&c| c == b' ')?;
let target = &rest[..t_end];
// < 1024 bytes, origin-form, no control bytes / DEL (C parity; NUL would truncate).
// < 1024 bytes, origin-form, no control bytes / DEL.
if target.first() != Some(&b'/') || target.len() > PATH_MAX {
return None;
}
Expand Down Expand Up @@ -178,7 +178,7 @@ fn parse_complete(head: &[u8]) -> Option<Head<'_>> {
})
}

// ---- serializer (spec §2.2), port of the `mq_http1_write_*` helpers ----
// ---- serializer (spec §2.2) ----

/// `HTTP/1.1 <code> <reason>\r\n`; the fetch client passes an empty reason.
pub fn write_status(out: &mut Vec<u8>, code: u16, reason: &str) {
Expand Down Expand Up @@ -219,7 +219,7 @@ pub fn chunk_end(out: &mut Vec<u8>) {
}

/// Listener/reject reply: status, `Connection: close`, `Content-Length: 0`,
/// optional `X-Mq-Error`, blank line (C `gw_build_error`).
/// optional `X-Mq-Error`, blank line.
pub fn error_reply(code: u16, reason: &str, xmq: Option<&str>) -> Vec<u8> {
let mut o = Vec::with_capacity(96);
write_status(&mut o, code, reason);
Expand Down Expand Up @@ -315,7 +315,7 @@ mod tests {
b[..5].copy_from_slice(b"GET /");
assert_eq!(parse_head(&b), Progress::TooLarge);
assert_eq!(parse_head(&b[..HEAD_MAX - 1]), Progress::Need);
b.push(b'a'); // C's 17 KiB case
b.push(b'a');
assert_eq!(parse_head(&b), Progress::TooLarge);
}

Expand Down Expand Up @@ -362,7 +362,7 @@ mod tests {
b.resize(5 + 1100, b'a');
b.extend_from_slice(b" HTTP/1.1\r\n\r\n");
b
}); // C test_path_too_long
});
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions crates/mq-http/src/headers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Gateway header rules (spec §2.3), port of `src/gateway/mq_gw_headers.c`.
//! Gateway header rules (spec §2.3).

use crate::h1::is_tchar;
use crate::limits::{METHOD_MAX, TARGET_PATH_MAX};

/// Longest `X-Mq-Cache` TTL in seconds (1 year).
pub const CACHE_TTL_MAX: u32 = 31_536_000;
/// Longest target authority, both intakes (C `char authority[256]`).
/// Longest target authority, both intakes.
pub const AUTHORITY_MAX: usize = 255;

/// Parsed `X-Mq-Target` (bytes: any non-control, non-DEL, non-space byte round-trips).
Expand Down Expand Up @@ -82,7 +82,7 @@ pub fn parse_target(s: &[u8]) -> Option<Target> {
.map(|p| p + scan_from)
{
let port = &auth[pc + 1..];
// No numeric range check (as C).
// No numeric range check.
if port.is_empty() || !port.iter().all(u8::is_ascii_digit) || pc == 0 {
return None;
}
Expand Down Expand Up @@ -328,7 +328,7 @@ mod tests {
t(s).unwrap_or_else(|| panic!("expected Some for {s:?}"))
}

// ---- parse_target (test_gw_headers.c) ----
// ---- parse_target ----
#[test]
fn target_https_full() {
let x = ok("https://example.com/foo/bar?x=1&y=2");
Expand Down Expand Up @@ -415,7 +415,7 @@ mod tests {
assert!(t("https://h:/x").is_none());
assert!(t("https://:80").is_none());
assert!(t("https://h:80:90").is_none());
// no numeric range check, as C
// no numeric range check
assert_eq!(ok("https://h:99999").authority, b"h:99999");
}
#[test]
Expand Down
6 changes: 3 additions & 3 deletions crates/mq-http/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! `mq.req` log-line formatter (spec §2.4), byte-for-byte as C `mq_gw_format_req_line`.
//! `mq.req` log-line formatter (spec §2.4).
//! Works on bytes: no `String`, truncation is bytewise.
use std::io::Write;

/// C's `char line[1024]` including the NUL: a line of this length or more is dropped.
/// A line of this length or more is dropped.
pub const LINE_MAX: usize = 1024;
const AUTHORITY_CAP: usize = 128;
/// The quoted `path` cap; the server's call site also cuts the path here (spec §6.6).
Expand Down Expand Up @@ -261,7 +261,7 @@ reset=\"client-reset\""

#[test]
fn line_1024_dropped() {
// inflate `method` (uncapped, like C's %s) to hit the boundary.
// inflate `method` (uncapped) to hit the boundary.
let l = base();
let len = fmt(&l).len();
let m1023 = vec![b'M'; 3 + 1023 - len];
Expand Down
18 changes: 9 additions & 9 deletions crates/mq-integration/src/shard_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ pub fn server_role() -> Role {
}
}

/// The client settings the ports use: token "secret", reconnect off (as the
/// C fixtures), a 1 s maximum backoff for the reconnect cases.
/// The client settings the pair tests use: token "secret", reconnect off, a
/// 1 s maximum backoff for the reconnect cases.
pub fn client_cfg() -> ClientConfig {
ClientConfig {
server: server_addr(),
Expand Down Expand Up @@ -720,7 +720,7 @@ impl<S: App + 'static, C: App + 'static> Pair<S, C> {
self.with_server(|n| n.tap().open_conns())
}

/// `Cx::close_conn` on the client side (C `mq_conn_close`).
/// `Cx::close_conn` on the client side.
pub fn close_client_conn(&self, c: ConnId) {
self.with_client(move |n| n.with_app(|_, cx| cx.close_conn(c)));
}
Expand Down Expand Up @@ -916,8 +916,8 @@ pub struct RawConn {

/// spec §8.1: a client app below the real `Client` — it opens connections
/// and streams on command, sends and receives raw bytes, and on each
/// connection's first stream sends the C `AUTH_REQUEST` (or, unauthenticated,
/// one `0x00` byte, as C's `preauth` case, and never authenticates).
/// connection's first stream sends the `AUTH_REQUEST` (or, unauthenticated,
/// one `0x00` byte, and never authenticates).
/// Received bytes are read on every `StreamReadable` (the app-owned stream
/// rule, spec §5.4). Commands run through `Node::with_app`.
///
Expand Down Expand Up @@ -1043,7 +1043,7 @@ impl App for RawClient {
let bytes = if k.authenticate {
auth_request(&self.token)
} else {
vec![0x00] // C `preauth_on_state`'s nudge: never a complete AUTH_REQUEST
vec![0x00] // a nudge that is never a complete AUTH_REQUEST
};
let s = self.open_stream(cx, c);
self.conns.get_mut(&c).expect("known").ctrl = Some(s);
Expand Down Expand Up @@ -1107,7 +1107,7 @@ impl App for RawClient {

// --- Wire bytes ---

/// The C `AUTH_REQUEST` (version 1, client id "client-1", features 0).
/// The `AUTH_REQUEST` (version 1, client id "client-1", features 0).
pub fn auth_request(token: &[u8]) -> Vec<u8> {
let mut b = [0u8; 512];
let n = AuthReq {
Expand All @@ -1121,7 +1121,7 @@ pub fn auth_request(token: &[u8]) -> Vec<u8> {
b[..n].to_vec()
}

/// Stream type 0x01 then `CONNECT_TCP_REQUEST` to an IPv4 target (C `open_tcp_data_stream`).
/// Stream type 0x01 then `CONNECT_TCP_REQUEST` to an IPv4 target.
pub fn connect_request(target: SocketAddr) -> Vec<u8> {
let SocketAddr::V4(a) = target else {
panic!("IPv4 target");
Expand Down Expand Up @@ -1190,7 +1190,7 @@ pub fn socks_udp(dst: SocketAddr, payload: &[u8]) -> Vec<u8> {
b
}

/// Deterministic payload (C bulk origin: byte i is `i & 0xff`).
/// Deterministic payload (byte i is `i & 0xff`).
pub fn bulk(n: usize) -> Vec<u8> {
(0..n).map(|i| i as u8).collect()
}
2 changes: 1 addition & 1 deletion crates/mq-integration/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn wait(timeout: Duration, mut f: impl FnMut() -> bool) -> bool {
}
}

/// Stream type 0x01 then C `mq_encode_connect_tcp_req` for example.com:443.
/// Stream type 0x01 then a `CONNECT_TCP_REQUEST` for example.com:443.
pub const CONNECT_REQ_C: &[u8] = &[
0x01, 0x00, 0x03, 11, b'e', b'x', b'a', b'm', b'p', b'l', b'e', b'.', b'c', b'o', b'm', 0x01,
0xBB, 0x00,
Expand Down
1 change: 0 additions & 1 deletion crates/mq-integration/tests/driver_loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ fn accept_meta_has_local() {
stop(h);
}

/// Port of C `test_path_bind`.
#[test]
fn driver_path_bind() {
let mut d = Driver::new(DriverConfig {
Expand Down
21 changes: 6 additions & 15 deletions crates/mq-integration/tests/driver_socks5_listener.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
//! Port of C `tests/integration/test_socks5_listener.c` (spec §8.1, §6.1):
//! the real `Client`'s SOCKS5 and HTTP CONNECT listeners under the production
//! driver; the test is the ingress client, and the scripted transport stands
//! in for the server (AUTH answered by the rig, CONNECT answered here) where C
//! used a mock `tcp_open`.
//!
//! Ported: `socks5_happy`, `socks5_unsupported`, `socks5_malformed`,
//! `http_connect_smoke`, `socks5_pipelined_prebuf`, `http_pipelined_prebuf`,
//! `assoc_refused_no_udp`, `assoc_refused_unavail`. Not ported (SP2, UDP
//! ASSOCIATE served): `assoc_establish_free`, `assoc_tcp_close_teardown`,
//! `assoc_mixed_shutdown`, `assoc_no_close_after_err`,
//! `assoc_availability_sweep`, `assoc_dst_reclaim_churn`.
//! Spec §8.1, §6.1: the real `Client`'s SOCKS5 and HTTP CONNECT listeners
//! under the production driver; the test is the ingress client, and the
//! scripted transport stands in for the server (AUTH answered by the rig,
//! CONNECT answered here). Served UDP ASSOCIATE (SP2) is not covered here.
#![forbid(unsafe_code)]

mod common;
Expand Down Expand Up @@ -137,8 +129,7 @@ fn associate_refused(r: &ClientRig, opened: usize) {
assert_eq!(r.opened(), opened);
}

/// C: a listener without the UDP boundary. Here: no tunnel at all, so UDP
/// availability is still unknown and the ASSOCIATE is accepted optimistically
/// No tunnel at all, so UDP availability is still unknown and the ASSOCIATE is accepted optimistically
/// (SP2 spec §6.2) on a UDP socket bound on the control connection's local IP.
#[test]
fn assoc_refused_no_udp() {
Expand All @@ -153,7 +144,7 @@ fn assoc_refused_no_udp() {
r.stop();
}

/// C: UDP availability 0. Here: serving, the `AUTH_RESPONSE` without
/// Serving, the `AUTH_RESPONSE` without
/// `MQ_FEAT_UDP_RELAY`.
#[test]
fn assoc_refused_unavail() {
Expand Down
28 changes: 13 additions & 15 deletions crates/mq-integration/tests/loop_e2e_single_path.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
//! Ports `tests/integration/test_e2e_single_path.c` onto the loopback harness
//! (spec §8.1 "Loopback"): the real `Server` and `Client` on real transports,
//! Single-path cases on the loopback harness (spec §8.1 "Loopback"): the real `Server` and `Client` on real transports,
//! each on its own production driver thread, over loopback UDP. The test
//! thread plays the origin (`TcpListener`) and the application (`TcpStream`
//! through the client's SOCKS5 / HTTP CONNECT listener).
//!
//! | C case | here | sizes / patterns |
//! |--------------------------------|-----------------------------|------------------|
//! | `test_socks5_download` | `socks5_download` | 200000 B, `i & 0xff`, then EOF |
//! | `test_socks5_echo` | `socks5_echo` | 4096 B, `i*31+7` |
//! | `test_http_echo` | `http_echo` | 4096 B, `i*17+3` |
//! | `test_socks5_pipelined_payload`| `socks5_pipelined_payload` | `EARLY-PIPELINED-BYTES` in the request write |
//! | `test_http_pipelined_payload` | `http_pipelined_payload` | `EARLY-HTTP-BYTES` in the head write |
//! | `test_concurrent_ingress` | `concurrent_ingress` | 1 SOCKS5 + 1 HTTP, 4096 B each, `i*31+7` / `(i*17+3)^0xa5` |
//! | `test_socks5_refused` | `socks5_refused` | dead port → REP 0x05, then close |
//! | `test_http_refused` | `http_refused` | dead port → `HTTP/1.1 502`, then close |
//! | test | sizes / patterns |
//! |-----------------------------|------------------|
//! | `socks5_download` | 200000 B, `i & 0xff`, then EOF |
//! | `socks5_echo` | 4096 B, `i*31+7` |
//! | `http_echo` | 4096 B, `i*17+3` |
//! | `socks5_pipelined_payload` | `EARLY-PIPELINED-BYTES` in the request write |
//! | `http_pipelined_payload` | `EARLY-HTTP-BYTES` in the head write |
//! | `concurrent_ingress` | 1 SOCKS5 + 1 HTTP, 4096 B each, `i*31+7` / `(i*17+3)^0xa5` |
//! | `socks5_refused` | dead port → REP 0x05, then close |
//! | `http_refused` | dead port → `HTTP/1.1 502`, then close |
//!
//! `socks5_echo_ipv6_path` (no C counterpart in this file; C supports
//! `AF_INET6` QUIC paths) runs the tunnel over `::1` with IPv4 ingress;
//! `socks5_echo_ipv6_path` runs the tunnel over `::1` with IPv4 ingress;
//! `socks5_echo_v6_wildcard_path_to_v4_server` runs it from a dual-stack
//! `[::]` client path to a `127.0.0.1` server (C's `--path ::`).
//! `[::]` client path (`--path ::`) to a `127.0.0.1` server.
//!
//! Each test ends with both drivers stopped through their handles, exit (0, 0).
#![forbid(unsafe_code)]
Expand Down
14 changes: 6 additions & 8 deletions crates/mq-integration/tests/loop_engine_boot.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! Ports `tests/integration/test_engine_boot.c` onto the loopback harness
//! (spec §8.1 "Loopback"). The C test boots one client transport + runtime,
//! opens the primary UDP path on `127.0.0.1:0`, runs the loop and stops it
//! cleanly. Here both sides boot (real xquic engine, production driver, each
//! on its own thread), the client completes a handshake with the server, and
//! each driver is stopped through its `ShutdownHandle` with exit status 0.
//! Engine boot on the loopback harness (spec §8.1 "Loopback"): both sides
//! boot (real xquic engine, production driver, each on its own thread) on
//! `127.0.0.1:0`, the client completes a handshake with the server, and each
//! driver is stopped through its `ShutdownHandle` with exit status 0.

use mq_integration::loopback::LoopbackPair;
use mq_runtime::Shard;
Expand Down Expand Up @@ -90,7 +88,7 @@ fn loop_engine_boot() {
(Shard::new(t, app, local, 2), rec)
},
);
// The C test's `mq_runtime_open_udp_path("127.0.0.1", 0) == 0`.
// Both primary UDP paths are bound on loopback with a kernel-chosen port.
for a in [pair.server.udp_addr, pair.client.udp_addr] {
assert!(a.ip().is_loopback() && a.port() != 0, "{a}");
}
Expand All @@ -109,6 +107,6 @@ fn loop_engine_boot() {
);
assert!(pair.server.stats.iterations() > 0 && pair.client.stats.iterations() > 0);

// The C test's stop timer: each side stopped through its own handle.
// Each side stopped through its own handle.
assert_eq!(pair.join_both(), (0, 0));
}
41 changes: 19 additions & 22 deletions crates/mq-integration/tests/pair_cs.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
//! Ports `tests/integration/test_client_server.c` onto the shard pair
//! (spec §8.1 "Shard pair"), one test per C case:
//! Client↔server cases on the shard pair (spec §8.1 "Shard pair"):
//!
//! | C case | test | client side |
//! | case | test | client side |
//! |---|---|---|
//! | `test_auth_wrong_token` (A) | `pair_cs_auth_wrong_token` | real `Client` |
//! | `test_auth_matching_token` (B) | `pair_cs_auth_matching_token` | `RawClient` |
//! | `test_data_stream_echo` (C) | `pair_cs_data_stream_echo` | `RawClient` |
//! | `test_data_stream_refused` (D) | `pair_cs_data_stream_refused` | `RawClient` |
//! | `test_data_stream_preauth_reset` (E) | `pair_cs_data_stream_preauth_reset` | `RawClient` |
//! | `test_data_stream_teardown` (F) | `pair_cs_data_stream_teardown` | `RawClient` |
//! | `test_data_stream_download_completion` (G) | `pair_cs_data_stream_download_completion` | `RawClient` |
//! | `test_data_stream_halfclose_nospin` (H) | `pair_cs_data_stream_halfclose_nospin` | `RawClient` |
//! | `test_data_stream_coalesced_payload` (M) | `pair_cs_data_stream_coalesced_payload` | `RawClient` |
//! | `test_client_open_echo` (I) | `pair_cs_client_open_echo` | real `Client` |
//! | `test_client_open_download` (J) | `pair_cs_client_open_download` | real `Client` |
//! | `test_client_open_preauth_queue` (K) | `pair_cs_client_open_preauth_queue` | real `Client` |
//! | `test_client_open_refused` (L) | `pair_cs_client_open_refused` | real `Client` |
//! | A | `pair_cs_auth_wrong_token` | real `Client` |
//! | B | `pair_cs_auth_matching_token` | `RawClient` |
//! | C | `pair_cs_data_stream_echo` | `RawClient` |
//! | D | `pair_cs_data_stream_refused` | `RawClient` |
//! | E | `pair_cs_data_stream_preauth_reset` | `RawClient` |
//! | F | `pair_cs_data_stream_teardown` | `RawClient` |
//! | G | `pair_cs_data_stream_download_completion` | `RawClient` |
//! | H | `pair_cs_data_stream_halfclose_nospin` | `RawClient` |
//! | M | `pair_cs_data_stream_coalesced_payload` | `RawClient` |
//! | I | `pair_cs_client_open_echo` | real `Client` |
//! | J | `pair_cs_client_open_download` | real `Client` |
//! | K | `pair_cs_client_open_preauth_queue` | real `Client` |
//! | L | `pair_cs_client_open_refused` | real `Client` |
//!
//! No case is excluded. The C test's in-process origins become the server
//! side's `Origin` (echo / bulk-then-close / refusing); the C
//! `mq_client_tcp_open` over a socketpair becomes a SOCKS5 request from a
//! scripted local socket, whose SOCKS5 reply stands for the open callback
//! (`ok` = success reply, `err` = the reply code). Reconnect is off, as in
//! the C fixture.
//! The origins are the server side's `Origin` (echo / bulk-then-close /
//! refusing); a client open is a SOCKS5 request from a scripted local socket,
//! whose SOCKS5 reply stands for the open callback (`ok` = success reply,
//! `err` = the reply code). Reconnect is off.
#![forbid(unsafe_code)]

use mq_integration::shard_pair::*;
Expand Down
8 changes: 3 additions & 5 deletions crates/mq-integration/tests/pair_max_conns.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Ports `tests/integration/test_max_conns_e2e.c` onto the shard pair (spec
//! §8.1 "Shard pair", §4.7 `max_conns`). The C test's three `mq_client`s A,
//! B and C are three connections of one `RawClient` (each sends the C
//! `AUTH_REQUEST`); C's `mq_transport_n_conns` is `Transport::conn_count`.
//! Nothing is excluded.
//! `max_conns` on the shard pair (spec §8.1 "Shard pair", §4.7 `max_conns`):
//! clients A, B and C are three connections of one `RawClient` (each sends the
//! `AUTH_REQUEST`), counted by `Transport::conn_count`.
#![forbid(unsafe_code)]

use mq_integration::shard_pair::*;
Expand Down
Loading
Loading