OpenVPN
test_openvpnserv.c
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) 2025 Frank Lichtenheld <frank@lichtenheld.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 as published by the
12 * Free Software Foundation, either version 2 of the License,
13 * or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include <setjmp.h>
29#include <cmocka.h>
30#include "test_common.h"
31
32#include <winsock2.h>
33#include <windows.h>
34
35#include "interactive.c"
36
37BOOL
38ReportStatusToSCMgr(SERVICE_STATUS_HANDLE service, SERVICE_STATUS *status)
39{
40 return TRUE;
41}
42
43static void
45{
46 PCWSTR domain = L"openvpn.net";
47 size_t domain_len = wcslen(domain);
48 assert_true(ListContainsDomain(domain, domain, domain_len));
49 assert_true(ListContainsDomain(L"openvpn.com,openvpn.net", domain, domain_len));
50 assert_true(ListContainsDomain(L"openvpn.net,openvpn.com", domain, domain_len));
51
52 assert_false(ListContainsDomain(L"openvpn.com", domain, domain_len));
53 assert_false(ListContainsDomain(L"internal.openvpn.net", domain, domain_len));
54}
55
56#define BUF_SIZE 64
57static void
59{
60 DWORD size, orig_size, len, res_len;
61 LSTATUS err;
62 const DWORD glyph_size = sizeof(wchar_t);
63
64 wchar_t domains_1[BUF_SIZE] = L"openvpn.com";
65 len = (DWORD)wcslen(domains_1) + 1;
66 size = orig_size = len * glyph_size;
67 wchar_t domains_1_res[BUF_SIZE] = L".openvpn.com";
68 res_len = len + 2; /* adds . and \0 */
69 err = ConvertItfDnsDomains(L"openvpn.net", domains_1, &size, BUF_SIZE);
70 assert_memory_equal(domains_1, domains_1_res, size);
71 assert_int_equal(size, res_len * glyph_size);
72 assert_int_equal(err, NO_ERROR);
73
74 wchar_t domains_2[BUF_SIZE] = L"openvpn.com,openvpn.net";
75 len = (DWORD)wcslen(domains_2) + 1;
76 size = orig_size = len * glyph_size;
77 wchar_t domains_2_res[BUF_SIZE] = L".openvpn.com";
78 res_len = (DWORD)wcslen(domains_2_res) + 2;
79 err = ConvertItfDnsDomains(L"openvpn.net", domains_2, &size, BUF_SIZE);
80 assert_memory_equal(domains_2, domains_2_res, size);
81 assert_int_equal(size, res_len * glyph_size);
82 assert_int_equal(err, NO_ERROR);
83
84 wchar_t domains_3[BUF_SIZE] = L"openvpn.com,openvpn.net";
85 len = (DWORD)wcslen(domains_3) + 1;
86 size = orig_size = len * glyph_size;
87 wchar_t domains_3_res[BUF_SIZE] = L".openvpn.net";
88 res_len = (DWORD)wcslen(domains_3_res) + 2;
89 err = ConvertItfDnsDomains(L"openvpn.com", domains_3, &size, BUF_SIZE);
90 assert_memory_equal(domains_3, domains_3_res, size);
91 assert_int_equal(size, res_len * glyph_size);
92 assert_int_equal(err, NO_ERROR);
93
94 wchar_t domains_4[BUF_SIZE] = L"openvpn.com,openvpn.net";
95 len = (DWORD)wcslen(domains_4) + 1;
96 size = orig_size = len * glyph_size;
97 wchar_t domains_4_res[BUF_SIZE] = L".openvpn.com\0.openvpn.net";
98 res_len = len + 3; /* adds two . and one \0 */
99 err = ConvertItfDnsDomains(NULL, domains_4, &size, BUF_SIZE);
100 assert_memory_equal(domains_4, domains_4_res, size);
101 assert_int_equal(size, res_len * glyph_size);
102 assert_int_equal(err, NO_ERROR);
103}
104
105int
106wmain(void)
107{
109 const struct CMUnitTest tests[] = {
110 cmocka_unit_test(test_list_contains_domain),
111 cmocka_unit_test(test_convert_itf_dns_domains),
112 };
113
114 int ret = cmocka_run_group_tests_name("openvpnserv tests", tests, NULL, NULL);
115
116 return ret;
117}
static SERVICE_STATUS status
Definition interactive.c:51
static BOOL ListContainsDomain(PCWSTR list, PCWSTR domain, size_t len)
Check if a domain is contained in a comma separated list of domains.
static LSTATUS ConvertItfDnsDomains(PCWSTR search_domains, PWSTR domains, PDWORD size, const DWORD buf_size)
Convert interface specific domain suffix(es) from comma-separated string to MULTI_SZ string.
static SERVICE_STATUS_HANDLE service
Definition interactive.c:50
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
int wmain(void)
static void test_list_contains_domain(void **state)
static void test_convert_itf_dns_domains(void **state)
#define BUF_SIZE
BOOL ReportStatusToSCMgr(SERVICE_STATUS_HANDLE service, SERVICE_STATUS *status)