OpenVPN
test_buffer.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 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 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29 
30 #include "syshead.h"
31 
32 #include <setjmp.h>
33 #include <cmocka.h>
34 
35 #include "buffer.h"
36 #include "buffer.c"
37 
38 static void
39 test_buffer_strprefix(void **state)
40 {
41  assert_true(strprefix("123456", "123456"));
42  assert_true(strprefix("123456", "123"));
43  assert_true(strprefix("123456", ""));
44  assert_false(strprefix("123456", "456"));
45  assert_false(strprefix("12", "123"));
46 }
47 
48 #define testsep ","
49 #define testnosep ""
50 #define teststr1 "one"
51 #define teststr2 "two"
52 #define teststr3 "three"
53 #define teststr4 "four"
54 
55 #define assert_buf_equals_str(buf, str) \
56  assert_int_equal(BLEN(buf), strlen(str)); \
57  assert_memory_equal(BPTR(buf), str, BLEN(buf));
58 
60  struct buffer_list *empty;
64 };
65 
66 static int test_buffer_list_setup(void **state)
67 {
68  struct test_buffer_list_aggregate_ctx *ctx = calloc(1, sizeof(*ctx));
69  ctx->empty = buffer_list_new(0);
70 
75 
79 
81  uint8_t data = 0;
82  buffer_list_push_data(ctx->empty_buffers, &data, 0);
83  buffer_list_push_data(ctx->empty_buffers, &data, 0);
84 
85  *state = ctx;
86  return 0;
87 }
88 
89 static int test_buffer_list_teardown(void **state)
90 {
91  struct test_buffer_list_aggregate_ctx *ctx = *state;
92 
93  buffer_list_free(ctx->empty);
97  free(ctx);
98  return 0;
99 }
100 
101 static void
103 {
104  struct test_buffer_list_aggregate_ctx *ctx = *state;
105 
106  /* list full */
110 }
111 
112 static void
114 {
115  struct test_buffer_list_aggregate_ctx *ctx = *state;
116 
117  /* aggregating an empty buffer list results in an empty buffer list */
119  assert_null(ctx->empty->head);
120 }
121 
122 static void
124 {
125  struct test_buffer_list_aggregate_ctx *ctx = *state;
126 
127  /* With a max length of 2, no aggregation should take place */
130  struct buffer *buf = buffer_list_peek(ctx->one_two_three);
132 }
133 
134 static void
136 {
137  struct test_buffer_list_aggregate_ctx *ctx = *state;
138  const char *expected = teststr1 testsep teststr2 testsep;
139 
140  /* Aggregate the first two elements
141  * (add 1 to max_len to test if "three" is not sneaked in too)
142  */
143  buffer_list_aggregate_separator(ctx->one_two_three, strlen(expected) + 1,
144  testsep);
146  struct buffer *buf = buffer_list_peek(ctx->one_two_three);
147  assert_buf_equals_str(buf, expected);
148 }
149 
150 static void
152 {
153  struct test_buffer_list_aggregate_ctx *ctx = *state;
154 
155  /* Aggregate all */
158  struct buffer *buf = buffer_list_peek(ctx->one_two_three);
161 }
162 
163 static void
165 {
166  struct test_buffer_list_aggregate_ctx *ctx = *state;
167 
168  /* Aggregate all */
171  struct buffer *buf = buffer_list_peek(ctx->one_two_three);
173 }
174 
175 static void
177 {
178  struct test_buffer_list_aggregate_ctx *ctx = *state;
179  struct buffer_list *bl_zerolen = ctx->zero_length_strings;
180 
181  /* Aggregate all */
182  buffer_list_aggregate_separator(bl_zerolen, 1<<16, testnosep);
183  assert_int_equal(bl_zerolen->size, 1);
184  struct buffer *buf = buffer_list_peek(bl_zerolen);
185  assert_buf_equals_str(buf, "");
186 }
187 
188 static void
190 {
191  struct test_buffer_list_aggregate_ctx *ctx = *state;
192  struct buffer_list *bl_emptybuffers = ctx->empty_buffers;
193 
194  /* Aggregate all */
195  buffer_list_aggregate_separator(bl_emptybuffers, 1<<16, testnosep);
196  assert_int_equal(bl_emptybuffers->size, 1);
197  struct buffer *buf = buffer_list_peek(bl_emptybuffers);
198  assert_int_equal(BLEN(buf), 0);
199 }
200 
201 static void
203 {
204  struct gc_arena gc = gc_new();
205  struct buffer buf = alloc_buf_gc(1024, &gc);
206 
207  assert_ptr_equal(gc.list + 1, buf.data);
208  free_buf_gc(&buf, &gc);
209  assert_null(gc.list);
210 
211  gc_free(&gc);
212 }
213 
214 static void
216 {
217  struct gc_arena gc = gc_new();
218  struct buffer buf1 = alloc_buf_gc(1024, &gc);
219  struct buffer buf2 = alloc_buf_gc(1024, &gc);
220  struct buffer buf3 = alloc_buf_gc(1024, &gc);
221 
222  struct gc_entry *e;
223 
224  e = gc.list;
225 
226  assert_ptr_equal(e + 1, buf3.data);
227  assert_ptr_equal(e->next + 1, buf2.data);
228  assert_ptr_equal(e->next->next + 1, buf1.data);
229 
230  free_buf_gc(&buf2, &gc);
231 
232  assert_non_null(gc.list);
233 
234  while (e)
235  {
236  assert_ptr_not_equal(e + 1, buf2.data);
237  e = e->next;
238  }
239 
240  gc_free(&gc);
241 }
242 
243 int
244 main(void)
245 {
246  const struct CMUnitTest tests[] = {
274  };
275 
276  return cmocka_run_group_tests_name("buffer", tests, NULL, NULL);
277 }
#define assert_true(c)
Definition: cmocka.h:1045
#define assert_non_null(c)
Definition: cmocka.h:1102
#define cmocka_unit_test(f)
Initializes a CMUnitTest structure.
Definition: cmocka.h:1653
struct buffer_list * empty_buffers
Definition: test_buffer.c:63
#define teststr3
Definition: test_buffer.c:52
struct buffer_entry * head
Definition: buffer.h:1112
#define teststr4
Definition: test_buffer.c:53
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1023
struct buffer_list * buffer_list_new(const int max_size)
Allocate an empty buffer list of capacity max_size.
Definition: buffer.c:1201
struct gc_entry * next
Pointer to the next item in the linked list.
Definition: buffer.h:89
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition: buffer.c:1211
static void test_buffer_list_aggregate_separator_two(void **state)
Definition: test_buffer.c:135
#define assert_ptr_equal(a, b)
Definition: cmocka.h:1136
struct buffer_list * empty
Definition: test_buffer.c:60
int main(void)
Definition: test_buffer.c:244
static void test_buffer_strprefix(void **state)
Definition: test_buffer.c:39
#define assert_buf_equals_str(buf, str)
Definition: test_buffer.c:55
static int test_buffer_list_teardown(void **state)
Definition: test_buffer.c:89
#define teststr1
Definition: test_buffer.c:50
Garbage collection entry for one dynamically allocated block of memory.
Definition: buffer.h:87
#define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown)
Definition: cmocka.h:1818
static int test_buffer_list_setup(void **state)
Definition: test_buffer.c:66
static void test_buffer_free_gc_one(void **state)
Definition: test_buffer.c:202
static struct gc_arena gc_new(void)
Definition: buffer.h:1015
int size
Definition: buffer.h:1114
static void test_buffer_list_aggregate_separator_zerolen(void **state)
Definition: test_buffer.c:176
static void test_buffer_list_aggregate_separator_noop(void **state)
Definition: test_buffer.c:123
#define testnosep
Definition: test_buffer.c:49
struct gc_entry * list
First element of the linked list of gc_entry structures.
Definition: buffer.h:118
static void test_buffer_list_full(void **state)
Definition: test_buffer.c:102
struct buffer_list * zero_length_strings
Definition: test_buffer.c:62
uint8_t * data
Pointer to the allocated memory.
Definition: buffer.h:68
#define BLEN(buf)
Definition: buffer.h:127
static void test_buffer_list_aggregate_separator_nosep(void **state)
Definition: test_buffer.c:164
#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
unsigned __int8 uint8_t
Definition: config-msvc.h:123
void buffer_list_push(struct buffer_list *ol, const char *str)
Allocates and appends a new buffer containing str as data to ol.
Definition: buffer.c:1242
static void test_buffer_list_aggregate_separator_empty(void **state)
Definition: test_buffer.c:113
#define assert_ptr_not_equal(a, b)
Definition: cmocka.h:1155
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
#define teststr2
Definition: test_buffer.c:51
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition: buffer.c:90
#define assert_null(c)
Definition: cmocka.h:1119
struct buffer_list * one_two_three
Definition: test_buffer.c:61
#define free
Definition: cmocka.c:1850
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
static void test_buffer_list_aggregate_separator_all(void **state)
Definition: test_buffer.c:151
#define testsep
Definition: test_buffer.c:48
static void free_buf_gc(struct buffer *buf, struct gc_arena *gc)
Definition: buffer.c:195
#define assert_false(c)
Definition: cmocka.h:1063
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition: buffer.c:1283
void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, const char *sep)
Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len ...
Definition: buffer.c:1296
struct buffer_entry * buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size)
Allocates and appends a new buffer containing data of length size.
Definition: buffer.c:1256
static bool strprefix(const char *str, const char *prefix)
Return true iff str starts with prefix.
Definition: buffer.h:962
#define assert_int_equal(a, b)
Definition: cmocka.h:1174
static void test_buffer_list_aggregate_separator_emptybuffers(void **state)
Definition: test_buffer.c:189
static void test_buffer_free_gc_two(void **state)
Definition: test_buffer.c:215