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-2018 Fox Crypto B.V. <openvpn@fox-it.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 {
46  } test_buf_data;
47  struct buffer test_buf;
49 };
50 
51 static int
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 
64  *state = data;
65  return 0;
66 }
67 
68 static int
70  free(*state);
71  return 0;
72 }
73 
74 static void
76 {
77  struct test_packet_id_write_data *data = *state;
78 
79  now = 5010;
80  assert_true(packet_id_write(&data->pis, &data->test_buf, false, false));
81  assert_true(data->pis.id == 1);
82  assert_true(data->test_buf_data.buf_id == htonl(1));
84 }
85 
86 static void
88 {
89  struct test_packet_id_write_data *data = *state;
90 
91  now = 5010;
92  assert_true(packet_id_write(&data->pis, &data->test_buf, true, false));
93  assert(data->pis.id == 1);
94  assert(data->pis.time == now);
95  assert_true(data->test_buf_data.buf_id == htonl(1));
96  assert_true(data->test_buf_data.buf_time == htonl(now));
97 }
98 
99 static void
101 {
102  struct test_packet_id_write_data *data = *state;
103 
104  data->test_buf.offset = sizeof(packet_id_type);
105  now = 5010;
106  assert_true(packet_id_write(&data->pis, &data->test_buf, false, true));
107  assert_true(data->pis.id == 1);
108  assert_true(data->test_buf_data.buf_id == htonl(1));
109  assert_true(data->test_buf_data.buf_time == 0);
110 }
111 
112 static void
114 {
115  struct test_packet_id_write_data *data = *state;
116 
117  data->test_buf.offset = sizeof(data->test_buf_data);
118  now = 5010;
119  assert_true(packet_id_write(&data->pis, &data->test_buf, true, true));
120  assert(data->pis.id == 1);
121  assert(data->pis.time == now);
122  assert_true(data->test_buf_data.buf_id == htonl(1));
123  assert_true(data->test_buf_data.buf_time == htonl(now));
124 }
125 
126 static void
128 {
129  struct test_packet_id_write_data *data = *state;
130 
131  data->pis.id = ~0;
132  assert_false(packet_id_write(&data->pis, &data->test_buf, false, false));
133 }
134 
135 static void
137 {
138  struct test_packet_id_write_data *data = *state;
139 
140  data->pis.id = ~0;
141  data->pis.time = 5006;
142 
143  /* Write fails if time did not change */
144  now = 5006;
145  assert_false(packet_id_write(&data->pis, &data->test_buf, true, false));
146 
147  /* Write succeeds if time moved forward */
148  now = 5010;
149  assert_true(packet_id_write(&data->pis, &data->test_buf, true, false));
150 
151  assert(data->pis.id == 1);
152  assert(data->pis.time == now);
153  assert_true(data->test_buf_data.buf_id == htonl(1));
154  assert_true(data->test_buf_data.buf_time == htonl(now));
155 }
156 
157 int
158 main(void) {
159  const struct CMUnitTest tests[] = {
172  };
173 
174  return cmocka_run_group_tests_name("packet_id tests", tests, NULL, NULL);
175 }
static void test_packet_id_write_long_prepend(void **state)
#define assert_true(c)
Definition: cmocka.h:1045
struct test_packet_id_write_data::@13 test_buf_data
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:349
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
unsigned __int32 uint32_t
Definition: config-msvc.h:121
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