Skip to content

Commit ea11bec

Browse files
committed
[acquire/tests/test_handle.h] Increase test coverage ; [*.h] Get all tests to pass (at least on macOS)
1 parent 986b1fa commit ea11bec

File tree

9 files changed

+128
-16
lines changed

9 files changed

+128
-16
lines changed

acquire/acquire_checksums.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int acquire_verify_async_start(struct acquire_handle *handle,
9393
if (handle->error.code != ACQUIRE_OK)
9494
return -1;
9595
#endif
96-
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT,
96+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
9797
"Unsupported checksum or no backend");
9898
return -1;
9999
}

acquire/acquire_net_common.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,25 @@ bool is_downloaded(const char *url, enum Checksum checksum, const char *hash,
5454
target_location = get_download_dir();
5555
}
5656

57-
if (!is_directory(target_location)) {
58-
free(filename_from_url);
59-
return false;
60-
}
61-
62-
snprintf(full_local_fname, NAME_MAX + 1, "%s" PATH_SEP "%s", target_location,
63-
filename);
64-
65-
if (!is_file(full_local_fname)) {
66-
free(filename_from_url);
67-
return false;
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)) {
65+
free(filename_from_url);
66+
return false;
67+
}
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)) {
73+
free(filename_from_url);
74+
return false;
75+
}
6876
}
6977

7078
verify_handle = acquire_handle_init();

acquire/acquire_url_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ char *get_path_from_url(const char *url) {
9090
}
9191

