-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_tx_ser_input.c
315 lines (276 loc) · 15.4 KB
/
test_tx_ser_input.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
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <cmocka.h>
#include "ergo/tx_ser_input.h"
#include "common/rwbuffer.h"
#include "macro_helpers.h"
const uint8_t test_box_id[] = {0xf1, 0xca, 0x1e, 0x06, 0x0a, 0xa1, 0x1f, 0x98, 0x9b, 0x3d, 0x70,
0xec, 0x0e, 0x0c, 0x98, 0x7c, 0x95, 0x2e, 0x23, 0x89, 0xbe, 0x5b,
0x82, 0x0b, 0xc7, 0xdb, 0xfc, 0x32, 0x6f, 0x86, 0x13, 0x73};
uint8_t test_tokens_array[] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
token_table_t test_tokens_table = {1, {{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}}};
#define ERGO_TX_SERIALIZER_INPUT_INIT(name) \
ergo_tx_serializer_input_context_t name; \
uint8_t token_frames_count = 1; \
uint32_t proof_data_size = 3; \
cx_blake2b_t hash; \
blake2b_256_init(&hash); \
assert_int_equal(ergo_tx_serializer_input_init(&context, \
test_box_id, \
token_frames_count, \
proof_data_size, \
&test_tokens_table, \
&hash), \
ERGO_TX_SERIALIZER_INPUT_RES_OK);
#define VERIFY_HASH(hash, expected) \
uint8_t *data; \
size_t data_len; \
_cx_blake2b_get_data(hash, &data, &data_len); \
assert_int_equal(data_len, sizeof(expected)); \
assert_memory_equal(data, expected, sizeof(expected)); \
_cx_blake2b_free_data(hash);
static void test_ergo_tx_serializer_input_init(void **state) {
(void) state;
ERGO_TX_SERIALIZER_INPUT_INIT(context);
assert_memory_equal(context.box_id, test_box_id, ERGO_ID_LEN);
assert_int_equal(context.frames_count, token_frames_count);
assert_int_equal(context.frames_processed, 0);
assert_int_equal(context.context_extension_data_size, proof_data_size);
assert_memory_equal(context.tokens_table, &test_tokens_table, sizeof(test_tokens_table));
assert_memory_equal(context.hash, &hash, sizeof(hash));
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_FRAMES_STARTED);
}
static void test_ergo_tx_serializer_input_init_bad_extension_size(void **state) {
(void) state;
ergo_tx_serializer_input_context_t context;
uint8_t token_frames_count = 1;
uint32_t proof_data_size = 1;
cx_blake2b_t hash;
blake2b_256_init(&hash);
assert_int_equal(ergo_tx_serializer_input_init(&context,
test_box_id,
token_frames_count,
proof_data_size,
&test_tokens_table,
&hash),
ERGO_TX_SERIALIZER_INPUT_RES_ERR_BAD_CONTEXT_EXTENSION_SIZE);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_ERROR);
}
static void test_ergo_tx_serializer_input_add_tokens(void **state) {
(void) state;
ERGO_TX_SERIALIZER_INPUT_INIT(context);
uint8_t token_frame_index = 0;
BUFFER_FROM_ARRAY(tokens, test_tokens_array, sizeof(test_tokens_array));
assert_int_equal(
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens),
ERGO_TX_SERIALIZER_INPUT_RES_OK);
uint8_t expected_hash[33] = {0xf1, 0xca, 0x1e, 0x06, 0x0a, 0xa1, 0x1f, 0x98, 0x9b, 0x3d, 0x70,
0xec, 0x0e, 0x0c, 0x98, 0x7c, 0x95, 0x2e, 0x23, 0x89, 0xbe, 0x5b,
0x82, 0x0b, 0xc7, 0xdb, 0xfc, 0x32, 0x6f, 0x86, 0x13, 0x73, 0x00};
VERIFY_HASH(context.hash, expected_hash);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_EXTENSION_STARTED);
}
static void test_ergo_tx_serializer_input_add_tokens_empty_extension(void **state) {
(void) state;
ergo_tx_serializer_input_context_t context;
uint8_t token_frames_count = 1;
uint32_t proof_data_size = 0;
cx_blake2b_t hash;
blake2b_256_init(&hash);
ergo_tx_serializer_input_init(&context,
test_box_id,
token_frames_count,
proof_data_size,
&test_tokens_table,
&hash);
uint8_t token_frame_index = 0;
BUFFER_FROM_ARRAY(tokens, test_tokens_array, sizeof(test_tokens_array));
assert_int_equal(
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens),
ERGO_TX_SERIALIZER_INPUT_RES_OK);
uint8_t expected_hash[34] = {0xf1, 0xca, 0x1e, 0x06, 0x0a, 0xa1, 0x1f, 0x98, 0x9b,
0x3d, 0x70, 0xec, 0x0e, 0x0c, 0x98, 0x7c, 0x95, 0x2e,
0x23, 0x89, 0xbe, 0x5b, 0x82, 0x0b, 0xc7, 0xdb, 0xfc,
0x32, 0x6f, 0x86, 0x13, 0x73, 0x00, 0x00};
VERIFY_HASH(context.hash, expected_hash);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_FINISHED);
}
static void test_ergo_tx_serializer_input_add_tokens_bad_state(void **state) {
(void) state;
ergo_tx_serializer_input_context_t context;
context.state = ERGO_TX_SERIALIZER_INPUT_STATE_EXTENSION_STARTED;
uint8_t token_frame_index = 0;
BUFFER_FROM_ARRAY(tokens, test_tokens_array, sizeof(test_tokens_array));
assert_int_equal(
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens),
ERGO_TX_SERIALIZER_INPUT_RES_ERR_BAD_STATE);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_ERROR);
}
static void test_ergo_tx_serializer_input_add_tokens_bad_input(void **state) {
(void) state;
ERGO_TX_SERIALIZER_INPUT_INIT(context);
uint8_t token_frame_index = 0;
BUFFER_FROM_ARRAY(tokens, test_tokens_array, sizeof(test_tokens_array));
const uint8_t box_id[32] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
assert_int_equal(
ergo_tx_serializer_input_add_tokens(&context, box_id, token_frame_index, &tokens),
ERGO_TX_SERIALIZER_INPUT_RES_ERR_BAD_INPUT_ID);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_ERROR);
}
static void test_ergo_tx_serializer_input_add_tokens_too_many_input_frames(void **state) {
(void) state;
ERGO_TX_SERIALIZER_INPUT_INIT(context);
uint8_t token_frame_index = 1;
BUFFER_FROM_ARRAY(tokens, test_tokens_array, sizeof(test_tokens_array));
assert_int_equal(
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens),
ERGO_TX_SERIALIZER_INPUT_RES_ERR_TOO_MANY_INPUT_FRAMES);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_ERROR);
}
static void test_ergo_tx_serializer_input_add_tokens_bad_frame_index(void **state) {
(void) state;
ergo_tx_serializer_input_context_t context;
uint8_t token_frames_count = 2;
uint32_t proof_data_size = 3;
cx_blake2b_t hash;
blake2b_256_init(&hash);
ergo_tx_serializer_input_init(&context,
test_box_id,
token_frames_count,
proof_data_size,
&test_tokens_table,
&hash);
uint8_t token_frame_index = 1;
BUFFER_FROM_ARRAY(tokens, test_tokens_array, sizeof(test_tokens_array));
assert_int_equal(
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens),
ERGO_TX_SERIALIZER_INPUT_RES_ERR_BAD_FRAME_INDEX);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_ERROR);
}
static void test_ergo_tx_serializer_input_add_tokens_bad_token_id(void **state) {
(void) state;
ERGO_TX_SERIALIZER_INPUT_INIT(context);
uint8_t token_frame_index = 0;
uint8_t tokens_array[31] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
BUFFER_FROM_ARRAY(tokens, tokens_array, sizeof(tokens_array));
assert_int_equal(
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens),
ERGO_TX_SERIALIZER_INPUT_RES_ERR_BAD_TOKEN_ID);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_ERROR);
}
static void test_ergo_tx_serializer_input_add_tokens_bad_token_value(void **state) {
(void) state;
ERGO_TX_SERIALIZER_INPUT_INIT(context);
uint8_t token_frame_index = 0;
uint8_t tokens_array[39] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
BUFFER_FROM_ARRAY(tokens, tokens_array, sizeof(tokens_array));
assert_int_equal(
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens),
ERGO_TX_SERIALIZER_INPUT_RES_ERR_BAD_TOKEN_VALUE);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_ERROR);
}
static void test_ergo_tx_serializer_input_add_tokens_more_data(void **state) {
(void) state;
ergo_tx_serializer_input_context_t context;
uint8_t token_frames_count = 2;
uint32_t proof_data_size = 3;
cx_blake2b_t hash;
blake2b_256_init(&hash);
ergo_tx_serializer_input_init(&context,
test_box_id,
token_frames_count,
proof_data_size,
&test_tokens_table,
&hash);
uint8_t token_frame_index = 0;
BUFFER_FROM_ARRAY(tokens, test_tokens_array, sizeof(test_tokens_array));
assert_int_equal(
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens),
ERGO_TX_SERIALIZER_INPUT_RES_MORE_DATA);
}
static void test_ergo_tx_serializer_input_add_context_extension(void **state) {
(void) state;
ERGO_TX_SERIALIZER_INPUT_INIT(context);
uint8_t token_frame_index = 0;
BUFFER_FROM_ARRAY(tokens, test_tokens_array, sizeof(test_tokens_array));
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens);
uint8_t chunk_array[3] = {0x01, 0x02, 0x03};
BUFFER_FROM_ARRAY(chunk, chunk_array, sizeof(chunk_array));
assert_int_equal(ergo_tx_serializer_input_add_context_extension(&context, &chunk),
ERGO_TX_SERIALIZER_INPUT_RES_OK);
assert_int_equal(context.context_extension_data_size, 0);
uint8_t expected_hash[36] = {0xf1, 0xca, 0x1e, 0x06, 0x0a, 0xa1, 0x1f, 0x98, 0x9b,
0x3d, 0x70, 0xec, 0x0e, 0x0c, 0x98, 0x7c, 0x95, 0x2e,
0x23, 0x89, 0xbe, 0x5b, 0x82, 0x0b, 0xc7, 0xdb, 0xfc,
0x32, 0x6f, 0x86, 0x13, 0x73, 0x00, 0x01, 0x02, 0x03};
VERIFY_HASH(context.hash, expected_hash);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_FINISHED);
}
static void test_ergo_tx_serializer_input_add_context_extension_bad_state(void **state) {
(void) state;
ERGO_TX_SERIALIZER_INPUT_INIT(context);
uint8_t chunk_array[3] = {0x01, 0x02, 0x03};
BUFFER_FROM_ARRAY(chunk, chunk_array, sizeof(chunk_array));
assert_int_equal(ergo_tx_serializer_input_add_context_extension(&context, &chunk),
ERGO_TX_SERIALIZER_INPUT_RES_ERR_BAD_STATE);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_ERROR);
}
static void test_ergo_tx_serializer_input_add_context_extension_too_much_data(void **state) {
(void) state;
ERGO_TX_SERIALIZER_INPUT_INIT(context);
uint8_t token_frame_index = 0;
BUFFER_FROM_ARRAY(tokens, test_tokens_array, sizeof(test_tokens_array));
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens);
uint8_t chunk_array[4] = {0x01, 0x02, 0x03, 0x04};
BUFFER_FROM_ARRAY(chunk, chunk_array, sizeof(chunk_array));
assert_int_equal(ergo_tx_serializer_input_add_context_extension(&context, &chunk),
ERGO_TX_SERIALIZER_INPUT_RES_ERR_TOO_MUCH_PROOF_DATA);
assert_int_equal(context.state, ERGO_TX_SERIALIZER_INPUT_STATE_ERROR);
}
static void test_ergo_tx_serializer_input_add_context_extension_more_data(void **state) {
(void) state;
ERGO_TX_SERIALIZER_INPUT_INIT(context);
uint8_t token_frame_index = 0;
BUFFER_FROM_ARRAY(tokens, test_tokens_array, sizeof(test_tokens_array));
ergo_tx_serializer_input_add_tokens(&context, test_box_id, token_frame_index, &tokens);
uint8_t chunk_array[2] = {0x01, 0x02};
BUFFER_FROM_ARRAY(chunk, chunk_array, sizeof(chunk_array));
assert_int_equal(ergo_tx_serializer_input_add_context_extension(&context, &chunk),
ERGO_TX_SERIALIZER_INPUT_RES_MORE_DATA);
}
int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_ergo_tx_serializer_input_init),
cmocka_unit_test(test_ergo_tx_serializer_input_init_bad_extension_size),
cmocka_unit_test(test_ergo_tx_serializer_input_add_tokens),
cmocka_unit_test(test_ergo_tx_serializer_input_add_tokens_empty_extension),
cmocka_unit_test(test_ergo_tx_serializer_input_add_tokens_bad_state),
cmocka_unit_test(test_ergo_tx_serializer_input_add_tokens_bad_input),
cmocka_unit_test(test_ergo_tx_serializer_input_add_tokens_too_many_input_frames),
cmocka_unit_test(test_ergo_tx_serializer_input_add_tokens_bad_frame_index),
cmocka_unit_test(test_ergo_tx_serializer_input_add_tokens_bad_token_id),
cmocka_unit_test(test_ergo_tx_serializer_input_add_tokens_bad_token_value),
cmocka_unit_test(test_ergo_tx_serializer_input_add_tokens_more_data),
cmocka_unit_test(test_ergo_tx_serializer_input_add_context_extension),
cmocka_unit_test(test_ergo_tx_serializer_input_add_context_extension_bad_state),
cmocka_unit_test(test_ergo_tx_serializer_input_add_context_extension_too_much_data),
cmocka_unit_test(test_ergo_tx_serializer_input_add_context_extension_more_data)};
return cmocka_run_group_tests(tests, NULL, NULL);
}