Skip to content

Commit bf2a840

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 46c7258 commit bf2a840

9 files changed

Lines changed: 193 additions & 59 deletions

File tree

acquire/acquire_checksums.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ extern "C" {
88
#include "acquire_handle.h"
99
#include "libacquire_export.h"
1010

11-
#include "acquire_crc32c.h"
1211
#include "acquire_librhash.h"
1312
#include "acquire_openssl.h"
14-
#include "acquire_wincrypt.h"
1513

1614
extern LIBACQUIRE_EXPORT enum Checksum string2checksum(const char *s);
1715
extern LIBACQUIRE_EXPORT int

acquire/acquire_common_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ enum Checksum {
1111
LIBACQUIRE_CRC32C,
1212
LIBACQUIRE_SHA256,
1313
LIBACQUIRE_SHA512,
14-
LIBACQUIRE_INVALID_CHECKSUM,
14+
LIBACQUIRE_INVALID_CHECKSUM_ALGO,
1515
LIBACQUIRE_UNSUPPORTED_CHECKSUM
1616
};
1717

acquire/acquire_openssl.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,7 @@ int _openssl_verify_async_start(struct acquire_handle *handle,
121121
CC_SHA512_Init(&be->ctx.sha512);
122122
break;
123123
default:
124-
/* Should have been caught by the switch above, but for safety: */
125-
cleanup_openssl_backend(handle);
126-
acquire_handle_set_error(
127-
handle, ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
128-
"Internal error: unsupported algorithm in CommonCrypto backend");
129-
return -1;
124+
break;
130125
}
131126
#else
132127
const EVP_MD *md =
@@ -150,10 +145,15 @@ enum acquire_status _openssl_verify_async_poll(struct acquire_handle *handle) {
150145
struct openssl_backend *be;
151146
unsigned char buffer[CHUNK_SIZE];
152147
size_t bytes_read;
153-
if (!handle || !handle->backend_handle)
148+
if (!handle)
154149
return ACQUIRE_ERROR;
155150
if (handle->status != ACQUIRE_IN_PROGRESS)
156151
return handle->status;
152+
if (!handle->backend_handle) {
153+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNKNOWN,
154+
"In-progress poll with NULL backend");
155+
return ACQUIRE_ERROR;
156+
}
157157
if (handle->cancel_flag) {
158158
acquire_handle_set_error(handle, ACQUIRE_ERROR_CANCELLED,
159159
"Checksum cancelled");
@@ -172,14 +172,13 @@ enum acquire_status _openssl_verify_async_poll(struct acquire_handle *handle) {
172172
CC_SHA512_Update(&be->ctx.sha512, buffer, (CC_LONG)bytes_read);
173173
break;
174174
default:
175-
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNKNOWN,
176-
"Internal CC algorithm error");
177175
break;
178176
}
179177
#else
180-
if (1 != EVP_DigestUpdate(be->ctx, buffer, bytes_read))
178+
if (1 != EVP_DigestUpdate(be->ctx, buffer, bytes_read)) {
181179
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNKNOWN,
182180
"EVP_DigestUpdate failed");
181+
}
183182
#endif
184183
if (handle->error.code == ACQUIRE_OK) {
185184
handle->bytes_processed += bytes_read;
@@ -205,14 +204,13 @@ enum acquire_status _openssl_verify_async_poll(struct acquire_handle *handle) {
205204
CC_SHA512_Final(hash, &be->ctx.sha512);
206205
break;
207206
default:
208-
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNKNOWN,
209-
"Internal CC algorithm error");
210207
break;
211208
}
212209
#else
213-
if (1 != EVP_DigestFinal_ex(be->ctx, hash, &len))
210+
if (1 != EVP_DigestFinal_ex(be->ctx, hash, &len)) {
214211
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNKNOWN,
215212
"EVP_DigestFinal_ex failed");
213+
}
216214
#endif
217215
if (handle->error.code == ACQUIRE_OK) {
218216
for (i = 0; (unsigned int)i < len; i++)

acquire/tests/CMakeLists.txt

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ file(WRITE "${CORRUPT_ZIP_PATH}" "This is not a real zip file, just some text.")
4141
file(TO_NATIVE_PATH "${CORRUPT_ZIP_PATH_NATIVE}" CORRUPT_ZIP_PATH_NATIVE)
4242
if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND NOT MSYS AND NOT CYGWIN)
4343
string(REPLACE "\\" "\\\\" CORRUPT_ZIP_PATH_NATIVE "${CORRUPT_ZIP_PATH_NATIVE}")
44-
endif()
44+
endif ()
4545

