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, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include "syshead.h"
29
30#include <stdarg.h>
31#include <stddef.h>
32#include <setjmp.h>
33#include <cmocka.h>
34
35#include "packet_id.h"
36#include "reliable.h"
37#include "test_common.h"
38
40{
41 struct
42 {
43 uint32_t buf_id;
44 uint32_t buf_time;
48 struct gc_arena gc;
49};
50
51static int
53{
54 struct test_packet_id_write_data *data = 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 struct reliable *rel = malloc(sizeof(struct reliable));
167 reliable_init(rel, 100, 50, 8, false);
168
169 rel->array[5].active = true;
170 rel->array[5].packet_id = 100;
171
172 rel->packet_id = 103;
173
174 assert_int_equal(5, reliable_get_num_output_sequenced_available(rel));
175
176 rel->array[6].active = true;
177 rel->array[6].packet_id = 97;
178 assert_int_equal(2, reliable_get_num_output_sequenced_available(rel));
179
180 /* test ids close to int/unsigned int barrier */
181
182 rel->array[5].active = true;
183 rel->array[5].packet_id = (0x80000000u - 3);
184 rel->array[6].active = false;
185 rel->packet_id = (0x80000000u - 1);
186
187 assert_int_equal(6, reliable_get_num_output_sequenced_available(rel));
188
189 rel->array[5].active = true;
190 rel->array[5].packet_id = (0x80000000u - 3);
191 rel->packet_id = 0x80000001u;
192
193 assert_int_equal(4, reliable_get_num_output_sequenced_available(rel));
194
195
196 /* test wrapping */
197 rel->array[5].active = true;
198 rel->array[5].packet_id = (0xffffffffu - 3);
199 rel->array[6].active = false;
200 rel->packet_id = (0xffffffffu - 1);
201
202 assert_int_equal(6, reliable_get_num_output_sequenced_available(rel));
203
204 rel->array[2].packet_id = 0;
205 rel->array[2].active = true;
206
207 assert_int_equal(6, reliable_get_num_output_sequenced_available(rel));
208
209 rel->packet_id = 3;
210 assert_int_equal(1, reliable_get_num_output_sequenced_available(rel));
211
212 reliable_free(rel);
213}
214
215static void
217{
218 struct test_packet_id_write_data *data = *state;
219
220 struct buffer buf = alloc_buf_gc(128, &data->gc);
221
222 /* test normal writing of packet id to the buffer */
223 assert_true(packet_id_write_epoch(&data->pis, 0x23, &buf));
224
225 assert_int_equal(buf.len, 8);
226 uint8_t expected_header[8] = { 0x00, 0x23, 0, 0, 0, 0, 0, 1 };
228
229 /* too small buffer should error out */
230 struct buffer buf_short = alloc_buf_gc(5, &data->gc);
232
233 /* test a true 48 bit packet id */
234 data->pis.id = 0xfa079ab9d2e8;
235 struct buffer buf_48 = alloc_buf_gc(128, &data->gc);
237 uint8_t expected_header_48[8] = { 0xff, 0xfe, 0xfa, 0x07, 0x9a, 0xb9, 0xd2, 0xe9 };
239
240 /* test writing/checking the 48 bit per epoch packet counter
241 * overflow */
242 data->pis.id = 0xfffffffffffe;
243 struct buffer buf_of = alloc_buf_gc(128, &data->gc);
245 uint8_t expected_header_of[8] = { 0xf0, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
247
248 /* This is go over 2^48 - 1 and should error out. */
250
251 /* Now read back the packet ids and check if they are the same as what we
252 * have written */
253 struct packet_id_net pin;
254 assert_int_equal(packet_id_read_epoch(&pin, &buf), 0x23);
255 assert_int_equal(pin.id, 1);
256
257 assert_int_equal(packet_id_read_epoch(&pin, &buf_48), 0xfffe);
258 assert_int_equal(pin.id, 0xfa079ab9d2e9);
259
260 assert_int_equal(packet_id_read_epoch(&pin, &buf_of), 0xf00f);
261 assert_int_equal(pin.id, 0xffffffffffff);
262}
263
264static void
266{
267 struct reliable_ack ack = { .len = 4, .packet_id = { 2, 1, 3, 2 } };
268
269 struct reliable_ack mru_ack = { 0 };
270
271 /* Test copying to empty ack structure */
274 assert_int_equal(mru_ack.packet_id[0], 2);
275 assert_int_equal(mru_ack.packet_id[1], 1);
276 assert_int_equal(mru_ack.packet_id[2], 3);
277
278 /* Copying again should not change the result */
281 assert_int_equal(mru_ack.packet_id[0], 2);
282 assert_int_equal(mru_ack.packet_id[1], 1);
283 assert_int_equal(mru_ack.packet_id[2], 3);
284
285 /* Copying just the first two element should not change the order
286 * as they are still the most recent*/
289 assert_int_equal(mru_ack2.packet_id[0], 2);
290 assert_int_equal(mru_ack2.packet_id[1], 1);
291 assert_int_equal(mru_ack2.packet_id[2], 3);
292
293 /* Adding just two packets shoudl ignore the 42 in array and
294 * reorder the order in the MRU */
295 struct reliable_ack ack2 = { .len = 3, .packet_id = { 3, 2, 42 } };
297 assert_int_equal(mru_ack2.packet_id[0], 3);
298 assert_int_equal(mru_ack2.packet_id[1], 2);
299 assert_int_equal(mru_ack2.packet_id[2], 1);
300
301 /* Copying a zero array into it should also change nothing */
302 struct reliable_ack empty_ack = { .len = 0 };
305 assert_int_equal(mru_ack.packet_id[0], 2);
306 assert_int_equal(mru_ack.packet_id[1], 1);
307 assert_int_equal(mru_ack.packet_id[2], 3);
308
309 /* Or should just 0 elements of the ack */
312 assert_int_equal(mru_ack.packet_id[0], 2);
313 assert_int_equal(mru_ack.packet_id[1], 1);
314 assert_int_equal(mru_ack.packet_id[2], 3);
315
316 struct reliable_ack ack3 = { .len = 7, .packet_id = { 5, 6, 7, 8, 9, 10, 11 } };
317
318 /* Adding multiple acks tests if the a full array is handled correctly */
320
321 struct reliable_ack expected_ack = { .len = 8, .packet_id = { 5, 6, 7, 8, 9, 10, 11, 2 } };
323
324 assert_memory_equal(mru_ack.packet_id, expected_ack.packet_id, sizeof(expected_ack.packet_id));
325}
326
327int
328main(void)
329{
331 const struct CMUnitTest tests[] = {
332 cmocka_unit_test_setup_teardown(test_packet_id_write_short, test_packet_id_write_setup,
334 cmocka_unit_test_setup_teardown(test_packet_id_write_long, test_packet_id_write_setup,
336 cmocka_unit_test_setup_teardown(test_packet_id_write_short_prepend,
338 cmocka_unit_test_setup_teardown(test_packet_id_write_long_prepend,
340 cmocka_unit_test_setup_teardown(test_packet_id_write_short_wrap, test_packet_id_write_setup,
342 cmocka_unit_test_setup_teardown(test_packet_id_write_long_wrap, test_packet_id_write_setup,
344 cmocka_unit_test_setup_teardown(test_packet_id_write_epoch, test_packet_id_write_setup,
346
348 cmocka_unit_test(test_copy_acks_to_lru)
349
350 };
351
352 return cmocka_run_group_tests_name("packet_id tests", tests, NULL, NULL);
353}
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:89
#define BPTR(buf)
Definition buffer.h:123
static void gc_free(struct gc_arena *a)
Definition buffer.h:1015
static struct gc_arena gc_new(void)
Definition buffer.h:1007
void reliable_free(struct reliable *rel)
Free allocated memory associated with a reliable structure and the pointer itself.
Definition reliable.c:364
void reliable_init(struct reliable *rel, int buf_size, int offset, int array_size, bool hold)
Initialize a reliable structure.
Definition reliable.c:348
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:204
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:533
time_t now
Definition otime.c:33
uint16_t packet_id_read_epoch(struct packet_id_net *pin, struct buffer *buf)
Reads the packet ID containing both the epoch and the per-epoch counter from the buf.
Definition packet_id.c:647
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.
Definition packet_id.c:666
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:377
uint32_t packet_id_type
Definition packet_id.h:45
Reliability Layer module header file.
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
int capacity
Size in bytes of memory allocated by malloc().
Definition buffer.h:61
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:67
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
int offset
Offset in bytes of the actual content within the allocated memory.
Definition buffer.h:63
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
Data structure for describing the packet id that is received/send to the network.
Definition packet_id.h:191
uint64_t id
Definition packet_id.h:194
uint64_t id
Definition packet_id.h:153
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
Definition reliable.h:64
packet_id_type packet_id
Definition reliable.h:81
bool active
Definition reliable.h:78
The reliability layer storage structure for one VPN tunnel's control channel in one direction.
Definition reliable.h:94
struct reliable_entry array[RELIABLE_CAPACITY]
Definition reliable.h:100
packet_id_type packet_id
Definition reliable.h:97
struct packet_id_send pis
struct test_packet_id_write_data::@31 test_buf_data
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:35
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)