Skip to content

Commit 4d9b948

Browse files
committed
[wip]: More tests
1 parent 93e1636 commit 4d9b948

File tree

1 file changed

+196
-1
lines changed

1 file changed

+196
-1
lines changed

program/tests/processor.rs

Lines changed: 196 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ use {
1010
solana_sdk::{
1111
account::{create_account_for_test, Account as SolanaAccount, AccountSharedData},
1212
instruction::Instruction,
13+
program_error::ProgramError,
1314
program_pack::Pack,
1415
pubkey::Pubkey,
1516
rent::Rent,
1617
},
17-
spl_token::{error::TokenError, instruction::initialize_mint, state::Mint},
18+
spl_token::{
19+
error::TokenError,
20+
instruction::{initialize_account, initialize_mint, initialize_mint2},
21+
state::{Account, Mint},
22+
},
1823
std::collections::HashSet,
1924
};
2025

@@ -56,6 +61,10 @@ fn do_process_instructions(
5661
)
5762
}
5863

64+
fn account_minimum_balance() -> u64 {
65+
Rent::default().minimum_balance(Account::get_packed_len())
66+
}
67+
5968
fn mint_minimum_balance() -> u64 {
6069
Rent::default().minimum_balance(Mint::get_packed_len())
6170
}
@@ -130,3 +139,189 @@ fn test_initialize_mint() {
130139
],
131140
);
132141
}
142+
143+
#[test]
144+
fn test_initialize_mint2() {
145+
let program_id = spl_token::id();
146+
let owner_key = Pubkey::new_unique();
147+
let mint_key = Pubkey::new_unique();
148+
let mut mint_account = SolanaAccount::new(42, Mint::get_packed_len(), &program_id);
149+
let mint2_key = Pubkey::new_unique();
150+
let mut mint2_account =
151+
SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
152+
153+
// mint is not rent exempt
154+
do_process_instructions(
155+
&[(
156+
initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
157+
vec![&mint_account],
158+
)],
159+
&[Check::err(TokenError::NotRentExempt.into())],
160+
);
161+
162+
mint_account.lamports = mint_minimum_balance();
163+
164+
// create new mint
165+
do_process_instructions(
166+
&[(
167+
initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
168+
vec![&mint_account],
169+
)],
170+
&[Check::success()],
171+
);
172+
173+
// create twice
174+
do_process_instructions(
175+
&[
176+
(
177+
initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
178+
vec![&mint_account],
179+
),
180+
(
181+
initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
182+
vec![&mint_account],
183+
),
184+
],
185+
&[Check::err(TokenError::AlreadyInUse.into())],
186+
);
187+
188+
// create another mint that can freeze
189+
do_process_instructions(
190+
&[(
191+
initialize_mint2(&program_id, &mint2_key, &owner_key, Some(&owner_key), 2).unwrap(),
192+
vec![&mut mint2_account],
193+
)],
194+
&[
195+
// Account successfully re-initialized.
196+
Check::success(),
197+
// mint authority is set
198+
Check::account(&mint2_key)
199+
.data_slice(46, &[1, 0, 0, 0])
200+
.build(),
201+
// mint authority matches owner
202+
Check::account(&mint2_key)
203+
.data_slice(50, owner_key.as_ref())
204+
.build(),
205+
],
206+
);
207+
}
208+
209+
#[test]
210+
fn test_initialize_mint_account() {
211+
let program_id = spl_token::id();
212+
let account_key = Pubkey::new_unique();
213+
let mut account_account = SolanaAccount::new(42, Account::get_packed_len(), &program_id);
214+
let owner_key = Pubkey::new_unique();
215+
let owner_account = SolanaAccount::default();
216+
let mint_key = Pubkey::new_unique();
217+
let mut mint_account =
218+
SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
219+
let rent_sysvar = rent_sysvar();
220+
221+
// account is not rent exempt
222+
do_process_instructions(
223+
&[(
224+
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
225+
vec![
226+
&account_account,
227+
&mint_account,
228+
&owner_account,
229+
&rent_sysvar,
230+
],
231+
)],
232+
&[Check::err(TokenError::NotRentExempt.into())],
233+
);
234+
235+
account_account.lamports = account_minimum_balance();
236+
237+
// mint is not valid (not initialized)
238+
do_process_instructions(
239+
&[(
240+
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
241+
vec![
242+
&account_account,
243+
&mint_account,
244+
&owner_account,
245+
&rent_sysvar,
246+
],
247+
)],
248+
&[Check::err(TokenError::InvalidMint.into())],
249+
);
250+
251+
// create mint
252+
do_process_instructions(
253+
&[(
254+
initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
255+
vec![&mint_account, &rent_sysvar],
256+
)],
257+
&[Check::success()],
258+
);
259+
260+
// mint not owned by program
261+
let not_program_id = Pubkey::new_unique();
262+
mint_account.owner = not_program_id;
263+
264+
do_process_instructions(
265+
&[(
266+
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
267+
vec![
268+
&account_account,
269+
&mint_account,
270+
&owner_account,
271+
&rent_sysvar,
272+
],
273+
)],
274+
&[Check::err(ProgramError::IncorrectProgramId)],
275+
);
276+
277+
mint_account.owner = program_id;
278+
279+
// create account
280+
do_process_instructions(
281+
&[
282+
(
283+
initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
284+
vec![&mint_account, &rent_sysvar],
285+
),
286+
(
287+
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
288+
vec![
289+
&account_account,
290+
&mint_account,
291+
&owner_account,
292+
&rent_sysvar,
293+
],
294+
),
295+
],
296+
&[Check::success()],
297+
);
298+
299+
// create twice
300+
do_process_instructions(
301+
&[
302+
(
303+
initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
304+
vec![&mint_account, &rent_sysvar],
305+
),
306+
(
307+
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
308+
vec![
309+
&account_account,
310+
&mint_account,
311+
&owner_account,
312+
&rent_sysvar,
313+
],
314+
),
315+
(
316+
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
317+
vec![
318+
&account_account,
319+
&mint_account,
320+
&owner_account,
321+
&rent_sysvar,
322+
],
323+
),
324+
],
325+
&[Check::err(TokenError::AlreadyInUse.into())],
326+
);
327+
}

0 commit comments

Comments
 (0)