-
Notifications
You must be signed in to change notification settings - Fork 101
/
util_test.h
124 lines (102 loc) · 3.8 KB
/
util_test.h
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
START_TEST(test_hex_enc)
{
struct {
const char* input;
const char* expect_output;
} tests[] = {
{ "\x01", "\x30\x31" },
{ "\x11", "\x31\x31" },
{ "\xff", "\x66\x66" },
{ "\x12\xab", "\x31\x32\x61\x62" },
};
size_t n = sizeof(tests) / sizeof(tests[0]);
for (int i = 0; i < n; i++) {
char got[1024];
size_t len = 1024;
hex_enc(got, &len, tests[i].input, strlen(tests[i].input));
ck_assert_int_eq(len, strlen(tests[i].expect_output));
ck_assert_mem_eq(got, tests[i].expect_output, len);
}
}
END_TEST
START_TEST(test_hex_dec)
{
struct {
const char* input;
const char* expect_output;
} tests[] = {
{ "\x30\x31", "\x01" },
{ "\x31\x31", "\x11" },
{ "\x46\x46", "\xff" },
{ "\x66\x66", "\xff" },
{ "\x31\x32\x41\x42", "\x12\xab" },
{ "\x31\x32\x61\x62", "\x12\xab" },
};
size_t n = sizeof(tests) / sizeof(tests[0]);
for (int i = 0; i < n; i++) {
char got[1024];
size_t len = 1024;
hex_dec(got, &len, tests[i].input, strlen(tests[i].input));
ck_assert_int_eq(len, strlen(tests[i].expect_output));
ck_assert_mem_eq(got, tests[i].expect_output, len);
}
}
END_TEST
START_TEST(test_eth_pubkey2addr)
{
struct {
const char* input_hex;
int addr_format;
const char* expect_output_hex;
} tests[] = {
{ "0477eb4560b9535593f074704e9d0e593c4c09b4f8914971ac53766a4ddd0126e15fae8641a3c07d11a78b14dcd1f0f407781c8feac953c23efdfe71a82ccb9a1f",
VCF_PUBKEY,
"c660a638f696f8d22d8d593f19dcdbe1bb21716e" },
{ "0452b5bcf0ba1cdf9c4aaaa1463658f1e830b968f07fefe80d8e416758c20c727b83d30cdee31b85a8c92a6555fc00d6d2b0f572755f1880d011d9c5d56f4e9605",
VCF_PUBKEY,
"ef5a3f547f84d811998c505f0c5b7a8b74f5b79d" },
{ "0477eb4560b9535593f074704e9d0e593c4c09b4f8914971ac53766a4ddd0126e15fae8641a3c07d11a78b14dcd1f0f407781c8feac953c23efdfe71a82ccb9a1f",
VCF_CONTRACT,
"0e1bb5eb3ccfcacf6df3267c2a15705a7ba453b0" },
{ "0452b5bcf0ba1cdf9c4aaaa1463658f1e830b968f07fefe80d8e416758c20c727b83d30cdee31b85a8c92a6555fc00d6d2b0f572755f1880d011d9c5d56f4e9605",
VCF_CONTRACT,
"b42efaaf71a364905fd5fe95604dbb63bb2c2674" },
};
size_t n = sizeof(tests) / sizeof(tests[0]);
for (int i = 0; i < n; i++) {
size_t addr_len = 20;
char got[addr_len];
char input[65];
memcpy(input, from_hex(tests[i].input_hex), sizeof(input));
char expect_output[addr_len];
memcpy(expect_output, from_hex(tests[i].expect_output_hex), sizeof(expect_output));
eth_pubkey2addr((const unsigned char*)input, tests[i].addr_format, (unsigned char*)got);
ck_assert_mem_eq(got, expect_output, addr_len);
}
}
END_TEST
START_TEST(test_eth_encode_checksum_addr)
{
struct {
const char* input_hex;
const char* expect_output;
} tests[] = {
/* All caps */
{ "52908400098527886e0f7030069857d2e4169ee7", "52908400098527886E0F7030069857D2E4169EE7" },
/* All Lower */
{ "de709f2102306220921060314715629080e2fb77", "de709f2102306220921060314715629080e2fb77" },
/* Normal */
{ "5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed" },
};
size_t n = sizeof(tests) / sizeof(tests[0]);
for (int i = 0; i < n; i++) {
size_t checksum_addr = 40;
char got[checksum_addr];
size_t input_len = 20;
char input[input_len];
memcpy(input, from_hex(tests[i].input_hex), sizeof(input));
eth_encode_checksum_addr((void*)input, input_len, (char*)got, checksum_addr);
ck_assert_mem_eq(got, tests[i].expect_output, checksum_addr);
}
}
END_TEST