Skip to content

Commit 3cb8a67

Browse files
committed
[acquire/CMakeLists.txt] Add arch to all source files on Windows ; [acquire/tests/{CMakeLists.txt,test_curl_helpers{.h,.c}}] Add new test suite for libcurl wrapper
1 parent 37c09a8 commit 3cb8a67

File tree

5 files changed

+140
-2
lines changed

5 files changed

+140
-2
lines changed

acquire/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ else ()
352352
message(STATUS "Nothing special with ${src}")
353353
set_source_files_properties(${src} PROPERTIES COMPILE_DEFINITIONS "")
354354
endif ()
355+
if (WIN32)
356+
set_source_files_properties(
357+
"${src}"
358+
PROPERTIES COMPILE_DEFINITIONS "_${TARGET_ARCH}_"
359+
)
360+
endif (WIN32)
355361
endforeach (src IN LISTS gen_source_files)
356362

357363
if (WIN32)

acquire/acquire_libcurl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
extern "C" {
1919
#endif /* __cplusplus */
2020

21+
#ifdef ACQUIRE_TESTING
22+
#define ACQUIRE_STATIC
23+
#else
24+
#define ACQUIRE_STATIC static
25+
#endif /* ACQUIRE_TESTING */
26+
2127
#include <errno.h>
2228
#include <memory.h>
2329
#include <stdint.h>
@@ -61,7 +67,7 @@ struct dnld_params_t {
6167
uint64_t dnld_file_sz;
6268
};
6369

64-
static int get_oname_from_cd(char const *const cd, char *oname) {
70+
ACQUIRE_STATIC int get_oname_from_cd(char const *const cd, char *oname) {
6571
char const *const cdtag = "Content-disposition:";
6672
char const *const key = "filename=";
6773
char *val = NULL;
@@ -89,7 +95,7 @@ static int get_oname_from_cd(char const *const cd, char *oname) {
8995
return EXIT_SUCCESS;
9096
}
9197

92-
static int get_oname_from_url(char const *url, char *oname) {
98+
ACQUIRE_STATIC int get_oname_from_url(char const *url, char *oname) {
9399
char const *u = url;
94100

95101
/* Remove "http(s)://" */

acquire/tests/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,29 @@ foreach (EXEC_NAME ${_targets})
152152
test_wrapper()
153153
endforeach (EXEC_NAME ${_targets})
154154

155+
if (LIBACQUIRE_USE_LIBCURL)
156+
set(EXEC_NAME "test_curl_helpers")
157+
158+
add_executable("${EXEC_NAME}")
159+
160+
target_sources(
161+
"${EXEC_NAME}"
162+
PRIVATE
163+
"test_curl_helpers.c"
164+
"${CMAKE_BINARY_DIR}/gen/gen_acquire_libcurl.c"
165+
"${CMAKE_BINARY_DIR}/gen/gen_acquire_string_extras.c"
166+
)
167+
168+
target_compile_definitions(${EXEC_NAME} PRIVATE ACQUIRE_TESTING=1)
169+
170+
target_link_libraries("${EXEC_NAME}" PRIVATE CURL::libcurl)
171+
if (HAS_LIBBSD)
172+
target_link_libraries("${EXEC_NAME}" PRIVATE LibBSD::LibBSD)
173+
endif (HAS_LIBBSD)
174+
175+
test_wrapper()
176+
endif (LIBACQUIRE_USE_LIBCURL)
177+
155178
# Extraction archive tests
156179
if (NOT (EXTRACT_LIB STREQUAL "WINCOMPRESSAPI"))
157180
foreach (ARCHIVE_LIB ${_archives})

acquire/tests/test_curl_helpers.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <greatest.h>
2+
3+
#include "test_curl_helpers.h"
4+
5+
GREATEST_MAIN_DEFS();
6+
7+
int main(int argc, char **argv) {
8+
GREATEST_MAIN_BEGIN();
9+
RUN_SUITE(curl_helpers_suite);
10+
GREATEST_MAIN_END();
11+
}

acquire/tests/test_curl_helpers.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#ifndef TEST_CURL_HELPERS_H
2+
#define TEST_CURL_HELPERS_H
3+
4+
#include "acquire_common_defs.h"
5+
#include <greatest.h>
6+
7+
int get_oname_from_cd(char const *const cd, char *oname);
8+
int get_oname_from_url(char const *url, char *oname);
9+
10+
TEST test_get_oname_from_url_simple(void) {
11+
char oname[MAX_FILENAME];
12+
int result = get_oname_from_url("http://example.com/somefile.txt", oname);
13+
ASSERT_EQ(EXIT_SUCCESS, result);
14+
ASSERT_STR_EQ("somefile.txt", oname);
15+
PASS();
16+
}
17+
18+
TEST test_get_oname_from_url_with_query(void) {
19+
char oname[MAX_FILENAME];
20+
/* This implementation is simple and doesn't handle query strings. */
21+
int result =
22+
get_oname_from_url("https://site.com/archive.zip?version=1.2", oname);
23+
ASSERT_EQ(EXIT_SUCCESS, result);
24+
ASSERT_STR_EQ("archive.zip?version=1.2", oname);
25+
PASS();
26+
}
27+
28+
TEST test_get_oname_from_url_no_path(void) {
29+
char oname[MAX_FILENAME];
30+
int result = get_oname_from_url("http://example.com", oname);
31+
ASSERT_EQ(EXIT_SUCCESS, result);
32+
ASSERT_STR_EQ("http://example.com", oname);
33+
PASS();
34+
}
35+
36+
TEST test_get_oname_from_url_trailing_slash(void) {
37+
char oname[MAX_FILENAME];
38+
int result = get_oname_from_url("http://example.com/some/path/", oname);
39+
ASSERT_EQ(EXIT_SUCCESS, result);
40+
ASSERT_STR_EQ("", oname);
41+
PASS();
42+
}
43+
44+
TEST test_get_oname_from_cd_simple(void) {
45+
char oname[MAX_FILENAME];
46+
const char *cd = "attachment; filename=simple.dat";
47+
int result = get_oname_from_cd(cd, oname);
48+
ASSERT_EQ(EXIT_SUCCESS, result);
49+
ASSERT_STR_EQ("simple.dat", oname);
50+
PASS();
51+
}
52+
53+
TEST test_get_oname_from_cd_case_insensitive(void) {
54+
char oname[MAX_FILENAME];
55+
const char *cd = "attachment; FILENAME=case.txt";
56+
int result = get_oname_from_cd(cd, oname);
57+
ASSERT_EQ(EXIT_SUCCESS, result);
58+
ASSERT_STR_EQ("case.txt", oname);
59+
PASS();
60+
}
61+
62+
TEST test_get_oname_from_cd_with_other_params(void) {
63+
char oname[MAX_FILENAME];
64+
const char *cd = "attachment; filename=ext.zip; charset=UTF-8";
65+
int result = get_oname_from_cd(cd, oname);
66+
ASSERT_EQ(EXIT_SUCCESS, result);
67+
ASSERT_STR_EQ("ext.zip", oname);
68+
PASS();
69+
}
70+
71+
TEST test_get_oname_from_cd_no_filename(void) {
72+
char oname[MAX_FILENAME] = "initial";
73+
const char *cd = "attachment; something=else";
74+
int result = get_oname_from_cd(cd, oname);
75+
ASSERT_EQ(EXIT_FAILURE, result);
76+
ASSERT_STR_EQ("initial", oname); /* Should not be modified */
77+
PASS();
78+
}
79+
80+
SUITE(curl_helpers_suite) {
81+
RUN_TEST(test_get_oname_from_url_simple);
82+
RUN_TEST(test_get_oname_from_url_with_query);
83+
RUN_TEST(test_get_oname_from_url_no_path);
84+
RUN_TEST(test_get_oname_from_url_trailing_slash);
85+
86+
RUN_TEST(test_get_oname_from_cd_simple);
87+
RUN_TEST(test_get_oname_from_cd_case_insensitive);
88+
RUN_TEST(test_get_oname_from_cd_with_other_params);
89+
RUN_TEST(test_get_oname_from_cd_no_filename);
90+
}
91+
92+
#endif /* !TEST_CURL_HELPERS_H */

0 commit comments

Comments
 (0)