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 "mbedtls_compat.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#endif
52
60static inline void
62{
63 assert_int_equal(setvbuf(stdout, NULL, _IONBF, BUFSIZ), 0);
64 assert_int_equal(setvbuf(stderr, NULL, _IONBF, BUFSIZ), 0);
65#if defined(ENABLE_CRYPTO_MBEDTLS)
67#endif
68}
69
81void
82openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename)
83{
84 const char *srcdir = getenv("srcdir");
85
86#if defined(UNIT_TEST_SOURCEDIR)
87 if (!srcdir)
88 {
89 srcdir = UNIT_TEST_SOURCEDIR;
90 }
91#endif
92 assert_non_null(srcdir);
93
94 snprintf(buf, bufsize, "%s/%s", srcdir, filename);
95}
mbedtls compatibility stub.
static void mbedtls_compat_psa_crypto_init(void)
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:61
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:82