OpenVPN
test_misc.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-2023 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, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include "syshead.h"
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <setjmp.h>
35 #include <cmocka.h>
36 
37 #include "ssl_util.h"
38 #include "options_util.h"
39 #include "test_common.h"
40 
41 static void
43 {
44  struct gc_arena gc = gc_new();
45 
46  const char *input = "V4,dev-type tun,link-mtu 1457,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
47 
48  const char *output = options_string_compat_lzo(input, &gc);
49 
50  assert_string_equal(output, "V4,dev-type tun,link-mtu 1458,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server,comp-lzo");
51 
52  /* This string is has a much too small link-mtu so we should fail on it" */
53  input = "V4,dev-type tun,link-mtu 2,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
54 
55  output = options_string_compat_lzo(input, &gc);
56 
57  assert_string_equal(input, output);
58 
59  /* not matching at all */
60  input = "V4,dev-type tun";
61  output = options_string_compat_lzo(input, &gc);
62 
63  assert_string_equal(input, output);
64 
65 
66  input = "V4,dev-type tun,link-mtu 999,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
67  output = options_string_compat_lzo(input, &gc);
68 
69  /* 999 -> 1000, 3 to 4 chars */
70  assert_string_equal(output, "V4,dev-type tun,link-mtu 1000,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server,comp-lzo");
71 
72  gc_free(&gc);
73 }
74 
75 static void
77 {
78  struct options o;
79 
80  const char *teststr = "TEMP:There are no flags here [really not]";
81 
82  const char *msg = parse_auth_failed_temp(&o, teststr + strlen("TEMP"));
83  assert_string_equal(msg, "There are no flags here [really not]");
84 }
85 
86 static void
88 {
89  struct options o;
90 
91  const char *teststr = "[backoff 42,advance no]";
92 
93  const char *msg = parse_auth_failed_temp(&o, teststr);
94  assert_string_equal(msg, "");
95  assert_int_equal(o.server_backoff_time, 42);
96  assert_int_equal(o.no_advance, true);
97 }
98 
99 static void
101 {
102  struct options o;
103 
104  const char *teststr = "[advance remote,backoff 77]:go round and round";
105 
106  const char *msg = parse_auth_failed_temp(&o, teststr);
107  assert_string_equal(msg, "go round and round");
108  assert_int_equal(o.server_backoff_time, 77);
109 }
110 
111 const struct CMUnitTest misc_tests[] = {
112  cmocka_unit_test(test_compat_lzo_string),
113  cmocka_unit_test(test_auth_fail_temp_no_flags),
114  cmocka_unit_test(test_auth_fail_temp_flags),
115  cmocka_unit_test(test_auth_fail_temp_flags_msg),
116 };
117 
118 int
119 main(void)
120 {
122  return cmocka_run_group_tests(misc_tests, NULL, NULL);
123 }
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1031
openvpn_unit_test_setup
static void openvpn_unit_test_setup()
Sets up the environment for unit tests like making both stderr and stdout non-buffered to avoid messa...
Definition: test_common.h:36
test_common.h
test_auth_fail_temp_flags
static void test_auth_fail_temp_flags(void **state)
Definition: test_misc.c:87
main
int main(void)
Definition: test_misc.c:119
parse_auth_failed_temp
const char * parse_auth_failed_temp(struct options *o, const char *reason)
Definition: options_util.c:34
misc_tests
const struct CMUnitTest misc_tests[]
Definition: test_misc.c:111
test_auth_fail_temp_flags_msg
static void test_auth_fail_temp_flags_msg(void **state)
Definition: test_misc.c:100
options_util.h
ssl_util.h
options
Definition: options.h:236
options::no_advance
bool no_advance
Definition: options.h:280
syshead.h
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
test_compat_lzo_string
static void test_compat_lzo_string(void **state)
Definition: test_misc.c:42
test_auth_fail_temp_no_flags
static void test_auth_fail_temp_no_flags(void **state)
Definition: test_misc.c:76
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1039
options_string_compat_lzo
const char * options_string_compat_lzo(const char *options, struct gc_arena *gc)
Takes a locally produced OCC string for TLS server mode and modifies as if the option comp-lzo was en...
Definition: ssl_util.c:78
config.h
options::server_backoff_time
int server_backoff_time
Definition: options.h:291
msg
#define msg(flags,...)
Definition: error.h:150