nvpn 4.1.7

CLI and daemon for Nostr VPN private mesh networks
Documentation
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#[test]
fn canonical_reconcile_removes_an_identical_reasserted_default() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let initial = runner.state.clone();
    let primary = initial.main_routes[0].clone();
    let runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        None,
        Some(&primary),
    )
    .expect("initial strict exit");
    runner.state.main_routes.push(primary.clone());

    let runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        Some(&runtime),
        Some(&primary),
    )
    .expect("unchanged-fingerprint route reconciliation");

    assert!(
        runner
            .state
            .main_routes
            .iter()
            .all(|route| route.contains("dev nvwg0"))
    );
    cleanup_linux_wireguard_exit_upstream_with(&mut runner, &runtime).expect("cleanup");
    assert_eq!(runner.state, initial);
}

#[test]
fn same_interface_reconnect_uses_the_fresh_default_for_endpoint_replacement() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let primary = runner.state.main_routes[0].clone();
    let runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        None,
        Some(&primary),
    )
    .expect("initial strict exit");
    let reconnected = "default via 192.0.2.254 dev eth0 metric 25".to_string();
    runner.state.main_routes.push(reconnected.clone());

    let runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        Some(&runtime),
        Some(&primary),
    )
    .expect("same-interface gateway refresh");

    assert_eq!(
        runner.state.endpoint_routes["198.51.100.20/32"],
        vec!["198.51.100.20/32 via 192.0.2.254 dev eth0".to_string()],
        "the old managed /32 must not override the exact fresh default captured immediately before replacement"
    );
    assert_eq!(
        runtime.previous_default_route.as_deref(),
        Some(reconnected.as_str())
    );
}

#[test]
fn refreshed_underlay_details_replace_only_the_same_interface() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let primary = runner.state.main_routes[0].clone();
    let secondary = runner.state.main_routes[1].clone();
    let mut runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        None,
        Some(&primary),
    )
    .expect("initial apply");
    let refreshed_primary = "default via 192.0.2.254 dev eth0 src 192.0.2.44 metric 25".to_string();

    runtime.refresh_underlay_default_route(refreshed_primary.clone());

    assert!(!runtime.previous_main_default_routes.contains(&primary));
    assert!(runtime.previous_main_default_routes.contains(&secondary));
    assert!(
        runtime
            .previous_main_default_routes
            .contains(&refreshed_primary)
    );
    assert_eq!(
        runtime
            .underlay_default_route_for_interface("eth0")
            .expect("refreshed primary")
            .line,
        refreshed_primary
    );
}

