-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathtest_buffer.c
499 lines (396 loc) · 16.7 KB
/
test_buffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <cmocka.h>
#include "common/buffer.h"
static void test_buffer_can_read(void **state) {
(void) state;
uint8_t temp[20] = {0};
buffer_t buf = {.ptr = temp, .size = sizeof(temp), .offset = 0};
assert_true(buffer_can_read(&buf, 20));
assert_true(buffer_seek_cur(&buf, 20));
assert_false(buffer_can_read(&buf, 1));
}
static void test_buffer_seek(void **state) {
(void) state;
uint8_t temp[20] = {0};
buffer_t buf = {.ptr = temp, .size = sizeof(temp), .offset = 0};
assert_true(buffer_can_read(&buf, 20));
assert_true(buffer_seek_cur(&buf, 20)); // seek at offset 20
assert_false(buffer_can_read(&buf, 1)); // can't read 1 byte
assert_false(buffer_seek_cur(&buf, 1)); // can't move at offset 21
assert_true(buffer_seek_end(&buf, 19));
assert_int_equal(buf.offset, 1);
assert_false(buffer_seek_end(&buf, 21)); // can't seek at offset -1
assert_true(buffer_seek_set(&buf, 10));
assert_int_equal(buf.offset, 10);
assert_false(buffer_seek_set(&buf, 21)); // can't seek at offset 21
}
static void test_buffer_get_cur(void **state) {
(void) state;
// clang-format off
uint8_t temp[6] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55
};
buffer_t buf = {.ptr = temp, .size = sizeof(temp), .offset = 0};
uint8_t *result;
result = buffer_get_cur(&buf);
assert_ptr_equal(temp, result);
buffer_seek_set(&buf, 3);
result = buffer_get_cur(&buf);
assert_ptr_equal(temp + 3, result);
buffer_seek_set(&buf, 5);
result = buffer_get_cur(&buf);
assert_ptr_equal(temp + 5, result);
}
static void test_buffer_read(void **state) {
(void) state;
// clang-format off
uint8_t temp[15] = {
0xFF,
0x01, 0x02,
0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E
};
buffer_t buf = {.ptr = temp, .size = sizeof(temp), .offset = 0};
uint8_t first = 0;
assert_true(buffer_read_u8(&buf, &first));
assert_int_equal(first, 255); // 0xFF
assert_true(buffer_seek_end(&buf, 0)); // seek at offset 19
assert_false(buffer_read_u8(&buf, &first)); // can't read 1 byte
uint16_t second = 0;
assert_true(buffer_seek_set(&buf, 1)); // set back to offset 1
assert_true(buffer_read_u16(&buf, &second, BE)); // big endian
assert_int_equal(second, 258); // 0x01 0x02
assert_true(buffer_seek_set(&buf, 1)); // set back to offset 1
assert_true(buffer_read_u16(&buf, &second, LE)); // little endian
assert_int_equal(second, 513); // 0x02 0x01
assert_true(buffer_seek_set(&buf, 14)); // seek at offset 14
assert_false(buffer_read_u16(&buf, &second, BE)); // can't read 2 bytes
uint32_t third = 0;
assert_true(buffer_seek_set(&buf, 3)); // set back to offset 3
assert_true(buffer_read_u32(&buf, &third, BE)); // big endian
assert_int_equal(third, 50595078); // 0x03 0x04 0x05 0x06
assert_true(buffer_seek_set(&buf, 3)); // set back to offset 3
assert_true(buffer_read_u32(&buf, &third, LE)); // little endian
assert_int_equal(third, 100992003); // 0x06 0x05 0x04 0x03
assert_true(buffer_seek_set(&buf, 12)); // seek at offset 12
assert_false(buffer_read_u32(&buf, &third, BE)); // can't read 4 bytes
uint64_t fourth = 0;
assert_true(buffer_seek_set(&buf, 7)); // set back to offset 7
assert_true(buffer_read_u64(&buf, &fourth, BE)); // big endian
assert_int_equal(fourth, 506664896818842894); // 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E
assert_true(buffer_seek_set(&buf, 7)); // set back to offset 7
assert_true(buffer_read_u64(&buf, &fourth, LE)); // little endian
assert_int_equal(fourth, 1012478732780767239); // 0x0E 0x0D 0x0C 0x0B 0x0A 0x09 0x08 0x07
assert_true(buffer_seek_set(&buf, 8)); // seek at offset 8
assert_false(buffer_read_u64(&buf, &fourth, BE)); // can't read 8 bytes
uint8_t bytes[32];
memset(bytes, 0x42, sizeof(bytes)); // we use 0x42 as marker for data that is left unchanged
assert_true(buffer_seek_set(&buf, 7)); // set back to offset 7
assert_true(buffer_read_bytes(&buf, bytes, 0)); // read zero bytes
assert_int_equal(bytes[0], 0x42);
memset(bytes, 0x42, sizeof(bytes));
assert_true(buffer_seek_set(&buf, 7)); // set back to offset 7
assert_true(buffer_read_bytes(&buf, bytes, 1));
assert_int_equal(bytes[0], 0x07);
assert_int_equal(bytes[1], 0x42);
memset(bytes, 0x42, sizeof(bytes));
assert_true(buffer_seek_set(&buf, 7)); // set back to offset 7
assert_true(buffer_read_bytes(&buf, bytes, 5));
assert_int_equal(bytes[0], 0x07);
assert_int_equal(bytes[1], 0x08);
assert_int_equal(bytes[2], 0x09);
assert_int_equal(bytes[3], 0x0A);
assert_int_equal(bytes[4], 0x0B);
assert_int_equal(bytes[5], 0x42);
// clang-format off
uint8_t temp_varint[] = {
0xFC, // 1 byte varint
0xFD, 0x00, 0x01, // 2 bytes varint
0xFE, 0x00, 0x01, 0x02, 0x03, // 4 bytes varint
0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 // 8 bytes varint
};
buffer_t buf_varint = {.ptr = temp_varint, .size = sizeof(temp_varint), .offset = 0};
uint64_t varint = 0;
assert_true(buffer_read_varint(&buf_varint, &varint));
assert_int_equal(varint, 0xFC);
assert_true(buffer_read_varint(&buf_varint, &varint));
assert_int_equal(varint, 0x0100);
assert_true(buffer_read_varint(&buf_varint, &varint));
assert_int_equal(varint, 0x03020100);
assert_true(buffer_read_varint(&buf_varint, &varint));
assert_int_equal(varint, 0x0706050403020100);
assert_false(buffer_read_varint(&buf_varint, &varint));
}
static void test_buffer_peek(void **state) {
(void) state;
uint8_t temp[6] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55
};
buffer_t buf = {.ptr = temp, .size = sizeof(temp), .offset = 0};
bool result;
uint8_t c;
result = buffer_peek(&buf, &c);
assert_true(result);
assert_int_equal(c, 0x00);
buf.offset += 3;
result = buffer_peek(&buf, &c);
assert_true(result);
assert_int_equal(c, 0x33);
buf.offset += 2;
result = buffer_peek(&buf, &c);
assert_true(result);
assert_int_equal(c, 0x55);
buf.offset += 1; // buffer is now empty
result = buffer_peek(&buf, &c);
assert_false(result);
assert_int_equal(c, 0x55); // unchanged because of failure
}
static void test_buffer_peek_n(void **state) {
(void) state;
uint8_t temp[6] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55
};
buffer_t buf = {.ptr = temp, .size = sizeof(temp), .offset = 0};
bool result;
uint8_t c;
for (int i = 0; i < 6; i++) {
result = buffer_peek_n(&buf, i, &c);
assert_true(result);
assert_int_equal(c, temp[i]);
}
c = 42;
result = buffer_peek_n(&buf, 6, &c); // past the end
assert_false(result);
assert_int_equal(c, 42); // c should not change on failure
buf.offset += 3;
for (int i = 0; i < 3; i++) {
result = buffer_peek_n(&buf, i, &c);
assert_true(result);
assert_int_equal(c, temp[3+i]);
}
c = 42;
result = buffer_peek_n(&buf, 4, &c); // past the end
assert_false(result);
assert_int_equal(c, 42); // c should not change on failure
}
static void test_buffer_write(void **state) {
(void) state;
uint8_t template[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
};
uint8_t data[sizeof(template)];
memcpy(data, template, sizeof(template));
buffer_t buf = {.ptr = data, .size = sizeof(data), .offset = 0};
// TEST buffer_write_u8
assert_true(buffer_write_u8(&buf, 42));
assert_int_equal(data[0], 42);
assert_int_equal(data[1], 0x01);
assert_int_equal(buf.offset, 1);
buffer_seek_end(&buf, 0);
assert_false(buffer_write_u8(&buf, 42));
assert_int_equal(buf.offset, buf.size);
buffer_seek_end(&buf, 1);
assert_true(buffer_write_u8(&buf, 42));
// reset data
memcpy(data, template, sizeof(template));
buffer_seek_set(&buf, 0);
// TEST buffer_write_u16
buffer_seek_set(&buf, 3);
assert_true(buffer_write_u16(&buf, 0x3344, BE));
assert_int_equal(data[2], 0x02);
assert_int_equal(data[3], 0x33);
assert_int_equal(data[4], 0x44);
assert_int_equal(data[5], 0x05);
assert_int_equal(buf.offset, 5);
buffer_seek_set(&buf, 3);
assert_true(buffer_write_u16(&buf, 0x3344, LE));
assert_int_equal(data[2], 0x02);
assert_int_equal(data[3], 0x44);
assert_int_equal(data[4], 0x33);
assert_int_equal(data[5], 0x05);
assert_int_equal(buf.offset, 5);
buffer_seek_end(&buf, 1);
assert_false(buffer_write_u16(&buf, 0x4242, BE)); // not enough space
assert_int_equal(data[sizeof(data) - 1], template[sizeof(data) - 1]); // shouldn't change data if not enough space
buffer_seek_end(&buf, 2);
assert_true(buffer_write_u16(&buf, 0x4242, BE)); // enough space this time
// reset data
memcpy(data, template, sizeof(template));
buffer_seek_set(&buf, 0);
// TEST buffer_write_u32
buffer_seek_set(&buf, 3);
assert_true(buffer_write_u32(&buf, 0x33445566, BE));
assert_int_equal(data[2], 0x02);
assert_int_equal(data[3], 0x33);
assert_int_equal(data[4], 0x44);
assert_int_equal(data[5], 0x55);
assert_int_equal(data[6], 0x66);
assert_int_equal(data[7], 0x07);
assert_int_equal(buf.offset, 7);
buffer_seek_set(&buf, 3);
assert_true(buffer_write_u32(&buf, 0x33445566, LE));
assert_int_equal(data[2], 0x02);
assert_int_equal(data[3], 0x66);
assert_int_equal(data[4], 0x55);
assert_int_equal(data[5], 0x44);
assert_int_equal(data[6], 0x33);
assert_int_equal(data[7], 0x07);
assert_int_equal(buf.offset, 7);
buffer_seek_end(&buf, 3);
assert_false(buffer_write_u32(&buf, 0x42424242, BE)); // not enough space
assert_int_equal(data[sizeof(data) - 1], template[sizeof(data) - 1]); // shouldn't change data if not enough space
buffer_seek_end(&buf, 4);
assert_true(buffer_write_u32(&buf, 0x42424242, BE)); // enough space this time
// reset data
memcpy(data, template, sizeof(template));
buffer_seek_set(&buf, 0);
// TEST buffer_write_u64
buffer_seek_set(&buf, 3);
assert_true(buffer_write_u64(&buf, 0x33445566778899aaULL, BE));
assert_int_equal(data[2], 0x02);
assert_int_equal(data[3], 0x33);
assert_int_equal(data[4], 0x44);
assert_int_equal(data[5], 0x55);
assert_int_equal(data[6], 0x66);
assert_int_equal(data[7], 0x77);
assert_int_equal(data[8], 0x88);
assert_int_equal(data[9], 0x99);
assert_int_equal(data[10], 0xaa);
assert_int_equal(data[11], 0x0b);
assert_int_equal(buf.offset, 11);
buffer_seek_set(&buf, 3);
assert_true(buffer_write_u64(&buf, 0x33445566778899aaULL, LE));
assert_int_equal(data[2], 0x02);
assert_int_equal(data[3], 0xaa);
assert_int_equal(data[4], 0x99);
assert_int_equal(data[5], 0x88);
assert_int_equal(data[6], 0x77);
assert_int_equal(data[7], 0x66);
assert_int_equal(data[8], 0x55);
assert_int_equal(data[9], 0x44);
assert_int_equal(data[10], 0x33);
assert_int_equal(data[11], 0x0b);
assert_int_equal(buf.offset, 11);
buffer_seek_end(&buf, 7);
assert_false(buffer_write_u64(&buf, 0x4242424242424242ULL, BE)); // not enough space
assert_int_equal(data[sizeof(data) - 1], template[sizeof(data) - 1]); // shouldn't change data if not enough space
buffer_seek_end(&buf, 8);
assert_true(buffer_write_u64(&buf, 0x4242424242424242ULL, BE)); // enough space this time
}
static void test_buffer_create(void **state) {
(void) state;
uint8_t data[32];
buffer_t buffer = buffer_create(data, 15);
assert_ptr_equal(buffer.ptr, data);
assert_int_equal(buffer.size, 15);
assert_int_equal(buffer.offset, 0);
}
static void test_buffer_alloc(void **state) {
(void) state;
// declare as uint32 to make sure it is aligned in memory
uint32_t data_uint32[32];
uint8_t *data = (uint8_t *)data_uint32;
buffer_t buf;
void *result;
// tests with aligned memory buffer
for (int size = 1; size <= 10; size++) {
buf = buffer_create(data, 32 * sizeof(uint32_t));
result = buffer_alloc(&buf, size, false);
assert_ptr_equal(result, data);
assert_int_equal(buf.offset, size);
buf = buffer_create(data, 32 * sizeof(uint32_t));
// aligned = true doesn't make a difference, since the buffer is aligned
result = buffer_alloc(&buf, 1, true);
assert_ptr_equal(result, data);
assert_int_equal(buf.offset, 1);
}
// unaligned memory buffer, by 1 to 3 bytes
for (int offset = 1; offset <= 3; offset++) {
for (int size = 1; size <= 10; size++) {
buf = buffer_create(data + offset, 32 * sizeof(uint32_t) - offset);
result = buffer_alloc(&buf, size, false);
assert_ptr_equal(result, data + offset);
assert_int_equal(buf.offset, size);
buf = buffer_create(data + offset, 32 * sizeof(uint32_t) - offset);
// aligned = true doesn't make a difference, since the buffer is aligned
result = buffer_alloc(&buf, size, true);
assert_ptr_equal(result, data + 4);
assert_int_equal(buf.offset, (4 - offset) + size);
}
}
// can allocate the whole buffer
buf = buffer_create(data, 7);
result = buffer_alloc(&buf, 7, false);
assert_ptr_equal(result, data);
assert_int_equal(buf.offset, 7);
// test with buffer too small
buf = buffer_create(data, 7);
result = buffer_alloc(&buf, 8, false);
assert_ptr_equal(result, NULL);
assert_int_equal(buf.offset, 0);
// test with buffer too small (can only allocate 3 bytes because 3 are lost because of the memory alignment)
buf = buffer_create(data + 1, 6);
result = buffer_alloc(&buf, 4, true);
assert_ptr_equal(result, NULL);
assert_int_equal(buf.offset, 0);
// allocate maximum size, accounting for memory alignment
buf = buffer_create(data + 1, 7);
result = buffer_alloc(&buf, 3, true);
assert_ptr_equal(result, data+4);
assert_int_equal(buf.offset, 3+3);
}
static void test_buffer_is_cur_aligned(void **state) {
(void) state;
uint8_t data[32] __attribute__ ((aligned (4)));
buffer_t buf = buffer_create(data + 2, sizeof(data) - 2);
assert_false(buffer_is_cur_aligned(&buf)); //2
buffer_seek_cur(&buf, 1);
assert_false(buffer_is_cur_aligned(&buf)); //3
buffer_seek_cur(&buf, 1);
assert_true(buffer_is_cur_aligned(&buf)); //4
buffer_seek_cur(&buf, 1);
assert_false(buffer_is_cur_aligned(&buf)); //5
buffer_seek_cur(&buf, 1);
assert_false(buffer_is_cur_aligned(&buf)); //6
buffer_seek_cur(&buf, 1);
assert_false(buffer_is_cur_aligned(&buf)); //7
buffer_seek_cur(&buf, 1);
assert_true(buffer_is_cur_aligned(&buf)); //8
buffer_seek_cur(&buf, 1);
assert_false(buffer_is_cur_aligned(&buf)); //9
}
// tests the buffer_snapshot/buffer_restore functions
static void test_buffer_snapshot_restore(void **state) {
(void) state;
uint8_t data[32];
buffer_snapshot_t snap;
buffer_t buf;
buffer_t buf_correct;
buf = buffer_create(data, sizeof(data));
buf_correct = buf;
snap = buffer_snapshot(&buf);
buffer_alloc(&buf, 11, false);
buffer_restore(&buf, snap);
assert_int_equal(buf.offset, buf_correct.offset);
assert_ptr_equal(buf.ptr, buf_correct.ptr);
assert_int_equal(buf.size, buf_correct.size);
}
int main() {
const struct CMUnitTest tests[] = {cmocka_unit_test(test_buffer_can_read),
cmocka_unit_test(test_buffer_seek),
cmocka_unit_test(test_buffer_get_cur),
cmocka_unit_test(test_buffer_read),
cmocka_unit_test(test_buffer_peek),
cmocka_unit_test(test_buffer_peek_n),
cmocka_unit_test(test_buffer_write),
cmocka_unit_test(test_buffer_create),
cmocka_unit_test(test_buffer_alloc),
cmocka_unit_test(test_buffer_is_cur_aligned),
cmocka_unit_test(test_buffer_snapshot_restore)};
return cmocka_run_group_tests(tests, NULL, NULL);
}