Skip to content

Commit 6db186e

Browse files
flichtenheldcron2
authored andcommitted
tests/unit_tests: Port to cmocka 2.0.0 API
But add compat layer so that we can still build against older versions of cmocka. Mostly this is trivial but the custom check function changed its prototype, so that requires some more work. Change-Id: Ifb6594700db71d219643a29c581099c778bcbbc6 Signed-off-by: Frank Lichtenheld <[email protected]> Acked-by: Gert Doering <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1449 Message-Id: <[email protected]> URL: https://www.mail-archive.com/[email protected]/msg35144.html Signed-off-by: Gert Doering <[email protected]>
1 parent ab5887c commit 6db186e

File tree

7 files changed

+78
-45
lines changed

7 files changed

+78
-45
lines changed

CMakeLists.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,18 @@ add_custom_command(
379379
)
380380
set(HAVE_CONFIG_VERSION_H YES)
381381

382+
if (BUILD_TESTING)
383+
find_package(cmocka CONFIG)
384+
if (TARGET cmocka::cmocka)
385+
set(CMOCKA_LIBRARIES cmocka::cmocka)
386+
else ()
387+
pkg_search_module(cmocka cmocka REQUIRED IMPORTED_TARGET)
388+
set(CMOCKA_LIBRARIES PkgConfig::cmocka)
389+
endif ()
390+
set(CMAKE_REQUIRED_LIBRARIES ${CMOCKA_LIBRARIES})
391+
check_include_files(cmocka_version.h HAVE_CMOCKA_VERSION_H)
392+
endif ()
393+
382394
configure_file(config.h.cmake.in config.h)
383395
configure_file(include/openvpn-plugin.h.in openvpn-plugin.h)
384396
# TODO we should remove the need for this, and always include config.h
@@ -636,14 +648,6 @@ endif ()
636648
option(UT_ALLOW_BIG_ALLOC "Allow unit-tests to use > 1 GB of memory" ON)
637649

638650
if (BUILD_TESTING)
639-
find_package(cmocka CONFIG)
640-
if (TARGET cmocka::cmocka)
641-
set(CMOCKA_LIBRARIES cmocka::cmocka)
642-
else ()
643-
pkg_search_module(cmocka cmocka REQUIRED IMPORTED_TARGET)
644-
set(CMOCKA_LIBRARIES PkgConfig::cmocka)
645-
endif ()
646-
647651
set(unit_tests
648652
"test_argv"
649653
"test_auth_token"

config.h.cmake.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
/* git version information in config-version.h */
8787
#cmakedefine HAVE_CONFIG_VERSION_H
8888

89+
/* cmocka version information available in cmocka_version.h (>= 2.0.0) */
90+
#cmakedefine HAVE_CMOCKA_VERSION_H
91+
8992
/* Define to 1 if you have the `daemon' function. */
9093
#cmakedefine HAVE_DAEMON
9194

configure.ac

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,16 @@ PKG_CHECK_MODULES(
14151415
[have_cmocka="yes"],
14161416
[AC_MSG_WARN([cmocka.pc not found on the system using pkg-config ${pkg_config_found}. Unit tests disabled])]
14171417
)
1418-
AM_CONDITIONAL([ENABLE_UNITTESTS], [test "${enable_unit_tests}" = "yes" -a "${have_cmocka}" = "yes" ])
1418+
AM_CONDITIONAL([ENABLE_UNITTESTS], [false])
1419+
if test "${enable_unit_tests}" = "yes" -a "${have_cmocka}" = "yes"; then
1420+
AM_CONDITIONAL([ENABLE_UNITTESTS], [true])
1421+
1422+
saved_CFLAGS="${CFLAGS}"
1423+
CFLAGS="${CFLAGS} ${CMOCKA_CFLAGS}"
1424+
# detect cmocka < 2.0.0 that had no cmocka_version.h
1425+
AC_CHECK_HEADERS([cmocka_version.h])
1426+
CFLAGS="${saved_CFLAGS}"
1427+
fi
14191428
AC_SUBST([ENABLE_UNITTESTS])
14201429

14211430
TEST_LDFLAGS="${OPTIONAL_CRYPTO_LIBS} ${OPTIONAL_PKCS11_HELPER_LIBS} ${OPTIONAL_LIBCAPNG_LIBS}"

tests/unit_tests/openvpn/test_common.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@
2424
#include <setjmp.h>
2525
#include <cmocka.h>
2626

27+
/* Do we use cmocka < 2.0.0? */
28+
#ifndef HAVE_CMOCKA_VERSION_H
29+
#define HAVE_OLD_CMOCKA_API 1
30+
/* compat with various versions of cmocka.h
31+
* Older versions have LargestIntegralType. Newer
32+
* versions use uintmax_t. But LargestIntegralType
33+
* is not guaranteed to be equal to uintmax_t, so
34+
* we can't use that unconditionally. So we only use
35+
* it if cmocka.h does not define LargestIntegralType.
36+
*/
37+
#ifndef LargestIntegralType
38+
#define LargestIntegralType uintmax_t
39+
#endif
40+
/* redefine 2.x API in terms of 1.x API */
41+
#define CMockaValueData LargestIntegralType
42+
#define check_expected_uint check_expected
43+
#define expect_uint_value expect_value
44+
#define expect_check_data expect_check
45+
#define cast_ptr_to_cmocka_value(x) (x)
46+
#endif
47+
2748
/**
2849
* Sets up the environment for unit tests like making both stderr and stdout
2950
* non-buffered to avoid messages getting lost if the program exits early.

tests/unit_tests/openvpn/test_options_parse.c

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ add_option(struct options *options, char *p[], bool is_inline, const char *file,
4444
struct env_set *es)
4545
{
4646
function_called();
47-
check_expected(p);
48-
check_expected(is_inline);
47+
check_expected_ptr(p);
48+
check_expected_uint(is_inline);
4949
}
5050

5151
void
@@ -198,31 +198,27 @@ read_single_config(struct options *options, const char *config)
198198
&option_types_found, &es);
199199
}
200200

201-
/* compat with various versions of cmocka.h
202-
* Older versions have LargestIntegralType. Newer
203-
* versions use uintmax_t. But LargestIntegralType
204-
* is not guaranteed to be equal to uintmax_t, so
205-
* we can't use that unconditionally. So we only use
206-
* it if cmocka.h does not define LargestIntegralType.
207-
*/
208-
#ifndef LargestIntegralType
209-
#define LargestIntegralType uintmax_t
210-
#endif
211-
212-
union tokens_parameter
201+
#if HAVE_OLD_CMOCKA_API
202+
union token_parameter
213203
{
214-
LargestIntegralType as_int;
215-
void *as_pointer;
204+
LargestIntegralType int_val;
205+
void *ptr;
216206
};
207+
#endif
217208

218209
static int
219-
check_tokens(const LargestIntegralType value, const LargestIntegralType expected)
210+
check_tokens(const CMockaValueData value, const CMockaValueData expected)
220211
{
221-
union tokens_parameter temp;
222-
temp.as_int = value;
223-
const char **p = (const char **)temp.as_pointer;
224-
temp.as_int = expected;
225-
const char **expected_p = (const char **)temp.as_pointer;
212+
#if HAVE_OLD_CMOCKA_API
213+
union token_parameter temp;
214+
temp.int_val = value;
215+
const char **p = (const char **)temp.ptr;
216+
temp.int_val = expected;
217+
const char **expected_p = (const char **)temp.ptr;
218+
#else
219+
const char **p = (const char **)value.ptr;
220+
const char **expected_p = (const char **)expected.ptr;
221+
#endif
226222
for (int i = 0; i < MAX_PARMS; i++)
227223
{
228224
if (!p[i] && !expected_p[i])
@@ -271,33 +267,33 @@ test_read_config(void **state)
271267

272268
/* basic test */
273269
expect_function_call(add_option);
274-
expect_check(add_option, p, check_tokens, p_expect_someopt);
275-
expect_value(add_option, is_inline, 0);
270+
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_someopt));
271+
expect_uint_value(add_option, is_inline, 0);
276272
expect_function_call(add_option);
277-
expect_check(add_option, p, check_tokens, p_expect_otheropt);
278-
expect_value(add_option, is_inline, 0);
273+
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_otheropt));
274+
expect_uint_value(add_option, is_inline, 0);
279275
read_single_config(&o, "someopt parm1 parm2\n otheropt 1 2");
280276