#[test]
fn new_underlay_is_cached_and_switch_back_survives_reapply() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let initial_defaults = runner.state.main_routes.clone();
    let primary = initial_defaults[0].clone();
    let new_underlay = "default via 10.42.0.1 dev wlan0 src 10.42.0.20 metric 5".to_string();
    let mut runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        None,
        Some(&primary),
    )
    .expect("initial primary apply");

    assert_eq!(
        runtime.underlay_default_route_hints_with(&mut runner),
        std::slice::from_ref(&primary)
    );

    runner.usable_default_interfaces.remove("eth0");
    let hints = runtime
        .underlay_default_route_hints_with(&mut runner)
        .to_vec();
    assert_eq!(hints, initial_defaults);
    let usable_hints = hints
        .into_iter()
        .filter(|route| {
            crate::linux_default_route_spec_from_line(route)
                .is_some_and(|route| runner.ipv4_default_route_is_usable(&route))
        })
        .collect::<Vec<_>>();
    let alternate = lowest_metric_default_route(&usable_hints.join("\n"))
        .map(|(route, _)| route)
        .expect("carrier-up journal route");
    assert_eq!(alternate, initial_defaults[1]);
    runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        Some(&runtime),
        Some(&alternate),
    )
    .expect("fail over to captured alternate");
    assert!(runner.state.endpoint_routes["198.51.100.20/32"][0].contains("dev eth1"));
    runner.usable_default_interfaces.insert("eth0".to_string());

    runner.state.main_routes.push(new_underlay.clone());
    runtime.refresh_underlay_default_route(new_underlay.clone());
    let mut runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        Some(&runtime),
        Some(&new_underlay),
    )
    .expect("refresh onto newly activated underlay");
    assert!(
        runner
            .state
            .main_routes
            .iter()
            .all(|route| route.contains("dev nvwg0")),
        "a newly appearing physical default must be cached then removed under strict exit"
    );

    assert!(
        initial_defaults
            .iter()
            .chain(std::iter::once(&new_underlay))
            .all(|route| runtime.previous_main_default_routes.contains(route)),
        "runtime must retain old defaults for switch-back and add the new cleanup route"
    );

    runtime.refresh_underlay_default_route(primary.clone());
    let mut runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        Some(&runtime),
        Some(&primary),
    )
    .expect("switch back to original underlay");
    assert_eq!(
        runtime.underlay_default_route_hints_with(&mut runner),
        std::slice::from_ref(&primary),
        "only the active primary may feed later route selection"
    );
    for _ in 0..2 {
        let hint = lowest_metric_default_route(
            &runtime
                .underlay_default_route_hints_with(&mut runner)
                .join("\n"),
        )
        .map(|(route, _)| route)
        .expect("active underlay hint");
        runtime = apply_linux_wireguard_exit_upstream_with(
            &mut runner,
            &config(),
            "10.44.0.0/16",
            Some(&runtime),
            Some(&hint),
        )
        .expect("repeated WireGuard/DNS reapply");
    }
    assert_eq!(
        runner.state.endpoint_routes["198.51.100.20/32"],
        vec!["198.51.100.20/32 via 192.0.2.1 dev eth0 src 192.0.2.10".to_string()]
    );
    for route in initial_defaults
        .iter()
        .chain(std::iter::once(&new_underlay))
    {
        assert_eq!(
            runtime
                .previous_main_default_routes
                .iter()
                .filter(|candidate| *candidate == route)
                .count(),
            1,
            "every captured physical default must remain exactly once in the cleanup journal"
        );
    }
}

#[test]
fn failed_reapply_cleanup_preserves_new_and_restores_usable_defaults() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let saved_defaults = runner.state.main_routes.clone();
    let mut runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        None,
        Some("default via 192.0.2.1 dev eth0 src 192.0.2.10 metric 10"),
    )
    .expect("apply");
    runtime.refresh_underlay_default_route(
        "default via 10.42.0.1 dev wlan0 src 10.42.0.20 metric 5".to_string(),
    );
    let fresh_physical = "default via 198.51.100.1 dev eth2 src 198.51.100.42".to_string();
    runner.fail_route_cache_once = true;
    apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        Some(&runtime),
        runtime.previous_default_route.as_deref(),
    )
    .expect_err("failed reapply rolls back before runtime cleanup");
    runner.state.main_routes.insert(0, fresh_physical.clone());
    runner.usable_default_interfaces.remove("wlan0");

    cleanup_linux_wireguard_exit_upstream_with(&mut runner, &runtime)
        .expect("cleanup after underlay handoff");

    assert_eq!(
        runner.state.main_routes,
        std::iter::once(fresh_physical)
            .chain(saved_defaults)
            .collect::<Vec<_>>(),
        "cleanup must delete the exact managed WG default, preserve the live physical route, \
         and restore every captured default that is still usable"
    );
    assert!(
        runner.commands.iter().any(|(_, args)| {
            args == &strings(&["-4", "route", "del", "default", "dev", "nvwg0"])
        })
    );
}

#[test]
fn handoff_cleanup_preserves_new_and_saved_defaults() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let captured = vec![
        "default via 192.0.2.1 dev eth0 src 192.0.2.10 metric 10".to_string(),
        "default dev nvwg0 src 10.77.0.2 metric 30".to_string(),
    ];
    let fresh_physical = "default via 198.51.100.1 dev eth2 src 198.51.100.42 metric 5".to_string();
    runner.state.main_routes = vec![
        fresh_physical.clone(),
        "default dev nvwg0 src 10.77.0.2".to_string(),
    ];

    restore_linux_main_default_snapshot(&mut runner, "nvwg0", &captured)
        .expect("handoff-aware cleanup");

    assert_eq!(
        runner.state.main_routes,
        vec![fresh_physical, captured[0].clone(), captured[1].clone()],
        "fresh physical state and all usable captured defaults must survive cleanup"
    );
}

