diff --git a/payjoin-cli/src/app/config.rs b/payjoin-cli/src/app/config.rs index 56f3db1b0..e06d7fbb4 100644 --- a/payjoin-cli/src/app/config.rs +++ b/payjoin-cli/src/app/config.rs @@ -156,7 +156,7 @@ impl Config { { match built_config.get::("v1") { Ok(v1) => { - if v1.pj_endpoint.port().is_none() != (v1.port == 0) { + if v1.port == 0 && v1.pj_endpoint.port().is_some() { return Err(ConfigError::Message( "If --port is 0, --pj-endpoint may not have a port".to_owned(), )); @@ -362,3 +362,50 @@ where .map(Some), } } + +#[cfg(all(test, feature = "v1"))] +mod tests { + use clap::Parser; + + use super::*; + use crate::cli::Cli; + + fn cli(port: &str, endpoint: &str) -> Cli { + Cli::parse_from([ + "payjoin-cli", + "--bip78", + "--port", + port, + "--pj-endpoint", + endpoint, + "receive", + "50000", + ]) + } + + #[test] + fn rejects_random_port_with_explicit_endpoint_port() { + let err = Config::new(&cli("0", "https://example.com:443/")).unwrap_err(); + assert!(err.to_string().contains("port"), "unexpected error: {err}"); + } + + #[test] + fn accepts_random_port_with_implicit_endpoint_port() { + Config::new(&cli("0", "https://example.com/")).unwrap(); + } + + #[test] + fn accepts_explicit_port_with_implicit_endpoint_port() { + Config::new(&cli("3000", "https://example.com/")).unwrap(); + } + + #[test] + fn accepts_explicit_port_with_matching_explicit_endpoint_port() { + Config::new(&cli("3000", "https://example.com:3000/")).unwrap(); + } + + #[test] + fn accepts_explicit_port_with_different_explicit_endpoint_port() { + Config::new(&cli("3000", "https://example.com:443/")).unwrap(); + } +}