OpenVPN
test_packet_id.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) 2016-2021 Fox Crypto B.V. <openvpn@foxcrypto.com>
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
20  * along with this program (see the file COPYING included with this
21  * distribution); if not, write to the Free Software Foundation, Inc.,
22  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #elif defined(_MSC_VER)
28 #include "config-msvc.h"
29 #endif
30 
31 #include "syshead.h"
32 
33 #include <stdarg.h>
34 #include <stddef.h>
35 #include <setjmp.h>
36 #include <cmocka.h>
37 
38 #include "packet_id.h"
39 
40 #include "mock_msg.h"
41 
43  struct {
44  uint32_t buf_id;
45  uint32_t buf_time;
46  } test_buf_data;
47  struct buffer test_buf;
49 };
50 
51 static int
53 {
54  struct test_packet_id_write_data *data =
55  calloc(1, sizeof(struct test_packet_id_write_data));
56 
57  if (!data)
58  {
59  return -1;
60  }
61 
62  data->test_buf.data = (void *) &data->test_buf_data;
63  data->test_buf.capacity = sizeof(data->test_buf_data);
64 
65  *state = data;
66  return 0;
67 }
68 
69 static int
71 {
72  free(*state);
73  return 0;
74 }
75 
76 static void
78 {
79  struct test_packet_id_write_data *data = *state;
80 
81  now = 5010;
82  assert_true(packet_id_write(&data->pis, &data->test_buf, false, false));
83  assert_true(data->pis.id == 1);
84  assert_true(data->test_buf_data.buf_id == htonl(1));
86 }
87 
88 static void
90 {
91  struct test_packet_id_write_data *data = *state;
92 
93  now = 5010;
94  assert_true(packet_id_write(&data->pis, &data->test_buf, true, false));
95  assert(data->pis.id == 1);
96  assert(data->pis.time == now);
97  assert_true(data->test_buf_data.buf_id == htonl(1));
98  assert_true(data->test_buf_data.buf_time == htonl(now));
99 }
100 
101 static void
103 {
104  struct test_packet_id_write_data *data = *state;
105 
106  data->test_buf.offset = sizeof(packet_id_type);
107  now = 5010;
108  assert_true(packet_id_write(&data->pis, &data->test_buf, false, true));
109  assert_true(data->pis.id == 1);
110  assert_true(data->test_buf_data.buf_id == htonl(1));
111  assert_true(data->test_buf_data.buf_time == 0);
112 }
113 
114 static void
116 {
117  struct test_packet_id_write_data *data = *state;
118 
119  data->test_buf.offset = sizeof(data->test_buf_data);
120  now = 5010;
121  assert_true(packet_id_write(&data->pis, &data->test_buf, true, true));
122  assert(data->pis.id == 1);
123  assert(data->pis.time == now);
124  assert_true(data->test_buf_data.buf_id == htonl(1));
125  assert_true(data->test_buf_data.buf_time == htonl(now));
126 }
127 
128 static void
130 {
131  struct test_packet_id_write_data *data = *state;
132 
133  data->pis.id = ~0;
134  assert_false(packet_id_write(&data->pis, &data->test_buf, false, false));
135 }
136 
137 static void
139 {
140  struct test_packet_id_write_data *data = *state;
141 
142  data->pis.id = ~0;
143  data->pis.time = 5006;
144 
145  /* Write fails if time did not change */
146  now = 5006;
147  assert_false(packet_id_write(&data->pis, &data->test_buf, true, false));
148 
149  /* Write succeeds if time moved forward */
150  now = 5010;
151  assert_true(packet_id_write(&data->pis, &data->test_buf, true, false));
152 
153  assert(data->pis.id == 1);
154  assert(data->pis.time == now);
155  assert_true(data->test_buf_data.buf_id == htonl(1));
156  assert_true(data->test_buf_data.buf_time == htonl(now));
157 }
158 
159 int
160 main(void)
161 {
162  const struct CMUnitTest tests[] = {
181  };
182 
183  return cmocka_run_group_tests_name("packet_id tests", tests, NULL, NULL);
184 }
static void test_packet_id_write_long_prepend(void **state)
#define assert_true(c)
Definition: cmocka.h:1045
struct packet_id_send pis
static int test_packet_id_write_setup(void **state)
static void test_packet_id_write_short(void **state)
int offset
Offset in bytes of the actual content within the allocated memory.
Definition: buffer.h:64
int capacity
Size in bytes of memory allocated by malloc().
Definition: buffer.h:62
bool packet_id_write(struct packet_id_send *p, struct buffer *buf, bool long_form, bool prepend)
Write a packet ID to buf, and update the packet ID state.
Definition: packet_id.c:346
static void test_packet_id_write_long_wrap(void **state)
#define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown)
Definition: cmocka.h:1818
static void test_packet_id_write_short_prepend(void **state)
static void test_packet_id_write_long(void **state)
time_t now
Definition: otime.c:36
struct test_packet_id_write_data::@14 test_buf_data
int main(void)
uint8_t * data
Pointer to the allocated memory.
Definition: buffer.h:68
#define cmocka_unit_test_setup_teardown(f, setup, teardown)
Initialize an array of CMUnitTest structures with a setup function for a test and a teardown function...
Definition: cmocka.h:1665
static int test_packet_id_write_teardown(void **state)
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
static void test_packet_id_write_short_wrap(void **state)
uint32_t packet_id_type
Definition: packet_id.h:49
#define free
Definition: cmocka.c:1850
#define assert_false(c)
Definition: cmocka.h:1063
packet_id_type id
Definition: packet_id.h:172