Skip to content

Commit

Permalink
[wip]: More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
febo committed Dec 21, 2024
1 parent 93e1636 commit 4d9b948
Showing 1 changed file with 196 additions and 1 deletion.
197 changes: 196 additions & 1 deletion program/tests/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ use {
solana_sdk::{
account::{create_account_for_test, Account as SolanaAccount, AccountSharedData},
instruction::Instruction,
program_error::ProgramError,
program_pack::Pack,
pubkey::Pubkey,
rent::Rent,
},
spl_token::{error::TokenError, instruction::initialize_mint, state::Mint},
spl_token::{
error::TokenError,
instruction::{initialize_account, initialize_mint, initialize_mint2},
state::{Account, Mint},
},
std::collections::HashSet,
};

Expand Down Expand Up @@ -56,6 +61,10 @@ fn do_process_instructions(
)
}

fn account_minimum_balance() -> u64 {
Rent::default().minimum_balance(Account::get_packed_len())
}

fn mint_minimum_balance() -> u64 {
Rent::default().minimum_balance(Mint::get_packed_len())
}
Expand Down Expand Up @@ -130,3 +139,189 @@ fn test_initialize_mint() {
],
);
}

#[test]
fn test_initialize_mint2() {
let program_id = spl_token::id();
let owner_key = Pubkey::new_unique();
let mint_key = Pubkey::new_unique();
let mut mint_account = SolanaAccount::new(42, Mint::get_packed_len(), &program_id);
let mint2_key = Pubkey::new_unique();
let mut mint2_account =
SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);

// mint is not rent exempt
do_process_instructions(
&[(
initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
vec![&mint_account],
)],
&[Check::err(TokenError::NotRentExempt.into())],
);

mint_account.lamports = mint_minimum_balance();

// create new mint
do_process_instructions(
&[(
initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
vec![&mint_account],
)],
&[Check::success()],
);

// create twice
do_process_instructions(
&[
(
initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
vec![&mint_account],
),
(
initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
vec![&mint_account],
),
],
&[Check::err(TokenError::AlreadyInUse.into())],
);

// create another mint that can freeze
do_process_instructions(
&[(
initialize_mint2(&program_id, &mint2_key, &owner_key, Some(&owner_key), 2).unwrap(),
vec![&mut mint2_account],
)],
&[
// Account successfully re-initialized.
Check::success(),
// mint authority is set
Check::account(&mint2_key)
.data_slice(46, &[1, 0, 0, 0])
.build(),
// mint authority matches owner
Check::account(&mint2_key)
.data_slice(50, owner_key.as_ref())
.build(),
],
);
}

#[test]
fn test_initialize_mint_account() {
let program_id = spl_token::id();
let account_key = Pubkey::new_unique();
let mut account_account = SolanaAccount::new(42, Account::get_packed_len(), &program_id);
let owner_key = Pubkey::new_unique();
let owner_account = SolanaAccount::default();
let mint_key = Pubkey::new_unique();
let mut mint_account =
SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
let rent_sysvar = rent_sysvar();

// account is not rent exempt
do_process_instructions(
&[(
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
vec![
&account_account,
&mint_account,
&owner_account,
&rent_sysvar,
],
)],
&[Check::err(TokenError::NotRentExempt.into())],
);

account_account.lamports = account_minimum_balance();

// mint is not valid (not initialized)
do_process_instructions(
&[(
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
vec![
&account_account,
&mint_account,
&owner_account,
&rent_sysvar,
],
)],
&[Check::err(TokenError::InvalidMint.into())],
);

// create mint
do_process_instructions(
&[(
initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
vec![&mint_account, &rent_sysvar],
)],
&[Check::success()],
);

// mint not owned by program
let not_program_id = Pubkey::new_unique();
mint_account.owner = not_program_id;

do_process_instructions(
&[(
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
vec![
&account_account,
&mint_account,
&owner_account,
&rent_sysvar,
],
)],
&[Check::err(ProgramError::IncorrectProgramId)],
);

mint_account.owner = program_id;

// create account
do_process_instructions(
&[
(
initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
vec![&mint_account, &rent_sysvar],
),
(
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
vec![
&account_account,
&mint_account,
&owner_account,
&rent_sysvar,
],
),
],
&[Check::success()],
);

// create twice
do_process_instructions(
&[
(
initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
vec![&mint_account, &rent_sysvar],
),
(
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
vec![
&account_account,
&mint_account,
&owner_account,
&rent_sysvar,
],
),
(
initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
vec![
&account_account,
&mint_account,
&owner_account,
&rent_sysvar,
],
),
],
&[Check::err(TokenError::AlreadyInUse.into())],
);
}

0 comments on commit 4d9b948

Please sign in to comment.