luciola-cli 0.15.0

Command-line interface and diagnostic radar for the Luciola P2P discovery engine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Process-level coverage for `luciola-cli transfer`.
//!
//! The unit tests in `src/transfer.rs` drive `run_send` / `run_recv` directly.
//! Those cannot catch a broken command wiring: a wrong clap subcommand, a pin
//! swapped between the two roles, a listener that never binds, or a receiver
//! that forgets to finalize. This suite spawns the real binary twice over a
//! loopback TCP socket, exactly as two machines would run it.
//!
//! Port 0 is used on the receiving side and the assigned port is read back
//! from the CLI's listening line — no fixed port, so parallel CI jobs and a
//! leftover listener on a dev box cannot make the suite flaky.

use std::io::{BufRead, BufReader};
use std::process::{Child, Command, Output, Stdio};
use std::sync::mpsc;
use std::thread;
use std::time::{Duration, Instant};

fn hex(bytes: &[u8]) -> String {
    let mut s = String::with_capacity(bytes.len() * 2);
    for b in bytes {
        s.push_str(&format!("{b:02x}"));
    }
    s
}

fn identity_hex(seed: &[u8; 32]) -> (String, String) {
    let id = luciola::trust::identity_from_seed(seed);
    (hex(seed), hex(&id.public_key))
}

/// Wait for a child without letting a stuck peer hang the whole suite.
fn wait_with_timeout(mut child: Child, timeout: Duration) -> Output {
    let deadline = Instant::now() + timeout;
    loop {
        if child.try_wait().expect("try_wait").is_some() {
            return child.wait_with_output().expect("wait_with_output");
        }
        if Instant::now() >= deadline {
            let _ = child.kill();
            let _ = child.wait();
            panic!("transfer process did not exit within {timeout:?}");
        }
        thread::sleep(Duration::from_millis(50));
    }
}

/// Drain the receiver's stdout for its whole lifetime.
///
/// The listening line is needed immediately to learn the ephemeral port, but
/// the read end must stay open until the process exits: Rust's `println!`
/// panics on EPIPE, so closing stdout after the first line would kill the
/// receiver mid-transfer and the sender would report a misleading
/// "Broken pipe". The first line is delivered on its own channel; the rest
/// accumulates and is returned at EOF for diagnostics.
struct StdoutDrain {
    first_line: mpsc::Receiver<String>,
    full: mpsc::Receiver<String>,
}

fn drain_stdout(stdout: std::process::ChildStdout) -> StdoutDrain {
    let (first_tx, first_line) = mpsc::channel();
    let (full_tx, full) = mpsc::channel();
    thread::spawn(move || {
        let mut reader = BufReader::new(stdout);
        let mut all = String::new();
        let mut sent_first = false;
        loop {
            let mut line = String::new();
            match reader.read_line(&mut line) {
                Ok(0) | Err(_) => break,
                Ok(_) => {
                    if !sent_first {
                        let _ = first_tx.send(line.clone());
                        sent_first = true;
                    }
                    all.push_str(&line);
                }
            }
        }
        if !sent_first {
            let _ = first_tx.send(String::new());
        }
        let _ = full_tx.send(all);
    });
    StdoutDrain { first_line, full }
}

fn listening_line(drain: &StdoutDrain) -> String {
    drain
        .first_line
        .recv_timeout(Duration::from_secs(15))
        .expect("receiver never printed its listening line")
}

fn listening_port(line: &str) -> u16 {
    let addr = line
        .split("Listening on ")
        .nth(1)
        .unwrap_or_else(|| panic!("unexpected listening line: {line:?}"))
        .split_whitespace()
        .next()
        .expect("missing socket address");
    addr.rsplit(':')
        .next()
        .expect("missing port")
        .parse()
        .unwrap_or_else(|e| panic!("cannot parse port from {addr:?}: {e}"))
}

fn temp_dir(name: &str) -> std::path::PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "luciola-cli-{name}-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis()
    ));
    std::fs::create_dir_all(&dir).expect("create temp dir");
    dir
}

