Skip to content

Commit 5c9d884

Browse files
committed
[acquire/tests/test_*.h] Increase test coverage ; [acquire_*.h] Modify code fixing issues test failures found ; [reports/test_coverage.svg] Regenerate on latest ctest run
1 parent 5a5bb45 commit 5c9d884

File tree

7 files changed

+153
-14
lines changed

7 files changed

+153
-14
lines changed

acquire/acquire_miniz.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,17 @@ int acquire_extract_async_start(struct acquire_handle *handle,
4444
}
4545

4646
handle->status = ACQUIRE_IN_PROGRESS;
47+
if (handle->cancel_flag) {
48+
acquire_handle_set_error(handle, ACQUIRE_ERROR_CANCELLED,
49+
"Extraction cancelled");
50+
return -1;
51+
}
4752
result = zip_extract(archive_path, dest_path, on_extract_entry, handle);
4853

4954
if (result == 0) {
5055
handle->status = ACQUIRE_COMPLETE;
5156
} else {
52-
if (handle->cancel_flag) {
57+
if (result == -1 && handle->cancel_flag) {
5358
acquire_handle_set_error(handle, ACQUIRE_ERROR_CANCELLED,
5459
"Extraction cancelled");
5560
} else {

acquire/acquire_wincompressapi.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,7 @@ typedef struct _ZIP_LOCAL_FILE_HEADER {
3636
static int extract_all_entries(struct acquire_handle *handle,
3737
const char *archive_path,
3838
const char *dest_path) {
39-
/* (This internal function contains the original synchronous logic) */
40-
/* ... complex synchronous logic from your original file ... */
41-
/* On error: acquire_handle_set_error(handle, ...); return -1; */
42-
/* On success: return 0; */
43-
44-
/* For brevity, we will treat this as a placeholder. The original logic was
45-
complex and only supported STORE method. A full implementation is beyond
46-
this scope. */
47-
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
39+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT,
4840
"WinCompressAPI backend is a placeholder and does "
4941
"not support extraction.");
5042
return -1;

acquire/tests/test_extract.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,46 @@ TEST test_extract_corrupted_archive(void) {
7474
PASS();
7575
}
7676

77+
/* --- Backend specific tests --- */
78+
79+
#ifdef LIBACQUIRE_USE_WINCOMPRESSAPI
80+
TEST test_extract_wincompressapi_placeholder(void) {
81+
struct acquire_handle *handle = acquire_handle_init();
82+
int result;
83+
ASSERT(handle != NULL);
84+
85+
result = acquire_extract_sync(handle, GREATEST_ARCHIVE, EXTRACT_DIR);
86+
87+
ASSERT_EQ_FMT(-1, result, "%d");
88+
ASSERT_EQ_FMT(ACQUIRE_ERROR, handle->status, "%d");
89+
ASSERT_EQ_FMT(ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT,
90+
acquire_handle_get_error_code(handle), "%d");
91+
acquire_handle_free(handle);
92+
PASS();
93+
}
94+
#endif /* LIBACQUIRE_USE_WINCOMPRESSAPI */
95+
96+
#ifdef LIBACQUIRE_USE_MINIZ
97+
TEST test_extract_miniz_cancellation(void) {
98+
struct acquire_handle *handle = acquire_handle_init();
99+
int result;
100+
ASSERT(handle != NULL);
101+
102+
/* The miniz backend is synchronous, but cancellation is checked
103+
via a callback. We can trigger it before starting. */
104+
acquire_extract_async_cancel(handle); /* Sets the flag */
105+
106+
result = acquire_extract_sync(handle, GREATEST_ARCHIVE, EXTRACT_DIR);
107+
108+
ASSERT_EQ(-1, result);
109+
ASSERT_EQ(ACQUIRE_ERROR, handle->status);
110+
ASSERT_EQ(ACQUIRE_ERROR_CANCELLED, acquire_handle_get_error_code(handle));
111+
112+
acquire_handle_free(handle);
113+
PASS();
114+
}
115+
#endif /* LIBACQUIRE_USE_MINIZ */
116+
77117
#ifdef LIBACQUIRE_USE_LIBARCHIVE
78118
TEST test_extract_async_success(void) {
79119
struct acquire_handle *handle = acquire_handle_init();
@@ -149,6 +189,13 @@ SUITE(extract_suite) {
149189
RUN_TEST(test_extract_non_existent_archive);
150190
RUN_TEST(test_extract_corrupted_archive);
151191

192+
#ifdef LIBACQUIRE_USE_WINCOMPRESSAPI
193+
RUN_TEST(test_extract_wincompressapi_placeholder);
194+
#endif /* LIBACQUIRE_USE_WINCOMPRESSAPI */
195+
#ifdef LIBACQUIRE_USE_MINIZ
196+
RUN_TEST(test_extract_miniz_cancellation);
197+
#endif /* LIBACQUIRE_USE_MINIZ */
198+
152199
#ifdef LIBACQUIRE_USE_LIBARCHIVE
153200
RUN_TEST(test_extract_async_success);
154201
RUN_TEST(test_extract_async_cancellation);

acquire/tests/test_libfetch.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,91 @@ TEST test_libfetch_base64_encoder(void) {
5555
PASS();
5656
}
5757

58+
/* === FILE.C TESTS === */
59+
TEST test_file_c_get(void) {
60+
struct url *u = fetchParseURL("file://" GREATEST_FILE);
61+
struct url_stat st;
62+
FILE *f;
63+
ASSERT(u);
64+
f = fetchXGetFile(u, &st, "");
65+
ASSERT(f);
66+
ASSERT(st.size > 0);
67+
fclose(f);
68+
fetchFreeURL(u);
69+
PASS();
70+
}
71+
72+
TEST test_file_c_put(void) {
73+
char test_path[PATH_MAX];
74+
struct url *u;
75+
FILE *f;
76+
snprintf(test_path, sizeof(test_path), "%s%sput_test.txt", DOWNLOAD_DIR,
77+
PATH_SEP);
78+
79+
u = fetchMakeURL("file", NULL, 0, test_path, NULL, NULL);
80+
ASSERT(u);
81+
f = fetchPutFile(u, "");
82+
ASSERT(f);
83+
fprintf(f, "hello");
84+
fclose(f);
85+
ASSERT(is_file(test_path));
86+
remove(test_path);
87+
fetchFreeURL(u);
88+
PASS();
89+
}
90+
91+
SUITE(libfetch_file_suite) {
92+
RUN_TEST(test_file_c_get);
93+
RUN_TEST(test_file_c_put);
94+
}
95+
96+
/* === COMMON.C TESTS === */
97+
#if !defined(_WIN32)
98+
TEST test_common_no_proxy_match(void) {
99+
setenv("no_proxy", "*.example.com, .google.com, other.org", 1);
100+
ASSERT(fetch_no_proxy_match("host.example.com"));
101+
ASSERT(fetch_no_proxy_match("sub.host.example.com"));
102+
ASSERT_FALSE(fetch_no_proxy_match("host.example.org"));
103+
ASSERT(fetch_no_proxy_match("www.google.com"));
104+
ASSERT_FALSE(fetch_no_proxy_match("google.com.bad"));
105+
ASSERT(fetch_no_proxy_match("other.org"));
106+
107+
setenv("no_proxy", "*", 1);
108+
ASSERT(fetch_no_proxy_match("anything.com"));
109+
110+
unsetenv("no_proxy");
111+
PASS();
112+
}
113+
114+
TEST test_common_netrc_auth(void) {
115+
char netrc_path[PATH_MAX];
116+
FILE *f;
117+
struct url *u;
118+
119+
snprintf(netrc_path, sizeof(netrc_path), "%s%s_test.netrc", DOWNLOAD_DIR,
120+
PATH_SEP);
121+
f = fopen(netrc_path, "w");
122+
ASSERT(f);
123+
fprintf(f, "machine test-host.com login myuser password mypass\n");
124+
fclose(f);
125+
setenv("NETRC", netrc_path, 1);
126+
u = fetchParseURL("ftp://test-host.com/file");
127+
ASSERT(u);
128+
ASSERT_EQ(0, fetch_netrc_auth(u));
129+
ASSERT_STR_EQ("myuser", u->user);
130+
ASSERT_STR_EQ("mypass", u->pwd);
131+
fetchFreeURL(u);
132+
unsetenv("NETRC");
133+
remove(netrc_path);
134+
PASS();
135+
}
136+
#endif
137+
58138
SUITE(libfetch_suite) {
139+
#if !defined(_WIN32)
140+
RUN_TEST(test_common_no_proxy_match);
141+
RUN_TEST(test_common_netrc_auth);
142+
#endif
59143
RUN_TEST(test_libfetch_url_parser);
60144
RUN_TEST(test_libfetch_base64_encoder);
61145
}

acquire/tests/test_string_extras.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ TEST x_strnstr_should_succeed(void) {
1616
PASS();
1717
}
1818

19+
TEST x_strnstr_should_succeed_with_embedded_null(void) {
20+
ASSERT_EQ(test_buffer,
21+
strnstr(test_buffer, test_target, strlen(test_buffer)));
22+
PASS();
23+
}
24+
1925
TEST x_strnstr_should_fail(void) {
20-
ASSERT_EQ(strcmp(test_buffer,
21-
strnstr(test_buffer, test_target, strlen(test_buffer))),
22-
0);
26+
ASSERT_EQ(NULL, strnstr(test_buffer, "world!", strlen(test_buffer)));
2327
PASS();
2428
}
2529

@@ -43,6 +47,7 @@ TEST test_strcasestr_not_found(void) {
4347
/* Suites can group multiple tests with common setup. */
4448
SUITE(string_extras_suite) {
4549
RUN_TEST(x_strnstr_should_succeed);
50+
RUN_TEST(x_strnstr_should_succeed_with_embedded_null);
4651
RUN_TEST(x_strnstr_should_fail);
4752
RUN_TEST(test_strcasestr_found);
4853
RUN_TEST(test_strcasestr_not_found);

acquire/tests/test_url_utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ TEST test_is_url_case_insensitive(void) {
120120
PASS();
121121
}
122122

123+
TEST test_is_url_file(void) {
124+
ASSERT(is_url("file:///path/to/thing"));
125+
PASS();
126+
}
127+
123128
SUITE(url_utils_suite) {
124129
RUN_TEST(test_get_path_valid_url);
125130
RUN_TEST(test_get_path_no_path);
@@ -132,6 +137,7 @@ SUITE(url_utils_suite) {
132137
RUN_TEST(test_is_url_https);
133138
RUN_TEST(test_is_url_ftp);
134139
RUN_TEST(test_is_url_ftps);
140+
RUN_TEST(test_is_url_file);
135141
RUN_TEST(test_is_url_too_short);
136142
RUN_TEST(test_is_url_no_scheme);
137143
RUN_TEST(test_is_url_case_insensitive);

reports/test_coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)