OpenVPN
test_dhcp.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 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 <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 "test_common.h"
37#include "mock_msg.h"
38
39#include "dhcp.c"
40
41uint16_t
42ip_checksum(const sa_family_t af, const uint8_t *payload, const int len_payload,
43 const uint8_t *src_addr, const uint8_t *dest_addr, const int proto)
44{
45 return 0;
46}
47const char *
48print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
49{
50 return "dummy";
51}
52
53static void
55{
56 struct gc_arena gc = gc_new();
57 struct buffer out_buf = alloc_buf_gc(512, &gc);
58 struct buffer clear_buf = alloc_buf_gc(512, &gc);
60 bool error = false;
61
62#define LONGDOMAIN "a-reaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaally-long-domain"
63 const char *search_list[] = {
64 "openvpn.net",
65 "openvpn.org",
67 "subdomain." LONGDOMAIN ".top123", /* maximum length */
68 "subdomain-" LONGDOMAIN "-top123", /* maximum length */
69 "subdomain." LONGDOMAIN ".top1234", /* too long */
70 "sub..tld", /* invalid */
71 };
72 const unsigned char output_1[28] = "\x77\x1a\x07openvpn\x03net\x00\x07openvpn\x03org";
76 assert_false(error);
77
78 /* buf too small */
79 struct buffer small_buf = alloc_buf_gc(sizeof(output_1) - 1, &gc);
83 assert_true(error);
84 error = false;
85
86 const unsigned char output_2[0xEC + 3 + 1] = "\x77\xEE\xEC" LONGDOMAIN;
90 assert_false(error);
91
92 const unsigned char output_3[0xEC + 3 + 10 + 7 + 1] = "\x77\xFF\x09subdomain\xEC" LONGDOMAIN "\x06top123";
96 assert_false(error);
97
98 const unsigned char output_4[0xEC + 3 + 10 + 7 + 1] = "\x77\xFF\xFDsubdomain-" LONGDOMAIN "-top123";
102 assert_false(error);
103
107 assert_true(error);
108 error = false;
109
113 assert_true(error);
114 error = false;
115
116 /* FIXME: should probably throw an error instead adding that \x00 ? */
117 const char output_5[12] = "\x77\x0a\x03sub\x00\x03tld";
121 assert_false(error);
122
123 gc_free(&gc);
124}
125
126int
127main(void)
128{
129 const struct CMUnitTest tests[] = {
130 cmocka_unit_test(test_write_dhcp_search_str),
131 };
132
133 return cmocka_run_group_tests_name("dhcp", tests, NULL, NULL);
134}
void buf_clear(struct buffer *buf)
Definition buffer.c:163
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:89
static struct buffer clear_buf(void)
Return an empty struct buffer.
Definition buffer.h:222
#define BPTR(buf)
Definition buffer.h:123
static void gc_free(struct gc_arena *a)
Definition buffer.h:1025
static struct gc_arena gc_new(void)
Definition buffer.h:1017
static int buf_forward_capacity_total(const struct buffer *buf)
Definition buffer.h:557
static void write_dhcp_search_str(struct buffer *buf, const uint8_t type, const char *const *str_array, int array_len, bool *error)
Definition dhcp.c:269
#define DHCP_DOMAIN_SEARCH
Definition dhcp.h:44
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
unsigned short sa_family_t
Definition syshead.h:396
static void test_write_dhcp_search_str(void **state)
Definition test_dhcp.c:54
#define LONGDOMAIN
int main(void)
Definition test_dhcp.c:127
uint16_t ip_checksum(const sa_family_t af, const uint8_t *payload, const int len_payload, const uint8_t *src_addr, const uint8_t *dest_addr, const int proto)
Calculates an IP or IPv6 checksum with a pseudo header as required by TCP, UDP and ICMPv6.
Definition test_dhcp.c:42
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
Definition test_dhcp.c:48
struct gc_arena gc
Definition test_ssl.c:131