#[test]
fn cleanup_skips_a_stale_primary_and_restores_the_healthy_secondary() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let captured = runner.state.main_routes.clone();
    runner.state.main_routes = vec!["default dev nvwg0 src 10.77.0.2".to_string()];
    runner.usable_default_interfaces.remove("eth0");

    restore_linux_main_default_snapshot(&mut runner, "nvwg0", &captured)
        .expect("restore healthy fallback");

    assert_eq!(
        runner.state.main_routes,
        vec![captured[1].clone()],
        "a link-down cached primary must not suppress or outrank the usable alternate"
    );
}

#[test]
fn cleanup_continues_restoring_alternates_after_one_route_fails() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let captured = runner.state.main_routes.clone();
    runner.state.main_routes = vec!["default dev nvwg0 src 10.77.0.2".to_string()];
    runner.fail_route_replace_once = Some("dev eth0".to_string());

    let error = restore_linux_main_default_snapshot(&mut runner, "nvwg0", &captured)
        .expect_err("the failed route remains reportable");

    assert!(format!("{error:#}").contains("synthetic route replacement failure"));
    assert_eq!(
        runner.state.main_routes,
        vec![captured[1].clone()],
        "a failed stale/primary restore must not prevent the healthy alternate"
    );
}

#[test]
fn transaction_rollback_restores_exact_defaults_even_if_a_physical_route_survives() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let captured = runner.state.main_routes.clone();
    runner.state.main_routes = vec![
        captured[1].clone(),
        "default dev nvwg0 src 10.77.0.2".to_string(),
    ];

    restore_linux_main_default_snapshot_exact(&mut runner, "nvwg0", &captured)
        .expect("exact transaction rollback");

    assert_eq!(runner.state.main_routes, captured);
    assert!(
        runner.commands.iter().any(|(_, args)| {
            args == &strings(&["-4", "route", "del", "default", "dev", "nvwg0"])
        })
    );
}

#[test]
fn transaction_rollback_preserves_a_new_physical_default() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let captured = runner.state.main_routes.clone();
    let fresh = "default via 198.51.100.1 dev eth2 src 198.51.100.42 metric 5".to_string();
    runner.state.main_routes = vec![fresh.clone(), "default dev nvwg0 src 10.77.0.2".to_string()];

    restore_linux_main_default_snapshot_exact(&mut runner, "nvwg0", &captured)
        .expect("handoff-safe transaction rollback");

    assert!(runner.state.main_routes.contains(&fresh));
    assert!(
        captured
            .iter()
            .all(|route| runner.state.main_routes.contains(route))
    );
}

#[test]
fn apply_replaces_kernel_wireguard_peer_set_and_clears_omitted_fields() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    runner.state.wireguard_config = "\
[Interface]
PrivateKey = old-private

[Peer]
PublicKey = unrelated-peer
AllowedIPs = 10.99.0.0/16

[Peer]
PublicKey = peer-key
PresharedKey = stale-psk
AllowedIPs = 10.88.0.0/16
PersistentKeepalive = 55
"
    .to_string();
    let mut desired = config();
    desired.peer_preshared_key.clear();
    desired.persistent_keepalive_secs = 0;

    apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &desired,
        "10.44.0.0/16",
        None,
        Some("default via 192.0.2.1 dev eth0 src 192.0.2.10 metric 10"),
    )
    .expect("replace WireGuard configuration");

    assert!(
        runner
            .commands
            .iter()
            .any(|(program, args)| program == "wg"
                && args.first().is_some_and(|arg| arg == "setconf")),
        "production apply must use replacement semantics"
    );
    assert!(
        !runner
            .commands
            .iter()
            .any(|(program, args)| program == "wg" && args.first().is_some_and(|arg| arg == "set")),
        "incremental `wg set` leaves unrelated peers and omitted peer fields behind"
    );
    assert!(
        runner
            .state
            .wireguard_config
            .contains("PublicKey = peer-key")
    );
    assert!(!runner.state.wireguard_config.contains("unrelated-peer"));
    assert!(!runner.state.wireguard_config.contains("PresharedKey"));
    assert!(
        !runner
            .state
            .wireguard_config
            .contains("PersistentKeepalive")
    );
}

