OpenVPN
test_socket.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) 2021-2025 Arne Schwabe <arne@rfc2549.org>
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 <stdio.h>
30#include <stdlib.h>
31#include <stdarg.h>
32#include <string.h>
33#include <setjmp.h>
34#include <cmocka.h>
35
36#include "socket.h"
37#include "win32.h"
38
39/* stubs for some unused functions instead of pulling in too many dependencies */
40struct signal_info siginfo_static; /* GLOBAL */
41
42int
43signal_reset(struct signal_info *si, int signum)
44{
45 assert_true(0);
46 return 0;
47}
48
49#ifdef _WIN32
50struct win32_signal win32_signal; /* GLOBAL */
51
52int
54{
55 assert_true(0);
56 return 0;
57}
58#endif
59
60int
61parse_line(const char *line, char **p, const int n, const char *file, const int line_num,
62 int msglevel, struct gc_arena *gc)
63{
64 assert_true(0);
65 return 0;
66}
67
68static void
69test_add_in6_addr_tc(const char *orig_str, uint32_t add, const char *expect_str)
70{
71 struct in6_addr orig, result, expected;
72 struct gc_arena gc = gc_new();
73 assert_int_equal(inet_pton(AF_INET6, orig_str, &orig), 1);
74 assert_int_equal(inet_pton(AF_INET6, expect_str, &expected), 1);
75 result = add_in6_addr(orig, add);
76 const char *result_str = print_in6_addr(result, 0, &gc);
77 assert_string_equal(result_str, expect_str);
78 assert_memory_equal(&result, &expected, sizeof(struct in6_addr));
79 gc_free(&gc);
80}
81
82static bool
84{
85 struct gc_arena gc = gc_new();
86 const char *ipv4_output = "::255.255.255.255";
87 struct in6_addr addr;
88 assert_int_equal(inet_pton(AF_INET6, ipv4_output, &addr), 1);
89 const char *test_output = print_in6_addr(addr, 0, &gc);
90 bool ret = strcmp(test_output, ipv4_output) == 0;
91 gc_free(&gc);
92 return ret;
93}
94
95static void
96test_add_in6_addr(void **state)
97{
98 /* Note that some of the result strings need to account for
99 print_in6_addr formatting the addresses potentially as IPv4 */
100 bool mapped_ipv4 = check_mapped_ipv4_address();
101 test_add_in6_addr_tc("::", 1, "::1");
102 test_add_in6_addr_tc("::ff", 1, "::100");
103 test_add_in6_addr_tc("::ffff", 1, mapped_ipv4 ? "::0.1.0.0" : "::1:0");
104 test_add_in6_addr_tc("ffff::ffff", 1, "ffff::1:0");
105 test_add_in6_addr_tc("::ffff:ffff", 1, "::1:0:0");
106 test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 1, "::");
107 test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 2, "::1");
108
109 test_add_in6_addr_tc("::", UINT32_MAX, mapped_ipv4 ? "::255.255.255.255" : "::ffff:ffff");
110 test_add_in6_addr_tc("::1", UINT32_MAX, "::1:0:0");
111 test_add_in6_addr_tc("::ffff", UINT32_MAX, "::1:0:fffe");
112 test_add_in6_addr_tc("ffff::ffff", UINT32_MAX, "ffff::1:0:fffe");
113 test_add_in6_addr_tc("::ffff:ffff", UINT32_MAX, "::1:ffff:fffe");
114 test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", UINT32_MAX,
115 mapped_ipv4 ? "::255.255.255.254" : "::ffff:fffe");
116}
117
118const struct CMUnitTest socket_tests[] = {
119 cmocka_unit_test(test_add_in6_addr)
120};
121
122int
123main(void)
124{
125 return cmocka_run_group_tests(socket_tests, NULL, NULL);
126}
static void gc_free(struct gc_arena *a)
Definition buffer.h:1015
static struct gc_arena gc_new(void)
Definition buffer.h:1007
struct in6_addr add_in6_addr(struct in6_addr base, uint32_t add)
const char * print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
static void test_add_in6_addr(void **state)
Definition test_socket.c:96
int signal_reset(struct signal_info *si, int signum)
Clear the signal if its current value equals signum.
Definition test_socket.c:43
static bool check_mapped_ipv4_address(void)
Definition test_socket.c:83
int main(void)
static void test_add_in6_addr_tc(const char *orig_str, uint32_t add, const char *expect_str)
Definition test_socket.c:69
struct signal_info siginfo_static
Definition test_socket.c:40
int win32_signal_get(struct win32_signal *ws)
Definition test_socket.c:53
const struct CMUnitTest socket_tests[]
int parse_line(const char *line, char **p, const int n, const char *file, const int line_num, int msglevel, struct gc_arena *gc)
Definition test_socket.c:61
struct gc_arena gc
Definition test_ssl.c:154