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#endif
28
29#include "syshead.h"
30
31#include <stdarg.h>
32#include <stddef.h>
33#include <setjmp.h>
34#include <cmocka.h>
35
36#include "packet_id.h"
37#include "reliable.h"
38#include "test_common.h"
39
41 struct {
42 uint32_t buf_id;
43 uint32_t buf_time;
47 struct gc_arena gc;
48};
49
50static int
52{
53 struct test_packet_id_write_data *data =
54 calloc(1, sizeof(struct test_packet_id_write_data));
55
56 if (!data)
57 {
58 return -1;
59 }
60
61 data->test_buf.data = (void *) &data->test_buf_data;
62 data->test_buf.capacity = sizeof(data->test_buf_data);
63 data->gc = gc_new();
64
65 *state = data;
66 return 0;
67}
68
69static int
71{
72 struct test_packet_id_write_data *data = *state;
73 gc_free(&data->gc);
74 free(*state);
75 return 0;
76}
77
78static void
80{
81 struct test_packet_id_write_data *data = *state;
82
83 now = 5010;
84 assert_true(packet_id_write(&data->pis, &data->test_buf, false, false));
85 assert_true(data->pis.id == 1);
86 assert_true(data->test_buf_data.buf_id == htonl(1));
87 assert_true(data->test_buf_data.buf_time == 0);
88}
89
90static void
92{
93 struct test_packet_id_write_data *data = *state;
94
95 now = 5010;
96 assert_true(packet_id_write(&data->pis, &data->test_buf, true, false));
97 assert_int_equal(data->pis.id, 1);
98 assert_int_equal(data->pis.time, now);
99 assert_true(data->test_buf_data.buf_id == htonl(1));
100 assert_true(data->test_buf_data.buf_time == htonl((uint32_t)now));
101}
102
103static void
105{
106 struct test_packet_id_write_data *data = *state;
107
108 data->test_buf.offset = sizeof(packet_id_type);
109 now = 5010;
110 assert_true(packet_id_write(&data->pis, &data->test_buf, false, true));
111 assert_true(data->pis.id == 1);
112 assert_true(data->test_buf_data.buf_id == htonl(1));
113 assert_true(data->test_buf_data.buf_time == 0);
114}
115
116static void
118{
119 struct test_packet_id_write_data *data = *state;
120
121 data->test_buf.offset = sizeof(data->test_buf_data);
122 now = 5010;
123 assert_true(packet_id_write(&data->pis, &data->test_buf, true, true));
124 assert_int_equal(data->pis.id, 1);
125 assert_int_equal(data->pis.time, now);
126 assert_true(data->test_buf_data.buf_id == htonl(1));
127 assert_true(data->test_buf_data.buf_time == htonl((uint32_t)now));
128}
129
130static void
132{
133 struct test_packet_id_write_data *data = *state;
134
135 /* maximum 32-bit packet id */
136 data->pis.id = (packet_id_type)(~0);
137 assert_false(packet_id_write(&data->pis, &data->test_buf, false, false));
138}
139
140static void
142{
143 struct test_packet_id_write_data *data = *state;
144
145 /* maximum 32-bit packet id */
146 data->pis.id = (packet_id_type)(~0);
147 data->pis.time = 5006;
148
149 /* Write fails if time did not change */
150 now = 5006;
151 assert_false(packet_id_write(&data->pis, &data->test_buf, true, false));
152
153 /* Write succeeds if time moved forward */
154 now = 5010;
155 assert_true(packet_id_write(&data->pis, &data->test_buf, true, false));
156
157 assert_int_equal(data->pis.id, 1);
158 assert_int_equal(data->pis.time, now);
159 assert_true(data->test_buf_data.buf_id == htonl(1));
160 assert_true(data->test_buf_data.buf_time == htonl((uint32_t)now));
161}
162
163static void
165{
166
167 struct reliable *rel = malloc(sizeof(struct reliable));
168 reliable_init(rel, 100, 50, 8, false);
169
170 rel->array[5].active = true;
171 rel->array[5].packet_id = 100;
172
173 rel->packet_id = 103;
174
175 assert_int_equal(5, reliable_get_num_output_sequenced_available(rel));
176
177 rel->array[6].active = true;
178 rel->array[6].packet_id = 97;
179 assert_int_equal(2, reliable_get_num_output_sequenced_available(rel));
180
181 /* test ids close to int/unsigned int barrier */
182
183 rel->array[5].active = true;
184 rel->array[5].packet_id = (0x80000000u -3);
185 rel->array[6].active = false;
186 rel->packet_id = (0x80000000u -1);
187
188 assert_int_equal(6, reliable_get_num_output_sequenced_available(rel));
189
190 rel->array[5].active = true;
191 rel->array[5].packet_id = (0x80000000u -3);
192 rel->packet_id = 0x80000001u;
193
194 assert_int_equal(4, reliable_get_num_output_sequenced_available(rel));
195
196
197 /* test wrapping */
198 rel->array[5].active = true;
199 rel->array[5].packet_id = (0xffffffffu -3);
200 rel->array[6].active = false;
201 rel->packet_id = (0xffffffffu - 1);
202
203 assert_int_equal(6, reliable_get_num_output_sequenced_available(rel));
204
205 rel->array[2].packet_id = 0;
206 rel->array[2].active = true;
207
208 assert_int_equal(6, reliable_get_num_output_sequenced_available(rel));
209
210 rel->packet_id = 3;
211 assert_int_equal(1, reliable_get_num_output_sequenced_available(rel));
212
213 reliable_free(rel);
214}
215
216static void
218{
219 struct test_packet_id_write_data *data = *state;
220
221 struct buffer buf = alloc_buf_gc(128, &data->gc);
222
223 /* test normal writing of packet id to the buffer */
224 assert_true(packet_id_write_epoch(&data->pis, 0x23, &buf));
225
226 assert_int_equal(buf.len, 8);
227 uint8_t expected_header[8] = { 0x00, 0x23, 0, 0, 0, 0, 0, 1};
229
230 /* too small buffer should error out */
231 struct buffer buf_short = alloc_buf_gc(5, &data->gc);
233
234 /* test a true 48 bit packet id */
235 data->pis.id = 0xfa079ab9d2e8;
236 struct buffer buf_48 = alloc_buf_gc(128, &data->gc);
238 uint8_t expected_header_48[8] = { 0xff, 0xfe, 0xfa, 0x07, 0x9a, 0xb9, 0xd2, 0xe9};
240
241 /* test writing/checking the 48 bit per epoch packet counter
242 * overflow */
243 data->pis.id = 0xfffffffffffe;
244 struct buffer buf_of = alloc_buf_gc(128, &data->gc);
246 uint8_t expected_header_of[8] = { 0xf0, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
248
249 /* This is go over 2^48 - 1 and should error out. */
251
252 /* Now read back the packet ids and check if they are the same as what we
253 * have written */
254 struct packet_id_net pin;
255 assert_int_equal(packet_id_read_epoch(&pin, &buf), 0x23);
256 assert_int_equal(pin.id, 1);
257
258 assert_int_equal(packet_id_read_epoch(&pin, &buf_48), 0xfffe);
259 assert_int_equal(pin.id, 0xfa079ab9d2e9);
260
261 assert_int_equal(packet_id_read_epoch(&pin, &buf_of), 0xf00f);
262 assert_int_equal(pin.id, 0xffffffffffff);
263}
264
265static void
267{
268 struct reliable_ack ack = { .len = 4, .packet_id = {2, 1, 3, 2} };
269
270 struct reliable_ack mru_ack = {0 };
271
272 /* Test copying to empty ack structure */
275 assert_int_equal(mru_ack.packet_id[0], 2);
276 assert_int_equal(mru_ack.packet_id[1], 1);
277 assert_int_equal(mru_ack.packet_id[2], 3);
278
279 /* Copying again should not change the result */
282 assert_int_equal(mru_ack.packet_id[0], 2);
283 assert_int_equal(mru_ack.packet_id[1], 1);
284 assert_int_equal(mru_ack.packet_id[2], 3);
285
286 /* Copying just the first two element should not change the order
287 * as they are still the most recent*/
290 assert_int_equal(mru_ack2.packet_id[0], 2);
291 assert_int_equal(mru_ack2.packet_id[1], 1);
292 assert_int_equal(mru_ack2.packet_id[2], 3);
293
294 /* Adding just two packets shoudl ignore the 42 in array and
295 * reorder the order in the MRU */
296 struct reliable_ack ack2 = { .len = 3, .packet_id = {3, 2, 42} };
298 assert_int_equal(mru_ack2.packet_id[0], 3);
299 assert_int_equal(mru_ack2.packet_id[1], 2);
300 assert_int_equal(mru_ack2.packet_id[2], 1);
301
302 /* Copying a zero array into it should also change nothing */
303 struct reliable_ack empty_ack = { .len = 0 };
306 assert_int_equal(mru_ack.packet_id[0], 2);
307 assert_int_equal(mru_ack.packet_id[1], 1);
308 assert_int_equal(mru_ack.packet_id[2], 3);
309
310 /* Or should just 0 elements of the ack */
313 assert_int_equal(mru_ack.packet_id[0], 2);
314 assert_int_equal(mru_ack.packet_id[1], 1);
315 assert_int_equal(mru_ack.packet_id[2], 3);
316
317 struct reliable_ack ack3 = { .len = 7, .packet_id = {5, 6, 7, 8, 9, 10, 11}};
318
319 /* Adding multiple acks tests if the a full array is handled correctly */
321
322 struct reliable_ack expected_ack = { .len = 8, .packet_id = {5, 6, 7, 8, 9, 10, 11, 2}};
324
325 assert_memory_equal(mru_ack.packet_id, expected_ack.packet_id, sizeof(expected_ack.packet_id));
326}
327
328int
329main(void)
330{
332 const struct CMUnitTest tests[] = {
333 cmocka_unit_test_setup_teardown(test_packet_id_write_short,
336 cmocka_unit_test_setup_teardown(test_packet_id_write_long,
339 cmocka_unit_test_setup_teardown(test_packet_id_write_short_prepend,
342 cmocka_unit_test_setup_teardown(test_packet_id_write_long_prepend,
345 cmocka_unit_test_setup_teardown(test_packet_id_write_short_wrap,
348 cmocka_unit_test_setup_teardown(test_packet_id_write_long_wrap,
351 cmocka_unit_test_setup_teardown(test_packet_id_write_epoch,
354
356 cmocka_unit_test(test_copy_acks_to_lru)
357
358 };
359
360 return cmocka_run_group_tests_name("packet_id tests", tests, NULL, NULL);
361}
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:88
#define BPTR(buf)
Definition buffer.h:124
static void gc_free(struct gc_arena *a)
Definition buffer.h:1033
static struct gc_arena gc_new(void)
Definition buffer.h:1025
void reliable_free(struct reliable *rel)
Free allocated memory associated with a reliable structure and the pointer itself.
Definition reliable.c:375
void reliable_init(struct reliable *rel, int buf_size, int offset, int array_size, bool hold)
Initialize a reliable structure.
Definition reliable.c:357
void copy_acks_to_mru(struct reliable_ack *ack, struct reliable_ack *ack_mru, int n)
Copies the first n acks from ack to ack_mru.
Definition reliable.c:211
int reliable_get_num_output_sequenced_available(struct reliable *rel)
Counts the number of free buffers in output that can be potentially used for sending.
Definition reliable.c:551
time_t now
Definition otime.c:34
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:386
uint32_t packet_id_type
Definition packet_id.h:46
bool packet_id_write_epoch(struct packet_id_send *p, uint16_t epoch, struct buffer *buf)
Writes the packet ID containing both the epoch and the packet id to the buffer specified by buf.
uint16_t packet_id_read_epoch(struct packet_id_net *p, struct buffer *buf)
Reads the packet ID containing both the epoch and the per-epoch counter from the buf.
Reliability Layer module header file.
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
int capacity
Size in bytes of memory allocated by malloc().
Definition buffer.h:62
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:68
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:66
int offset
Offset in bytes of the actual content within the allocated memory.
Definition buffer.h:64
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
Data structure for describing the packet id that is received/send to the network.
Definition packet_id.h:192
uint64_t id
Definition packet_id.h:195
uint64_t id
Definition packet_id.h:154
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
Definition reliable.h:62
packet_id_type packet_id
Definition reliable.h:79
bool active
Definition reliable.h:76
The reliability layer storage structure for one VPN tunnel's control channel in one direction.
Definition reliable.h:92
struct reliable_entry array[RELIABLE_CAPACITY]
Definition reliable.h:98
packet_id_type packet_id
Definition reliable.h:95
struct test_packet_id_write_data::@26 test_buf_data
struct packet_id_send pis
static void openvpn_unit_test_setup(void)
Sets up the environment for unit tests like making both stderr and stdout non-buffered to avoid messa...
Definition test_common.h:36
static int test_packet_id_write_setup(void **state)
static void test_packet_id_write_epoch(void **state)
static void test_packet_id_write_long_prepend(void **state)
static void test_packet_id_write_short_wrap(void **state)
static void test_packet_id_write_short_prepend(void **state)
static void test_packet_id_write_long(void **state)
static int test_packet_id_write_teardown(void **state)
int main(void)
static void test_get_num_output_sequenced_available(void **state)
static void test_copy_acks_to_lru(void **state)
static void test_packet_id_write_long_wrap(void **state)
static void test_packet_id_write_short(void **state)