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
100 changes: 99 additions & 1 deletion crates/transferd-overlay/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,43 @@ mod tests {
)
}

/// The same link, with a switch: while `cut` is set, nothing written reaches the other end and nothing arrives
/// from it. That is what an unresponsive peer looks like from here: its acknowledgements never come.
fn switchable_link_pair(
id: u32,
cut: Arc<std::sync::atomic::AtomicBool>,
) -> (Arc<PipeLink>, Arc<PipeLink>) {
let a_to_b: Queue = Default::default();
let b_to_a: Queue = Default::default();
let end = |out: Queue, inbox: Queue, cut: Arc<std::sync::atomic::AtomicBool>| {
let cut_write = cut.clone();
Pipe {
write: Box::new(move |bytes| {
if !cut_write.load(Ordering::Relaxed) {
out.lock().push_back(bytes);
}
Ok(())
}),
incoming: Box::new(move || {
if cut.load(Ordering::Relaxed) {
return Ok(None);
}
Ok(inbox.lock().pop_front())
}),
close: Box::new(|| {}),
}
};
(
PipeLink::new(
id,
RouteKind::Direct,
"pipe",
end(a_to_b.clone(), b_to_a.clone(), cut.clone()),
),
PipeLink::new(id, RouteKind::Direct, "pipe", end(b_to_a, a_to_b, cut)),
)
}