4646
################################
4747
# Configure file (interpolate) #
@@ -120,16 +120,17 @@ 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"
124123
"test_checksum.h"
125124
"test_checksums_dispatch.h"
125+
"test_cli.h"
126126
"test_download.h"
127127
"test_fileutils.h"
128+
"test_handle.h"
129+
"test_libfetch.h"
128130
"test_net_common.h"
131+
"test_openssl.h"
129132
"test_string_extras.h"
130133
"test_url_utils.h"
131-
"test_cli.h"
132-
"test_libfetch.h"
133134
)
134135
source_group("Header Files" FILES "${Header_Files}")
135136

@@ -157,9 +158,9 @@ target_link_libraries(
157158
"${PROJECT_NAME}_compiler_flags"
158159
)
159160

160-
if(LIBACQUIRE_USE_MY_LIBFETCH)
161+
if (LIBACQUIRE_USE_MY_LIBFETCH)
161162
target_link_libraries(${EXEC_NAME} PRIVATE freebsd_libfetch)
162-
endif()
163+
endif ()
163164

164165
if (LIBACQUIRE_USE_MINIZ)
165166
find_package(kubazip CONFIG REQUIRED)
@@ -178,6 +179,50 @@ target_include_directories(
178179
)
179180
test_wrapper()
180181

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+
)
190+
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+
)
198+
source_group("${EXEC_NAME} Source Files" FILES "${Source_Files}")
199+
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+
205+
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}")
223+
test_wrapper()
224+
endif (LIBACQUIRE_USE_OPENSSL OR CRYPTO_LIB STREQUAL "OpenSSL" OR CRYPTO_LIB STREQUAL "CommonCrypto" OR CRYPTO_LIB STREQUAL "LibreSSL")
225+
181226
if (LIBACQUIRE_USE_LIBCURL)
182227
set(EXEC_NAME "test_curl_helpers")
183228

acquire/tests/test.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
#if defined(LIBACQUIRE_USE_LIBFETCH) && LIBACQUIRE_USE_MY_LIBFETCH
1313
#include "test_libfetch.h"
1414
#endif /* defined(LIBACQUIRE_USE_LIBFETCH) && LIBACQUIRE_USE_MY_LIBFETCH */
15+
#include "acquire_config.h"
1516
#include "test_net_common.h"
1617
#include "test_string_extras.h"
1718
#include "test_url_utils.h"
19+
#if (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) || \
20+
(defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO) || \
21+
(defined(LIBACQUIRE_USE_LIBRESSL) && LIBACQUIRE_USE_LIBRESSL)
22+
#include "test_openssl.h"
23+
#endif
1824

