diff --git a/src/config.rs b/src/config.rs index c6df5e9..713e4ca 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,6 +5,7 @@ use std::fs; use std::path::PathBuf; #[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(default)] pub struct ServerConfig { pub port: u16, pub bind: String, @@ -95,6 +96,7 @@ impl Default for ServerConfig { } #[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(default)] pub struct ClientConfig { pub update_repo: Option, pub update_port: Option, @@ -330,3 +332,63 @@ pub fn expand_tilde(path: &str) -> PathBuf { } PathBuf::from(path) } + +#[cfg(test)] +mod tests { + use super::{ClientConfig, ServerConfig}; + + #[test] + fn old_client_configs_fill_new_fields_from_defaults() { + let raw = r#" +server = "palav" +dosh_port = 50000 +"#; + + let config: ClientConfig = toml::from_str(raw).expect("parse old client config"); + + assert_eq!(config.server, "palav"); + assert!(config.predict); + assert_eq!(config.predict_mode, "experimental"); + assert_eq!(config.auth_preference, "native,ssh"); + assert_eq!(config.native_auth_timeout_ms, 700); + assert_eq!(config.known_hosts, "~/.config/dosh/known_hosts"); + assert_eq!(config.identity_files, vec!["~/.ssh/id_ed25519"]); + assert!(config.use_ssh_agent); + assert!(!config.forward_agent); + assert!(config.disconnect_status); + assert_eq!(config.escape_key, "^]"); + assert!(config.send_env.iter().any(|value| value == "TERM")); + } + + #[test] + fn old_server_configs_fill_new_fields_from_defaults() { + let raw = r#" +port = 50000 +"#; + + let config: ServerConfig = toml::from_str(raw).expect("parse old server config"); + + assert_eq!(config.port, 50000); + assert_eq!(config.bind, "0.0.0.0"); + assert_eq!(config.client_timeout_secs, 2_592_000); + assert_eq!(config.output_frame_interval_ms, 16); + assert!(config.native_auth); + assert_eq!(config.host_key, "~/.config/dosh/host_key"); + assert!( + config + .authorized_keys + .iter() + .any(|path| path == "~/.ssh/authorized_keys") + ); + assert_eq!(config.native_auth_rate_limit_per_minute, 30); + assert_eq!(config.rekey_after_packets, 100_000); + assert_eq!(config.rekey_after_secs, 3600); + assert!(config.allow_tcp_forwarding); + assert!(!config.allow_remote_forwarding); + assert!(!config.allow_agent_forwarding); + assert!(config.allow_file_transfer); + assert!(config.allow_exec_command); + assert!(config.persist_sessions); + assert!(config.accept_env.iter().any(|value| value == "TERM")); + } +}