unit-tests: Move test suites to its own subfolder
[strongswan.git] / src / libstrongswan / tests / suites / test_bio_writer.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "test_suite.h"
17
18 #include <bio/bio_writer.h>
19
20 /*******************************************************************************
21 * different integer writes
22 */
23
24 static inline void verify_int_buffer(chunk_t data, int bits, int val)
25 {
26 size_t i;
27 int len = bits / 8;
28
29 ck_assert_int_eq(data.len, (val + 1) * len);
30 for (i = 0; i < data.len; i++)
31 {
32 if ((i + 1) % len)
33 {
34 ck_assert_int_eq(data.ptr[i], 0);
35 }
36 else
37 {
38 ck_assert_int_eq(data.ptr[i], i / len);
39 }
40 }
41 }
42
43 #define assert_integer_write(init, bits) ({ \
44 int i; \
45 bio_writer_t *writer = bio_writer_create(init); \
46 for (i = 0; i < 16; i++) \
47 { \
48 writer->write_uint##bits(writer, i); \
49 verify_int_buffer(writer->get_buf(writer), bits, i); \
50 } \
51 writer->destroy(writer); \
52 })
53
54 START_TEST(test_write_uint8)
55 {
56 /* use default buffer (and increase) size */
57 assert_integer_write(0, 8);
58 /* force a resize by the given size */
59 assert_integer_write(1, 8);
60 }
61 END_TEST
62
63 START_TEST(test_write_uint16)
64 {
65 assert_integer_write(0, 16);
66 assert_integer_write(1, 16);
67 }
68 END_TEST
69
70 START_TEST(test_write_uint24)
71 {
72 assert_integer_write(0, 24);
73 assert_integer_write(1, 24);
74 }
75 END_TEST
76
77 START_TEST(test_write_uint32)
78 {
79 assert_integer_write(0, 32);
80 assert_integer_write(1, 32);
81 }
82 END_TEST
83
84 START_TEST(test_write_uint64)
85 {
86 assert_integer_write(0, 64);
87 assert_integer_write(1, 64);
88 }
89 END_TEST
90
91 /*******************************************************************************
92 * write data / skip
93 */
94
95 static inline void assert_writer_after_write(bio_writer_t *writer, int count)
96 {
97 chunk_t buf;
98 size_t i;
99
100 buf = writer->get_buf(writer);
101 ck_assert_int_eq(buf.len, count * 3);
102 for (i = 0; i < buf.len; i++)
103 {
104 ck_assert(buf.ptr[i] == i % 3);
105 }
106 }
107
108 START_TEST(test_write_data)
109 {
110 chunk_t buf, data = chunk_from_chars(0x00, 0x01, 0x02);
111 bio_writer_t *writer;
112
113 /* no allocation, but default buffer size */
114 writer = bio_writer_create(0);
115 buf = writer->get_buf(writer);
116 ck_assert_int_eq(buf.len, 0);
117 ck_assert(buf.ptr == NULL);
118
119 writer->write_data(writer, chunk_empty);
120 buf = writer->get_buf(writer);
121 ck_assert_int_eq(buf.len, 0);
122 ck_assert(buf.ptr == NULL);
123 writer->destroy(writer);
124
125 /* custom buffer size, initial buffer allocated */
126 writer = bio_writer_create(1);
127 buf = writer->get_buf(writer);
128 ck_assert_int_eq(buf.len, 0);
129 ck_assert(buf.ptr != NULL);
130
131 writer->write_data(writer, chunk_empty);
132 buf = writer->get_buf(writer);
133 ck_assert_int_eq(buf.len, 0);
134 ck_assert(buf.ptr != NULL);
135 writer->destroy(writer);
136
137 writer = bio_writer_create(0);
138
139 writer->write_data(writer, data);
140 assert_writer_after_write(writer, 1);
141
142 writer->write_data(writer, data);
143 assert_writer_after_write(writer, 2);
144
145 writer->write_data(writer, data);
146 assert_writer_after_write(writer, 3);
147
148 writer->destroy(writer);
149 }
150 END_TEST
151
152 START_TEST(test_skip)
153 {
154 chunk_t skipped, buf, data = chunk_from_chars(0x00, 0x01, 0x02);
155 bio_writer_t *writer;
156
157 writer = bio_writer_create(4);
158 skipped = writer->skip(writer, 3);
159 ck_assert_int_eq(skipped.len, 3);
160 buf = writer->get_buf(writer);
161 ck_assert(skipped.ptr == buf.ptr);
162 memset(skipped.ptr, 0, skipped.len);
163
164 writer->write_data(writer, data);
165 buf = writer->get_buf(writer);
166 ck_assert(chunk_equals(buf, chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x01, 0x02)));
167 writer->destroy(writer);
168
169 writer = bio_writer_create(1);
170 skipped = writer->skip(writer, 3);
171 memcpy(skipped.ptr, data.ptr, data.len);
172
173 writer->write_data(writer, data);
174 assert_writer_after_write(writer, 2);
175 writer->destroy(writer);
176 }
177 END_TEST
178
179 /*******************************************************************************
180 * write length followed by data
181 */
182
183 #define assert_write_data_len(init, bits) ({ \
184 bio_writer_t *writer; \
185 chunk_t buf, data; \
186 int i, len = bits / 8; \
187 writer = bio_writer_create(init); \
188 writer->write_data##bits(writer, chunk_empty); \
189 buf = writer->get_buf(writer); \
190 ck_assert_int_eq(buf.len, len); \
191 ck_assert_int_eq(buf.ptr[len - 1], 0); \
192 writer->destroy(writer); \
193 data = chunk_alloca(32); \
194 memset(data.ptr, 0, data.len); \
195 for (i = 0; i < 32; i++) \
196 { \
197 data.ptr[i] = i; \
198 data.len = i; \
199 writer = bio_writer_create(init); \
200 writer->write_data##bits(writer, data); \
201 buf = writer->get_buf(writer); \
202 ck_assert_int_eq(buf.len, len + i); \
203 ck_assert_int_eq(buf.ptr[len - 1], i); \
204 ck_assert(chunk_equals(chunk_create(buf.ptr + len, buf.len - len), data)); \
205 writer->destroy(writer); \
206 } \
207 })
208
209 START_TEST(test_write_data8)
210 {
211 assert_write_data_len(0, 8);
212 assert_write_data_len(1, 8);
213 }
214 END_TEST
215
216 START_TEST(test_write_data16)
217 {
218 assert_write_data_len(0, 16);
219 assert_write_data_len(1, 16);
220 }
221 END_TEST
222
223 START_TEST(test_write_data24)
224 {
225 assert_write_data_len(0, 24);
226 assert_write_data_len(1, 24);
227 }
228 END_TEST
229
230 START_TEST(test_write_data32)
231 {
232 assert_write_data_len(0, 32);
233 assert_write_data_len(1, 32);
234 }
235 END_TEST
236
237
238 /*******************************************************************************
239 * add length header before current data
240 */
241
242 #define assert_wrap_data(init, bits) ({ \
243 bio_writer_t *writer; \
244 chunk_t buf, data; \
245 int i, len = bits / 8; \
246 writer = bio_writer_create(init); \
247 writer->wrap##bits(writer); \
248 buf = writer->get_buf(writer); \
249 ck_assert_int_eq(buf.len, len); \
250 ck_assert_int_eq(buf.ptr[len - 1], 0); \
251 writer->destroy(writer); \
252 data = chunk_alloca(32); \
253 memset(data.ptr, 0, data.len); \
254 for (i = 0; i < 32; i++) \
255 { \
256 data.ptr[i] = i; \
257 data.len = i; \
258 writer = bio_writer_create(init); \
259 writer->write_data(writer, data); \
260 writer->wrap##bits(writer); \
261 buf = writer->get_buf(writer); \
262 ck_assert_int_eq(buf.len, len + i); \
263 ck_assert_int_eq(buf.ptr[len - 1], i); \
264 ck_assert(chunk_equals(chunk_create(buf.ptr + len, buf.len - len), data)); \
265 writer->wrap##bits(writer); \
266 buf = writer->get_buf(writer); \
267 ck_assert_int_eq(buf.len, 2 * len + i); \
268 ck_assert_int_eq(buf.ptr[len - 1], len + i); \
269 ck_assert(chunk_equals(chunk_create(buf.ptr + 2 * len, buf.len - 2 * len), data)); \
270 writer->destroy(writer); \
271 } \
272 })
273
274 START_TEST(test_wrap8)
275 {
276 assert_wrap_data(0, 8);
277 assert_wrap_data(1, 8);
278 }
279 END_TEST
280
281 START_TEST(test_wrap16)
282 {
283 assert_wrap_data(0, 16);
284 assert_wrap_data(1, 16);
285 }
286 END_TEST
287
288 START_TEST(test_wrap24)
289 {
290 assert_wrap_data(0, 24);
291 assert_wrap_data(1, 24);
292 }
293 END_TEST
294
295 START_TEST(test_wrap32)
296 {
297 assert_wrap_data(0, 32);
298 assert_wrap_data(1, 32);
299 }
300 END_TEST
301
302 /*******************************************************************************
303 * test data extraction
304 */
305
306 START_TEST(test_get_buf)
307 {
308 bio_writer_t *writer;
309 chunk_t data1, data2;
310
311 writer = bio_writer_create(0);
312 writer->write_uint8(writer, 1);
313 data1 = writer->get_buf(writer);
314 ck_assert_int_eq(data1.len, 1);
315 ck_assert(data1.ptr[0] == 1);
316
317 data2 = writer->get_buf(writer);
318 ck_assert(chunk_equals(data1, data2));
319 ck_assert(data1.ptr == data2.ptr);
320 writer->destroy(writer);
321 }
322 END_TEST
323
324 START_TEST(test_extract_buf)
325 {
326 bio_writer_t *writer;
327 chunk_t data1, data2;
328
329 writer = bio_writer_create(0);
330 writer->write_uint8(writer, 1);
331 data1 = writer->extract_buf(writer);
332 ck_assert_int_eq(data1.len, 1);
333 ck_assert(data1.ptr[0] == 1);
334
335 data2 = writer->get_buf(writer);
336 ck_assert_int_eq(data2.len, 0);
337 ck_assert(data2.ptr == NULL);
338 data2 = writer->extract_buf(writer);
339 ck_assert_int_eq(data2.len, 0);
340 ck_assert(data2.ptr == NULL);
341
342 writer->write_uint8(writer, 1);
343 data2 = writer->get_buf(writer);
344 ck_assert(chunk_equals(data1, data2));
345 ck_assert(data1.ptr != data2.ptr);
346
347 writer->destroy(writer);
348 chunk_free(&data1);
349 }
350 END_TEST
351
352 Suite *bio_writer_suite_create()
353 {
354 Suite *s;
355 TCase *tc;
356
357 s = suite_create("bio_writer");
358
359 tc = tcase_create("integer writes");
360 tcase_add_test(tc, test_write_uint8);
361 tcase_add_test(tc, test_write_uint16);
362 tcase_add_test(tc, test_write_uint24);
363 tcase_add_test(tc, test_write_uint32);
364 tcase_add_test(tc, test_write_uint64);
365 suite_add_tcase(s, tc);
366
367 tc = tcase_create("data writes/skip");
368 tcase_add_test(tc, test_write_data);
369 tcase_add_test(tc, test_skip);
370 suite_add_tcase(s, tc);
371
372 tc = tcase_create("data length writes");
373 tcase_add_test(tc, test_write_data8);
374 tcase_add_test(tc, test_write_data16);
375 tcase_add_test(tc, test_write_data24);
376 tcase_add_test(tc, test_write_data32);
377 suite_add_tcase(s, tc);
378
379 tc = tcase_create("wrap writes");
380 tcase_add_test(tc, test_wrap8);
381 tcase_add_test(tc, test_wrap16);
382 tcase_add_test(tc, test_wrap24);
383 tcase_add_test(tc, test_wrap32);
384 suite_add_tcase(s, tc);
385
386 tc = tcase_create("get/extract");
387 tcase_add_test(tc, test_get_buf);
388 tcase_add_test(tc, test_extract_buf);
389 suite_add_tcase(s, tc);
390
391 return s;
392 }