OpenVPN
test_route.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) 2002-2026 OpenVPN Inc <sales@openvpn.net>
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#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "test_common.h"
30
31#include "route.h"
32#include "networking.h"
33
34#include "test_route.h"
35
36/* Stubs for functions route.c references but that no test here reaches.
37 * They assert rather than return quietly, so a test straying onto one
38 * fails loudly instead of silently doing nothing.
39 */
40struct argv
42{
43 assert_true(0);
44 struct argv a = { 0 };
45 return a;
46}
47
48void
49argv_free(struct argv *a)
50{
51 assert_true(0);
52}
53
54bool
55get_ipv6_addr(const char *prefix_str, struct in6_addr *network, unsigned int *netbits,
56 msglvl_t msglevel)
57{
58 assert_true(0);
59 return false;
60}
61
63getaddr(unsigned int flags, const char *hostname, int resolve_retry, bool *succeeded,
64 struct signal_info *sig_info)
65{
66 assert_true(0);
67 return 0;
68}
69
70int
71openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *servname,
72 int resolve_retry_seconds, struct signal_info *sig_info, int ai_family,
73 struct addrinfo **res)
74{
75 assert_true(0);
76 return 0;
77}
78
79struct signal_info siginfo_static; /* GLOBAL */
80
81int
82signal_reset(struct signal_info *si, int signum)
83{
84 assert_true(0);
85 return 0;
86}
87
88static void
90{
91 assert_true(is_special_addr("net_gateway"));
92 assert_true(is_special_addr("vpn_gateway"));
93 assert_true(is_special_addr("remote_host"));
94
95 assert_false(is_special_addr("10.0.0.1"));
96 assert_false(is_special_addr("default"));
97 assert_false(is_special_addr(NULL));
98}
99
100static void
102{
103 int netbits;
104
105 assert_true(netmask_to_netbits(0x0a000000, 0xff000000, &netbits)); /* 10/8 */
106 assert_int_equal(netbits, 8);
107
108 assert_true(netmask_to_netbits(0xc0a80100, 0xffffff00, &netbits)); /* 192.168.1/24 */
109 assert_int_equal(netbits, 24);
110
111 assert_true(netmask_to_netbits(0x00000000, 0x00000000, &netbits)); /* default route */
112 assert_int_equal(netbits, 0);
113
114 /* a full-length mask reports -1 rather than 32 */
115 assert_true(netmask_to_netbits(0x0a010203, 0xffffffff, &netbits));
116 assert_int_equal(netbits, -1);
117
118 /* host bits set in the network part */
119 assert_false(netmask_to_netbits(0x0a000001, 0xff000000, &netbits));
120
121 /* not a contiguous netmask */
122 assert_false(netmask_to_netbits(0x0a000000, 0xff00ff00, &netbits));
123}
124
125static void
127{
128 assert_int_equal(netmask_to_netbits2(0xff000000), 8);
129 assert_int_equal(netmask_to_netbits2(0xffffff00), 24);
130 assert_int_equal(netmask_to_netbits2(0x00000000), 0);
131
132 /* unlike netmask_to_netbits(), this one reports a full-length mask as 32 */
133 assert_int_equal(netmask_to_netbits2(0xffffffff), 32);
134
135 assert_int_equal(netmask_to_netbits2(0xff00ff00), -1);
136}
137
138static void
139assert_clear_host_bits(const char *addr, unsigned int netbits, const char *expected)
140{
141 struct route_ipv6 r6;
142 struct in6_addr want;
143
144 CLEAR(r6);
145 assert_int_equal(inet_pton(AF_INET6, addr, &r6.network), 1);
146 assert_int_equal(inet_pton(AF_INET6, expected, &want), 1);
147 r6.netbits = netbits;
148
150 assert_memory_equal(&r6.network, &want, sizeof(struct in6_addr));
151}
152
153static void
155{
156 /* whole bytes cleared */
157 assert_clear_host_bits("2001:db8::1", 64, "2001:db8::");
158 assert_clear_host_bits("2001:db8:dead:beef::1", 32, "2001:db8::");
159
160 /* a prefix that does not fall on a byte boundary */
161 assert_clear_host_bits("2001:db8:0:00ff::1", 60, "2001:db8:0:00f0::");
162 assert_clear_host_bits("2001:db8::ffff", 121, "2001:db8::ff80");
163
164 /* nothing to clear, and everything to clear */
165 assert_clear_host_bits("2001:db8::1", 128, "2001:db8::1");
166 assert_clear_host_bits("2001:db8::1", 0, "::");
167}
168
169static const struct CMUnitTest route_test_list[] = {
170 cmocka_unit_test(test_is_special_addr),
171 cmocka_unit_test(test_netmask_to_netbits),
172 cmocka_unit_test(test_netmask_to_netbits2),
173 cmocka_unit_test(test_route_ipv6_clear_host_bits),
174};
175
176int
178{
180 return cmocka_run_group_tests(route_test_list, NULL, NULL);
181}
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:87
#define CLEAR(x)
Definition basic.h:32
unsigned int msglvl_t
Definition error.h:77
bool is_special_addr(const char *addr_str)
Definition route.c:289
int netmask_to_netbits2(in_addr_t netmask)
Definition route.c:3835
void route_ipv6_clear_host_bits(struct route_ipv6 *r6)
Definition route.c:1721
bool netmask_to_netbits(const in_addr_t network, const in_addr_t netmask, int *netbits)
Definition route.c:3805
struct signal_info siginfo_static
Definition sig.c:44
Definition argv.h:35
uint32_t in_addr_t
Definition syshead.h:52
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
char * r6[]
static void test_route_ipv6_clear_host_bits(void **state)
Definition test_route.c:154
int route_tests(void)
Runs the route.c unit tests, returns the number of failures.
Definition test_route.c:177
static void test_netmask_to_netbits(void **state)
Definition test_route.c:101
int openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *servname, int resolve_retry_seconds, struct signal_info *sig_info, int ai_family, struct addrinfo **res)
Definition test_route.c:71
bool get_ipv6_addr(const char *prefix_str, struct in6_addr *network, unsigned int *netbits, msglvl_t msglevel)
Translate an IPv6 addr or hostname from string form to in6_addr.
Definition test_route.c:55
int signal_reset(struct signal_info *si, int signum)
Clear the signal if its current value equals signum.
Definition test_route.c:82
static void assert_clear_host_bits(const char *addr, unsigned int netbits, const char *expected)
Definition test_route.c:139
in_addr_t getaddr(unsigned int flags, const char *hostname, int resolve_retry, bool *succeeded, struct signal_info *sig_info)
Translate an IPv4 addr or hostname from string form to in_addr_t.
Definition test_route.c:63
static const struct CMUnitTest route_test_list[]
Definition test_route.c:169
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition test_route.c:49
static void test_netmask_to_netbits2(void **state)
Definition test_route.c:126
static void test_is_special_addr(void **state)
Definition test_route.c:89