-
Notifications
You must be signed in to change notification settings - Fork 70
/
test_tx_parser.c
80 lines (70 loc) · 2.93 KB
/
test_tx_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
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <cmocka.h>
#include "transaction/serialize.h"
#include "transaction/deserialize.h"
#include "types.h"
static void test_tx_serialization(void **state) {
(void) state;
transaction_t tx;
// clang-format off
uint8_t raw_tx[] = {
// nonce (8)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
// to (20)
0x7a, 0xc3, 0x39, 0x97, 0x54, 0x4e, 0x31, 0x75,
0xd2, 0x66, 0xbd, 0x02, 0x24, 0x39, 0xb2, 0x2c,
0xdb, 0x16, 0x50, 0x8c,
// value (8)
0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x08, 0x07,
// memo length (varint: 1-9)
0xf1,
// memo (var: 241)
0x54, 0x68, 0x65, 0x20, 0x54, 0x68, 0x65, 0x6f,
0x72, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x47, 0x72,
0x6f, 0x75, 0x70, 0x73, 0x20, 0x69, 0x73, 0x20,
0x61, 0x20, 0x62, 0x72, 0x61, 0x6e, 0x63, 0x68,
0x20, 0x6f, 0x66, 0x20, 0x6d, 0x61, 0x74, 0x68,
0x65, 0x6d, 0x61, 0x74, 0x69, 0x63, 0x73, 0x20,
0x69, 0x6e, 0x20, 0x77, 0x68, 0x69, 0x63, 0x68,
0x20, 0x6f, 0x6e, 0x65, 0x20, 0x64, 0x6f, 0x65,
0x73, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x74, 0x68,
0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20, 0x73,
0x6f, 0x6d, 0x65, 0x74, 0x68, 0x69, 0x6e, 0x67,
0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65,
0x6e, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72,
0x65, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72,
0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x77, 0x69,
0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72,
0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x6f, 0x62,
0x74, 0x61, 0x69, 0x6e, 0x65, 0x64, 0x20, 0x66,
0x72, 0x6f, 0x6d, 0x20, 0x64, 0x6f, 0x69, 0x6e,
0x67, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x61,
0x6d, 0x65, 0x20, 0x74, 0x68, 0x69, 0x6e, 0x67,
0x20, 0x74, 0x6f, 0x20, 0x73, 0x6f, 0x6d, 0x65,
0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6c,
0x73, 0x65, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x73,
0x6f, 0x6d, 0x65, 0x74, 0x68, 0x69, 0x6e, 0x67,
0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x74, 0x6f,
0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x61, 0x6d,
0x65, 0x20, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x2e,
0x20, 0x4e, 0x65, 0x77, 0x6d, 0x61, 0x6e, 0x2c,
0x20, 0x4a, 0x61, 0x6d, 0x65, 0x73, 0x20, 0x52,
0x2e
};
buffer_t buf = {.ptr = raw_tx, .size = sizeof(raw_tx), .offset = 0};
parser_status_e status = transaction_deserialize(&buf, &tx);
assert_int_equal(status, PARSING_OK);
uint8_t output[300];
int length = transaction_serialize(&tx, output, sizeof(output));
assert_int_equal(length, sizeof(raw_tx));
assert_memory_equal(raw_tx, output, sizeof(raw_tx));
}
int main() {
const struct CMUnitTest tests[] = {cmocka_unit_test(test_tx_serialization)};
return cmocka_run_group_tests(tests, NULL, NULL);
}