9292
bool is_url(const char *maybe_url) {
93-
if (strlen(maybe_url) < 8)
93+
if (maybe_url == NULL || strlen(maybe_url) < 8)
9494
return false;
9595
else if (maybe_url[0] == 'h' && maybe_url[1] == 't' && maybe_url[2] == 't' &&
9696
maybe_url[3] == 'p')

acquire/acquire_wincompressapi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static int extract_all_entries(struct acquire_handle *handle,
4444
/* For brevity, we will treat this as a placeholder. The original logic was
4545
complex and only supported STORE method. A full implementation is beyond
4646
this scope. */
47-
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT,
47+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
4848
"WinCompressAPI backend is a placeholder and does "
4949
"not support extraction.");
5050
return -1;

acquire/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ configure_file(config_for_tests.h.in "${PROJECT_BINARY_DIR}/src/config_for_tests
121121

122122
set(EXEC_NAME ${exec_name})
123123
set(Header_Files
124+
"test_handle.h"
124125
"test_checksum.h"
125126
"test_checksums_dispatch.h"
126127
"test_download.h"

acquire/tests/test.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "test_download.h"
88
#include "test_extract.h"
99
#include "test_fileutils.h"
10+
#include "test_handle.h"
1011
#include "test_net_common.h"
1112
#include "test_string_extras.h"
1213
#include "test_url_utils.h"
@@ -16,10 +17,11 @@ GREATEST_MAIN_DEFS();
1617

1718
int main(int argc, char **argv) {
1819
GREATEST_MAIN_BEGIN();
19-
RUN_SUITE(checksums_suite);
2020
RUN_SUITE(checksum_dispatch_suite);
21+
RUN_SUITE(checksums_suite);
2122
RUN_SUITE(downloads_suite);
2223
RUN_SUITE(fileutils_suite);
24+
RUN_SUITE(handle_suite);
2325
RUN_SUITE(net_common_suite);
2426
RUN_SUITE(string_extras_suite);
2527
RUN_SUITE(url_utils_suite);

acquire/tests/test_handle.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#ifndef TEST_HANDLE_H
2+
#define TEST_HANDLE_H
3+
4+
#include "acquire_handle.h"
5+
#include <greatest.h>
6+
#include <string.h>
7+
8+
TEST test_handle_initialization(void) {
9+
struct acquire_handle *h = acquire_handle_init();
10+
ASSERT(h != NULL);
11+
ASSERT_EQ_FMT(-1L, (long)h->total_size, "%ld");
12+
ASSERT_EQ_FMT(ACQUIRE_IDLE, h->status, "%d");
13+
ASSERT_EQ(ACQUIRE_OK, h->error.code);
14+
ASSERT_EQ_FMT(0, h->cancel_flag, "%d");
15+
ASSERT_EQ(NULL, h->backend_handle);
16+
acquire_handle_free(h);
17+
PASS();
18+
}
19+
20+
TEST test_handle_set_and_get_error(void) {
21+
struct acquire_handle *h = acquire_handle_init();
22+
const char *test_msg = "A test error occurred";
23+
ASSERT(h != NULL);
24+
25+
acquire_handle_set_error(h, ACQUIRE_ERROR_UNKNOWN, "%s", test_msg);
26+
27+
ASSERT_EQ_FMT(ACQUIRE_ERROR, h->status, "%d");
28+
ASSERT_EQ_FMT(ACQUIRE_ERROR_UNKNOWN, acquire_handle_get_error_code(h), "%d");
29+
ASSERT_STR_EQ(test_msg, acquire_handle_get_error_string(h));
30+
31+
acquire_handle_free(h);
32+
PASS();
33+
}
34+
35+
TEST test_handle_null_safety(void) {
36+
/* These functions should not crash when given a NULL handle. */
37+
acquire_handle_free(NULL);
38+
ASSERT_EQ(ACQUIRE_ERROR_INVALID_ARGUMENT,
39+
acquire_handle_get_error_code(NULL));
40+
ASSERT_STR_EQ("Invalid handle provided.",
41+
acquire_handle_get_error_string(NULL));
42+
acquire_handle_set_error(NULL, ACQUIRE_ERROR_UNKNOWN, "test");
43+
/* No crash is a pass. */
44+
PASS();
45+
}
46+
47+
TEST test_handle_set_error_with_formatting(void) {
48+
struct acquire_handle *h = acquire_handle_init();
49+
ASSERT(h != NULL);
50+
51+
acquire_handle_set_error(h, ACQUIRE_ERROR_HTTP_FAILURE, "HTTP status was %d",
52+
404);
53+
54+
ASSERT_EQ_FMT(ACQUIRE_ERROR_HTTP_FAILURE, acquire_handle_get_error_code(h),
55+
"%d");
56+
ASSERT_STR_EQ("HTTP status was 404", acquire_handle_get_error_string(h));
57+
58+
acquire_handle_free(h);
59+
PASS();
60+
}
61+
62+
SUITE(handle_suite) {
63+
RUN_TEST(test_handle_initialization);
64+
RUN_TEST(test_handle_set_and_get_error);
65+
RUN_TEST(test_handle_null_safety);
66+
RUN_TEST(test_handle_set_error_with_formatting);
67+
}
68+
69+
#endif /* !TEST_HANDLE_H */

acquire/tests/test_net_common.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,44 @@ TEST test_is_downloaded_bad_algorithm(void) {
5757
PASS();
5858
}
5959

60+
TEST test_is_downloaded_invalid_args(void) {
61+
/* Invalid URL should result in a NULL filename and early exit */
62+
bool result =
63+
is_downloaded(NULL, LIBACQUIRE_SHA256, GREATEST_SHA256, DOWNLOAD_DIR);
64+
ASSERT_FALSE(result);
65+
66+
/* Invalid hash should cause an early exit */
67+
result = is_downloaded(GREATEST_URL, LIBACQUIRE_SHA256, NULL, DOWNLOAD_DIR);
68+
ASSERT_FALSE(result);
69+
70+
PASS();
71+
}
72+
73+
TEST test_is_downloaded_non_existent_target_dir(void) {
74+
/* Providing a target directory that doesn't exist should fail. */
75+
bool result =
76+
is_downloaded(GREATEST_URL, LIBACQUIRE_SHA256, GREATEST_SHA256, BAD_DIR);
77+
ASSERT_FALSE(result);
78+
PASS();
79+
}
80+
81+
TEST test_is_downloaded_from_local_path(void) {
82+
/* 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);
85+
ASSERT(result);
86+
PASS();
87+
}
88+
6089
SUITE(net_common_suite) {
6190
setup_net_common_suite(NULL); /* Manually call setup. */
6291
RUN_TEST(test_is_downloaded_success);
6392
RUN_TEST(test_is_downloaded_file_missing);
6493
RUN_TEST(test_is_downloaded_bad_hash);
6594
RUN_TEST(test_is_downloaded_bad_algorithm);
95+
RUN_TEST(test_is_downloaded_invalid_args);
96+
RUN_TEST(test_is_downloaded_non_existent_target_dir);
97+
RUN_TEST(test_is_downloaded_from_local_path);
6698
}
6799

68100
#endif /* !TEST_NET_COMMON_H */

reports/test_coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)