nvpn 4.1.3

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
use clap::{CommandFactory, Parser, error::ErrorKind};

#[cfg(feature = "paid-exit")]
use crate::PaidExitCommand;
use crate::{Cli, Command, PubsubCommand};

#[test]
fn explicit_config_path_environment_value_is_preserved() {
    assert_eq!(
        crate::config_bootstrap::configured_config_path(Some("/var/lib/nvpn/config.toml".into())),
        Some("/var/lib/nvpn/config.toml".into())
    );
    assert_eq!(
        crate::config_bootstrap::configured_config_path(Some("".into())),
        None
    );
}

#[test]
fn clap_binary_name_is_nvpn() {
    let command = Cli::command();
    assert_eq!(command.get_name(), "nvpn");
}

#[test]
fn clap_supports_root_version_flag() {
    let error = Cli::command()
        .try_get_matches_from(["nvpn", "--version"])
        .expect_err("--version should display version and exit");
    assert_eq!(error.kind(), ErrorKind::DisplayVersion);
    assert!(
        error
            .to_string()
            .contains(&format!("nvpn {}", env!("CARGO_PKG_VERSION"))),
        "version output should include binary name and package version"
    );
}

#[test]
fn clap_parses_join_request_wait_controls() {
    let cli = Cli::parse_from([
        "nvpn",
        "join-request",
        "--config",
        "/var/lib/nvpn/config.toml",
        "--no-wait",
        "--no-qr",
        "--reset",
    ]);
    let Command::JoinRequest(args) = cli.command else {
        panic!("expected join-request command");
    };
    assert_eq!(
        args.config.expect("config").to_string_lossy(),
        "/var/lib/nvpn/config.toml"
    );
    assert!(args.no_wait);
    assert!(args.no_qr);
    assert!(args.reset);
}

#[test]
fn daemon_parses_paired_fips_ethernet_options() {
    let cli = Cli::parse_from([
        "nvpn",
        "daemon",
        "--fips-ethernet-interface",
        "eth0",
        "--fips-ethernet-discovery-scope",
        "local-pairing",
    ]);
    let Command::Daemon(args) = cli.command else {
        panic!("expected daemon command");
    };
    assert_eq!(args.fips_ethernet_interface.as_deref(), Some("eth0"));
    assert_eq!(
        args.fips_ethernet_discovery_scope.as_deref(),
        Some("local-pairing")
    );
}

#[test]
fn daemon_rejects_unpaired_fips_ethernet_options() {
    let error = Cli::try_parse_from(["nvpn", "daemon", "--fips-ethernet-interface", "eth0"])
        .expect_err("Ethernet interface must require its discovery scope");
    assert_eq!(error.kind(), ErrorKind::MissingRequiredArgument);
}

#[test]
fn daemon_accepts_multiple_valid_fips_websocket_seeds() {
    let cli = Cli::parse_from([
        "nvpn",
        "daemon",
        "--fips-websocket-seed-url",
        "wss://seed-a.example/fips",
        "--fips-websocket-seed-url",
        "wss://seed-b.example/fips",
    ]);
    let Command::Daemon(args) = cli.command else {
        panic!("expected daemon command");
    };
    assert_eq!(
        args.fips_websocket_seed_urls,
        ["wss://seed-a.example/fips", "wss://seed-b.example/fips"]
    );
}

#[test]
fn daemon_rejects_invalid_or_remote_plaintext_fips_websocket_seeds() {
    for value in [
        "bootstrap garbage",
        "https://seed.example/fips",
        "ws://seed.example/fips",
    ] {
        let error = Cli::try_parse_from(["nvpn", "daemon", "--fips-websocket-seed-url", value])
            .expect_err("invalid FIPS WebSocket seed must be rejected");
        assert_eq!(error.kind(), ErrorKind::ValueValidation);
    }
}

#[test]
fn set_parses_explicit_lan_discovery_disable() {
    let cli = Cli::parse_from([
        "nvpn",
        "set",
        "--lan-discovery-enabled=false",
        "--config",
        "/var/lib/nvpn/config.toml",
    ]);
    let Command::Set(args) = cli.command else {
        panic!("expected set command");
    };
    assert_eq!(args.lan_discovery_enabled, Some(false));
}

