Skip to content

Commit

Permalink
fixup! Check that the daemon version is correct post-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Nov 4, 2024
1 parent b30555e commit 0fa7a6d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 34 deletions.
42 changes: 42 additions & 0 deletions mullvad-version/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
use regex::Regex;
use std::str::FromStr;

/// The Mullvad VPN app product version
pub const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/product-version.txt"));

const VERSION_REGEX: &str = r"^20([0-9]{2})\.([1-9][0-9]?)(-beta([1-9][0-9]?))?(-dev-[0-9a-f]+)?$";

#[derive(Debug, Clone, PartialEq)]
pub struct Version {
pub year: String,
pub incremental: String,
pub beta: Option<String>,
}

impl Version {
pub fn parse(version: &str) -> Version {
Version::from_str(version).unwrap()
}
}

impl FromStr for Version {
type Err = &'static str;

fn from_str(version: &str) -> Result<Self, Self::Err> {
let re = Regex::new(VERSION_REGEX).unwrap();
let captures = re
.captures(version)
.ok_or("Version does not match expected format")?;
let year = captures.get(1).expect("Missing year").as_str().to_owned();
let incremental = captures
.get(2)
.ok_or("Missing incremental")?
.as_str()
.to_owned();
let beta = captures.get(4).map(|m| m.as_str().to_owned());

Ok(Version {
year,
incremental,
beta,
})
}
}
38 changes: 5 additions & 33 deletions mullvad-version/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use regex::Regex;
use mullvad_version::Version;
use std::{env, process::exit};

const ANDROID_VERSION: &str =
include_str!(concat!(env!("OUT_DIR"), "/android-product-version.txt"));

const VERSION_REGEX: &str = r"^20([0-9]{2})\.([1-9][0-9]?)(-beta([1-9][0-9]?))?(-dev-[0-9a-f]+)?$";

const ANDROID_STABLE_VERSION_CODE_SUFFIX: &str = "99";

fn main() {
let command = env::args().nth(1);
match command.as_deref() {
Expand Down Expand Up @@ -53,7 +49,9 @@ fn to_semver(version: &str) -> String {
/// Version: 2021.34
/// versionCode: 21340099
fn to_android_version_code(version: &str) -> String {
let version = parse_version(version);
const ANDROID_STABLE_VERSION_CODE_SUFFIX: &str = "99";

let version = Version::parse(version);
format!(
"{}{:0>2}00{:0>2}",
version.year,
Expand All @@ -67,7 +65,7 @@ fn to_android_version_code(version: &str) -> String {
fn to_windows_h_format(version: &str) -> String {
let Version {
year, incremental, ..
} = parse_version(version);
} = Version::parse(version);

format!(
"#define MAJOR_VERSION 20{year}
Expand All @@ -76,29 +74,3 @@ fn to_windows_h_format(version: &str) -> String {
#define PRODUCT_VERSION \"{version}\""
)
}

struct Version {
year: String,
incremental: String,
beta: Option<String>,
}

fn parse_version(version: &str) -> Version {
let re = Regex::new(VERSION_REGEX).unwrap();
let captures = re
.captures(version)
.expect("Version does not match expected format");
let year = captures.get(1).expect("Missing year").as_str().to_owned();
let incremental = captures
.get(2)
.expect("Missing incremental")
.as_str()
.to_owned();
let beta = captures.get(4).map(|m| m.as_str().to_owned());

Version {
year,
incremental,
beta,
}
}
8 changes: 8 additions & 0 deletions test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/test-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mullvad-api = { path = "../../mullvad-api", features = ["api-override"] }
mullvad-management-interface = { path = "../../mullvad-management-interface" }
mullvad-relay-selector = { path = "../../mullvad-relay-selector" }
mullvad-types = { path = "../../mullvad-types" }
mullvad-version = { path = "../../mullvad-version" }
talpid-types = { path = "../../talpid-types" }

ssh2 = "0.9.4"
Expand Down
3 changes: 2 additions & 1 deletion test/test-manager/src/tests/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ pub async fn test_upgrade_app(
// TODO: Verify that `app_package_filename` is the expected app version.
let running_daemon_version = rpc.mullvad_daemon_version().await?;
ensure!(
running_daemon_version == TEST_CONFIG.app_package_filename,
mullvad_version::Version::parse(&running_daemon_version)
== mullvad_version::Version::parse(&TEST_CONFIG.app_package_filename),
Error::DaemonVersion {
expected: TEST_CONFIG.app_package_filename.clone(),
actual: running_daemon_version,
Expand Down

0 comments on commit 0fa7a6d

Please sign in to comment.