OpenVPN
test_common.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2016-2026 Sentyron B.V. <openvpn@sentyron.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <setjmp.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <cmocka.h>
27
28#if defined(ENABLE_CRYPTO_MBEDTLS)
29#include <psa/crypto.h>
30#endif
31
32/* Do we use cmocka < 2.0.0? */
33#ifndef HAVE_CMOCKA_VERSION_H
34#define HAVE_OLD_CMOCKA_API 1
35/* compat with various versions of cmocka.h
36 * Older versions have LargestIntegralType. Newer
37 * versions use uintmax_t. But LargestIntegralType
38 * is not guaranteed to be equal to uintmax_t, so
39 * we can't use that unconditionally. So we only use
40 * it if cmocka.h does not define LargestIntegralType.
41 */
42#ifndef LargestIntegralType
43#define LargestIntegralType uintmax_t
44#endif
45/* redefine 2.x API in terms of 1.x API */
46#define CMockaValueData LargestIntegralType
47#define check_expected_uint check_expected
48#define expect_uint_value expect_value
49#define expect_check_data expect_check
50#define cast_ptr_to_cmocka_value(x) (x)
51#define assert_int_in_range(value, min, max) assert_in_range((value), (min), (max))
52#define assert_uint_in_range(value, min, max) assert_in_range((value), (min), (max))
53#endif
54
62static inline void
64{
65 assert_int_equal(setvbuf(stdout, NULL, _IONBF, BUFSIZ), 0);
66 assert_int_equal(setvbuf(stderr, NULL, _IONBF, BUFSIZ), 0);
67#if defined(ENABLE_CRYPTO_MBEDTLS)
68 /* psa_crypto_init() can return an error, but if initialization fails,
69 * it will only lead to test failures. */
70 (void)psa_crypto_init();
71#endif
72}
73
85static inline void
86openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename)
87{
88 const char *srcdir = getenv("srcdir");
89
90#if defined(UNIT_TEST_SOURCEDIR)
91 if (!srcdir)
92 {
93 srcdir = UNIT_TEST_SOURCEDIR;
94 }
95#endif
96 assert_non_null(srcdir);
97
98 snprintf(buf, bufsize, "%s/%s", srcdir, filename);
99}
static void openvpn_unit_test_setup(void)
Sets up the environment for unit tests like making both stderr and stdout non-buffered to avoid messa...
Definition test_common.h:63
static void openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename)
Helper function to get a file path from the unit test directory to open it or pass its path to anothe...
Definition test_common.h:86