#[test]
fn build_reports_fips_core_component_version() {
    let version = crate::fips_core_build_version();
    assert!(!version.trim().is_empty());
    assert!(version.starts_with(fips_core::version::VERSION));
}

#[test]
fn clap_includes_tailscale_style_commands() {
    let command = Cli::command();
    for name in [
        "start",
        "stop",
        "repair-network",
        "reload",
        "pause",
        "resume",
        "connect",
        "join-request",
        "status",
        "set",
        "ping",
        "doctor",
        "ip",
        "whois",
        "pubsub",
        "install-cli",
        "uninstall-cli",
        "service",
        "version",
    ] {
        assert!(
            command
                .get_subcommands()
                .any(|subcommand| subcommand.get_name() == name),
            "missing subcommand {name}"
        );
    }
}

#[test]
fn clap_parses_control_pubsub_publish_event_file() {
    let cli = Cli::parse_from([
        "nvpn",
        "pubsub",
        "publish",
        "--event",
        "/tmp/control-event.json",
        "--config",
        "/tmp/nvpn.toml",
        "--json",
    ]);
    let Command::Pubsub(args) = cli.command else {
        panic!("expected pubsub command");
    };
    let PubsubCommand::Publish(args) = args.command;
    assert_eq!(args.event.to_string_lossy(), "/tmp/control-event.json");
    assert_eq!(
        args.config.expect("config").to_string_lossy(),
        "/tmp/nvpn.toml"
    );
    assert!(args.json);
}

#[tokio::test]
async fn control_pubsub_publish_command_queues_a_signed_event() {
    use nostr_sdk::prelude::{EventBuilder, Keys, Kind};

    let nonce = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("clock")
        .as_nanos();
    let directory = std::env::temp_dir().join(format!("nvpn-pubsub-cli-{nonce}"));
    let config_path = directory.join("config.toml");
    let event_path = directory.join("event.json");
    let app = nostr_vpn_core::config::AppConfig::generated();
    app.save(&config_path).expect("save config");
    let event = EventBuilder::new(Kind::Custom(37_196), "paid exit offer")
        .sign_with_keys(&Keys::generate())
        .expect("signed event");
    std::fs::write(&event_path, serde_json::to_vec(&event).expect("event JSON"))
        .expect("write event");

    let cli = Cli::parse_from([
        "nvpn",
        "pubsub",
        "publish",
        "--event",
        event_path.to_str().expect("event path"),
        "--config",
        config_path.to_str().expect("config path"),
        "--json",
    ]);
    crate::run_command(cli.command)
        .await
        .expect("queue event through CLI dispatch");

    assert!(
        crate::control_pubsub_runtime::control_pubsub_outbox_directory(&config_path)
            .join(format!("{}.json", event.id.to_hex()))
            .exists()
    );
    let _ = std::fs::remove_dir_all(directory);
}

#[cfg(feature = "paid-exit")]
#[test]
fn clap_includes_paid_exit_command_when_enabled() {
    let command = Cli::command();
    assert!(
        command
            .get_subcommands()
            .any(|subcommand| subcommand.get_name() == "paid-exit"),
        "missing paid-exit subcommand"
    );
}

#[cfg(feature = "paid-exit")]
#[test]
fn clap_paid_exit_buy_supports_best_rated_selector() {
    let cli = Cli::parse_from(["nvpn", "paid-exit", "buy", "--best-rated"]);
    let Command::PaidExit(args) = cli.command else {
        panic!("expected paid-exit command");
    };
    let PaidExitCommand::Buy(args) = args.command else {
        panic!("expected buy command");
    };
    assert!(args.best_rated);
    assert_eq!(args.offer, None);
}

#[cfg(not(feature = "paid-exit"))]
#[test]
fn clap_omits_paid_exit_command_by_default() {
    let command = Cli::command();
    assert!(
        !command
            .get_subcommands()
            .any(|subcommand| subcommand.get_name() == "paid-exit"),
        "paid-exit subcommand should require the paid-exit feature"
    );
}

