mirror of
https://github.com/clearlinux/swupd-client.git
synced 2026-09-06 21:51:32 +00:00
Verify versions can be read
read_versions is a function that attempts to read the current version of the target system and the server version, but if there is an error reading any of those values no action was performed so it was necessary to validate these values wherever the function was being used, duplicating code. This commit adds a verification to make sure the current version of the target system and the server version was able to be identified. Closes #589 Signed-off-by: Castulo Martinez <castulo.martinez@intel.com>
This commit is contained in:
committed by
Otavio Pontes
parent
f4e09e08cd
commit
55ebbfa2f2
@@ -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 \
|
||||
|
||||
+4
-10
@@ -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) {
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
|
||||
+7
-12
@@ -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",
|
||||
|
||||
+14
-13
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
}
|
||||
|
||||
+59
@@ -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"
|
||||
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user