#[test]
fn send_recv_over_two_cli_processes() {
    let bin = env!("CARGO_BIN_EXE_luciola-cli");
    let dir = temp_dir("e2e");
    let src = dir.join("payload.bin");
    let out = dir.join("out");
    std::fs::create_dir_all(&out).expect("create out dir");

    let payload: Vec<u8> = (0..256 * 1024u32).map(|i| (i % 251) as u8).collect();
    std::fs::write(&src, &payload).expect("write payload");

    let (seed_a, pk_a) = identity_hex(&[0x11; 32]);
    let (seed_b, pk_b) = identity_hex(&[0x22; 32]);

    let mut recv = Command::new(bin)
        .args([
            "transfer",
            "recv",
            "--port",
            "0",
            "--seed",
            &seed_b,
            "--accept-pin",
            &pk_a,
            "--out",
            out.to_str().unwrap(),
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn receiver");
    let drain = drain_stdout(recv.stdout.take().expect("receiver stdout"));
    let port = listening_port(&listening_line(&drain));

    let send = Command::new(bin)
        .args([
            "transfer",
            "send",
            "--file",
            src.to_str().unwrap(),
            "--connect",
            &format!("127.0.0.1:{port}"),
            "--seed",
            &seed_a,
            "--pin-peer",
            &pk_b,
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn sender");

    let send_out = wait_with_timeout(send, Duration::from_secs(30));
    let recv_out = wait_with_timeout(recv, Duration::from_secs(45));
    let recv_stdout = drain.full.recv().unwrap_or_default();
    let diagnostics = format!(
        "--- sender stdout ---\n{}\n--- sender stderr ---\n{}\n--- receiver stdout ---\n{recv_stdout}\n--- receiver stderr ---\n{}",
        String::from_utf8_lossy(&send_out.stdout),
        String::from_utf8_lossy(&send_out.stderr),
        String::from_utf8_lossy(&recv_out.stderr),
    );
    assert!(send_out.status.success(), "sender failed:\n{diagnostics}");
    assert!(recv_out.status.success(), "receiver failed:\n{diagnostics}");

    let landed = out.join("payload.bin");
    assert!(
        landed.exists(),
        "file must land at the basename inside --out"
    );
    assert_eq!(
        std::fs::read(&landed).expect("read received file"),
        payload,
        "received bytes must match exactly"
    );
    assert!(
        recv_stdout.contains("SHA-256"),
        "receiver must report a verified checksum; got: {recv_stdout}"
    );
    assert!(
        recv_stdout.contains("262144 bytes"),
        "receiver must report the verified byte count; got: {recv_stdout}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Deny-by-default: a sender pinned to the wrong peer key must be rejected,
/// and the receiver must not write a file. Guards against a regression where
/// `--accept-pin` was accidentally treated as advisory.
#[test]
fn wrong_pin_is_rejected_and_no_file_is_written() {
    let bin = env!("CARGO_BIN_EXE_luciola-cli");
    let dir = temp_dir("wrong-pin");
    let src = dir.join("secret.bin");
    let out = dir.join("out");
    std::fs::create_dir_all(&out).expect("create out dir");
    std::fs::write(&src, b"do not land").expect("write payload");

    let (seed_a, pk_a) = identity_hex(&[0x33; 32]);
    let (seed_b, _pk_b) = identity_hex(&[0x44; 32]);
    let (_seed_c, wrong_pin) = identity_hex(&[0x55; 32]);

    let mut recv = Command::new(bin)
        .args([
            "transfer",
            "recv",
            "--port",
            "0",
            "--seed",
            &seed_b,
            "--accept-pin",
            &pk_a,
            "--out",
            out.to_str().unwrap(),
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn receiver");
    let drain = drain_stdout(recv.stdout.take().expect("receiver stdout"));
    let port = listening_port(&listening_line(&drain));

    let send = Command::new(bin)
        .args([
            "transfer",
            "send",
            "--file",
            src.to_str().unwrap(),
            "--connect",
            &format!("127.0.0.1:{port}"),
            "--seed",
            &seed_a,
            "--pin-peer",
            &wrong_pin,
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn sender");

    let send_out = wait_with_timeout(send, Duration::from_secs(30));
    let recv_out = wait_with_timeout(recv, Duration::from_secs(30));
    assert!(
        !send_out.status.success(),
        "a sender pinned to the wrong key must fail"
    );
    assert!(
        !recv_out.status.success(),
        "receiver must reject the mismatched peer"
    );
    assert!(
        std::fs::read_dir(&out)
            .expect("read out dir")
            .next()
            .is_none(),
        "no file may be written for a rejected handshake"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `transfer pin` is the documented way to derive the value peers paste into
/// `--pin-peer` / `--accept-pin`; keep its output tied to the real identity.
#[test]
fn pin_command_prints_the_identity_public_key() {
    let (seed, expected_pk) = identity_hex(&[0x66; 32]);
    let out = Command::new(env!("CARGO_BIN_EXE_luciola-cli"))
        .args(["transfer", "pin", "--seed", &seed])
        .output()
        .expect("run transfer pin");
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains(&format!("public_key={expected_pk}")),
        "unexpected pin output: {stdout}"
    );
}

/// Verify process-level checkpoint resume: when receiver starts with existing
/// `.luciola-part` and `.luciola-meta`, it announces its resume offset, the
/// sender skips already written bytes, and the final file is verified byte-exact.
#[test]
fn transfer_resumes_from_existing_checkpoint() {
    use luciola_transfer::checkpoint::{meta_path_for, part_path_for, TransferMeta};

    let bin = env!("CARGO_BIN_EXE_luciola-cli");
    let dir = temp_dir("resume-cli");
    let src = dir.join("data.bin");
    let out = dir.join("out");
    std::fs::create_dir_all(&out).expect("create out dir");

    let total = 256 * 1024;
    let payload: Vec<u8> = (0..total).map(|i| (i % 251) as u8).collect();
    std::fs::write(&src, &payload).expect("write src payload");

    let dest = out.join("data.bin");
    let part_file = part_path_for(&dest);
    let meta_file = meta_path_for(&dest);

    // Pre-create checkpoint: first 128 KiB already on disk
    let ms = std::fs::metadata(&src)
        .and_then(|m| m.modified())
        .ok()
        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
        .map(|d| d.as_millis())
        .unwrap_or(0);
    let transfer_id = format!("data.bin-{total}-{ms}");
    let mut meta = TransferMeta::new(&transfer_id, total as u64, 64 * 1024);
    meta.mark_chunk(0);
    meta.mark_chunk(1);
    assert_eq!(meta.contiguous_bytes(), 128 * 1024);

    std::fs::write(&meta_file, meta.encode()).expect("write meta file");
    // Write pre-existing 128 KiB into part file
    let mut part_bytes = vec![0u8; total];
    part_bytes[0..128 * 1024].copy_from_slice(&payload[0..128 * 1024]);
    std::fs::write(&part_file, &part_bytes).expect("write part file");

    let (seed_a, pk_a) = identity_hex(&[0x71; 32]);
    let (seed_b, pk_b) = identity_hex(&[0x72; 32]);

    let mut recv = Command::new(bin)
        .args([
            "transfer",
            "recv",
            "--port",
            "0",
            "--seed",
            &seed_b,
            "--accept-pin",
            &pk_a,
            "--out",
            out.to_str().unwrap(),
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn receiver");
    let drain = drain_stdout(recv.stdout.take().expect("receiver stdout"));
    let port = listening_port(&listening_line(&drain));

    let send = Command::new(bin)
        .args([
            "transfer",
            "send",
            "--file",
            src.to_str().unwrap(),
            "--connect",
            &format!("127.0.0.1:{port}"),
            "--seed",
            &seed_a,
            "--pin-peer",
            &pk_b,
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn sender");

    let send_out = wait_with_timeout(send, Duration::from_secs(30));
    let recv_out = wait_with_timeout(recv, Duration::from_secs(30));

    assert!(
        send_out.status.success(),
        "sender failed: {}",
        String::from_utf8_lossy(&send_out.stderr)
    );
    assert!(
        recv_out.status.success(),
        "receiver failed: {}",
        String::from_utf8_lossy(&recv_out.stderr)
    );

    let recv_stderr = String::from_utf8_lossy(&recv_out.stderr);
    let send_stderr = String::from_utf8_lossy(&send_out.stderr);

    // Verify resume logs
    assert!(
        recv_stderr.contains("Resuming transfer from checkpoint"),
        "receiver stderr: {recv_stderr}"
    );
    assert!(
        send_stderr.contains("Remote peer requested resume"),
        "sender stderr: {send_stderr}"
    );

    // Destination file exists and matches original payload byte-for-byte
    assert!(dest.exists(), "destination file must exist");
    assert!(!part_file.exists(), "part file must be removed");
    assert!(!meta_file.exists(), "meta file must be removed");

    let received = std::fs::read(&dest).expect("read dest");
    assert_eq!(received, payload);

    std::fs::remove_dir_all(&dir).ok();
}