#[test]
fn clap_roster_device_commands_keep_participant_aliases() {
    Cli::command()
        .try_get_matches_from(["nvpn", "add-device", "--device", "npub1example"])
        .expect("new device command parses");
    Cli::command()
        .try_get_matches_from(["nvpn", "add-participant", "--participant", "npub1example"])
        .expect("legacy participant command parses");
}

#[test]
fn clap_set_supports_autoconnect_flag() {
    let command = Cli::command();
    let set = command
        .get_subcommands()
        .find(|subcommand| subcommand.get_name() == "set")
        .expect("set subcommand exists");
    assert!(
        set.get_arguments()
            .any(|argument| argument.get_long() == Some("autoconnect")),
        "missing --autoconnect on set command"
    );
}

#[test]
fn clap_set_supports_join_request_listener_flag() {
    let command = Cli::command();
    let set = command
        .get_subcommands()
        .find(|subcommand| subcommand.get_name() == "set")
        .expect("set subcommand exists");
    assert!(
        set.get_arguments()
            .any(|argument| argument.get_long() == Some("join-requests-enabled")),
        "missing --join-requests-enabled on set command"
    );
}

#[test]
fn clap_set_supports_route_advertisement_flags() {
    let command = Cli::command();
    let set = command
        .get_subcommands()
        .find(|subcommand| subcommand.get_name() == "set")
        .expect("set subcommand exists");
    assert!(
        set.get_arguments()
            .any(|argument| argument.get_long() == Some("advertise-routes")),
        "missing --advertise-routes on set command"
    );
    assert!(
        set.get_arguments()
            .any(|argument| argument.get_long() == Some("advertise-exit-node")),
        "missing --advertise-exit-node on set command"
    );
    assert!(
        set.get_arguments()
            .any(|argument| argument.get_long() == Some("exit-node")),
        "missing --exit-node on set command"
    );
    assert!(
        set.get_arguments()
            .any(|argument| argument.get_long() == Some("exit-node-leak-protection")),
        "missing --exit-node-leak-protection on set command"
    );
}

#[cfg(feature = "paid-exit")]
#[test]
fn clap_set_supports_paid_exit_seller_flags() {
    let command = Cli::command();
    let set = command
        .get_subcommands()
        .find(|subcommand| subcommand.get_name() == "set")
        .expect("set subcommand exists");
    for flag in [
        "paid-exit-enabled",
        "paid-exit-upstream",
        "paid-exit-price-msat",
        "paid-exit-per-units",
        "paid-exit-accepted-mints",
        "paid-exit-country-code",
        "paid-exit-region",
        "paid-exit-asn",
        "paid-exit-network-class",
        "paid-exit-ipv4",
        "paid-exit-ipv6",
        "paid-exit-max-channel-capacity-sat",
        "paid-exit-channel-expiry-secs",
        "paid-exit-free-probe-units",
        "paid-exit-grace-units",
    ] {
        assert!(
            set.get_arguments()
                .any(|argument| argument.get_long() == Some(flag)),
            "missing --{flag} on set command"
        );
    }
    assert!(
        !set.get_arguments()
            .any(|argument| argument.get_long() == Some("paid-exit-meter")),
        "byte billing must not expose a legacy meter selector"
    );
}

#[test]
fn clap_set_supports_wireguard_exit_flags() {
    let command = Cli::command();
    let set = command
        .get_subcommands()
        .find(|subcommand| subcommand.get_name() == "set")
        .expect("set subcommand exists");
    for flag in [
        "wireguard-exit-enabled",
        "wireguard-exit-address",
        "wireguard-exit-private-key",
        "wireguard-exit-peer-public-key",
        "wireguard-exit-endpoint",
        "wireguard-exit-allowed-ips",
        "wireguard-exit-config",
        "wireguard-exit-config-file",
    ] {
        assert!(
            set.get_arguments()
                .any(|argument| argument.get_long() == Some(flag)),
            "missing --{flag} on set command"
        );
    }
}