1925
#ifdef LIBACQUIRE_DOWNLOAD_DIR_IMPL
2026
const char *get_download_dir(void) { return ".test_downloads"; }
@@ -34,6 +40,12 @@ int main(int argc, char **argv) {
3440
RUN_SUITE(downloads_suite);
3541
RUN_SUITE(net_common_suite);
3642

43+
#if (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) || \
44+
(defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO) || \
45+
(defined(LIBACQUIRE_USE_LIBRESSL) && LIBACQUIRE_USE_LIBRESSL)
46+
RUN_SUITE(openssl_backend_suite);
47+
#endif
48+
3749
#if defined(LIBACQUIRE_USE_LIBFETCH) && LIBACQUIRE_USE_MY_LIBFETCH
3850
RUN_SUITE(libfetch_suite);
3951
#endif /* defined(LIBACQUIRE_USE_LIBFETCH) && LIBACQUIRE_USE_MY_LIBFETCH */

acquire/tests/test_checksum.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -170,35 +170,6 @@ TEST test_invalid_hash_length(void) {
170170
PASS();
171171
}
172172

173-
#if defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH
174-
TEST test_librhash_invalid_hash_length_sha512(void) {
175-
struct acquire_handle *h = acquire_handle_init();
176-
int result;
177-
/* SHA512 has special handling for 128 vs 129, test a different failure */
178-
result = acquire_verify_sync(h, GREATEST_FILE, LIBACQUIRE_SHA512, "short");
179-
ASSERT_EQ(-1, result);
180-
ASSERT_EQ(ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
181-
acquire_handle_get_error_code(h));
182-
acquire_handle_free(h);
183-
PASS();
184-
}
185-
#endif
186-
187-
#if (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) || \
188-
(defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO)
189-
TEST test_openssl_unsupported_algorithm(void) {
190-
struct acquire_handle *h = acquire_handle_init();
191-
/* crc32c is unsupported by the openssl backend */
192-
const int result = acquire_verify_sync(
193-
h, GREATEST_FILE, LIBACQUIRE_INVALID_CHECKSUM, "12345678");
194-
ASSERT_EQ(-1, result);
195-
ASSERT_EQ(ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
196-
acquire_handle_get_error_code(h));
197-
acquire_handle_free(h);
198-
PASS();
199-
}
200-
#endif
201-
202173
TEST test_verify_reusability(void) {
203174
struct acquire_handle *h = acquire_handle_init();
204175
int result;
@@ -229,13 +200,6 @@ TEST test_verify_reusability(void) {
229200
SUITE(checksums_suite) {
230201
RUN_TEST(test_unsupported_algorithm);
231202
RUN_TEST(test_invalid_hash_length);
232-
#if defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH
233-
RUN_TEST(test_librhash_invalid_hash_length_sha512);
234-
#endif
235-
#if (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) || \
236-
(defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO)
237-
RUN_TEST(test_openssl_unsupported_algorithm);
238-
#endif
239203
RUN_TEST(test_verify_sync_success_sha256);
240204
RUN_TEST(test_verify_sync_success_sha512);
241205
#if defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH || \

acquire/tests/test_openssl.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <greatest.h>
2+
3+
#include "acquire_common_defs.h"
4+
5+
#if (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) || \
6+
(defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO) || \
7+
(defined(LIBACQUIRE_USE_LIBRESSL) && LIBACQUIRE_USE_LIBRESSL)
8+
#include "test_openssl.h"
9+
#endif
10+
#include "acquire_config.h"
11+
12+
#ifdef LIBACQUIRE_DOWNLOAD_DIR_IMPL
13+
const char *get_download_dir(void) { return ".test_downloads"; }
14+
#endif /* LIBACQUIRE_DOWNLOAD_DIR_IMPL */
15+
16+
/* Add definitions that need to be in the test runner's main file. */
17+
GREATEST_MAIN_DEFS();
18+
19+
int main(int argc, char **argv) {
20+
GREATEST_MAIN_BEGIN();
21+
#if (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) || \
22+
(defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO) || \
23+
(defined(LIBACQUIRE_USE_LIBRESSL) && LIBACQUIRE_USE_LIBRESSL)
24+
if (GREATEST_IS_VERBOSE()) {
25+
printf("\n--- Running OpenSSL/CommonCrypto Backend Tests ---\n");
26+
}
27+
RUN_SUITE(openssl_backend_suite);
28+
#endif
29+
GREATEST_MAIN_END();
30+
}

acquire/tests/test_openssl.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#ifndef TEST_OPENSSL_H
2+
#define TEST_OPENSSL_H
3+
4+
#include <greatest.h>
5+
#include <stdio.h>
6+
7+
#include "acquire_checksums.h"
8+
#include "config_for_tests.h"
9+
10+
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
11+
#else
12+
#include <unistd.h>
13+
#endif
14+
15+
#if (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) || \
16+
(defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO) || \
17+
(defined(LIBACQUIRE_USE_LIBRESSL) && LIBACQUIRE_USE_LIBRESSL)
18+
19+
TEST test_openssl_backend_unsupported_algo(void) {
20+
struct acquire_handle *h = acquire_handle_init();
21+
int res;
22+
23+
/*
24+
* The OpenSSL backend doesn't support CRC32C.
25+
* This should fail in the `_start` function when it's dispatched.
26+
*/
27+
h->active_backend = ACQUIRE_BACKEND_CHECKSUM_OPENSSL;
28+
res = acquire_verify_sync(h, GREATEST_FILE, LIBACQUIRE_INVALID_CHECKSUM_ALGO,
29+
GREATEST_CRC32C);
30+
ASSERT_EQ(-1, res);
31+
ASSERT_EQ(ACQUIRE_ERROR_UNSUPPORTED_CHECKSUM_FORMAT,
32+
acquire_handle_get_error_code(h));
33+
acquire_handle_free(h);
34+
PASS();
35+
}
36+
37+
TEST test_openssl_backend_poll_on_finished_handle(void) {
38+
struct acquire_handle *h = acquire_handle_init();
39+
enum acquire_status status;
40+
h->active_backend = ACQUIRE_BACKEND_CHECKSUM_OPENSSL;
41+
42+
/* Run a successful verification to completion */
43+
acquire_verify_sync(h, GREATEST_FILE, LIBACQUIRE_SHA256, GREATEST_SHA256);
44+
ASSERT_EQ(ACQUIRE_COMPLETE, h->status);
45+
46+
/* Polling again on a completed handle should return the same status */
47+
h->active_backend = ACQUIRE_BACKEND_CHECKSUM_OPENSSL;
48+
status = acquire_verify_async_poll(h);
49+
ASSERT_EQ_FMT(ACQUIRE_COMPLETE, status, "%d");
50+
51+
acquire_handle_free(h);
52+
PASS();
53+
}
54+
55+
TEST test_openssl_backend_poll_on_null_backend(void) {
56+
struct acquire_handle *h = acquire_handle_init();
57+
h->status = ACQUIRE_IN_PROGRESS;
58+
h->active_backend = ACQUIRE_BACKEND_CHECKSUM_OPENSSL; /* Force backend */
59+
/* Do not create backend handle */
60+
61+
ASSERT_EQ(ACQUIRE_ERROR, acquire_verify_async_poll(h));
62+
ASSERT_EQ(ACQUIRE_ERROR_UNKNOWN, acquire_handle_get_error_code(h));
63+
ASSERT(strstr(acquire_handle_get_error_string(h),
64+
"In-progress poll with NULL backend") != NULL);
65+
66+
acquire_handle_free(h);
67+
PASS();
68+
}
69+
70+
TEST test_openssl_backend_cancel_null(void) {
71+
acquire_verify_async_cancel(NULL);
72+
PASS();
73+
}
74+
75+
SUITE(openssl_backend_suite) {
76+
RUN_TEST(test_openssl_backend_unsupported_algo);
77+
RUN_TEST(test_openssl_backend_poll_on_finished_handle);
78+
RUN_TEST(test_openssl_backend_poll_on_null_backend);
79+
RUN_TEST(test_openssl_backend_cancel_null);
80+
}
81+
82+
#endif /* (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) || \
83+
(defined(LIBACQUIRE_USE_COMMON_CRYPTO) && \
84+
LIBACQUIRE_USE_COMMON_CRYPTO) || (defined(LIBACQUIRE_USE_LIBRESSL) \
85+
&& LIBACQUIRE_USE_LIBRESSL) */
86+
87+
#endif /* !TEST_OPENSSL_H */

reports/test_coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)