Skip to content

ours-testkit ​

crates/testkit — helpers for the tests of modules and the binary.

API ​

  • config(text, decoders) -> Config — a native configuration loaded the way the kernel takes it, with the blocks the modules decoded; panics with the error position.
  • try_config(text, decoders) -> Result<Config, String> — the same, with an error.
  • free_port() — a loopback port free for both TCP and UDP, from 20000–32000: below the Linux ephemeral range (32768–60999), because a port from bind(0) may go to another test's outgoing socket before the server under test takes it. The helper releases both sockets before returning, so another process can still take the port. If the test owns the listener, bind it to port 0 and read the address while keeping the socket open.
  • duplex(capacity) -> (Stream, DuplexStream) — an inbound's stream and its peer, in memory.
  • datagrams() -> (Box<dyn PacketConn>, Datagrams) — an inbound's packet flow and its client, in memory. Datagrams::send(payload, to) sends a datagram as a client sends it to to; Datagrams::recv() waits up to 5 s for a reply and the address it came from.
  • Recorder — a Dispatch that sends every flow handed to it (Routed::Stream or Routed::Packet, with its metadata) to a channel; connect is refused.
  • serve(inbound) -> Served — an inbound fed through Inbound::inject and Inbound::inject_packet instead of listening sockets, with a Recorder for its dispatcher. Served::connect(source) gives the client's end of a connection, Served::send_packets(source) that of a packet flow; Served::routed(), routed_stream() and routed_packet() wait up to 5 s for what the inbound hands over. Only an inbound that implements inject (for connect) or inject_packet (for send_packets) can be served this way; for the others they return Unsupported.
  • hostile_inputs(frame, parse) — feeds a parser truncated, corrupted and padded copies of a valid frame and names the case that panics.
  • forms::stream(reads, writes, capacity) -> (Stream, DuplexStream, Arc<Calls>) — a protocol stream in memory that reads in the given buffer form (Pieces, which declines one read after every fifth piece, Ranges between five-byte headers, or Copy) and writes in the given one (Owned { fill }, Records { fill } — fill says whether a socket fills the stream through write_from — or Copy); Calls counts the calls of each form. forms::raw(tcp) — a protocol stream over a socket, raw both ways. For the relay's test of every pair of forms.
  • skip_unless(capability, available) — whether a test that needs something from the environment runs. Without it the test is skipped with skipping: `<capability>` is unavailable; if OURS_EXPECT=<names> (comma-separated) lists it, the test panics instead, so a broken runner does not pass silently. The kTLS and zerocopy tests use it.
  • transport — what transport and protocol tests share: pair() (two ends of a loopback TCP connection), echo, echo_server, exchange, round_trip (bulk exchanges).

YAML accepts JSON flow syntax, so a short configuration is written in a single line:

rust
let text = r#"{"outbounds": {"out": {"direct": null}}, "route": {"final": {"outbound": "out"}}}"#;
let options = ours_testkit::config(text.as_bytes(), &MODULES.decoders());

A test of an inbound alone needs neither the kernel nor other modules (examples: tcp_gets_through in trojan, one_udp_flow_carries_several_destinations in shadowsocks):

rust
let mut served = ours_testkit::serve(inbound);
let client = served.connect("192.0.2.1:1234".parse()?)?;
// the client speaks the protocol on `client`
let (stream, metadata) = served.routed_stream().await;

let mut client = served.send_packets("192.0.2.1:1234".parse()?)?;
client.send(&datagram, server);
let (conn, metadata, idle) = served.routed_packet().await;

A typical module test (modules/system/ntp/tests/instance.rs) declares its own module LIST, builds Modules::new(LIST) and a kernel instance Instance::new(&options, &MODULES).

Who uses it ​

Only as a dev dependency: of most modules, ours-kernel, ours-relay and the binary.

Specifics ​

Depends on bytes, ours-api, ours-bufpool, ours-native-config and tokio; the test itself has to add ours-kernel and the needed modules to [dev-dependencies].

GPL-3.0-or-later