diff options
| author | David Lönnhager <david.l@mullvad.net> | 2025-03-04 15:45:40 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2025-03-05 10:19:04 +0100 |
| commit | 89a41936b9a96b383db76c7069a1081ff89bfd88 (patch) | |
| tree | a8d2770579ea42761088dbfdc65217ec8fdcc893 | |
| parent | 8c1abc6d0df834da38c6c2be56f1cc38802c7e83 (diff) | |
| download | mullvadvpn-89a41936b9a96b383db76c7069a1081ff89bfd88.tar.xz mullvadvpn-89a41936b9a96b383db76c7069a1081ff89bfd88.zip | |
Add test for v9 tunnel type migration
Co-authored-by: Markus Pettersson <markus.pettersson@mullvad.net>
| -rw-r--r-- | mullvad-daemon/src/migrations/v9.rs | 334 |
1 files changed, 285 insertions, 49 deletions
diff --git a/mullvad-daemon/src/migrations/v9.rs b/mullvad-daemon/src/migrations/v9.rs index c117905f7f..36ac89ed6d 100644 --- a/mullvad-daemon/src/migrations/v9.rs +++ b/mullvad-daemon/src/migrations/v9.rs @@ -98,30 +98,30 @@ fn migrate_tunnel_type(settings: &mut serde_json::Value) -> Result<()> { }; match normal.get_mut("tunnel_protocol") { // Already migrated - Some(serde_json::Value::String(_s)) => (), - // Migrate - Some(serde_json::Value::Object(ref mut constraint)) => { - if constraint.get("any").is_some() { - // If openvpn is selected, migrate to openvpn tunnel type - // Otherwise, select wireguard - let hostname = normal - .get_mut("location") - .and_then(|location| location.get_mut("only")) - .and_then(|only| only.get_mut("location")) - .and_then(|only| only.get_mut("hostname").cloned()); + Some(serde_json::Value::String(s)) if s == "any" => { + // If openvpn is selected, migrate to openvpn tunnel type + // Otherwise, select wireguard + let hostname = normal + .get_mut("location") + .and_then(|location| location.get_mut("only")) + .and_then(|only| only.get_mut("location")) + .and_then(|only| only.get_mut("hostname").cloned()); - let protocol = if let Some(serde_json::Value::String(s)) = hostname { - if s.split('-').any(|token| token == "ovpn") { - TunnelType::OpenVpn - } else { - TunnelType::Wireguard - } + let protocol = if let Some(serde_json::Value::String(s)) = hostname { + if s.split('-').any(|token| token == "ovpn") { + TunnelType::OpenVpn } else { TunnelType::Wireguard - }; + } + } else { + TunnelType::Wireguard + }; - normal["tunnel_protocol"] = serde_json::json!(protocol); - } else if let Some(tunnel_type) = constraint.get("only") { + normal["tunnel_protocol"] = serde_json::json!(protocol); + } + // Migrate + Some(serde_json::Value::Object(ref mut constraint)) => { + if let Some(tunnel_type) = constraint.get("only") { let tunnel_type: TunnelType = serde_json::from_value(tunnel_type.clone()) .map_err(|_| Error::InvalidSettingsContent)?; normal["tunnel_protocol"] = serde_json::json!(tunnel_type); @@ -258,40 +258,41 @@ mod android { } } -#[cfg(target_os = "android")] #[cfg(test)] mod test { - use crate::migrations::v9::test::constants::V9_SETTINGS; + use super::{migrate, version_matches}; - /// Assert that split-tunneling settings has been added to the android settings post-migration. - #[test] - fn test_v9_to_v10_migration() { - use crate::migrations::v9::{ - add_split_tunneling_settings, - test::constants::{V10_ANDROID_SETTINGS, V9_ANDROID_SETTINGS}, - }; + #[cfg(target_os = "android")] + mod android { + /// Assert that split-tunneling settings has been added to the android settings post-migration. + #[test] + fn test_v9_to_v10_migration() { + use crate::migrations::v9::{ + add_split_tunneling_settings, + test::android::constants::{V10_ANDROID_SETTINGS, V9_ANDROID_SETTINGS}, + }; - let enabled = true; - let apps = ["com.android.chrome", "net.mullvad.mullvadvpn"]; + let enabled = true; + let apps = ["com.android.chrome", "net.mullvad.mullvadvpn"]; - let mut settings = serde_json::from_str(V9_ANDROID_SETTINGS).unwrap(); - // Perform the actual settings migration while skipping the I/O performed in - // `migrate_split_tunnel_settings`. - add_split_tunneling_settings(settings, enabled, apps); - let new_settings = serde_json::from_str(V10_ANDROID_SETTINGS).unwrap(); - assert_eq!(settings, new_settings); - } + let mut settings = serde_json::from_str(V9_ANDROID_SETTINGS).unwrap(); + // Perform the actual settings migration while skipping the I/O performed in + // `migrate_split_tunnel_settings`. + add_split_tunneling_settings(settings, enabled, apps); + let new_settings = serde_json::from_str(V10_ANDROID_SETTINGS).unwrap(); + assert_eq!(settings, new_settings); + } - mod constants { - /// This settings blob does not contain the "split_tunnel" option. - pub const V9_ANDROID_SETTINGS: &str = r#" -{ + mod constants { + /// This settings blob does not contain the "split_tunnel" option. + pub const V9_ANDROID_SETTINGS: &str = r#" + { "relay_settings": { "normal": { "location": { "only": { "location": { - "country": "se" + "hostname": "at-vie-ovpn-001" } } }, @@ -386,12 +387,12 @@ mod test { "relay_overrides": [], "show_beta_releases": true, "settings_version": 9 -} -"#; + } + "#; - /// This settings blob *should* contain the "split_tunnel" option. - pub const V10_ANDROID_SETTINGS: &str = r#" -{ + /// This settings blob *should* contain the "split_tunnel" option. + pub const V10_ANDROID_SETTINGS: &str = r#" + { "relay_settings": { "normal": { "location": { @@ -403,7 +404,7 @@ mod test { }, "providers": "any", "ownership": "any", - "tunnel_protocol": "any", + "tunnel_protocol": "wireguard", "wireguard_constraints": { "port": "any", "ip_version": "any", @@ -496,7 +497,242 @@ mod test { "apps": ["com.android.chrome", "net.mullvad.mullvadvpn"] } "settings_version": 10 + } + "#; + } + } + + /// Assert that tunnel type is migrated + #[test] + fn test_v9_to_v10_migration() { + // TODO: Also test the case where the location is not an openvpn relay and the tunnel type is any + let mut old_settings = serde_json::from_str(V9_SETTINGS).unwrap(); + + assert!(version_matches(&old_settings)); + migrate(&mut old_settings).unwrap(); + let new_settings: serde_json::Value = serde_json::from_str(V10_SETTINGS).unwrap(); + + eprintln!( + "old_settings: {}", + serde_json::to_string_pretty(&old_settings).unwrap() + ); + eprintln!( + "new_settings: {}", + serde_json::to_string_pretty(&new_settings).unwrap() + ); + + assert_eq!(&old_settings, &new_settings); + } + + /// This settings blob contains no constraint for tunnel type + pub const V9_SETTINGS: &str = r#" +{ + "relay_settings": { + "normal": { + "location": { + "only": { + "location": { + "hostname": "at-vie-ovpn-001" + } + } + }, + "providers": "any", + "ownership": "any", + "tunnel_protocol": "any", + "wireguard_constraints": { + "port": "any", + "ip_version": "any", + "use_multihop": false, + "entry_location": { + "only": { + "location": { + "country": "se" + } + } + } + }, + "openvpn_constraints": { + "port": "any" + } + } + }, + "bridge_settings": { + "bridge_type": "normal", + "normal": { + "location": "any", + "providers": "any", + "ownership": "any" + }, + "custom": null + }, + "obfuscation_settings": { + "selected_obfuscation": "auto", + "udp2tcp": { + "port": "any" + } + }, + "bridge_state": "auto", + "custom_lists": { + "custom_lists": [] + }, + "api_access_methods": { + "direct": { + "id": "d81121bf-c942-4ca4-971f-8ea6581bc915", + "name": "Direct", + "enabled": true, + "access_method": { + "built_in": "direct" + } + }, + "mullvad_bridges": { + "id": "92135711-534d-4950-963d-93e446a792e4", + "name": "Mullvad Bridges", + "enabled": true, + "access_method": { + "built_in": "bridge" + } + }, + "custom": [] + }, + "allow_lan": false, + "block_when_disconnected": false, + "auto_connect": false, + "tunnel_options": { + "openvpn": { + "mssfix": null + }, + "wireguard": { + "mtu": null, + "quantum_resistant": "auto", + "rotation_interval": null + }, + "generic": { + "enable_ipv6": false + }, + "dns_options": { + "state": "default", + "default_options": { + "block_ads": false, + "block_trackers": false, + "block_malware": false, + "block_adult_content": false, + "block_gambling": false, + "block_social_media": false + }, + "custom_options": { + "addresses": [] + } + } + }, + "relay_overrides": [], + "show_beta_releases": true, + "settings_version": 9 } "#; + + /// This settings blob does not contain an "any" tunnel type + pub const V10_SETTINGS: &str = r#" +{ + "relay_settings": { + "normal": { + "location": { + "only": { + "location": { + "hostname": "at-vie-ovpn-001" + } + } + }, + "providers": "any", + "ownership": "any", + "tunnel_protocol": "openvpn", + "wireguard_constraints": { + "port": "any", + "ip_version": "any", + "use_multihop": false, + "entry_location": { + "only": { + "location": { + "country": "se" + } + } + } + }, + "openvpn_constraints": { + "port": "any" + } } + }, + "bridge_settings": { + "bridge_type": "normal", + "normal": { + "location": "any", + "providers": "any", + "ownership": "any" + }, + "custom": null + }, + "obfuscation_settings": { + "selected_obfuscation": "auto", + "udp2tcp": { + "port": "any" + } + }, + "bridge_state": "auto", + "custom_lists": { + "custom_lists": [] + }, + "api_access_methods": { + "direct": { + "id": "d81121bf-c942-4ca4-971f-8ea6581bc915", + "name": "Direct", + "enabled": true, + "access_method": { + "built_in": "direct" + } + }, + "mullvad_bridges": { + "id": "92135711-534d-4950-963d-93e446a792e4", + "name": "Mullvad Bridges", + "enabled": true, + "access_method": { + "built_in": "bridge" + } + }, + "custom": [] + }, + "allow_lan": false, + "block_when_disconnected": false, + "auto_connect": false, + "tunnel_options": { + "openvpn": { + "mssfix": null + }, + "wireguard": { + "mtu": null, + "quantum_resistant": "auto", + "rotation_interval": null + }, + "generic": { + "enable_ipv6": false + }, + "dns_options": { + "state": "default", + "default_options": { + "block_ads": false, + "block_trackers": false, + "block_malware": false, + "block_adult_content": false, + "block_gambling": false, + "block_social_media": false + }, + "custom_options": { + "addresses": [] + } + } + }, + "relay_overrides": [], + "show_beta_releases": true, + "settings_version": 9 +} +"#; } |
