forked from LedgerHQ/app-aptos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tx_parser.c
100 lines (91 loc) · 4.43 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <cmocka.h>
#include "transaction/deserialize.h"
#include "transaction/types.h"
static void test_tx_deserialization(void **state) {
(void) state;
static transaction_t tx;
// clang-format off
static const uint8_t raw_tx[] = {
0xb5, 0xe9, 0x7d, 0xb0, 0x7f, 0xa0, 0xbd, 0x0e,
0x55, 0x98, 0xaa, 0x36, 0x43, 0xa9, 0xbc, 0x6f,
0x66, 0x93, 0xbd, 0xdc, 0x1a, 0x9f, 0xec, 0x9e,
0x67, 0x4a, 0x46, 0x1e, 0xaa, 0x00, 0xb1, 0x93,
0x86, 0xbf, 0x1b, 0x58, 0x94, 0x2d, 0x9b, 0xf1,
0x24, 0x75, 0xa4, 0x1f, 0x2f, 0x43, 0xb9, 0x70,
0x87, 0xdd, 0x91, 0x93, 0x7f, 0x40, 0x1e, 0xec,
0x08, 0x31, 0x11, 0x68, 0xa9, 0xba, 0xc2, 0xf3,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 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,
0x01, 0x04, 0x63, 0x6f, 0x69, 0x6e, 0x08, 0x74,
0x72, 0x61, 0x6e, 0x73, 0x66, 0x65, 0x72, 0x01,
0x07, 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,
0x01, 0x0a, 0x61, 0x70, 0x74, 0x6f, 0x73, 0x5f,
0x63, 0x6f, 0x69, 0x6e, 0x09, 0x41, 0x70, 0x74,
0x6f, 0x73, 0x43, 0x6f, 0x69, 0x6e, 0x00, 0x02,
0x20, 0xa7, 0x67, 0x6a, 0x00, 0x3b, 0x6f, 0xb4,
0x74, 0x48, 0xb7, 0x9b, 0x8d, 0x68, 0xd2, 0x88,
0x46, 0xb9, 0x29, 0x32, 0x94, 0x1c, 0x92, 0xbe,
0xec, 0xd1, 0x9f, 0x1b, 0xee, 0x6a, 0x68, 0x52,
0x08, 0x08, 0xcd, 0x02, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x20, 0x4e, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x13, 0x84, 0x65, 0x63, 0x00, 0x00,
0x00, 0x00, 0x24
};
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);
// clang-format off
static const uint8_t sender [] = {
0x86, 0xbf, 0x1b, 0x58, 0x94, 0x2d, 0x9b, 0xf1,
0x24, 0x75, 0xa4, 0x1f, 0x2f, 0x43, 0xb9, 0x70,
0x87, 0xdd, 0x91, 0x93, 0x7f, 0x40, 0x1e, 0xec,
0x08, 0x31, 0x11, 0x68, 0xa9, 0xba, 0xc2, 0xf3
};
assert_memory_equal(tx.sender, sender, 32);
assert_int_equal(tx.sequence, 1);
assert_int_equal(tx.max_gas_amount, 20000);
assert_int_equal(tx.gas_unit_price, 100);
assert_int_equal(tx.expiration_timestamp_secs, 1667597331);
assert_int_equal(tx.chain_id, 36);
assert_int_equal(tx.payload_variant, PAYLOAD_ENTRY_FUNCTION);
assert_int_equal(tx.payload.entry_function.known_type, FUNC_COIN_TRANSFER);
// clang-format off
static const uint8_t coin_module_address [] = {
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, 0x01
};
assert_memory_equal(tx.payload.entry_function.args.coin_transfer.ty_coin.address, coin_module_address, 32);
assert_int_equal(tx.payload.entry_function.args.coin_transfer.ty_coin.module_name.len, 10);
assert_memory_equal(tx.payload.entry_function.args.coin_transfer.ty_coin.module_name.bytes, "aptos_coin", 10);
assert_int_equal(tx.payload.entry_function.args.coin_transfer.ty_coin.name.len, 9);
assert_memory_equal(tx.payload.entry_function.args.coin_transfer.ty_coin.name.bytes, "AptosCoin", 9);
assert_int_equal(tx.payload.entry_function.args.coin_transfer.ty_coin.type_args_size, 0);
// clang-format off
static const uint8_t receiver [] = {
0xa7, 0x67, 0x6a, 0x00, 0x3b, 0x6f, 0xb4, 0x74,
0x48, 0xb7, 0x9b, 0x8d, 0x68, 0xd2, 0x88, 0x46,
0xb9, 0x29, 0x32, 0x94, 0x1c, 0x92, 0xbe, 0xec,
0xd1, 0x9f, 0x1b, 0xee, 0x6a, 0x68, 0x52, 0x08
};
assert_memory_equal(tx.payload.entry_function.args.coin_transfer.receiver, receiver, 32);
assert_int_equal(tx.payload.entry_function.args.coin_transfer.amount, 717);
}
int main() {
const struct CMUnitTest tests[] = {cmocka_unit_test(test_tx_deserialization)};
return cmocka_run_group_tests(tests, NULL, NULL);
}