fn setup(me: &NodeIdentity, peer: &NodeIdentity) -> SessionSetup {
SessionSetup {
peer_node_id: peer.node_id().to_string(),
Expand Down Expand Up @@ -2248,11 +2285,24 @@ mod tests {
SessionHandle,
StreamId,
mpsc::UnboundedReceiver<SessionEvent>,
) {
let (route, phone_end) = link_pair(3);
open_stream_pair_over(route, phone_end).await
}

/// The same pair, over the link ends given.
async fn open_stream_pair_over(
route: Arc<PipeLink>,
phone_end: Arc<PipeLink>,
) -> (
SessionHandle,
SessionHandle,
StreamId,
mpsc::UnboundedReceiver<SessionEvent>,
) {
let desk_id = NodeIdentity::generate("desktop");
let phone_id = NodeIdentity::generate("phone");
let (desk_events_tx, mut desk_events) = mpsc::unbounded_channel();
let (route, phone_end) = link_pair(3);
let desk = spawn(
setup(&desk_id, &phone_id),
vec![route as Arc<dyn Link>],
Expand Down Expand Up @@ -2409,6 +2459,54 @@ mod tests {
assert!(desk.recv(stream).await.is_err());
}

/// A close that cannot be flushed because the peer stopped answering gives up after `FLUSH_TIMEOUT`, and ends
/// the stream with a reset. The peer's reader must see that error once it is reachable again. A clean end here
/// would tell it the stream was complete when the bytes behind it were never acknowledged.
#[tokio::test]
async fn a_close_against_an_unresponsive_peer_resets_the_stream_instead_of_ending_it() {
let cut = Arc::new(std::sync::atomic::AtomicBool::new(false));
let (route, phone_end) = switchable_link_pair(3, cut.clone());
let (desk, phone, stream, _events) = open_stream_pair_over(route, phone_end).await;

// The peer stops answering, and the desktop writes: nothing it sends can be acknowledged.
cut.store(true, Ordering::Relaxed);
desk.send(stream, b"never acknowledged".to_vec(), false)
.await
.expect("the write is queued");

// The close waits for the flush, which cannot finish while the peer is silent. It must give up on its own,
// after FLUSH_TIMEOUT, and not hang.
let started = tokio::time::Instant::now();
desk.close(stream).await;
let waited = started.elapsed();
assert!(
waited >= FLUSH_TIMEOUT,
"the close returned after {waited:?}, before the flush could have timed out"
);
assert!(
waited < FLUSH_TIMEOUT + Duration::from_secs(5),
"the close took {waited:?}: it did not give up after FLUSH_TIMEOUT"
);

// The peer is reachable again. The reset is a control frame, so it is resent until the peer acknowledges it.
cut.store(false, Ordering::Relaxed);
let end = tokio::time::timeout(Duration::from_secs(15), async {
loop {
match phone.recv(stream).await {
// Bytes that made it through before the reset are read first.
Ok(Some(_)) => {}
other => return other.map(|_| ()),
}
}
})
.await
.expect("the peer's reader gets an answer");
assert!(
end.is_err(),
"the peer read a clean end of a stream that was never flushed: {end:?}"
);
}

/// A route that refuses a frame must not stop the session. The frame goes out on the next
/// live route, so both sides get their bytes, and the refusing route is tried first.
#[tokio::test]
Expand Down
15 changes: 12 additions & 3 deletions crates/transferd-overlay/tests/relay_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,24 @@ async fn a_relay_that_stops_being_reachable_is_reported_not_hung() {
stream.write_all(b"alive").await.expect("write");
assert_eq!(&read_exact(&mut stream, 6).await, b"\xE5alive");

// When the relay is gone the session stops carrying traffic, and `overlay.peers` says so
// instead of the node pretending it is fine.
// When the relay is gone its route stops carrying traffic, and `overlay.peers` says so instead
// of the node pretending it is fine.
//
// The check is on the relayed route only. Both nodes are on loopback, so they can also
// hole-punch a direct UDP path while the test runs; that route is real, and once it is up it
// may carry the traffic. "Every route is down" is therefore not the property under test, and
// it was the cause of this test failing intermittently.
relay.stop();
eventually("the relayed route to be reported down", || async {
let peers = a.node.peer_list();
peers.len() == 1
&& peers[0]["routes"]
.as_array()
.map(|routes| !routes.is_empty() && routes.iter().all(|r| r["active"] == false))
.map(|routes| {
let relayed: Vec<&serde_json::Value> =
routes.iter().filter(|r| r["kind"] == "relay").collect();
!relayed.is_empty() && relayed.iter().all(|r| r["active"] == false)
})
.unwrap_or(false)
})
.await;
Expand Down
46 changes: 40 additions & 6 deletions gui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Run these from this folder (`H:\Projects\Transferd\gui`). Packages are installed

```
python -m venv .venv
.venv\Scripts\python.exe -m pip install PySide6 cryptography blake3 pytest pyflakes
.venv\Scripts\python.exe -m pip install -r requirements.txt
```

## Run
Expand Down Expand Up @@ -43,9 +43,21 @@ The tests run offscreen (`QT_QPA_PLATFORM=offscreen` is set by `tests\conftest.p
(`test_node_loopback.py`);
- the service facade: start, settings saved and refused, failed actions logged and handed back (`test_service.py`);
- every screen, offscreen, in an empty state and in a populated state where a real peer is connected, a file has moved and
an invite has been made (`test_qt_screens.py`).

The relay tests need the built relay. Point `TRANSFERD_RELAYD` at it and they run; without it they are skipped:
an invite has been made (`test_qt_screens.py`);
- the debug protocol without a network: the wire format, the device policy, and the agent and client over in-memory
channels, with real shell commands, a real loopback forward and a real log process (`test_debug_protocol.py`);
- debugging between real nodes on 127.0.0.1: the device answers under its policy, consent for shell commands, forwards,
the device refusing unlisted computers, and the main claim: a second route joins the session and a log stream survives
the USB route being cut, with every line arriving once and in order (`test_debug_node.py`). The adb program is a fake
there, and the test says so;
- the Debug screen and the debugging settings, offscreen, with the real window and service (`test_debug_screen.py`);
- the wormhole (`test_wormhole_*.py`, described in `docs\WORMHOLE.md`): the codes and keys, the mailbox against the real
server, its limits and refusal codes, TLS and pins, the mailbox list, a hosted mailbox, the message checks, the folder archive and its safe unpacking, whole attempts in memory, the overlay transit on
loopback, the Wormhole screen with two real windows, and end-to-end runs with a cut connection, a cut route and the relay.

The relay tests need the built relay (`test_node_loopback.py` and the relay case in `test_wormhole_e2e.py`). Point
`TRANSFERD_RELAYD` at it and they run; without it they are skipped. One wormhole relay test is an expected failure for a
known stall above about 1 MiB (see "Known limits" in `docs\WORMHOLE.md`):

```
set TRANSFERD_RELAYD=H:\Projects\Transferd\target\debug\relayd.exe
Expand All @@ -64,11 +76,25 @@ Navigation is in the left sidebar, in five sections.
registration counts, locate a peer); Peers (sessions, disconnect, accept incoming, dial a peer by address or through the
relay); Invites (make an invite, copy it, check one, connect to its inviter).
- **Transfer**: Transfer (send a file to a ready peer, open a stream and read its reply, accept or refuse incoming
requests, history); Hole punch (locate, with the start button disabled).
requests, history); Wormhole (send a file, a folder or a text with a short code, or type a code to receive it; needs a
mailbox, listed in Settings or hosted on this computer, and is disabled until there is one); Hole punch (locate, with the start button disabled).
- **Tools**: DNS lookup (disabled); Ghost keys (create the key, load the issuer's key, make the request, finish with the
answer, reset).
- **Debug**: Debugging. Choose the device (a connected peer, or paste its node id) and see its state. Connect a route over
Wi-Fi or USB; the routes it has are listed with their bytes. Ask the device for its information; start and stop its log,
with an optional filter; run a shell command with a time limit and see its output and exit code; forward a port to the
device; list every operation running now and stop one. Requests from computers wait here to be accepted or refused.
- **System**: Events (the bounded log, filtered by level, clearable); Settings (every setting, validated; a value that
does not check out is refused with a message and nothing is saved).
does not check out is refused with a message and nothing is saved). Settings has two debugging groups: what listed
computers may do with this device (on/off, the list of computer ids, allow/ask/deny for each operation, forward ports,
the log command, the command limits), and this computer's side (the device to debug, transport, Wi-Fi address, adb
program, USB device port). A Wormhole group sets the mailboxes a wormhole meets on (a list, tried in order), whether this
computer hosts a mailbox and on which port, whether other computers may reach it, and how many data streams a wormhole
file uses (1 to 8).

The debug protocol is specified in `docs\DEBUG_PROTOCOL.md`. It runs over the overlay's services `debug-info`,
`debug-logcat`, `debug-shell` and `debug-forward`, so it needs nothing from the relay or the library beyond sessions and
streams.

Empty states say what is missing and what to do. Telemetry that has not been measured shows "no data yet"; no value is
invented.
Expand Down Expand Up @@ -108,3 +134,11 @@ These features are visible and disabled, each with the reason. The reasons are i
window, so layout, colours and the look of the charts are unchecked.
- The window was not driven by a person. Button clicks in the tests are programmatic.
- The relay tests run only when `TRANSFERD_RELAYD` is set to a built `relayd.exe`.
- Wormhole: a relay transfer above about 1 MiB stalls (a known issue, see `docs\WORMHOLE.md`); the mailbox, its TLS and its hosting were tested only
on 127.0.0.1, with a self-made certificate; the Wormhole screen was driven by its buttons in tests, not by a person, and its file pickers were not
opened.
- Debugging has not been run with a real phone. No phone, cable or Android device was available. The Android agent
(an app that runs the device side on Android) is not built; the device side is this Python app, which runs on any
machine with Transferd. The USB route is tested with a fake adb program and with a second loopback connection that
stands in for `adb forward`. The real `adb forward` and the real cable were not exercised.
- The debug log and shell output were checked with commands on this computer, not on a device's real `logcat`.
Loading
Loading