Skip to content
This repository was archived by the owner on Dec 10, 2022. It is now read-only.

Commit 30d2cdf

Browse files
committed
test(cli): add tests for cli args parsing
1 parent b5bafbc commit 30d2cdf

File tree

1 file changed

+257
-4
lines changed

1 file changed

+257
-4
lines changed

src/node_config.rs

Lines changed: 257 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@ pub struct NodeConfig {
147147
pub unused: HashMap<String, Value>,
148148
}
149149

150-
/// Parse command line arguments.
151-
pub fn cli_parse() -> NodeConfig {
152-
let matches = App::new(crate_name!())
150+
fn app() -> App<'static, 'static> {
151+
App::new(crate_name!())
153152
.version(crate_version!())
154153
.author(crate_authors!("\n"))
155154
.about(crate_description!())
@@ -245,7 +244,11 @@ pub fn cli_parse() -> NodeConfig {
245244
.arg(Arg::with_name("lan-discovery")
246245
.long("lan-discovery")
247246
.help("Enable LAN discovery (disabled by default)"))
248-
.get_matches();
247+
}
248+
249+
/// Parse command line arguments.
250+
pub fn cli_parse() -> NodeConfig {
251+
let matches = app().get_matches();
249252

250253
match matches.subcommand() {
251254
("config", Some(m)) => run_config(m),
@@ -355,3 +358,253 @@ fn run_args(matches: &ArgMatches) -> NodeConfig {
355358
unused: HashMap::new(),
356359
}
357360
}
361+
362+
#[cfg(test)]
363+
mod tests {
364+
use super::*;
365+
366+
#[test]
367+
fn args_udp_only() {
368+
let saddr = "127.0.0.1:33445";
369+
let matches = app().get_matches_from(vec![
370+
"tox-node",
371+
"--keys-file",
372+
"./keys",
373+
"--udp-address",
374+
saddr,
375+
]);
376+
let config = run_args(&matches);
377+
assert_eq!(config.keys_file.unwrap(), "./keys");
378+
assert_eq!(config.udp_addr.unwrap(), saddr.parse().unwrap());
379+
assert!(config.tcp_addrs.is_empty());
380+
assert!(!config.lan_discovery_enabled);
381+
}
382+
383+
#[test]
384+
fn args_tcp_only() {
385+
let saddr_1 = "127.0.0.1:33445";
386+
let saddr_2 = "127.0.0.1:33446";
387+
let matches = app().get_matches_from(vec![
388+
"tox-node",
389+
"--keys-file",
390+
"./keys",
391+
"--tcp-address",
392+
saddr_1,
393+
"--tcp-address",
394+
saddr_2,
395+
]);
396+
let config = run_args(&matches);
397+
assert_eq!(config.keys_file.unwrap(), "./keys");
398+
assert!(config.udp_addr.is_none());
399+
assert_eq!(config.tcp_addrs, vec![
400+
saddr_1.parse().unwrap(),
401+
saddr_2.parse().unwrap()
402+
]);
403+
assert!(!config.lan_discovery_enabled);
404+
}
405+
406+
#[test]
407+
fn args_udp_tcp() {
408+
let saddr_1 = "127.0.0.1:33445";
409+
let saddr_2 = "127.0.0.1:33446";
410+
let matches = app().get_matches_from(vec![
411+
"tox-node",
412+
"--keys-file",
413+
"./keys",
414+
"--udp-address",
415+
saddr_1,
416+
"--tcp-address",
417+
saddr_2,
418+
]);
419+
let config = run_args(&matches);
420+
assert_eq!(config.keys_file.unwrap(), "./keys");
421+
assert_eq!(config.udp_addr.unwrap(), saddr_1.parse().unwrap());
422+
assert_eq!(config.tcp_addrs, vec![saddr_2.parse().unwrap()]);
423+
assert!(!config.lan_discovery_enabled);
424+
}
425+
426+
#[test]
427+
fn args_udp_tcp_with_secret_key() {
428+
let saddr_1 = "127.0.0.1:33445";
429+
let saddr_2 = "127.0.0.1:33446";
430+
let sk = "d5ff9ceafe9e1145bc807dc94b4ee911a5878705b5f9ee68f6ccc51e498f313c";
431+
let matches = app().get_matches_from(vec![
432+
"tox-node",
433+
"--secret-key",
434+
sk,
435+
"--udp-address",
436+
saddr_1,
437+
"--tcp-address",
438+
saddr_2,
439+
]);
440+
let config = run_args(&matches);
441+
assert_eq!(config.sk.unwrap(), {
442+
let sk_bytes = <[u8; 32]>::from_hex(sk).unwrap();
443+
SecretKey::from_slice(&sk_bytes).unwrap()
444+
});
445+
assert!(config.sk_passed_as_arg);
446+
assert_eq!(config.udp_addr.unwrap(), saddr_1.parse().unwrap());
447+
assert_eq!(config.tcp_addrs, vec![saddr_2.parse().unwrap()]);
448+
assert!(!config.lan_discovery_enabled);
449+
}
450+
451+
#[test]
452+
fn args_udp_or_tcp_required() {
453+
let matches = app().get_matches_from_safe(vec![
454+
"tox-node",
455+
"--keys-file",
456+
"./keys",
457+
]);
458+
assert!(matches.is_err());
459+
}
460+
461+
#[test]
462+
fn args_keys_file_or_secret_key_required() {
463+
let matches = app().get_matches_from_safe(vec![
464+
"tox-node",
465+
"--udp-address",
466+
"127.0.0.1:33445",
467+
]);
468+
assert!(matches.is_err());
469+
}
470+
471+
#[test]
472+
fn args_keys_file_and_secret_key_conflicts() {
473+
let matches = app().get_matches_from_safe(vec![
474+
"tox-node",
475+
"--keys-file",
476+
"./keys",
477+
"--secret-key",
478+
"d5ff9ceafe9e1145bc807dc94b4ee911a5878705b5f9ee68f6ccc51e498f313c",
479+
"--udp-address",
480+
"127.0.0.1:33445",
481+
]);
482+
assert!(matches.is_err());
483+
}
484+
485+
#[test]
486+
fn args_motd() {
487+
let motd = "abcdef";
488+
let matches = app().get_matches_from(vec![
489+
"tox-node",
490+
"--keys-file",
491+
"./keys",
492+
"--udp-address",
493+
"127.0.0.1:33445",
494+
"--motd",
495+
motd,
496+
]);
497+
let config = run_args(&matches);
498+
assert_eq!(config.motd, motd);
499+
}
500+
501+
#[test]
502+
fn args_lan_discovery() {
503+
let matches = app().get_matches_from(vec![
504+
"tox-node",
505+
"--keys-file",
506+
"./keys",
507+
"--udp-address",
508+
"127.0.0.1:33445",
509+
"--lan-discovery",
510+
]);
511+
let config = run_args(&matches);
512+
assert!(config.lan_discovery_enabled);
513+
}
514+
515+
#[test]
516+
fn args_bootstrap_nodes() {
517+
let pk_1 = "F404ABAA1C99A9D37D61AB54898F56793E1DEF8BD46B1038B9D822E8460FAB67";
518+
let addr_1 = "node.tox.biribiri.org:33445";
519+
let pk_2 = "8E7D0B859922EF569298B4D261A8CCB5FEA14FB91ED412A7603A585A25698832";
520+
let addr_2 = "85.172.30.117:33445";
521+
let matches = app().get_matches_from(vec![
522+
"tox-node",
523+
"--keys-file",
524+
"./keys",
525+
"--udp-address",
526+
"127.0.0.1:33445",
527+
"--bootstrap-node",
528+
pk_1,
529+
addr_1,
530+
"--bootstrap-node",
531+
pk_2,
532+
addr_2,
533+
]);
534+
let config = run_args(&matches);
535+
let node_1 = BootstrapNode {
536+
pk: {
537+
let pk_bytes = <[u8; 32]>::from_hex(pk_1).unwrap();
538+
PublicKey::from_slice(&pk_bytes).unwrap()
539+
},
540+
addr: addr_1.into(),
541+
};
542+
let node_2 = BootstrapNode {
543+
pk: {
544+
let pk_bytes = <[u8; 32]>::from_hex(pk_2).unwrap();
545+
PublicKey::from_slice(&pk_bytes).unwrap()
546+
},
547+
addr: addr_2.into(),
548+
};
549+
assert_eq!(config.bootstrap_nodes, vec![node_1, node_2]);
550+
}
551+
552+
#[test]
553+
fn args_log_type() {
554+
let matches = app().get_matches_from(vec![
555+
"tox-node",
556+
"--keys-file",
557+
"./keys",
558+
"--udp-address",
559+
"127.0.0.1:33445",
560+
"--log-type",
561+
"None"
562+
]);
563+
let config = run_args(&matches);
564+
assert_eq!(config.log_type, LogType::None);
565+
}
566+
567+
#[test]
568+
fn args_tcp_connections_limit() {
569+
let matches = app().get_matches_from(vec![
570+
"tox-node",
571+
"--keys-file",
572+
"./keys",
573+
"--tcp-address",
574+
"127.0.0.1:33445",
575+
"--tcp-connections-limit",
576+
"42"
577+
]);
578+
let config = run_args(&matches);
579+
assert_eq!(config.tcp_connections_limit, 42);
580+
}
581+
582+
#[test]
583+
fn args_tcp_connections_limit_requires_tcp_addr() {
584+
let matches = app().get_matches_from_safe(vec![
585+
"tox-node",
586+
"--keys-file",
587+
"./keys",
588+
"--udp-address",
589+
"127.0.0.1:33445",
590+
"--tcp-connections-limit",
591+
"42"
592+
]);
593+
assert!(matches.is_err());
594+
}
595+
596+
#[test]
597+
fn args_threads() {
598+
let matches = app().get_matches_from(vec![
599+
"tox-node",
600+
"--keys-file",
601+
"./keys",
602+
"--udp-address",
603+
"127.0.0.1:33445",
604+
"--threads",
605+
"42"
606+
]);
607+
let config = run_args(&matches);
608+
assert_eq!(config.threads, Threads::N(42));
609+
}
610+
}

0 commit comments

Comments
 (0)