#[test]
fn wireguard_apply_and_rollback_stream_exact_configs_over_stdin() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    runner.state.wireguard_config = "\
[Interface]
PrivateKey = old-private
ListenPort = 41194

[Peer]
PublicKey = old-peer
PresharedKey = old-preshared
AllowedIPs = 10.99.0.0/16
"
    .to_string();
    let initial_config = runner.state.wireguard_config.clone();
    let desired = config();
    let desired_config = linux_wireguard_kernel_config(
        &desired,
        super::super::resolve_linux_wireguard_exit_endpoint(&desired.endpoint)
            .expect("numeric endpoint"),
    );
    runner.fail_route_cache_once = true;

    apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &desired,
        "10.44.0.0/16",
        None,
        Some("default via 192.0.2.1 dev eth0 src 192.0.2.10 metric 10"),
    )
    .expect_err("final apply failure must stream the exact rollback config");

    let setconf_calls = runner
        .stdin_commands
        .iter()
        .filter(|(program, args, _)| {
            program == "wg" && args == &strings(&["setconf", "nvwg0", "/dev/stdin"])
        })
        .collect::<Vec<_>>();
    assert_eq!(
        setconf_calls.len(),
        2,
        "apply and rollback must each use wg setconf /dev/stdin"
    );
    assert_eq!(
        setconf_calls[0].2,
        desired_config.as_bytes(),
        "apply must stream the desired config without rewriting it"
    );
    assert_eq!(
        setconf_calls[1].2,
        initial_config.as_bytes(),
        "rollback must stream the captured config byte-for-byte"
    );
    assert_eq!(runner.state.wireguard_config, initial_config);
    let setconf_commands = runner
        .commands
        .iter()
        .filter(|(program, args)| {
            program == "wg" && args.first().is_some_and(|arg| arg == "setconf")
        })
        .collect::<Vec<_>>();
    assert_eq!(setconf_commands.len(), 2);
    for (_, args) in setconf_commands {
        assert_eq!(
            args,
            &strings(&["setconf", "nvwg0", "/dev/stdin"]),
            "setconf must never receive a config file path"
        );
    }
    for (_, args) in &runner.commands {
        assert!(
            args.iter().all(|arg| ![
                "private-key",
                "peer-key",
                "old-private",
                "old-peer",
                "old-preshared"
            ]
            .iter()
            .any(|secret| arg.contains(secret))),
            "WireGuard secrets must never appear in command arguments: {args:?}"
        );
    }
}

#[test]
fn system_stdin_runner_reaps_early_failure_and_preserves_stderr() {
    let mut runner = SystemLinuxCommandRunner;
    let stdin = vec![b'x'; 1024 * 1024];
    let output = runner
        .output_with_stdin(
            "sh",
            &strings(&["-c", "printf 'setconf rejected' >&2; exit 23"]),
            &stdin,
        )
        .expect("child failure remains a command result even when stdin closes early");

    assert!(!output.success);
    assert_eq!(output.code, Some(23));
    assert_eq!(output.stderr, "setconf rejected");
}

#[test]
fn absent_policy_table_is_empty_but_other_route_errors_are_fatal() {
    let _guard = lock_tests();
    let missing = LinuxCommandOutput {
        success: false,
        code: Some(2),
        stdout: String::new(),
        stderr: "Error: ipv4: FIB table does not exist.\nDump terminated".to_string(),
    };
    assert!(linux_missing_fib_table_error(&missing));
    assert!(!linux_missing_fib_table_error(&LinuxCommandOutput {
        stderr: "RTNETLINK answers: Operation not permitted".to_string(),
        ..missing
    }));

    let mut runner = FakeRunner::existing();
    runner.state.table_routes.clear();
    runner.state.policy_rule_present = false;
    runner.table_missing_when_empty = true;
    let initial = runner.state.clone();
    let runtime = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        None,
        Some("default via 192.0.2.1 dev eth0 src 192.0.2.10 metric 10"),
    )
    .expect("missing dedicated table is initially empty");
    cleanup_linux_wireguard_exit_upstream_with(&mut runner, &runtime).expect("cleanup");
    assert_eq!(runner.state, initial);

    let mut denied = FakeRunner::existing();
    denied.table_error = Some("RTNETLINK answers: Operation not permitted".to_string());
    let error = apply_linux_wireguard_exit_upstream_with(
        &mut denied,
        &config(),
        "10.44.0.0/16",
        None,
        None,
    )
    .expect_err("non-FIB route error must fail");
    assert!(error.to_string().contains("Operation not permitted"));
}

