-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstruction_test.c
More file actions
224 lines (207 loc) · 8.12 KB
/
Copy pathinstruction_test.c
File metadata and controls
224 lines (207 loc) · 8.12 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "common_byte_strings.h"
#include "instruction.h"
#include "sol/parser.h"
#include "stake_instruction.h"
#include "system_instruction.h"
#include "util.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
void test_instruction_program_id_system() {
Pubkey program_id;
memcpy(&program_id, &system_program_id, PUBKEY_SIZE);
Instruction instruction = {0, NULL, 0, NULL, 0};
{
MessageHeader header = {false, 0, {0, 0, 0, 1}, &program_id, NULL, 1};
assert(instruction_program_id(&instruction, &header) == ProgramIdSystem);
}
{
MessageHeader header = {true, 0, {0, 0, 0, 1}, &program_id, NULL, 1};
assert(instruction_program_id(&instruction, &header) == ProgramIdSystem);
}
}
void test_instruction_program_id_stake() {
Pubkey program_id;
memcpy(&program_id, &stake_program_id, PUBKEY_SIZE);
Instruction instruction = {0, NULL, 0, NULL, 0};
{
MessageHeader header = {false, 0, {0, 0, 0, 1}, &program_id, NULL, 1};
assert(instruction_program_id(&instruction, &header) == ProgramIdStake);
}
{
MessageHeader header = {true, 0, {0, 0, 0, 1}, &program_id, NULL, 1};
assert(instruction_program_id(&instruction, &header) == ProgramIdStake);
}
}
void test_instruction_program_id_unknown() {
Pubkey program_id = {{BYTES32_BS58_2}};
Instruction instruction = {0, NULL, 0, NULL, 0};
{
MessageHeader header = {false, 0, {0, 0, 0, 1}, &program_id, NULL, 1};
assert(instruction_program_id(&instruction, &header) == ProgramIdUnknown);
}
{
MessageHeader header = {true, 0, {0, 0, 0, 1}, &program_id, NULL, 1};
assert(instruction_program_id(&instruction, &header) == ProgramIdUnknown);
}
}
void test_instruction_validate_ok() {
uint8_t accounts[] = {1, 2, 3};
Instruction instruction = {0, accounts, 3, NULL, 0};
{
MessageHeader header = {false, 0, {0, 0, 0, 4}, NULL, NULL, 1};
assert(instruction_validate(&instruction, &header) == 0);
}
{
MessageHeader header = {true, 0, {0, 0, 0, 4}, NULL, NULL, 1};
assert(instruction_validate(&instruction, &header) == 0);
}
}
void test_instruction_validate_bad_program_id_index_fail() {
uint8_t accounts[] = {1, 2, 3};
Instruction instruction = {4, accounts, 3, NULL, 0};
{
MessageHeader header = {false, 0, {0, 0, 0, 4}, NULL, NULL, 1};
assert(instruction_validate(&instruction, &header) == 1);
}
{
MessageHeader header = {true, 0, {0, 0, 0, 4}, NULL, NULL, 1};
assert(instruction_validate(&instruction, &header) == 1);
}
}
void test_instruction_validate_bad_first_account_index_fail() {
uint8_t accounts[] = {4, 2, 3};
Instruction instruction = {0, accounts, 3, NULL, 0};
{
MessageHeader header = {false, 0, {0, 0, 0, 4}, NULL, NULL, 1};
assert(instruction_validate(&instruction, &header) == 1);
}
{
MessageHeader header = {true, 0, {0, 0, 0, 4}, NULL, NULL, 1};
assert(instruction_validate(&instruction, &header) == 1);
}
}
void test_instruction_validate_bad_last_account_index_fail() {
uint8_t accounts[] = {1, 2, 4};
Instruction instruction = {0, accounts, 3, NULL, 0};
{
MessageHeader header = {false, 0, {0, 0, 0, 4}, NULL, NULL, 1};
assert(instruction_validate(&instruction, &header) == 1);
}
{
MessageHeader header = {true, 0, {0, 0, 0, 4}, NULL, NULL, 1};
assert(instruction_validate(&instruction, &header) == 1);
}
}
void test_static_brief_initializer_macros() {
InstructionBrief system_test = SYSTEM_IX_BRIEF(SystemTransfer);
InstructionBrief system_expect = {ProgramIdSystem, .system = SystemTransfer};
assert(memcmp(&system_test, &system_expect, sizeof(InstructionBrief)) == 0);
InstructionBrief stake_test = STAKE_IX_BRIEF(StakeDelegate);
InstructionBrief stake_expect = {ProgramIdStake, .stake = StakeDelegate};
assert(memcmp(&stake_test, &stake_expect, sizeof(InstructionBrief)) == 0);
}
void test_instruction_info_matches_brief() {
InstructionInfo info = {
.kind = ProgramIdSystem,
.system =
{
.kind = SystemTransfer,
.transfer = {NULL, NULL, 0},
},
};
InstructionBrief brief_pass = SYSTEM_IX_BRIEF(SystemTransfer);
assert(instruction_info_matches_brief(&info, &brief_pass));
InstructionBrief brief_fail = SYSTEM_IX_BRIEF(SystemAdvanceNonceAccount);
assert(!instruction_info_matches_brief(&info, &brief_fail));
}
void test_instruction_infos_match_briefs() {
InstructionInfo infos[] = {{
.kind = ProgramIdSystem,
.system =
{
.kind = SystemTransfer,
.transfer = {NULL, NULL, 0},
},
},
{
.kind = ProgramIdStake,
.stake =
{
.kind = StakeDelegate,
.delegate_stake = {NULL, NULL, NULL},
},
}};
InstructionBrief briefs[] = {
SYSTEM_IX_BRIEF(SystemTransfer),
STAKE_IX_BRIEF(StakeDelegate),
};
InstructionBrief bad_briefs[] = {
SYSTEM_IX_BRIEF(SystemTransfer),
STAKE_IX_BRIEF(StakeSplit),
};
InstructionInfo* display_infos[] = {&infos[0], &infos[1]};
size_t infos_len = ARRAY_LEN(infos);
assert(infos_len == ARRAY_LEN(display_infos));
assert(infos_len == ARRAY_LEN(briefs));
assert(infos_len == ARRAY_LEN(bad_briefs));
assert(instruction_infos_match_briefs(display_infos, briefs, infos_len));
assert(!instruction_infos_match_briefs(display_infos, bad_briefs, infos_len));
}
void test_instruction_accounts_iterator_next() {
uint8_t instruction_accounts[] = {0, 1, 2};
Instruction instruction = {
2,
instruction_accounts,
ARRAY_LEN(instruction_accounts),
NULL,
0,
};
Pubkey header_pubkeys[] = {
{{BYTES32_BS58_2}},
{{BYTES32_BS58_3}},
{{BYTES32_BS58_4}},
{{BYTES32_BS58_5}},
};
MessageHeader header = {
false,
0,
{0, 0, 0, ARRAY_LEN(header_pubkeys)},
header_pubkeys,
NULL,
1,
};
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, &header, &instruction);
size_t expected_remaining = ARRAY_LEN(instruction_accounts);
assert(instruction_accounts_iterator_remaining(&it) == expected_remaining--);
const Pubkey* pubkey;
Pubkey expected1 = {{BYTES32_BS58_2}};
assert(instruction_accounts_iterator_next(&it, &pubkey) == 0);
assert(memcmp(pubkey, &expected1, PUBKEY_SIZE) == 0);
assert(instruction_accounts_iterator_remaining(&it) == expected_remaining--);
// Test skipping a pubkey
assert(instruction_accounts_iterator_next(&it, NULL) == 0);
assert(instruction_accounts_iterator_remaining(&it) == expected_remaining--);
Pubkey expected3 = {{BYTES32_BS58_4}};
assert(instruction_accounts_iterator_next(&it, &pubkey) == 0);
assert(instruction_accounts_iterator_remaining(&it) == expected_remaining);
assert(memcmp(pubkey, &expected3, PUBKEY_SIZE) == 0);
assert(instruction_accounts_iterator_next(&it, &pubkey) == 1);
assert(instruction_accounts_iterator_remaining(&it) == expected_remaining);
}
int main() {
test_instruction_validate_ok();
test_instruction_validate_bad_program_id_index_fail();
test_instruction_validate_bad_first_account_index_fail();
test_instruction_validate_bad_last_account_index_fail();
test_instruction_program_id_unknown();
test_instruction_program_id_stake();
test_instruction_program_id_system();
test_static_brief_initializer_macros();
test_instruction_info_matches_brief();
test_instruction_infos_match_briefs();
test_instruction_accounts_iterator_next();
printf("passed\n");
return 0;
}