diff --git a/Makefile.am b/Makefile.am index d3f37426..c4646cce 100644 --- a/Makefile.am +++ b/Makefile.am @@ -162,6 +162,7 @@ BATS = \ test/functional/update/update-missing-os-core.bats \ test/functional/update/update-newest-deleted.bats \ test/functional/update/update-newest-ghosted.bats \ + test/functional/update/update-older-server-version.bats \ test/functional/update/update-rename.bats \ test/functional/update/update-rename-ghosted.bats \ test/functional/update/update-re-update-bad-os-release.bats \ diff --git a/src/check_update.c b/src/check_update.c index 933a5c1f..4c4c87d2 100644 --- a/src/check_update.c +++ b/src/check_update.c @@ -132,6 +132,7 @@ err: static int check_update() { int current_version, server_version; + int ret; check_root(); if (!init_globals()) { @@ -139,16 +140,9 @@ static int check_update() } swupd_curl_init(); - read_versions(¤t_version, &server_version, path_prefix); - - if (server_version < 0) { - fprintf(stderr, "Error: server does not report any version\n"); - return ENOSWUPDSERVER; - } - - if (current_version < 0) { - fprintf(stderr, "Unable to determine current OS version\n"); - return ECURRENT_VERSION; + ret = read_versions(¤t_version, &server_version, path_prefix); + if (ret != 0) { + return ret; } else { fprintf(stderr, "Current OS version: %d\n", current_version); if (current_version < server_version) { diff --git a/src/swupd.h b/src/swupd.h index f9a6f3a3..01573f02 100644 --- a/src/swupd.h +++ b/src/swupd.h @@ -207,7 +207,7 @@ extern int main_verify(int current_version); extern int walk_tree(struct manifest *, const char *, bool, const regex_t *); extern int get_latest_version(char *v_url); -extern void read_versions(int *current_version, int *server_version, char *path_prefix); +extern int read_versions(int *current_version, int *server_version, char *path_prefix); extern int check_versions(int *current_version, int *server_version, int requested_version, char *path_prefix); extern int get_current_version(char *path_prefix); diff --git a/src/update.c b/src/update.c index 8ed9a16f..6de0733c 100644 --- a/src/update.c +++ b/src/update.c @@ -771,24 +771,19 @@ static int print_versions() (void)init_globals(); swupd_curl_init(); - read_versions(¤t_version, &server_version, path_prefix); - - if (current_version < 0) { - fprintf(stderr, "Cannot determine current OS version\n"); + if (read_versions(¤t_version, &server_version, path_prefix) != 0) { ret = 2; } else { + if (server_version <= current_version) { + ret = 1; + } + } + if (current_version > 0) { fprintf(stderr, "Current OS version: %d\n", current_version); } - - if (server_version < 0) { - fprintf(stderr, "Cannot get the latest server version. Could not reach server\n"); - ret = 2; - } else { + if (server_version > 0) { fprintf(stderr, "Latest server version: %d\n", server_version); } - if ((ret == 0) && (server_version <= current_version)) { - ret = 1; - } telemetry(ret ? TELEMETRY_WARN : TELEMETRY_INFO, "check", diff --git a/src/version.c b/src/version.c index f0721b1e..df37857a 100644 --- a/src/version.c +++ b/src/version.c @@ -127,31 +127,32 @@ int get_current_version(char *path_prefix) return v; } -void read_versions(int *current_version, - int *server_version, - char *path_prefix) +int read_versions(int *current_version, int *server_version, char *path_prefix) { *current_version = get_current_version(path_prefix); - *server_version = get_latest_version(NULL); -} - -int check_versions(int *current_version, - int *server_version, - int requested_version, - char *path_prefix) -{ - read_versions(current_version, server_version, path_prefix); if (*current_version < 0) { fprintf(stderr, "Error: Unable to determine current OS version\n"); + return ECURRENT_VERSION; + } + if (*server_version < 0) { + fprintf(stderr, "Error: Unable to determine the server version\n"); + return ENOSWUPDSERVER; + } + + return 0; +} + +int check_versions(int *current_version, int *server_version, int requested_version, char *path_prefix) +{ + if (read_versions(current_version, server_version, path_prefix) != 0) { return -1; } if (*current_version == 0) { fprintf(stderr, "Update from version 0 not supported yet.\n"); return -1; } - if (requested_version != -1) { if (requested_version <= *current_version) { fprintf(stderr, "Requested version for update (%d) must be greater than current version (%d)\n", diff --git a/test/functional/checkupdate/chk-update-no-server-content.bats b/test/functional/checkupdate/chk-update-no-server-content.bats index 7526bc68..2fd2a1c6 100755 --- a/test/functional/checkupdate/chk-update-no-server-content.bats +++ b/test/functional/checkupdate/chk-update-no-server-content.bats @@ -14,6 +14,6 @@ test_setup() { run sudo sh -c "$SWUPD check-update $SWUPD_OPTS_NO_CERT" assert_status_is_not 0 - assert_is_output "Error: server does not report any version" + assert_is_output "Error: Unable to determine the server version" } diff --git a/test/functional/checkupdate/chk-update-no-target-content.bats b/test/functional/checkupdate/chk-update-no-target-content.bats index ecd7cb79..71c28738 100755 --- a/test/functional/checkupdate/chk-update-no-target-content.bats +++ b/test/functional/checkupdate/chk-update-no-target-content.bats @@ -14,6 +14,6 @@ test_setup() { run sudo sh -c "$SWUPD check-update $SWUPD_OPTS_NO_CERT" assert_status_is_not 0 - assert_is_output "Unable to determine current OS version" + assert_is_output "Error: Unable to determine current OS version" } diff --git a/test/functional/update/update-older-server-version.bats b/test/functional/update/update-older-server-version.bats new file mode 100755 index 00000000..aedb36ad --- /dev/null +++ b/test/functional/update/update-older-server-version.bats @@ -0,0 +1,59 @@ +#!/usr/bin/env bats + +load "../testlib" + +test_setup() { + + create_test_environment "$TEST_NAME" 10 + +} + +@test "update with a server older than the target system" { + + set_current_version "$TEST_NAME" 20 + + run sudo sh -c "$SWUPD update $SWUPD_OPTS" + + assert_status_is 0 + expected_output=$(cat <<-EOM + Update started. + Version on server (10) is not newer than system version (20) + Update complete. System already up-to-date at version 20 + EOM + ) + assert_is_output "$expected_output" + +} + +@test "update with no server version file available" { + + sudo rm -rf "$WEBDIR"/version + + run sudo sh -c "$SWUPD update $SWUPD_OPTS" + + assert_status_is 1 + expected_output=$(cat <<-EOM + Update started. + Error: Unable to determine the server version + EOM + ) + assert_is_output "$expected_output" + +} + +@test "update with no target version file available" { + + sudo rm -rf "$TARGETDIR"/usr/lib/os-release + + run sudo sh -c "$SWUPD update $SWUPD_OPTS" + + assert_status_is 1 + expected_output=$(cat <<-EOM + Update started. + Error: Unable to determine current OS version + Unable to determine current OS version + EOM + ) + assert_is_output "$expected_output" + +} diff --git a/test/functional/update/update-status-no-server-content.bats b/test/functional/update/update-status-no-server-content.bats index 8a950241..52032792 100755 --- a/test/functional/update/update-status-no-server-content.bats +++ b/test/functional/update/update-status-no-server-content.bats @@ -14,8 +14,8 @@ test_setup() { run sudo sh -c "$SWUPD update $SWUPD_OPTS --status" assert_status_is 2 expected_output=$(cat <<-EOM + Error: Unable to determine the server version Current OS version: 10 - Cannot get the latest server version. Could not reach server EOM ) assert_is_output "$expected_output" diff --git a/test/functional/update/update-status-no-target-content.bats b/test/functional/update/update-status-no-target-content.bats index 6803c1b1..32359c23 100755 --- a/test/functional/update/update-status-no-target-content.bats +++ b/test/functional/update/update-status-no-target-content.bats @@ -15,7 +15,7 @@ test_setup() { run sudo sh -c "$SWUPD update $SWUPD_OPTS --status" assert_status_is 2 expected_output=$(cat <<-EOM - Cannot determine current OS version + Error: Unable to determine current OS version Latest server version: 100 EOM )