Skip to content

Commit 9808a82

Browse files
committed
[acquire/tests/test_*.h] Increase test coverage ; [acquire_*.h,acquire/cli/cli.c] Modify code fixing issues test failures found ; [reports/test_coverage.svg] Regenerate on latest ctest run
1 parent bf2a840 commit 9808a82

File tree

6 files changed

+138
-55
lines changed

6 files changed

+138
-55
lines changed

acquire/acquire_librhash.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ int _librhash_verify_async_start(struct acquire_handle *handle,
9393
rhash_lib_initialized = 1;
9494
}
9595
be = (struct rhash_backend *)calloc(1, sizeof(struct rhash_backend));
96-
if (!be) {
96+
if (!be) { /* LCOV_EXCL_START */
9797
acquire_handle_set_error(handle, ACQUIRE_ERROR_OUT_OF_MEMORY,
9898
"rhash backend allocation failed");
9999
return -1;
100-
}
100+
} /* LCOV_EXCL_STOP */
101101
be->file = fopen(filepath, "rb");
102102
if (!be->file) {
103103
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_OPEN_FAILED, "%s",
@@ -106,12 +106,12 @@ int _librhash_verify_async_start(struct acquire_handle *handle,
106106
return -1;
107107
}
108108
be->handle = rhash_init(rhash_algo_id);
109-
if (!be->handle) {
109+
if (!be->handle) { /* LCOV_EXCL_START */
110110
cleanup_rhash_backend(handle);
111111
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNKNOWN,
112112
"rhash_init failed");
113113
return -1;
114-
}
114+
} /* LCOV_EXCL_STOP */
115115
be->algorithm_id = rhash_algo_id;
116116
strncpy(be->expected_hash, expected_hash, sizeof(be->expected_hash) - 1);
117117
handle->backend_handle = be;
@@ -123,10 +123,15 @@ enum acquire_status _librhash_verify_async_poll(struct acquire_handle *handle) {
123123
struct rhash_backend *be;
124124
unsigned char buffer[CHUNK_SIZE];
125125
size_t bytes_read;
126-
if (!handle || !handle->backend_handle)
126+
if (!handle)
127127
return ACQUIRE_ERROR;
128128
if (handle->status != ACQUIRE_IN_PROGRESS)
129129
return handle->status;
130+
if (!handle->backend_handle) {
131+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNKNOWN,
132+
"In-progress poll with NULL backend");
133+
return ACQUIRE_ERROR;
134+
}
130135
if (handle->cancel_flag) {
131136
acquire_handle_set_error(handle, ACQUIRE_ERROR_CANCELLED,
132137
"Checksum cancelled");
@@ -136,18 +141,19 @@ enum acquire_status _librhash_verify_async_poll(struct acquire_handle *handle) {
136141
be = (struct rhash_backend *)handle->backend_handle;
137142
bytes_read = fread(buffer, 1, sizeof(buffer), be->file);
138143
if (bytes_read > 0) {
139-
if (rhash_update(be->handle, buffer, bytes_read) < 0) {
144+
if (rhash_update(be->handle, buffer, bytes_read) <
145+
0) { /* LCOV_EXCL_START */
140146
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNKNOWN,
141147
"rhash_update failed");
142-
} else {
148+
} else { /* LCOV_EXCL_STOP */
143149
handle->bytes_processed += bytes_read;
144150
return ACQUIRE_IN_PROGRESS;
145151
}
146152
}
147-
if (ferror(be->file)) {
153+
if (ferror(be->file)) { /* LCOV_EXCL_START */
148154
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_READ_FAILED, "%s",
149155
strerror(errno));
150-
} else {
156+
} else { /* LCOV_EXCL_STOP */
151157
unsigned char hash[64];
152158
char computed_hex[130];
153159
int digest_size = 0;

acquire/tests/CMakeLists.txt

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,16 @@ endmacro(test_wrapper)
120120
configure_file(config_for_tests.h.in "${PROJECT_BINARY_DIR}/src/config_for_tests.h" @ONLY)
121121

122122
set(Header_Files
123+
"test_handle.h"
123124
"test_checksum.h"
124125
"test_checksums_dispatch.h"
125-
"test_cli.h"
126126
"test_download.h"
127127
"test_fileutils.h"
128-
"test_handle.h"
129-
"test_libfetch.h"
130128
"test_net_common.h"
131-
"test_openssl.h"
132129
"test_string_extras.h"
133130
"test_url_utils.h"
131+
"test_cli.h"
132+
"test_libfetch.h"
134133
)
135134
source_group("Header Files" FILES "${Header_Files}")
136135

@@ -179,49 +178,18 @@ target_include_directories(
179178
)
180179
test_wrapper()
181180

182-
if (LIBACQUIRE_USE_OPENSSL OR CRYPTO_LIB STREQUAL "OpenSSL" OR CRYPTO_LIB STREQUAL "CommonCrypto" OR CRYPTO_LIB STREQUAL "LibreSSL")
183-
set(EXEC_NAME "test_openssl")
184-
185-
set(Header_Files "test_openssl.h"
186-
"${CMAKE_BINARY_DIR}/gen/gen_acquire_checksums.h"
187-
"${CMAKE_BINARY_DIR}/gen/gen_acquire_handle.h"
188-
"${CMAKE_BINARY_DIR}/gen/gen_acquire_openssl.h"
189-
)
181+
if (LIBACQUIRE_USE_LIBRHASH)
182+
set(EXEC_NAME "test_rhash_backend")
183+
set(Header_Files "test_librhash.h")
190184
source_group("${EXEC_NAME} Header Files" FILES "${Header_Files}")
191-
192-
set(Source_Files "test_openssl.c"
193-
"${CMAKE_BINARY_DIR}/gen/gen_acquire_checksums.c"
194-
"${CMAKE_BINARY_DIR}/gen/gen_acquire_handle.c"
195-
"${CMAKE_BINARY_DIR}/gen/gen_acquire_librhash.c"
196-
"${CMAKE_BINARY_DIR}/gen/gen_acquire_openssl.c"
197-
)
185+
set(Source_Files "test_librhash.c")
198186
source_group("${EXEC_NAME} Source Files" FILES "${Source_Files}")
199187

200-
set_source_files_properties("test_openssl.c"
201-
PROPERTIES COMPILE_DEFINITIONS "LIBACQUIRE_USE_OPENSSL=1")
202-
set_source_files_properties("${CMAKE_BINARY_DIR}/gen/gen_acquire_openssl.c"
203-
PROPERTIES COMPILE_DEFINITIONS "LIBACQUIRE_IMPLEMENTATION=1;LIBACQUIRE_USE_OPENSSL=1")
204-
205188
add_executable("${EXEC_NAME}" "${Header_Files}" "${Source_Files}")
206-
target_compile_definitions("${EXEC_NAME}" PUBLIC "_${TARGET_ARCH}_")
207-
target_compile_definitions("${EXEC_NAME}" PUBLIC "LIBACQUIRE_USE_OPENSSL=1")
208-
target_include_directories(
209-
"${EXEC_NAME}"
210-
PRIVATE
211-
"$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/acquire/cli>"
212-
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/acquire/cli>"
213-
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/tests>"
214-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
215-
)
216-
find_package(LibRHash REQUIRED)
217-
if (TARGET LibRHash::LibRHash)
218-
target_link_libraries("${EXEC_NAME}" PRIVATE LibRHash::LibRHash)
219-
else ()
220-
target_link_libraries("${EXEC_NAME}" PRIVATE "${LibRHash_LIBRARIES}")
221-
endif ()
222-
#target_link_libraries("${EXEC_NAME}" PRIVATE "${PROJECT_NAME}")
189+
target_compile_definitions("${EXEC_NAME}" PRIVATE LIBACQUIRE_USE_LIBRHASH=1)
190+
target_link_libraries("${EXEC_NAME}" PRIVATE "${PROJECT_NAME}")
223191
test_wrapper()
224-
endif (LIBACQUIRE_USE_OPENSSL OR CRYPTO_LIB STREQUAL "OpenSSL" OR CRYPTO_LIB STREQUAL "CommonCrypto" OR CRYPTO_LIB STREQUAL "LibreSSL")
192+
endif (LIBACQUIRE_USE_LIBRHASH)
225193

226194
if (LIBACQUIRE_USE_LIBCURL)
227195
set(EXEC_NAME "test_curl_helpers")

acquire/tests/test.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414
#endif /* defined(LIBACQUIRE_USE_LIBFETCH) && LIBACQUIRE_USE_MY_LIBFETCH */
1515
#include "acquire_config.h"
1616
#include "test_net_common.h"
17-
#include "test_string_extras.h"
18-
#include "test_url_utils.h"
1917
#if (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) || \
2018
(defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO) || \
2119
(defined(LIBACQUIRE_USE_LIBRESSL) && LIBACQUIRE_USE_LIBRESSL)
2220
#include "test_openssl.h"
2321
#endif
2422

23+
#if defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH
24+
#include "test_librhash.h"
25+
#endif /* defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH */
26+
27+
#include "test_string_extras.h"
28+
#include "test_url_utils.h"
29+
2530
#ifdef LIBACQUIRE_DOWNLOAD_DIR_IMPL
2631
const char *get_download_dir(void) { return ".test_downloads"; }
2732
#endif /* LIBACQUIRE_DOWNLOAD_DIR_IMPL */
@@ -45,7 +50,9 @@ int main(int argc, char **argv) {
4550
(defined(LIBACQUIRE_USE_LIBRESSL) && LIBACQUIRE_USE_LIBRESSL)
4651
RUN_SUITE(openssl_backend_suite);
4752
#endif
48-
53+
#if defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH
54+
RUN_SUITE(librhash_backend_suite);
55+
#endif /* defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH */
4956
#if defined(LIBACQUIRE_USE_LIBFETCH) && LIBACQUIRE_USE_MY_LIBFETCH
5057
RUN_SUITE(libfetch_suite);
5158
#endif /* defined(LIBACQUIRE_USE_LIBFETCH) && LIBACQUIRE_USE_MY_LIBFETCH */

acquire/tests/test_librhash.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH
2+
3+
#include "acquire_config.h"
4+
#include <greatest.h>
5+
6+
#include "test_librhash.h"
7+
8+
#ifdef LIBACQUIRE_DOWNLOAD_DIR_IMPL
9+
const char *get_download_dir(void) { return ".test_downloads"; }
10+
#endif /* LIBACQUIRE_DOWNLOAD_DIR_IMPL */
11+
12+
GREATEST_MAIN_DEFS();
13+
14+
int main(int argc, char **argv) {
15+
GREATEST_MAIN_BEGIN();
16+
#if defined(LIBACQUIRE_USE_LIBRHASH)
17+
if (GREATEST_IS_VERBOSE()) {
18+
printf("\n--- Running LibRHash Backend Tests ---\n");
19+
}
20+
RUN_SUITE(librhash_backend_suite);
21+
#else
22+
(void)argc;
23+
(void)argv;
24+
#endif
25+
GREATEST_MAIN_END();
26+
}
27+
#endif /* defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH */

acquire/tests/test_librhash.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#ifndef TEST_LIBRHASH_H
2+
#define TEST_LIBRHASH_H
3+
4+
#if defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH
5+
6+
#include <greatest.h>
7+
#include <stdio.h>
8+
9+
#include "acquire_checksums.h" /* For string2checksum and verify API */
10+
#include "acquire_fileutils.h" /* For is_file */
11+
#include "acquire_handle.h" /* For handle functions */
12+
#include "config_for_tests.h"
13+
14+
TEST test_rhash_backend_invalid_crc_hash_len(void) {
15+
struct acquire_handle *h = acquire_handle_init();
16+
int result;
17+
result = acquire_verify_sync(h, GREATEST_FILE, LIBACQUIRE_CRC32C, "short");
18+
ASSERT_EQ(-1, result);
19+
ASSERT_EQ(ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
20+
acquire_handle_get_error_code(h));
21+
acquire_handle_free(h);
22+
PASS();
23+
}
24+
25+
TEST test_rhash_backend_poll_null(void) {
26+
ASSERT_EQ(ACQUIRE_ERROR, _librhash_verify_async_poll(NULL));
27+
PASS();
28+
}
29+
30+
TEST test_rhash_backend_poll_on_null_backend(void) {
31+
struct acquire_handle *h = acquire_handle_init();
32+
h->status = ACQUIRE_IN_PROGRESS;
33+
h->backend_handle = NULL;
34+
h->active_backend = ACQUIRE_BACKEND_CHECKSUM_LIBRHASH;
35+
36+
ASSERT_EQ(ACQUIRE_ERROR, _librhash_verify_async_poll(h));
37+
ASSERT_EQ(ACQUIRE_ERROR_UNKNOWN, acquire_handle_get_error_code(h));
38+
ASSERT(strstr(acquire_handle_get_error_string(h),
39+
"In-progress poll with NULL backend") != NULL);
40+
41+
acquire_handle_free(h);
42+
PASS();
43+
}
44+
45+
TEST test_rhash_backend_poll_on_non_running_handle(void) {
46+
struct acquire_handle *h = acquire_handle_init();
47+
h->active_backend = ACQUIRE_BACKEND_CHECKSUM_LIBRHASH; /* Force backend */
48+
49+
h->status = ACQUIRE_IDLE;
50+
ASSERT_EQ(ACQUIRE_IDLE, _librhash_verify_async_poll(h));
51+
52+
h->status = ACQUIRE_COMPLETE;
53+
ASSERT_EQ(ACQUIRE_COMPLETE, _librhash_verify_async_poll(h));
54+
55+
h->status = ACQUIRE_ERROR;
56+
ASSERT_EQ(ACQUIRE_ERROR, _librhash_verify_async_poll(h));
57+
58+
acquire_handle_free(h);
59+
PASS();
60+
}
61+
62+
TEST test_rhash_backend_cancel_null(void) {
63+
_librhash_verify_async_cancel(NULL);
64+
PASS();
65+
}
66+
67+
SUITE(librhash_backend_suite) {
68+
RUN_TEST(test_rhash_backend_invalid_crc_hash_len);
69+
RUN_TEST(test_rhash_backend_poll_null);
70+
RUN_TEST(test_rhash_backend_poll_on_null_backend);
71+
RUN_TEST(test_rhash_backend_poll_on_non_running_handle);
72+
RUN_TEST(test_rhash_backend_cancel_null);
73+
}
74+
#endif /* defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH */
75+
#endif /* !TEST_LIBRHASH_H */

reports/test_coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)