-
Notifications
You must be signed in to change notification settings - Fork 74
/
test_parser.c
322 lines (243 loc) · 10.3 KB
/
test_parser.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
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <cmocka.h>
#include "common/parser.h"
// An example parser that reads an uint32_t, an array of 8 bytes, and an uint8_t.
typedef struct {
uint32_t a;
uint8_t b[8];
uint8_t c;
} parse_ABC_state_t;
static int parse_A(parse_ABC_state_t *state, buffer_t *buffers[2]) {
return dbuffer_read_u32(buffers, &state->a, BE);
}
static int parse_B(parse_ABC_state_t *state, buffer_t *buffers[2]) {
return dbuffer_read_bytes(buffers, state->b, 8);
}
static int parse_C(parse_ABC_state_t *state, buffer_t *buffers[2]) {
return dbuffer_read_u8(buffers, &state->c);
}
const parsing_step_t parse_ABC_steps[] = {(parsing_step_t) parse_A,
(parsing_step_t) parse_B,
(parsing_step_t) parse_C};
const int n_ABC_STEPS = sizeof(parse_ABC_steps) / sizeof(parse_ABC_steps[0]);
// A function that simulates a parsing error while parsing B
static int parse_B_error(parse_ABC_state_t *state, buffer_t *buffers[2]) {
return -1;
}
// A parser similar to parse_ABC, but always generates an error in the second parsing step
const parsing_step_t parse_ABC_error_steps[] = {(parsing_step_t) parse_A,
(parsing_step_t) parse_B_error,
(parsing_step_t) parse_C};
static void test_parser_init_context(void **state) {
(void) state;
parser_context_t parser_context;
parser_context.cur_step = 42;
parser_context.state = NULL;
parse_ABC_state_t parser_state;
parser_init_context(&parser_context, &parser_state);
assert_int_equal(parser_context.cur_step, 0);
assert_ptr_equal(parser_context.state, &parser_state);
}
static void test_parser_oneshot_from_store(void **state) {
(void) state;
// in this test, all the data is fetched from the store
// clang-format off
uint8_t store[32] = {
0xa0, 0xa1, 0xa2, 0xa3,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
0xc0
};
// clang-format on
uint8_t stream[32] = {0};
buffer_t store_buf = buffer_create(store, sizeof(store));
buffer_t stream_buf = buffer_create(stream, sizeof(stream));
buffer_t *buffers[2] = {&store_buf, &stream_buf};
parse_ABC_state_t parser_state;
parser_context_t parser_context;
memset(&parser_state, 0, sizeof(parser_state));
parser_init_context(&parser_context, &parser_state);
int result = parser_run(parse_ABC_steps, n_ABC_STEPS, &parser_context, buffers, NULL);
assert_int_equal(result, 1);
assert_int_equal(parser_context.cur_step, n_ABC_STEPS); // parsing completed
// Check buffer updates
assert_int_equal(store_buf.offset, 4 + 8 + 1);
assert_int_equal(stream_buf.offset, 0);
// Check expected parsing results
assert_int_equal(parser_state.a, 0xa0a1a2a3);
for (int i = 0; i < 8; i++) {
assert_int_equal(parser_state.b[i], 0xb0 + i);
}
assert_int_equal(parser_state.c, 0xc0);
}
static void test_parser_oneshot_with_empty_store(void **state) {
(void) state;
// in this test, the store is empty all the data is fetched from the stream
uint8_t store[8];
// clang-format off
uint8_t stream[32] = {
0xa0, 0xa1, 0xa2, 0xa3,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
0xc0
};
// clang-format on
buffer_t store_buf = buffer_create(store, 0);
buffer_t stream_buf = buffer_create(stream, 4 + 8 + 1);
buffer_t *buffers[2] = {&store_buf, &stream_buf};
parse_ABC_state_t parser_state;
parser_context_t parser_context;
memset(&parser_state, 0, sizeof(parser_state));
parser_init_context(&parser_context, &parser_state);
int result = parser_run(parse_ABC_steps, n_ABC_STEPS, &parser_context, buffers, NULL);
assert_int_equal(result, 1);
assert_int_equal(parser_context.cur_step, n_ABC_STEPS); // parsing completed
// Check buffer updates
assert_int_equal(store_buf.offset, 0);
assert_int_equal(stream_buf.offset, 4 + 8 + 1);
// Check expected parsing results
assert_int_equal(parser_state.a, 0xa0a1a2a3);
for (int i = 0; i < 8; i++) {
assert_int_equal(parser_state.b[i], 0xb0 + i);
}
assert_int_equal(parser_state.c, 0xc0);
}
static void test_parser_oneshot_part_store_part_stream(void **state) {
(void) state;
// in this test, some data comes from the stream, some from the
// clang-format off
uint8_t store[32] = {
0, 0, 0, 0, 0, // some initial bytes that should be ignored
0xa0, 0xa1, 0xa2, 0xa3,
0xb0, 0xb1 // partial parsing
};
uint8_t stream[32] = {
0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, // continuation of the parsing
0xc0
};
// clang-format on
buffer_t store_buf = buffer_create(store, 5 + 4 + 2); // size includes the initial zeros
buffer_seek_cur(&store_buf, 5); // skip initial zeros
buffer_t stream_buf = buffer_create(stream, 6 + 1);
buffer_t *buffers[2] = {&store_buf, &stream_buf};
parse_ABC_state_t parser_state;
parser_context_t parser_context;
memset(&parser_state, 0, sizeof(parser_state));
parser_init_context(&parser_context, &parser_state);
int result = parser_run(parse_ABC_steps, n_ABC_STEPS, &parser_context, buffers, NULL);
assert_int_equal(result, 1);
assert_int_equal(parser_context.cur_step, n_ABC_STEPS); // parsing completed
// Check buffer updates
assert_int_equal(store_buf.offset, 5 + 4 + 2);
assert_int_equal(stream_buf.offset, 6 + 1);
// Check expected parsing results
assert_int_equal(parser_state.a, 0xa0a1a2a3);
for (int i = 0; i < 8; i++) {
assert_int_equal(parser_state.b[i], 0xb0 + i);
}
assert_int_equal(parser_state.c, 0xc0);
}
static void test_parser_stream_ends(void **state) {
(void) state;
// in this test, the stream is exhausted before parsing is complete
// clang-format off
uint8_t store[32] = {
0, 0, 0, 0, 0, // some initial bytes that should be ignored
0xa0, 0xa1, 0xa2, 0xa3,
0xb0, 0xb1 // partial parsing
};
uint8_t stream[32] = {
0xb2, 0xb3, 0xb4, 0xb5, 0xb6 // stream ends while parsing the B field
};
// clang-format on
buffer_t store_buf = buffer_create(store, 5 + 4 + 2); // size includes the initial zeros
buffer_seek_cur(&store_buf, 5); // skip initial zeros
buffer_t stream_buf = buffer_create(stream, 5);
buffer_t *buffers[2] = {&store_buf, &stream_buf};
parse_ABC_state_t parser_state;
parser_context_t parser_context;
memset(&parser_state, 0, sizeof(parser_state));
parser_init_context(&parser_context, &parser_state);
int result = parser_run(parse_ABC_steps, n_ABC_STEPS, &parser_context, buffers, NULL);
assert_int_equal(result, 0);
assert_int_equal(parser_context.cur_step, 1); // second parsing step was not completed
// Check buffer updates
assert_int_equal(store_buf.offset, 5 + 4); // affset after reading A
assert_int_equal(stream_buf.offset,
0); // since read failed, the offset should not have changed
// Check expected parsing results
assert_int_equal(parser_state.a, 0xa0a1a2a3);
for (int i = 0; i < 8; i++) {
assert_int_equal(parser_state.b[i], 0); // not parsed yet
}
assert_int_equal(parser_state.c, 0); // not parsed yet
}
static void test_parser_continue_partial(void **state) {
(void) state;
// this tests completes the parsing from the point it left in the previous test
uint8_t store[32] = {
0xb0,
0xb1 // leftover from before
};
uint8_t stream[32] = {0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xc0};
buffer_t store_buf = buffer_create(store, 2);
buffer_t stream_buf = buffer_create(stream, 6 + 1);
buffer_t *buffers[2] = {&store_buf, &stream_buf};
parse_ABC_state_t parser_state = {.a = 0xa0a1a2a3, .b = {0}, .c = 0};
parser_context_t parser_context;
parser_context.state = &parser_state;
parser_context.cur_step = 1; // restart from step_B
int result = parser_run(parse_ABC_steps, n_ABC_STEPS, &parser_context, buffers, NULL);
assert_int_equal(result, 1);
assert_int_equal(parser_context.cur_step, 3); // completed
// Check buffer updates
assert_int_equal(store_buf.offset, 2); // affset after reading A
assert_int_equal(stream_buf.offset,
6 + 1); // since read failed, the offset should not have changed
// Check expected parsing results
assert_int_equal(parser_state.a, 0xa0a1a2a3);
for (int i = 0; i < 8; i++) {
assert_int_equal(parser_state.b[i], 0xb0 + i);
}
assert_int_equal(parser_state.c, 0xc0);
}
static void test_parser_error(void **state) {
(void) state;
// in this test, the second parsing stap causes an error; that should be returned
// clang-format off
uint8_t store[32] = {
0xa0, 0xa1, 0xa2, 0xa3,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
0xc0
};
// clang-format on
uint8_t stream[32] = {0};
buffer_t store_buf = buffer_create(store, 4 + 8 + 1);
buffer_t stream_buf = buffer_create(stream, sizeof(stream));
buffer_t *buffers[2] = {&store_buf, &stream_buf};
parse_ABC_state_t parser_state;
parser_context_t parser_context;
memset(&parser_state, 0, sizeof(parser_state));
parser_init_context(&parser_context, &parser_state);
int result = parser_run(parse_ABC_error_steps, n_ABC_STEPS, &parser_context, buffers, NULL);
assert_int_equal(result, -1); // parsing error
assert_int_equal(parser_context.cur_step,
1); // index of the parsing function causing the error
// Check expected parsing results
assert_int_equal(parser_state.a, 0xa0a1a2a3); // a should have been parsed correctly
}
int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_parser_init_context),
cmocka_unit_test(test_parser_oneshot_from_store),
cmocka_unit_test(test_parser_oneshot_with_empty_store),
cmocka_unit_test(test_parser_oneshot_part_store_part_stream),
cmocka_unit_test(test_parser_stream_ends),
cmocka_unit_test(test_parser_continue_partial),
cmocka_unit_test(test_parser_error),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}