Skip to content

Commit 653f263

Browse files
committed
[*.h,*.c,CMakeLists.txt] Improve test coverage throughout ; expand implementations for thread-safety ; add test coverage script and badge
1 parent eb3a541 commit 653f263

25 files changed

+862
-646
lines changed

.pre-commit-config.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
COVERAGE="$(cd cmake-build-debug && ctest -C 'Debug' -T Coverage 2>/dev/null | awk 'END {print $NF}')"
4+
printf '<svg xmlns="http://www.w3.org/2000/svg" width="114" height="20" role="img" aria-label="coverage: %s"><title>coverage: %s</title><linearGradient id="s" x2="0" y2="%s"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="114" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/><rect x="61" width="53" height="20" fill="#97ca00"/><rect width="114" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text><text aria-hidden="true" x="865" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">%s</text><text x="865" y="140" transform="scale(.1)" fill="#fff" textLength="430">%s</text></g></svg>' "${COVERAGE}" "${COVERAGE}" "${COVERAGE}" "${COVERAGE}" "${COVERAGE}" > reports/test_coverage.svg

CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,19 @@ endif ()
102102
add_library("${PROJECT_NAME}_compiler_flags" INTERFACE)
103103
target_compile_features("${PROJECT_NAME}_compiler_flags" INTERFACE "c_std_${CMAKE_C_STANDARD}")
104104

