-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparser_test.c
More file actions
346 lines (320 loc) · 10.4 KB
/
parser_test.c
File metadata and controls
346 lines (320 loc) · 10.4 KB
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
#include "instruction.h"
#include "parser.c"
#include "sol/printer.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
void test_parse_u8() {
uint8_t message[] = {1, 2};
Parser parser = {message, sizeof(message)};
uint8_t value;
assert(parse_u8(&parser, &value) == 0);
assert(parser.buffer_length == 1);
assert(parser.buffer == message + 1);
assert(value == 1);
}
void test_parse_u8_too_short() {
uint8_t message[] = {42};
Parser parser = {message, sizeof(message)};
uint8_t value;
assert(parse_u8(&parser, &value) == 0);
assert(parse_u8(&parser, &value) == 1);
}
void test_parse_u16() {
uint8_t message[] = {0, 0, 255, 255};
Parser parser = {message, sizeof(message)};
uint16_t value;
assert(parse_u16(&parser, &value) == 0);
assert(value == 0);
assert(parse_u16(&parser, &value) == 0);
assert(value == UINT16_MAX);
assert(parser_is_empty(&parser));
}
void test_parse_u32() {
uint8_t message[] = {0, 0, 0, 0, 255, 255, 255, 255};
Parser parser = {message, sizeof(message)};
uint32_t value;
assert(parse_u32(&parser, &value) == 0);
assert(value == 0);
assert(parse_u32(&parser, &value) == 0);
assert(value == UINT32_MAX);
assert(parser_is_empty(&parser));
}
void test_parse_u64() {
uint8_t message[] = {0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255};
Parser parser = {message, sizeof(message)};
uint64_t value;
assert(parse_u64(&parser, &value) == 0);
assert(value == 0);
assert(parse_u64(&parser, &value) == 0);
assert(value == UINT64_MAX);
assert(parser_is_empty(&parser));
}
void test_parse_i64() {
uint8_t buffer[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f};
Parser parser = {buffer, sizeof(buffer)};
int64_t value;
assert(parse_i64(&parser, &value) == 0);
assert(value == INT64_MIN);
assert(parse_i64(&parser, &value) == 0);
assert(value == 0);
assert(parse_i64(&parser, &value) == 0);
assert(value == INT64_MAX);
}
void test_parse_length() {
uint8_t message[] = {1, 2};
Parser parser = {message, sizeof(message)};
size_t value;
assert(parse_length(&parser, &value) == 0);
assert(parser.buffer_length == 1);
assert(parser.buffer == message + 1);
assert(value == 1);
}
void test_parser_option() {
uint8_t message[] = {0x00, 0x01, 0x02, 0xff};
Parser parser = {message, sizeof(message)};
enum Option value;
assert(parse_option(&parser, &value) == 0);
assert(value == OptionNone);
assert(parse_option(&parser, &value) == 0);
assert(value == OptionSome);
// First bad value
assert(parse_option(&parser, &value) == 1);
// Last bad value
assert(parse_option(&parser, &value) == 1);
// Parser empty
assert(parse_option(&parser, &value) == 1);
}
void test_parse_sized_string() {
SizedString value;
uint8_t buffer[] = {/* "test" */
0x04,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x74,
0x65,
0x73,
0x74,
/* length too long */
0xff,
0xff,
0xff,
0xff,
0xff,
0xff,
0xff,
0xff,
/* remaining buffer too short for length */
0x10,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
/* buffer to short to read length */
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00};
Parser parser = {buffer, sizeof(buffer)};
assert(parse_sized_string(&parser, &value) == 0);
assert(value.length == 4);
assert(strncmp("test", value.string, value.length) == 0);
assert(parse_sized_string(&parser, &value) == 1);
assert(parse_sized_string(&parser, &value) == 1);
assert(parse_sized_string(&parser, &value) == 1);
}
void test_parse_pubkey() {
const Pubkey* value;
const char* expected_string = "11111111111111111111111111111111";
char value_string[BASE58_PUBKEY_LENGTH];
uint8_t buffer[] = {/* valid */
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
/* too short */
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00};
Parser parser = {buffer, sizeof(buffer)};
assert(parse_pubkey(&parser, &value) == 0);
encode_base58(value, sizeof(Pubkey), value_string, sizeof(value_string));
assert_string_equal(expected_string, value_string);
assert(parse_pubkey(&parser, &value) == 1);
}
void test_parse_length_two_bytes() {
uint8_t message[] = {128, 1};
Parser parser = {message, sizeof(message)};
size_t value;
assert(parse_length(&parser, &value) == 0);
assert(parser_is_empty(&parser));
assert(parser.buffer == message + 2);
assert(value == 128);
}
void test_parse_pubkeys_header() {
uint8_t message[] = {1, 2, 3, 4};
Parser parser = {message, sizeof(message)};
PubkeysHeader header;
assert(parse_pubkeys_header(&parser, &header) == 0);
assert(parser_is_empty(&parser));
assert(parser.buffer == message + 4);
assert(header.pubkeys_length == 4);
}
void test_parse_pubkeys() {
uint8_t message[PUBKEY_SIZE + 4] = {1, 2, 3, 1, 42};
Parser parser = {message, sizeof(message)};
PubkeysHeader header;
const Pubkey* pubkeys;
assert(parse_pubkeys(&parser, &header, &pubkeys) == 0);
assert(parser_is_empty(&parser));
assert(parser.buffer == message + PUBKEY_SIZE + 4);
assert(pubkeys->data[0] == 42);
}
void test_parse_pubkeys_too_short() {
uint8_t message[] = {1, 2, 3, 1};
Parser parser = {message, sizeof(message)};
PubkeysHeader header;
const Pubkey* pubkeys;
assert(parse_pubkeys(&parser, &header, &pubkeys) == 1);
}
void test_parse_hash() {
uint8_t message[HASH_SIZE] = {42};
Parser parser = {message, sizeof(message)};
const Hash* hash;
assert(parse_hash(&parser, &hash) == 0);
assert(parser_is_empty(&parser));
assert(parser.buffer == message + HASH_SIZE);
assert(hash->data[0] == 42);
}
void test_parse_hash_too_short() {
uint8_t message[31]; // <--- Too short!
Parser parser = {message, sizeof(message)};
const Hash* hash;
assert(parse_hash(&parser, &hash) == 1);
}
void test_parse_data() {
uint8_t message[] = {1, 2};
Parser parser = {message, sizeof(message)};
const uint8_t* data;
size_t data_length;
assert(parse_data(&parser, &data, &data_length) == 0);
assert(parser_is_empty(&parser));
assert(parser.buffer == message + 2);
assert(data[0] == 2);
}
void test_parse_data_too_short() {
uint8_t message[] = {1}; // <--- Too short!
Parser parser = {message, sizeof(message)};
const uint8_t* data;
size_t data_length;
assert(parse_data(&parser, &data, &data_length) == 1);
}
void test_parse_instruction() {
uint8_t message[] = {0, 2, 33, 34, 1, 36};
Parser parser = {message, sizeof(message)};
Instruction instruction;
assert(parse_instruction(&parser, &instruction) == 0);
MessageHeader header = {false, 0, {0, 0, 0, 35}, NULL, NULL, 1};
assert(instruction_validate(&instruction, &header) == 0);
assert(parser_is_empty(&parser));
assert(instruction.accounts[0] == 33);
assert(instruction.data[0] == 36);
}
void test_parser_is_empty() {
uint8_t buf[1] = {0};
Parser nonempty = {buf, 1};
assert(!parser_is_empty(&nonempty));
Parser empty = {NULL, 0};
assert(parser_is_empty(&empty));
}
int main() {
test_parse_u8();
test_parse_u8_too_short();
test_parse_u16();
test_parse_u32();
test_parse_u64();
test_parse_i64();
test_parse_length();
test_parse_length_two_bytes();
test_parse_sized_string();
test_parse_pubkey();
test_parse_pubkeys_header();
test_parse_pubkeys();
test_parse_pubkeys_too_short();
test_parse_hash();
test_parse_hash_too_short();
test_parse_data();
test_parse_data_too_short();
test_parse_instruction();
test_parser_is_empty();
printf("passed\n");
return 0;
}