diff options
| author | David Lönnhager <david.l@mullvad.net> | 2020-11-20 15:22:43 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2020-11-23 18:38:38 +0100 |
| commit | 211e4a854baaf0e8c3eda2f927f2cb8827f01164 (patch) | |
| tree | 974747ad705f4a7039e19a0c653988912cad77d4 | |
| parent | cba3c06426b1cb13e23ab70173c6f864a9697b8d (diff) | |
| download | mullvadvpn-211e4a854baaf0e8c3eda2f927f2cb8827f01164.tar.xz mullvadvpn-211e4a854baaf0e8c3eda2f927f2cb8827f01164.zip | |
Parse dev versions
| -rw-r--r-- | mullvad-daemon/src/version_check.rs | 12 | ||||
| -rw-r--r-- | mullvad-types/src/version.rs | 70 |
2 files changed, 73 insertions, 9 deletions
diff --git a/mullvad-daemon/src/version_check.rs b/mullvad-daemon/src/version_check.rs index a4900e01b4..d0895671ba 100644 --- a/mullvad-daemon/src/version_check.rs +++ b/mullvad-daemon/src/version_check.rs @@ -20,8 +20,8 @@ use tokio::fs::File; const VERSION_INFO_FILENAME: &str = "version-info.json"; lazy_static::lazy_static! { - static ref APP_VERSION: Option<ParsedAppVersion> = ParsedAppVersion::from_str(PRODUCT_VERSION); - static ref IS_DEV_BUILD: bool = APP_VERSION.is_none(); + static ref APP_VERSION: ParsedAppVersion = ParsedAppVersion::from_str(PRODUCT_VERSION).unwrap(); + static ref IS_DEV_BUILD: bool = APP_VERSION.is_dev(); } const DOWNLOAD_TIMEOUT: Duration = Duration::from_secs(15); @@ -171,13 +171,15 @@ impl VersionUpdater { &mut self, response: mullvad_rpc::AppVersionResponse, ) -> AppVersionInfo { - let suggested_upgrade = APP_VERSION.and_then(|current_version| { + let suggested_upgrade = if !*IS_DEV_BUILD { Self::suggested_upgrade( - ¤t_version, + &*APP_VERSION, &response, self.show_beta_releases || is_beta_version(), ) - }); + } else { + None + }; AppVersionInfo { supported: response.supported, diff --git a/mullvad-types/src/version.rs b/mullvad-types/src/version.rs index b8b8bdcb57..5bbe10618c 100644 --- a/mullvad-types/src/version.rs +++ b/mullvad-types/src/version.rs @@ -1,12 +1,13 @@ #[cfg(target_os = "android")] use jnix::IntoJava; -use serde::{Deserialize, Serialize}; use regex::Regex; +use serde::{Deserialize, Serialize}; use std::cmp::{Ord, Ordering, PartialOrd}; lazy_static::lazy_static! { static ref STABLE_REGEX: Regex = Regex::new(r"^(\d{4})\.(\d+)$").unwrap(); static ref BETA_REGEX: Regex = Regex::new(r"^(\d{4})\.(\d+)-beta(\d+)$").unwrap(); + static ref DEV_REGEX: Regex = Regex::new(r"^(\d{4})\.(\d+)(\.\d+)?(-beta(\d+))?-dev-(\w+)$").unwrap(); } @@ -38,10 +39,11 @@ pub type AppVersion = String; /// Parses a version string into a type that can be used for comparisons. -#[derive(Eq, PartialEq, Debug, Copy, Clone)] +#[derive(Eq, PartialEq, Debug, Clone)] pub enum ParsedAppVersion { Stable(u32, u32), Beta(u32, u32, u32), + Dev(u32, u32, Option<u32>, String), } impl ParsedAppVersion { @@ -57,10 +59,23 @@ impl ParsedAppVersion { let version = get_int(&caps, 2)?; let beta_version = get_int(&caps, 3)?; Some(Self::Beta(year, version, beta_version)) + } else if let Some(caps) = DEV_REGEX.captures(version) { + let year = get_int(&caps, 1)?; + let version = get_int(&caps, 2)?; + let beta_version = caps.get(4).map(|_| get_int(&caps, 5).unwrap()); + let dev_hash = caps.get(6)?.as_str().to_string(); + Some(Self::Dev(year, version, beta_version, dev_hash)) } else { None } } + + pub fn is_dev(&self) -> bool { + match self { + ParsedAppVersion::Dev(..) => true, + _ => false, + } + } } impl Ord for ParsedAppVersion { @@ -75,6 +90,12 @@ impl Ord for ParsedAppVersion { .cmp(other_year) .then(version.cmp(other_version)) .then(Ordering::Greater), + // We assume that a dev version of the same year and version is newer + (Stable(year, version), Dev(other_year, other_version, ..)) => year + .cmp(other_year) + .then(version.cmp(other_version)) + .then(Ordering::Less), + ( Beta(year, version, beta_version), Beta(other_year, other_version, other_beta_version), @@ -86,6 +107,24 @@ impl Ord for ParsedAppVersion { .cmp(other_year) .then(version.cmp(other_version)) .then(Ordering::Less), + // We assume that a dev version of the same year and version is newer + (Beta(year, version, _), Dev(other_year, other_version, ..)) => year + .cmp(other_year) + .then(version.cmp(other_version)) + .then(Ordering::Less), + + // Dev versions of the same year and version are assumed to be equal + (Dev(year, version, ..), Dev(other_year, other_version, ..)) => { + year.cmp(other_year).then(version.cmp(other_version)) + } + (Dev(year, version, ..), Stable(other_year, other_version)) => year + .cmp(other_year) + .then(version.cmp(other_version)) + .then(Ordering::Greater), + (Dev(year, version, ..), Beta(other_year, other_version, _)) => year + .cmp(other_year) + .then(version.cmp(other_version)) + .then(Ordering::Greater), } } } @@ -103,6 +142,13 @@ impl ToString for ParsedAppVersion { Self::Beta(year, version, beta_version) => { format!("{}.{}-beta{}", year, version, beta_version) } + Self::Dev(year, version, beta_version, hash) => { + if let Some(beta_version) = beta_version { + format!("{}.{}-beta{}-dev-{}", year, version, beta_version, hash) + } else { + format!("{}.{}-dev-{}", year, version, hash) + } + } } } } @@ -121,6 +167,10 @@ mod test { assert!(!BETA_REGEX.is_match("2020.5-beta1-dev-f16be4")); assert!(!BETA_REGEX.is_match("2020.5-dev-f16be4")); assert!(!BETA_REGEX.is_match("2020.4")); + assert!(DEV_REGEX.is_match("2020.5-dev-f16be4")); + assert!(DEV_REGEX.is_match("2020.5-beta1-dev-f16be4")); + assert!(!DEV_REGEX.is_match("2020.5")); + assert!(!DEV_REGEX.is_match("2020.5-beta1")); } #[test] @@ -128,8 +178,20 @@ mod test { let tests = vec![ ("2020.4", Some(ParsedAppVersion::Stable(2020, 4))), ("2020.4-beta3", Some(ParsedAppVersion::Beta(2020, 4, 3))), - ("2020.15-beta1-dev-f16be4", None), - ("2020.15-dev-f16be4", None), + ( + "2020.15-beta1-dev-f16be4", + Some(ParsedAppVersion::Dev( + 2020, + 15, + Some(1), + "f16be4".to_string(), + )), + ), + ( + "2020.15-dev-f16be4", + Some(ParsedAppVersion::Dev(2020, 15, None, "f16be4".to_string())), + ), + ("2020.15-9000", None), ("", None), ]; |
