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
68 {
69  struct test_buffer_list_aggregate_ctx *ctx = calloc(1, sizeof(*ctx));
70  ctx->empty = buffer_list_new(0);
71 
76 
80 
82  uint8_t data = 0;
83  buffer_list_push_data(ctx->empty_buffers, &data, 0);
84  buffer_list_push_data(ctx->empty_buffers, &data, 0);
85 
86  *state = ctx;
87  return 0;
88 }
89 
90 static int
92 {
93  struct test_buffer_list_aggregate_ctx *ctx = *state;
94 
95  buffer_list_free(ctx->empty);
99  free(ctx);
100  return 0;
101 }
102 
103 static void
105 {
106  struct test_buffer_list_aggregate_ctx *ctx = *state;
107 
108  /* list full */
112 }
113 
114 static void
116 {
117  struct test_buffer_list_aggregate_ctx *ctx = *state;
118 
119  /* aggregating an empty buffer list results in an empty buffer list */
121  assert_null(ctx->empty->head);
122 }
123 
124 static void
126 {
127  struct test_buffer_list_aggregate_ctx *ctx = *state;
128 
129  /* With a max length of 2, no aggregation should take place */
132  struct buffer *buf = buffer_list_peek(ctx->one_two_three);
134 }
135 
136 static void
138 {
139  struct test_buffer_list_aggregate_ctx *ctx = *state;
140  const char *expected = teststr1 testsep teststr2 testsep;
141 
142  /* Aggregate the first two elements
143  * (add 1 to max_len to test if "three" is not sneaked in too)
144  */
145  buffer_list_aggregate_separator(ctx->one_two_three, strlen(expected) + 1,
146  testsep);
148  struct buffer *buf = buffer_list_peek(ctx->one_two_three);
149  assert_buf_equals_str(buf, expected);
150 }
151 
152 static void
154 {
155  struct test_buffer_list_aggregate_ctx *ctx = *state;
156 
157  /* Aggregate all */
160  struct buffer *buf = buffer_list_peek(ctx->one_two_three);
163 }
164 
165 static void
167 {
168  struct test_buffer_list_aggregate_ctx *ctx = *state;
169 
170  /* Aggregate all */
173  struct buffer *buf = buffer_list_peek(ctx->one_two_three);
175 }
176 
177 static void
179 {
180  struct test_buffer_list_aggregate_ctx *ctx = *state;
181  struct buffer_list *bl_zerolen = ctx->zero_length_strings;
182 
183  /* Aggregate all */
184  buffer_list_aggregate_separator(bl_zerolen, 1<<16, testnosep);
185  assert_int_equal(bl_zerolen->size, 1);
186  struct buffer *buf = buffer_list_peek(bl_zerolen);
187  assert_buf_equals_str(buf, "");
188 }
189 
190 static void
192 {
193  struct test_buffer_list_aggregate_ctx *ctx = *state;
194  struct buffer_list *bl_emptybuffers = ctx->empty_buffers;
195 
196  /* Aggregate all */
197  buffer_list_aggregate_separator(bl_emptybuffers, 1<<16, testnosep);
198  assert_int_equal(bl_emptybuffers->size, 1);
199  struct buffer *buf = buffer_list_peek(bl_emptybuffers);
200  assert_int_equal(BLEN(buf), 0);
201 }
202 
203 static void
205 {
206  struct gc_arena gc = gc_new();
207  struct buffer buf = alloc_buf_gc(1024, &gc);
208 
209  assert_ptr_equal(gc.list + 1, buf.data);
210  free_buf_gc(&buf, &gc);
211  assert_null(gc.list);
212 
213  gc_free(&gc);
214 }
215 
216 static void
218 {
219  struct gc_arena gc = gc_new();
220  struct buffer buf1 = alloc_buf_gc(1024, &gc);
221  struct buffer buf2 = alloc_buf_gc(1024, &gc);
222  struct buffer buf3 = alloc_buf_gc(1024, &gc);
223 
224  struct gc_entry *e;
225 
226  e = gc.list;
227 
228  assert_ptr_equal(e + 1, buf3.data);
229  assert_ptr_equal(e->next + 1, buf2.data);
230  assert_ptr_equal(e->next->next + 1, buf1.data);
231 
232  free_buf_gc(&buf2, &gc);
233 
234  assert_non_null(gc.list);
235 
236  while (e)
237  {
238  assert_ptr_not_equal(e + 1, buf2.data);
239  e = e->next;
240  }
241 
242  gc_free(&gc);
243 }
244 
245 int
246 main(void)
247 {
248  const struct CMUnitTest tests[] = {
276  };
277 
278  return cmocka_run_group_tests_name("buffer", tests, NULL, NULL);
279 }
#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:1198
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:1208
static void test_buffer_list_aggregate_separator_two(void **state)
Definition: test_buffer.c:137
#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:246
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:91
#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:67
static void test_buffer_free_gc_one(void **state)
Definition: test_buffer.c:204
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:178
static void test_buffer_list_aggregate_separator_noop(void **state)
Definition: test_buffer.c:125
#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:104
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:166
#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:159
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:1239
static void test_buffer_list_aggregate_separator_empty(void **state)
Definition: test_buffer.c:115
#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:153
#define testsep
Definition: test_buffer.c:48
static void free_buf_gc(struct buffer *buf, struct gc_arena *gc)
Definition: buffer.c:192
#define assert_false(c)
Definition: cmocka.h:1063
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition: buffer.c:1280
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:1293
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:1253
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:191
static void test_buffer_free_gc_two(void **state)
Definition: test_buffer.c:217