281277
/* -- gets stripped */
282278
expect_function_call(add_option);
283-
expect_check(add_option, p, check_tokens, p_expect_someopt);
284-
expect_value(add_option, is_inline, 0);
279+
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_someopt));
280+
expect_uint_value(add_option, is_inline, 0);
285281
expect_function_call(add_option);
286-
expect_check(add_option, p, check_tokens, p_expect_otheropt);
287-
expect_value(add_option, is_inline, 0);
282+
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_otheropt));
283+
expect_uint_value(add_option, is_inline, 0);
288284
read_single_config(&o, "someopt parm1 parm2\n\t--otheropt 1 2");
289285

290286
/* inline options */
291287
expect_function_call(add_option);
292-
expect_check(add_option, p, check_tokens, p_expect_inlineopt);
293-
expect_value(add_option, is_inline, 1);
288+
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_inlineopt));
289+
expect_uint_value(add_option, is_inline, 1);
294290
read_single_config(&o, "<inlineopt>\nsome text\nother text\n</inlineopt>");
295291

296292
p_expect_inlineopt[0] = "inlineopt";
297293
p_expect_inlineopt[1] = A_TIMES_256 A_TIMES_256 A_TIMES_256 A_TIMES_256 A_TIMES_256 "\n";
298294
expect_function_call(add_option);
299-
expect_check(add_option, p, check_tokens, p_expect_inlineopt);
300-
expect_value(add_option, is_inline, 1);
295+
expect_check_data(add_option, p, check_tokens, cast_ptr_to_cmocka_value(p_expect_inlineopt));
296+
expect_uint_value(add_option, is_inline, 1);
301297
read_single_config(&o, "<inlineopt>\n" A_TIMES_256 A_TIMES_256 A_TIMES_256 A_TIMES_256 A_TIMES_256 "\n</inlineopt>");
302298

303299
gc_free(&o.gc);

tests/unit_tests/openvpn/test_push_update_msg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ send_control_channel_string(struct context *c, const char *str, msglvl_t msgleve
171171
bool
172172
send_control_channel_string(struct context *c, const char *str, msglvl_t msglevel)
173173
{
174-
check_expected(str);
174+
check_expected_ptr(str);
175175
return true;
176176
}
177177

tests/unit_tests/openvpn/test_user_pass.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ query_user_exec_builtin(void)
5252
/* Loop through configured query_user slots */
5353
for (int i = 0; i < QUERY_USER_NUMSLOTS && query_user[i].response != NULL; i++)
5454
{
55-
check_expected(query_user[i].prompt);
55+
check_expected_ptr(query_user[i].prompt);
5656
strncpy(query_user[i].response, mock_ptr_type(char *), query_user[i].response_len);
5757
}
5858

0 commit comments

Comments
 (0)