forked from onflow/ledger-app-flow
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_rlp.c
160 lines (141 loc) · 8.06 KB
/
test_rlp.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
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <cmocka.h>
#include "hexutils.h"
#include "rlp.h"
struct RLPValueTestCase {
const char *data;
parser_error_t expectedError;
uint8_t expectedKind;
uint64_t expectedLen;
uint64_t expectedDataOffset;
uint64_t expectedConsumed;
};
struct RLPValueTestCase testCases[] = {
{"00", PARSER_OK, RLP_KIND_STRING, 1, 0, 1}, // Byte string (00)
{"01", PARSER_OK, RLP_KIND_STRING, 1, 0, 1}, // Byte string (01)
{"7F", PARSER_OK, RLP_KIND_STRING, 1, 0, 1}, // Byte string (7F)
{"80", PARSER_OK, RLP_KIND_STRING, 0, 1, 1}, // Empty string ("")
{"83646F67", PARSER_OK, RLP_KIND_STRING, 3, 1, 4}, // Short string ("dog")
{"B7"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000",
PARSER_OK, RLP_KIND_STRING, 55, 1, 56},
{"B90400"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000",
PARSER_OK, RLP_KIND_STRING, 1024, 3, 1027},
{"C0", PARSER_OK, RLP_KIND_LIST, 0, 1, 1},
{"C80000000000000000", PARSER_OK, RLP_KIND_LIST, 8, 1, 9},
{"F7"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000",
PARSER_OK, RLP_KIND_LIST, 55, 1, 56},
{"F90400"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000",
PARSER_OK, RLP_KIND_LIST, 1024, 3, 1027},
// Varios invalid RLP data examples
{"", PARSER_UNEXPECTED_BUFFER_END, RLP_KIND_UNKNOWN, 0, 0, 0},
{"BB", PARSER_UNEXPECTED_BUFFER_END, RLP_KIND_STRING, 0, 0, 0},
{"B800", PARSER_OK, RLP_KIND_STRING, 0, 2, 2},
{"B900", PARSER_UNEXPECTED_BUFFER_END, RLP_KIND_STRING, 0, 0, 0},
{"B90000", PARSER_OK, RLP_KIND_STRING, 0, 3, 3},
{"B9000100", PARSER_OK, RLP_KIND_STRING, 1, 3, 4},
{"BA000000", PARSER_RLP_ERROR_INVALID_VALUE_LEN, RLP_KIND_STRING, 0, 0, 0},
{"BB01000000", PARSER_RLP_ERROR_INVALID_VALUE_LEN, RLP_KIND_STRING, 0, 0, 0}
};
static void test_rlp(void **state) {
(void) state;
const size_t numberOfTests = (sizeof(testCases) / sizeof(*testCases));
for(int i=0; i<numberOfTests; i++) {
struct RLPValueTestCase *testCase = &(testCases[i]);
uint8_t data[2000];
const size_t dataSize = parseHexString(data, sizeof(data), testCase->data);
parser_context_t ctx_in;
parser_context_t ctx_out;
ctx_in.buffer = data;
ctx_in.bufferLen = dataSize;
ctx_in.offset = 0;
rlp_kind_e kind;
uint32_t bytesConsumed;
parser_error_t err = rlp_decode(&ctx_in, &ctx_out, &kind, &bytesConsumed);
assert_int_equal(err, testCase->expectedError);
assert_int_equal(kind, testCase->expectedKind);
assert_int_equal(ctx_out.bufferLen, testCase->expectedLen);
assert_int_equal(ctx_out.buffer - ctx_in.buffer, testCase->expectedDataOffset);
assert_int_equal(bytesConsumed, testCase->expectedConsumed);
}
}
int main() {
const struct CMUnitTest tests[] = {cmocka_unit_test(test_rlp)};
return cmocka_run_group_tests(tests, NULL, NULL);
}