English
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 frombind(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 toto;Datagrams::recv()waits up to 5 s for a reply and the address it came from.Recorder— aDispatchthat sends every flow handed to it (Routed::StreamorRouted::Packet, with its metadata) to a channel;connectis refused.serve(inbound) -> Served— an inbound fed throughInbound::injectandInbound::inject_packetinstead of listening sockets, with aRecorderfor 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()androuted_packet()wait up to 5 s for what the inbound hands over. Only an inbound that implementsinject(forconnect) orinject_packet(forsend_packets) can be served this way; for the others they returnUnsupported.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,Rangesbetween five-byte headers, orCopy) and writes in the given one (Owned { fill },Records { fill }—fillsays whether a socket fills the stream throughwrite_from— orCopy);Callscounts 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 withskipping: `<capability>` is unavailable; ifOURS_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].