Skip to content

Commit a81dea3

Browse files
committed
[acquire/tests/test_*.h] Increase test coverage ; [*.h] Modify code fixing issues test failures found ; [codecov_badge_gen.sh] Rename and write good shell code ; [reports/test_coverage.svg] Regenerate on latest ctest run
1 parent ea11bec commit a81dea3

12 files changed

+151
-57
lines changed

acquire/acquire_crc32c.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ int _crc32c_verify_async_start(struct acquire_handle *handle,
9898
struct checksum_backend *be;
9999
if (algorithm != LIBACQUIRE_CRC32C)
100100
return -1;
101+
if (strlen(expected_hash) != 8) {
102+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
103+
"Invalid hash length for CRC32C");
104+
return -1;
105+
}
101106
if (!handle || !filepath || !expected_hash) {
102107
if (handle)
103108
acquire_handle_set_error(handle, ACQUIRE_ERROR_INVALID_ARGUMENT,

acquire/acquire_libcurl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ static int progress_callback(void *clientp, curl_off_t dltotal,
9898
/* --- API Implementation --- */
9999
int acquire_download_sync(struct acquire_handle *handle, const char *url,
100100
const char *dest_path) {
101-
if (!handle)
101+
if (!handle || !url || !dest_path) {
102+
if (handle)
103+
acquire_handle_set_error(handle, ACQUIRE_ERROR_INVALID_ARGUMENT, NULL);
102104
return -1;
105+
}
103106
if (acquire_download_async_start(handle, url, dest_path) != 0)
104107
return -1;
105108
while (acquire_download_async_poll(handle) == ACQUIRE_IN_PROGRESS)

acquire/acquire_librhash.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,28 @@ int _librhash_verify_async_start(struct acquire_handle *handle,
6565
const char *expected_hash) {
6666
struct rhash_backend *be;
6767
unsigned int rhash_algo_id = 0;
68+
size_t expected_len = 0;
6869
switch (algorithm) {
6970
case LIBACQUIRE_CRC32C:
7071
rhash_algo_id = RHASH_CRC32C;
72+
expected_len = 8;
7173
break;
7274
case LIBACQUIRE_SHA256:
7375
rhash_algo_id = RHASH_SHA256;
76+
expected_len = 64;
7477
break;
7578
case LIBACQUIRE_SHA512:
7679
rhash_algo_id = RHASH_SHA512;
80+
expected_len = 128;
7781
break;
7882
default:
7983
return -1;
8084
}
85+
if (strlen(expected_hash) != expected_len) {
86+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
87+
"Invalid hash length for selected algorithm");
88+
return -1;
89+
}
8190
if (!rhash_lib_initialized) {
8291
rhash_library_init();
8392
rhash_lib_initialized = 1;

acquire/acquire_net_common.h

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,65 +29,53 @@ extern LIBACQUIRE_EXPORT const char *get_download_dir(void);
2929

3030
#include <string.h>
3131

32-
bool is_downloaded(const char *url, enum Checksum checksum, const char *hash,
33-
const char *target_location) {
34-
char full_local_fname[NAME_MAX + 1];
32+
bool is_downloaded(const char *url_or_path, enum Checksum checksum,
33+
const char *hash, const char *target_location) {
34+
35+
char path_buffer[NAME_MAX + 1];
36+
const char *file_to_check;
3537
char *filename_from_url = NULL;
36-
const char *filename;
3738
struct acquire_handle *verify_handle;
3839
int result;
39-
bool is_verified = false;
4040

41-
if (is_url(url)) {
42-
filename_from_url = get_path_from_url(url);
43-
filename = filename_from_url;
44-
} else {
45-
filename = url;
46-
}
47-
48-
if (filename == NULL || *filename == '\0' || hash == NULL) {
49-
free(filename_from_url);
41+
if (url_or_path == NULL || hash == NULL) {
5042
return false;
5143
}
5244

53-
if (target_location == NULL) {
54-
target_location = get_download_dir();
55-
}
56-
57-
if (is_file(filename)) {
58-
size_t len = strlen(filename);
59-
if (len > NAME_MAX)
60-
len = NAME_MAX - 1;
61-
memcpy(full_local_fname, filename, len);
62-
full_local_fname[len] = '\0';
63-
} else {
64-
if (!is_directory(target_location) && !is_file(target_location)) {
45+
if (is_url(url_or_path)) {
46+
filename_from_url = get_path_from_url(url_or_path);
47+
if (filename_from_url == NULL || *filename_from_url == '\0') {
6548
free(filename_from_url);
66-
return false;
49+
return false; /* Cannot determine filename from URL. */
6750
}
68-
69-
snprintf(full_local_fname, NAME_MAX + 1, "%s" PATH_SEP "%s",
70-
target_location, filename);
71-
72-
if (!is_file(full_local_fname)) {
51+
if (target_location == NULL) {
52+
target_location = get_download_dir();
53+
}
54+
if (!is_directory(target_location)) {
7355
free(filename_from_url);
74-
return false;
56+
return false; /* Download directory is invalid. */
7557
}
58+
snprintf(path_buffer, sizeof(path_buffer), "%s%s%s", target_location,
59+
PATH_SEP, filename_from_url);
60+
file_to_check = path_buffer;
61+
free(filename_from_url);
62+
} else {
63+
file_to_check = url_or_path;
64+
}
65+
66+
if (!is_file(file_to_check)) {
67+
return false;
7668
}
7769

7870
verify_handle = acquire_handle_init();
7971
if (!verify_handle) {
80-
free(filename_from_url);
8172
return false;
8273
}
8374

84-
result = acquire_verify_sync(verify_handle, full_local_fname, checksum, hash);
75+
result = acquire_verify_sync(verify_handle, file_to_check, checksum, hash);
8576
acquire_handle_free(verify_handle);
8677

87-
is_verified = (result == 0);
88-
89-
free(filename_from_url);
90-
return is_verified;
78+
return result == 0;
9179
}
9280

9381
#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && \

acquire/acquire_openssl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ int _openssl_verify_async_start(struct acquire_handle *handle,
8888
return -1;
8989
}
9090
if (strlen(expected_hash) != expected_len) {
91+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
92+
"Invalid hash length for selected algorithm");
9193
return -1;
9294
}
9395
be = (struct openssl_backend *)calloc(1, sizeof(struct openssl_backend));

acquire/acquire_wincrypt.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,24 @@ int _wincrypt_verify_async_start(struct acquire_handle *handle,
6262
const char *expected_hash) {
6363
struct wincrypt_backend *be;
6464
ALG_ID alg_id = 0;
65+
size_t expected_len = 0;
6566
switch (algorithm) {
6667
case LIBACQUIRE_SHA256:
6768
alg_id = CALG_SHA_256;
69+
expected_len = 64;
6870
break;
6971
case LIBACQUIRE_SHA512:
7072
alg_id = CALG_SHA_512;
73+
expected_len = 128;
7174
break;
7275
default:
7376
return -1;
7477
}
78+
if (strlen(expected_hash) != expected_len) {
79+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
80+
"Invalid hash length for selected algorithm");
81+
return -1;
82+
}
7583
be = (struct wincrypt_backend *)calloc(1, sizeof(struct wincrypt_backend));
7684
if (!be) {
7785
acquire_handle_set_error(handle, ACQUIRE_ERROR_OUT_OF_MEMORY, "wincrypt");

acquire/tests/test_checksum.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ TEST test_verify_sync_failure_bad_file(void) {
6060
struct acquire_handle *h = acquire_handle_init();
6161
int result;
6262
ASSERT(h != NULL);
63-
result =
64-
acquire_verify_sync(h, "nonexistent.file", LIBACQUIRE_SHA256, "hash");
63+
result = acquire_verify_sync(h, "nonexistent.file", LIBACQUIRE_SHA256,
64+
GREATEST_SHA256);
6565
ASSERT_EQ_FMT(-1, result, "%d");
6666
ASSERT_EQ_FMT(ACQUIRE_ERROR, h->status, "%d");
6767
ASSERT_EQ_FMT(ACQUIRE_ERROR_FILE_OPEN_FAILED,
@@ -139,7 +139,30 @@ TEST test_unsupported_algorithm(void) {
139139
PASS();
140140
}
141141

142+
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) || \
143+
defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL)
144+
TEST test_invalid_hash_length(void) {
145+
struct acquire_handle *h = acquire_handle_init();
146+
int result;
147+
ASSERT(h != NULL);
148+
/* SHA256 should be 64 chars, provide a shorter one */
149+
result =
150+
acquire_verify_sync(h, GREATEST_FILE, LIBACQUIRE_SHA256, "short_hash");
151+
ASSERT_EQ_FMT(-1, result, "%d");
152+
ASSERT_EQ(ACQUIRE_ERROR, h->status);
153+
ASSERT_EQ(ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
154+
acquire_handle_get_error_code(h));
155+
acquire_handle_free(h);
156+
PASS();
157+
}
158+
#endif
159+
142160
SUITE(checksums_suite) {
161+
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) || \
162+
defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL)
163+
RUN_TEST(test_invalid_hash_length);
164+
#endif
165+
RUN_TEST(test_unsupported_algorithm);
143166
RUN_TEST(test_verify_sync_success_sha256);
144167
#if defined(LIBACQUIRE_USE_LIBRHASH) || defined(LIBACQUIRE_USE_CRC32C)
145168
RUN_TEST(test_verify_sync_success_crc32c);
@@ -149,7 +172,6 @@ SUITE(checksums_suite) {
149172
RUN_TEST(test_verify_async_success);
150173
RUN_TEST(test_verify_async_cancellation);
151174
RUN_TEST(test_verify_empty_file);
152-
RUN_TEST(test_unsupported_algorithm);
153175
}
154176

155177
#endif /* !TEST_CHECKSUM_H */

acquire/tests/test_download.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,32 @@ TEST test_download_to_invalid_path(void) {
124124
PASS();
125125
}
126126

127+
TEST test_sync_download_invalid_args(void) {
128+
struct acquire_handle *h = acquire_handle_init();
129+
int result;
130+
131+
result = acquire_download_sync(NULL, GREATEST_URL, "file.tmp");
132+
ASSERT_EQ(-1, result);
133+
134+
result = acquire_download_sync(h, NULL, "file.tmp");
135+
ASSERT_EQ(-1, result);
136+
ASSERT_EQ(ACQUIRE_ERROR_INVALID_ARGUMENT, acquire_handle_get_error_code(h));
137+
138+
result = acquire_download_sync(h, GREATEST_URL, NULL);
139+
ASSERT_EQ(-1, result);
140+
ASSERT_EQ(ACQUIRE_ERROR_INVALID_ARGUMENT, acquire_handle_get_error_code(h));
141+
142+
acquire_handle_free(h);
143+
PASS();
144+
}
145+
127146
SUITE(downloads_suite) {
128-
RUN_TEST(test_sync_download);
129-
RUN_TEST(test_async_download);
130147
RUN_TEST(test_async_cancellation);
148+
RUN_TEST(test_async_download);
131149
RUN_TEST(test_download_404_error);
132150
RUN_TEST(test_download_to_invalid_path);
151+
RUN_TEST(test_sync_download);
152+
RUN_TEST(test_sync_download_invalid_args);
133153
}
134154

135155
#endif /* !TEST_DOWNLOAD_H */

acquire/tests/test_handle.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,26 @@ TEST test_handle_set_error_with_formatting(void) {
5959
PASS();
6060
}
6161

62+
TEST test_handle_set_error_no_fmt(void) {
63+
struct acquire_handle *h = acquire_handle_init();
64+
ASSERT(h != NULL);
65+
66+
acquire_handle_set_error(h, ACQUIRE_ERROR_UNKNOWN, NULL);
67+
68+
ASSERT_EQ_FMT(ACQUIRE_ERROR, h->status, "%d");
69+
ASSERT_EQ_FMT(ACQUIRE_ERROR_UNKNOWN, acquire_handle_get_error_code(h), "%d");
70+
ASSERT_STR_EQ("", acquire_handle_get_error_string(h));
71+
72+
acquire_handle_free(h);
73+
PASS();
74+
}
75+
6276
SUITE(handle_suite) {
6377
RUN_TEST(test_handle_initialization);
6478
RUN_TEST(test_handle_set_and_get_error);
6579
RUN_TEST(test_handle_null_safety);
6680
RUN_TEST(test_handle_set_error_with_formatting);
81+
RUN_TEST(test_handle_set_error_no_fmt);
6782
}
6883

6984
#endif /* !TEST_HANDLE_H */

acquire/tests/test_net_common.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ static void setup_net_common_suite(void *arg) {
2222
TEST test_is_downloaded_success(void) {
2323
/* This test uses the URL to determine the filename "greatest.h", then
2424
checks for it in DOWNLOAD_DIR. */
25-
bool result = is_downloaded(GREATEST_URL, LIBACQUIRE_SHA256, GREATEST_SHA256,
26-
DOWNLOAD_DIR);
25+
const bool result = is_downloaded(GREATEST_URL, LIBACQUIRE_SHA256,
26+
GREATEST_SHA256, DOWNLOAD_DIR);
2727
ASSERT(result);
2828
PASS();
2929
}
@@ -72,29 +72,33 @@ TEST test_is_downloaded_invalid_args(void) {
7272

7373
TEST test_is_downloaded_non_existent_target_dir(void) {
7474
/* Providing a target directory that doesn't exist should fail. */
75-
bool result =
75+
const bool result =
7676
is_downloaded(GREATEST_URL, LIBACQUIRE_SHA256, GREATEST_SHA256, BAD_DIR);
7777
ASSERT_FALSE(result);
7878
PASS();
7979
}
8080

8181
TEST test_is_downloaded_from_local_path(void) {
8282
/* Test using a local path directly instead of a URL. */
83-
const bool result = is_downloaded(NET_COMMON_TEST_FILE, LIBACQUIRE_SHA256,
84-
GREATEST_SHA256, NULL);
83+
const bool result =
84+
is_downloaded(NET_COMMON_TEST_FILE, LIBACQUIRE_SHA256, GREATEST_SHA256,
85+
NULL); /* target_location is ignored for local paths */
8586
ASSERT(result);
8687
PASS();
8788
}
8889

8990
SUITE(net_common_suite) {
9091
setup_net_common_suite(NULL); /* Manually call setup. */
91-
RUN_TEST(test_is_downloaded_success);
92-
RUN_TEST(test_is_downloaded_file_missing);
93-
RUN_TEST(test_is_downloaded_bad_hash);
9492
RUN_TEST(test_is_downloaded_bad_algorithm);
93+
RUN_TEST(test_is_downloaded_bad_hash);
94+
RUN_TEST(test_is_downloaded_file_missing);
95+
RUN_TEST(test_is_downloaded_from_local_path);
96+
RUN_TEST(test_is_downloaded_from_local_path);
97+
RUN_TEST(test_is_downloaded_invalid_args);
9598
RUN_TEST(test_is_downloaded_invalid_args);
9699
RUN_TEST(test_is_downloaded_non_existent_target_dir);
97-
RUN_TEST(test_is_downloaded_from_local_path);
100+
RUN_TEST(test_is_downloaded_non_existent_target_dir);
101+
RUN_TEST(test_is_downloaded_success);
98102
}
99103

100104
#endif /* !TEST_NET_COMMON_H */

0 commit comments

Comments
 (0)