forked from LedgerHQ/app-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bip32.c
101 lines (81 loc) · 2.72 KB
/
test_bip32.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
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdbool.h>
#include <cmocka.h>
#include "common/bip32.h"
static void test_bip32_format(void **state) {
(void) state;
char output[30];
bool b = false;
b = bip32_path_format((const uint32_t[5]){0x8000002C, 0x80000000, 0x80000000, 0, 0},
5,
output,
sizeof(output));
assert_true(b);
assert_string_equal(output, "44'/0'/0'/0/0");
b = bip32_path_format((const uint32_t[5]){0x8000002C, 0x80000001, 0x80000000, 0, 0},
5,
output,
sizeof(output));
assert_true(b);
assert_string_equal(output, "44'/1'/0'/0/0");
}
static void test_bad_bip32_format(void **state) {
(void) state;
char output[30];
bool b = true;
// More than MAX_BIP32_PATH (=10)
b = bip32_path_format(
(const uint32_t[11]){0x8000002C, 0x80000000, 0x80000000, 0, 0, 0, 0, 0, 0, 0, 0},
11,
output,
sizeof(output));
assert_false(b);
// No BIP32 path (=0)
b = bip32_path_format(NULL, 0, output, sizeof(output));
assert_false(b);
}
static void test_bip32_read(void **state) {
(void) state;
// clang-format off
uint8_t input[20] = {
0x80, 0x00, 0x00, 0x2C,
0x80, 0x00, 0x00, 0x01,
0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
uint32_t expected[5] = {0x8000002C, 0x80000001, 0x80000000, 0, 0};
uint32_t output[5] = {0};
bool b = false;
b = bip32_path_read(input, sizeof(input), output, 5);
assert_true(b);
assert_memory_equal(output, expected, 5);
}
static void test_bad_bip32_read(void **state) {
(void) state;
// clang-format off
uint8_t input[20] = {
0x80, 0x00, 0x00, 0x2C,
0x80, 0x00, 0x00, 0x01,
0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
uint32_t output[10] = {0};
// buffer too small (5 BIP32 paths instead of 10)
assert_false(bip32_path_read(input, sizeof(input), output, 10));
// No BIP32 path
assert_false(bip32_path_read(input, sizeof(input), output, 0));
// More than MAX_BIP32_PATH (=10)
assert_false(bip32_path_read(input, sizeof(input), output, 20));
}
int main() {
const struct CMUnitTest tests[] = {cmocka_unit_test(test_bip32_format),
cmocka_unit_test(test_bad_bip32_format),
cmocka_unit_test(test_bip32_read),
cmocka_unit_test(test_bad_bip32_read)};
return cmocka_run_group_tests(tests, NULL, NULL);
}