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 
40 static void
42 {
43  struct gc_arena gc = gc_new();
44 
45  const char *input = "V4,dev-type tun,link-mtu 1457,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
46 
47  const char *output = options_string_compat_lzo(input, &gc);
48 
49  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");
50 
51  /* This string is has a much too small link-mtu so we should fail on it" */
52  input = "V4,dev-type tun,link-mtu 2,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
53 
54  output = options_string_compat_lzo(input, &gc);
55 
56  assert_string_equal(input, output);
57 
58  /* not matching at all */
59  input = "V4,dev-type tun";
60  output = options_string_compat_lzo(input, &gc);
61 
62  assert_string_equal(input, output);
63 
64 
65  input = "V4,dev-type tun,link-mtu 999,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
66  output = options_string_compat_lzo(input, &gc);
67 
68  /* 999 -> 1000, 3 to 4 chars */
69  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");
70 
71  gc_free(&gc);
72 }
73 
74 static void
76 {
77  struct options o;
78 
79  const char *teststr = "TEMP:There are no flags here [really not]";
80 
81  const char *msg = parse_auth_failed_temp(&o, teststr + strlen("TEMP"));
82  assert_string_equal(msg, "There are no flags here [really not]");
83 }
84 
85 static void
87 {
88  struct options o;
89 
90  const char *teststr = "[backoff 42,advance no]";
91 
92  const char *msg = parse_auth_failed_temp(&o, teststr);
93  assert_string_equal(msg, "");
94  assert_int_equal(o.server_backoff_time, 42);
95  assert_int_equal(o.no_advance, true);
96 }
97 
98 static void
100 {
101  struct options o;
102 
103  const char *teststr = "[advance remote,backoff 77]:go round and round";
104 
105  const char *msg = parse_auth_failed_temp(&o, teststr);
106  assert_string_equal(msg, "go round and round");
107  assert_int_equal(o.server_backoff_time, 77);
108 }
109 
110 const struct CMUnitTest misc_tests[] = {
111  cmocka_unit_test(test_compat_lzo_string),
112  cmocka_unit_test(test_auth_fail_temp_no_flags),
113  cmocka_unit_test(test_auth_fail_temp_flags),
114  cmocka_unit_test(test_auth_fail_temp_flags_msg),
115 };
116 
117 int
118 main(void)
119 {
120  return cmocka_run_group_tests(misc_tests, NULL, NULL);
121 }
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1011
test_auth_fail_temp_flags
static void test_auth_fail_temp_flags(void **state)
Definition: test_misc.c:86
main
int main(void)
Definition: test_misc.c:118
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:110
test_auth_fail_temp_flags_msg
static void test_auth_fail_temp_flags_msg(void **state)
Definition: test_misc.c:99
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:41
test_auth_fail_temp_no_flags
static void test_auth_fail_temp_no_flags(void **state)
Definition: test_misc.c:75
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1019
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