105-
# add compiler warning flags just when building this project via
106-
# the BUILD_INTERFACE genex
107105
set(gcc_like "$<COMPILE_LANG_AND_ID:C,CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
108106
set(msvc "$<COMPILE_LANG_AND_ID:C,CXX,MSVC>")
109107
target_compile_options(
110108
"${PROJECT_NAME}_compiler_flags"
111109
INTERFACE
112-
"$<${gcc_like}:$<BUILD_INTERFACE:-Wshadow;-Wformat=2;-Wall;-Wno-missing-braces;-Wno-long-long;-pedantic>>"
110+
"$<$<AND:${gcc_like},$<CONFIG:Debug>>:$<BUILD_INTERFACE:-Wshadow;-Wformat=2;-Wall;-Wno-missing-braces;-Wno-long-long;-pedantic;-fprofile-arcs;-ftest-coverage>>"
113111
"$<${msvc}:$<BUILD_INTERFACE:-W3;-WX;-Zi;-permissive->>"
114112
)
113+
target_link_options(
114+
"${PROJECT_NAME}_compiler_flags"
115+
INTERFACE
116+
"$<$<AND:${gcc_like},$<CONFIG:Debug>>:$<BUILD_INTERFACE:--coverage>>"
117+
)
115118

116119
# control where the static and shared libraries are built so that on windows
117120
# we don't need to tinker with the path to run the executable

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ libacquire
33
[![License](https://img.shields.io/badge/license-Apache--2.0%20OR%20MIT-blue.svg)](https://opensource.org/licenses/Apache-2.0)
44
[![CI for Linux, Windows, macOS](https://github.com/offscale/libacquire/workflows/CI%20for%20Linux,%20Windows,%20macOS/badge.svg)](https://github.com/offscale/libacquire/actions)
55
[![CI for FreeBSD](https://api.cirrus-ci.com/github/offscale/libacquire.svg)](https://cirrus-ci.com/github/offscale/libacquire)
6+
![coverage](reports/test_coverage.svg)
67
[![C89](https://img.shields.io/badge/C-89-blue)](https://en.wikipedia.org/wiki/C89_(C_version))
78

89
The core for your package manager, minus the dependency graph components.

acquire/acquire_checksums.h

Lines changed: 61 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#ifdef __cplusplus
55
extern "C" {
6-
#endif
6+
#endif /* __cplusplus */
77

88
#include "acquire_handle.h"
99
#include "libacquire_export.h"
@@ -93,112 +93,101 @@ int acquire_verify_async_start(struct acquire_handle *handle,
9393
return -1;
9494
}
9595

96-
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) || \
97-
defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL)
98-
if (algorithm == LIBACQUIRE_SHA256 || algorithm == LIBACQUIRE_SHA512)
99-
return _openssl_verify_async_start(handle, filepath, algorithm,
100-
expected_hash);
101-
#endif
102-
96+
/* duplicate labels so can't have a 'global' switch/case */
97+
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) || defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL)
98+
switch (algorithm) {
99+
case LIBACQUIRE_SHA256:
100+
case LIBACQUIRE_SHA512:
101+
return _openssl_verify_async_start(handle, filepath, algorithm,
102+
expected_hash);
103+
default: break;
104+
}
105+
#endif /* defined(LIBACQUIRE_USE_COMMON_CRYPTO) || defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL) */
103106
#ifdef LIBACQUIRE_USE_WINCRYPT
104-
if (algorithm == LIBACQUIRE_SHA256 || algorithm == LIBACQUIRE_SHA512)
105-
return _wincrypt_verify_async_start(handle, filepath, algorithm,
106-
expected_hash);
107-
#endif
108-
107+
switch (algorithm) {
108+
case LIBACQUIRE_SHA256:
109+
case LIBACQUIRE_SHA512:
110+
return _wincrypt_verify_async_start(handle, filepath, algorithm,
111+
expected_hash);
112+
default: break;
113+
}
114+
#endif /* LIBACQUIRE_USE_WINCRYPT */
109115
#ifdef LIBACQUIRE_USE_LIBRHASH
110-
if (algorithm == LIBACQUIRE_CRC32C || algorithm == LIBACQUIRE_SHA256 ||
111-
algorithm == LIBACQUIRE_SHA512)
112-
return _librhash_verify_async_start(handle, filepath, algorithm,
116+
switch (algorithm) {
117+
case LIBACQUIRE_CRC32C:
118+
case LIBACQUIRE_SHA256:
119+
case LIBACQUIRE_SHA512:
120+
return _librhash_verify_async_start(handle, filepath, algorithm,
113121
expected_hash);
114-
#endif
115-
122+
default:
123+
break;
124+
}
125+
#endif /* LIBACQUIRE_USE_LIBRHASH */
116126
#ifdef LIBACQUIRE_USE_CRC32C
117-
if (algorithm == LIBACQUIRE_CRC32C)
118-
return _crc32c_verify_async_start(handle, filepath, algorithm,
127+
switch (algorithm) {
128+
case LIBACQUIRE_CRC32C:
129+
return _crc32c_verify_async_start(handle, filepath, algorithm,
119130
expected_hash);
120-
#endif
131+
default: break;
132+
}
133+
#endif /* LIBACQUIRE_USE_CRC32C */
121134

122-
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT,
123-
"Unsupported checksum algorithm");
135+
acquire_handle_set_error(
136+
handle, ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT,
137+
"Unsupported checksum algorithm or no backend for it");
124138
return -1;
125139
}
126140

127141
enum acquire_status acquire_verify_async_poll(struct acquire_handle *handle) {
128-
if (!handle)
142+
if (!handle || !handle->backend_handle)
129143
return ACQUIRE_ERROR;
130144

131145
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) || \
132146
defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL)
133-
if (handle->backend_handle)
134-
return _openssl_verify_async_poll(handle);
135-
#endif
136-
137-
#ifdef LIBACQUIRE_USE_WINCRYPT
138-
if (handle->backend_handle)
139-
return _wincrypt_verify_async_poll(handle);
140-
#endif
141-
142-
#ifdef LIBACQUIRE_USE_LIBRHASH
143-
if (handle->backend_handle)
144-
return _librhash_verify_async_poll(handle);
145-
#endif
146-
147-
#ifdef LIBACQUIRE_USE_CRC32C
148-
if (handle->backend_handle)
149-
return _crc32c_verify_async_poll(handle);
150-
#endif
151-
147+
return _openssl_verify_async_poll(handle);
148+
#elif defined(LIBACQUIRE_USE_WINCRYPT)
149+
return _wincrypt_verify_async_poll(handle);
150+
#elif defined(LIBACQUIRE_USE_LIBRHASH)
151+
return _librhash_verify_async_poll(handle);
152+
#elif defined(LIBACQUIRE_USE_CRC32C)
153+
return _crc32c_verify_async_poll(handle);
154+
#else
155+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNKNOWN,
156+
"No checksum backend compiled");
152157
return ACQUIRE_ERROR;
158+
#endif
153159
}
154160

155161
void acquire_verify_async_cancel(struct acquire_handle *handle) {
156-
if (!handle)
162+
if (!handle || !handle->backend_handle)
157163
return;
158164

159165
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) || \
160166
defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL)
161-
if (handle->backend_handle) {
162-
_openssl_verify_async_cancel(handle);
163-
return;
164-
}
165-
#endif
166-
167-
#ifdef LIBACQUIRE_USE_WINCRYPT
168-
if (handle->backend_handle) {
169-
_wincrypt_verify_async_cancel(handle);
170-
return;
171-
}
172-
#endif
173-
174-
#ifdef LIBACQUIRE_USE_LIBRHASH
175-
if (handle->backend_handle) {
176-
_librhash_verify_async_cancel(handle);
177-
return;
178-
}
179-
#endif
180-
181-
#ifdef LIBACQUIRE_USE_CRC32C
182-
if (handle->backend_handle) {
183-
_crc32c_verify_async_cancel(handle);
184-
return;
185-
}
167+
_openssl_verify_async_cancel(handle);
168+
#elif defined(LIBACQUIRE_USE_WINCRYPT)
169+
_wincrypt_verify_async_cancel(handle);
170+
#elif defined(LIBACQUIRE_USE_LIBRHASH)
171+
_librhash_verify_async_cancel(handle);
172+
#elif defined(LIBACQUIRE_USE_CRC32C)
173+
_crc32c_verify_async_cancel(handle);
186174
#endif
187175
}
188176

189177
int acquire_verify_sync(struct acquire_handle *handle, const char *filepath,
190178
enum Checksum algorithm, const char *expected_hash) {
179+
enum acquire_status status;
191180
if (!handle || !filepath || !expected_hash)
192181
return -1;
193182

194183
if (acquire_verify_async_start(handle, filepath, algorithm, expected_hash) !=
195184
0)
196185
return -1;
197186

198-
while (acquire_verify_async_poll(handle) == ACQUIRE_IN_PROGRESS)
187+
while ((status = acquire_verify_async_poll(handle)) == ACQUIRE_IN_PROGRESS)
199188
;
200189

201-
return (handle->status == ACQUIRE_COMPLETE) ? 0 : -1;
190+
return (status == ACQUIRE_COMPLETE) ? 0 : -1;
202191
}
203192

204193
enum Checksum string2checksum(const char *const s) {
@@ -217,6 +206,6 @@ enum Checksum string2checksum(const char *const s) {
217206

218207
#ifdef __cplusplus
219208
}
220-
#endif
209+
#endif /* __cplusplus */
221210

222211
#endif /* LIBACQUIRE_ACQUIRE_CHECKSUMS_H */

0 commit comments

Comments
 (0)