#[test]
fn capture_failure_after_link_creation_removes_link() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    runner.state.link_exists = false;
    runner.fail_showconf = true;
    let error = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        None,
        None,
    )
    .expect_err("capture failure");
    assert!(error.to_string().contains("Unable to access interface"));
    assert!(!runner.state.link_exists);
}

#[test]
fn new_interface_cleanup_intent_precedes_link_creation() {
    let _guard = lock_tests();
    let journal_ready = Rc::new(Cell::new(false));
    let mut runner = FakeRunner::existing();
    runner.state.link_exists = false;
    runner.fail_showconf = true;
    runner.link_add_journal_ready = Some(Rc::clone(&journal_ready));
    let mut first_obligation = None;

    let failure = apply_linux_wireguard_exit_upstream_with_journal(
        &mut runner,
        &config(),
        LinuxWireGuardExitApplyContext {
            source_cidr: "10.44.0.0/16",
            mesh_iface: "nvpn0",
            previous_runtime: None,
            previous_default_route_hint: None,
        },
        super::super::resolve_linux_wireguard_exit_endpoint,
        |obligation| {
            first_obligation.get_or_insert_with(|| obligation.clone());
            if matches!(
                obligation,
                LinuxWireGuardExitCleanupObligation::CreatedInterface { interface }
                    if interface == "nvwg0"
            ) {
                journal_ready.set(true);
            }
            Ok(())
        },
    )
    .expect_err("capture fails after the journaled interface creation");

    assert!(failure.to_string().contains("Unable to access interface"));
    assert!(
        journal_ready.get(),
        "link creation never received a cleanup intent"
    );
    assert!(matches!(
        first_obligation,
        Some(LinuxWireGuardExitCleanupObligation::CreatedInterface { ref interface })
            if interface == "nvwg0"
    ));
    assert!(!runner.state.link_exists);
}

#[test]
fn capture_failure_retains_new_interface_until_cleanup_can_finish() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    runner.state.link_exists = false;
    runner.fail_showconf = true;
    runner.fail_link_delete = true;

    let failure = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        None,
        None,
    )
    .expect_err("capture and immediate interface cleanup fail");
    let (_, cleanup) = failure.into_parts();
    let mut cleanup = cleanup.expect("new interface cleanup obligation");
    assert!(runner.state.link_exists);

    runner.fail_link_delete = false;
    cleanup_linux_wireguard_exit_obligation_with(&mut runner, &mut cleanup)
        .expect("retained interface cleanup");
    assert!(!runner.state.link_exists);
}

#[test]
fn failed_transaction_retains_exact_rollback_until_retry_succeeds() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    let initial = runner.state.clone();
    runner.fail_route_cache_once = true;
    runner.fail_restore_endpoint = true;

    let failure = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        None,
        Some("default via 192.0.2.1 dev eth0 src 192.0.2.10 metric 10"),
    )
    .expect_err("apply and immediate rollback fail");
    let (_, cleanup) = failure.into_parts();
    let mut cleanup = cleanup.expect("exact rollback obligation");

    runner.fail_restore_endpoint = false;
    cleanup_linux_wireguard_exit_obligation_with(&mut runner, &mut cleanup)
        .expect("retained exact rollback");
    assert_eq!(runner.state, initial);
}

#[test]
fn failed_link_creation_never_deletes_an_uncertain_same_name_interface() {
    let _guard = lock_tests();
    let mut runner = FakeRunner::existing();
    runner.state.link_exists = false;
    runner.fail_after_mutation = Some(1);
    let error = apply_linux_wireguard_exit_upstream_with(
        &mut runner,
        &config(),
        "10.44.0.0/16",
        None,
        None,
    )
    .expect_err("unacknowledged link creation");
    assert!(format!("{error:#}").contains("synthetic failure after mutation 1"));
    assert!(
        runner.state.link_exists,
        "a same-name interface observed after a failed add is not proven owned"
    );
}