Skip to content

Commit 2dcf291

Browse files
authored
tests: Add process instruction checks (solana-program#19)
* Add process instruction checks * Update mollusk git reference * Add missing check * Add return data checks
1 parent 35b9a8d commit 2dcf291

File tree

5 files changed

+733
-118
lines changed

5 files changed

+733
-118
lines changed

p-token/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ thiserror = "2.0"
2222

2323
[dev-dependencies]
2424
lazy_static = "1.5.0"
25-
mollusk-svm = "0.0.13"
25+
mollusk-svm = { version = "0.0.13", git = "https://github.com/buffalojoec/mollusk.git" }
2626
proptest = "1.5"
2727
serial_test = "3.2.0"
2828
solana-sdk = "2.1.0"

p-token/tests/assert_instruction_count.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod setup;
55
use {
66
mollusk_svm::{result::Check, Mollusk},
77
solana_sdk::{
8-
account::{AccountSharedData, ReadableAccount},
8+
account::{Account as SolanaAccount, ReadableAccount},
99
program_pack::Pack,
1010
pubkey::Pubkey,
1111
},
@@ -29,7 +29,7 @@ fn initialize_mint() {
2929
let mint_account = {
3030
let space = Mint::LEN;
3131
let lamports = mollusk.sysvars.rent.minimum_balance(space);
32-
AccountSharedData::new(lamports, space, &id())
32+
SolanaAccount::new(lamports, space, &id())
3333
};
3434

3535
mollusk.process_and_validate_instruction(
@@ -61,15 +61,15 @@ fn initialize_account() {
6161
let token_account = {
6262
let space = Account::LEN;
6363
let lamports = mollusk.sysvars.rent.minimum_balance(space);
64-
AccountSharedData::new(lamports, space, &id())
64+
SolanaAccount::new(lamports, space, &id())
6565
};
6666

6767
mollusk.process_and_validate_instruction(
6868
&instruction::initialize_account(&id(), &account, &mint, &owner).unwrap(),
6969
&[
7070
(account, token_account),
7171
(mint, mint_account),
72-
(owner, AccountSharedData::default()),
72+
(owner, SolanaAccount::default()),
7373
mollusk.sysvars.keyed_account_for_rent_sysvar(),
7474
],
7575
&[
@@ -99,7 +99,7 @@ fn mint_to() {
9999
&[
100100
(mint, mint_account),
101101
(account, token_account),
102-
(owner, AccountSharedData::default()),
102+
(owner, SolanaAccount::default()),
103103
],
104104
&[
105105
Check::success(),
@@ -133,7 +133,7 @@ fn transfer() {
133133
&[
134134
(source, source_token_account),
135135
(destination, destination_token_account),
136-
(owner, AccountSharedData::default()),
136+
(owner, SolanaAccount::default()),
137137
],
138138
&[
139139
Check::success(),
@@ -165,7 +165,7 @@ fn burn() {
165165
&[
166166
(mint, mint_account),
167167
(account, token_account),
168-
(owner, AccountSharedData::default()),
168+
(owner, SolanaAccount::default()),
169169
],
170170
&[
171171
Check::success(),
@@ -194,7 +194,7 @@ fn close_account() {
194194
&[
195195
(mint, mint_account),
196196
(account, token_account),
197-
(owner, AccountSharedData::default()),
197+
(owner, SolanaAccount::default()),
198198
],
199199
&[Check::success(), Check::account(&account).closed().build()],
200200
);

p-token/tests/close_account.rs

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod setup;
55
use {
66
mollusk_svm::{result::Check, Mollusk},
77
solana_sdk::{
8-
account::{AccountSharedData, ReadableAccount},
8+
account::{Account as SolanaAccount, ReadableAccount},
99
program_error::ProgramError,
1010
program_pack::Pack,
1111
pubkey::Pubkey,
@@ -24,44 +24,53 @@ fn success_init_after_close_account() {
2424
let destination = Pubkey::new_unique();
2525
let decimals = 9;
2626

27-
let owner_account = AccountSharedData::new(1_000_000_000, 0, &system_program::id());
27+
let owner_account = SolanaAccount::new(1_000_000_000, 0, &system_program::id());
2828
let mint_account = setup::setup_mint_account(None, None, 0, decimals);
2929
let token_account = setup::setup_token_account(&mint, &owner, 0);
3030

3131
let expected_destination_lamports = token_account.lamports();
3232

3333
mollusk.process_and_validate_instruction_chain(
3434
&[
35-
instruction::close_account(&spl_token::id(), &account, &destination, &owner, &[])
36-
.unwrap(),
37-
system_instruction::create_account(
38-
&owner,
39-
&account,
40-
1_000_000_000,
41-
Account::LEN as u64,
42-
&spl_token::id(),
35+
(
36+
&instruction::close_account(&spl_token::id(), &account, &destination, &owner, &[])
37+
.unwrap(),
38+
&[Check::success()],
39+
),
40+
(
41+
&system_instruction::create_account(
42+
&owner,
43+
&account,
44+
1_000_000_000,
45+
Account::LEN as u64,
46+
&spl_token::id(),
47+
),
48+
&[Check::success()],
49+
),
50+
(
51+
&instruction::initialize_account(&spl_token::id(), &account, &mint, &owner)
52+
.unwrap(),
53+
&[
54+
Check::success(),
55+
// Account successfully re-initialized.
56+
Check::account(&account)
57+
.data(setup::setup_token_account(&mint, &owner, 0).data())
58+
.owner(&spl_token::id())
59+
.build(),
60+
// The destination should have the lamports from the closed account.
61+
Check::account(&destination)
62+
.lamports(expected_destination_lamports)
63+
.build(),
64+
],
4365
),
44-
instruction::initialize_account(&spl_token::id(), &account, &mint, &owner).unwrap(),
4566
],
4667
&[
4768
(mint, mint_account),
4869
(account, token_account),
4970
(owner, owner_account),
50-
(destination, AccountSharedData::default()),
71+
(destination, SolanaAccount::default()),
5172
mollusk.sysvars.keyed_account_for_rent_sysvar(),
5273
],
53-
&[
54-
Check::success(),
55-
// Account successfully re-initialized.
56-
Check::account(&account)
57-
.data(setup::setup_token_account(&mint, &owner, 0).data())
58-
.owner(&spl_token::id())
59-
.build(),
60-
// The destination should have the lamports from the closed account.
61-
Check::account(&destination)
62-
.lamports(expected_destination_lamports)
63-
.build(),
64-
],
6574
);
6675
}
6776

@@ -75,37 +84,46 @@ fn fail_init_after_close_account() {
7584
let destination = Pubkey::new_unique();
7685
let decimals = 9;
7786

78-
let owner_account = AccountSharedData::new(1_000_000_000, 0, &system_program::id());
87+
let owner_account = SolanaAccount::new(1_000_000_000, 0, &system_program::id());
7988
let mint_account = setup::setup_mint_account(None, None, 0, decimals);
8089
let token_account = setup::setup_token_account(&mint, &owner, 0);
8190

8291
let expected_destination_lamports = token_account.lamports();
8392

8493
mollusk.process_and_validate_instruction_chain(
8594
&[
86-
instruction::close_account(&spl_token::id(), &account, &destination, &owner, &[])
87-
.unwrap(),
88-
system_instruction::transfer(&owner, &account, 1_000_000_000),
89-
instruction::initialize_account(&spl_token::id(), &account, &mint, &owner).unwrap(),
95+
(
96+
&instruction::close_account(&spl_token::id(), &account, &destination, &owner, &[])
97+
.unwrap(),
98+
&[Check::success()],
99+
),
100+
(
101+
&system_instruction::transfer(&owner, &account, 1_000_000_000),
102+
&[Check::success()],
103+
),
104+
(
105+
&instruction::initialize_account(&spl_token::id(), &account, &mint, &owner)
106+
.unwrap(),
107+
&[
108+
Check::err(ProgramError::InvalidAccountData),
109+
// Account not re-initialized.
110+
Check::account(&account)
111+
.lamports(1_000_000_000)
112+
.owner(&system_program::id())
113+
.build(),
114+
// The destination should have the lamports from the closed account.
115+
Check::account(&destination)
116+
.lamports(expected_destination_lamports)
117+
.build(),
118+
],
119+
),
90120
],
91121
&[
92122
(mint, mint_account),
93123
(account, token_account),
94124
(owner, owner_account),
95-
(destination, AccountSharedData::default()),
125+
(destination, SolanaAccount::default()),
96126
mollusk.sysvars.keyed_account_for_rent_sysvar(),
97127
],
98-
&[
99-
Check::err(ProgramError::InvalidAccountData),
100-
// Account not re-initialized.
101-
Check::account(&account)
102-
.lamports(1_000_000_000)
103-
.owner(&system_program::id())
104-
.build(),
105-
// The destination should have the lamports from the closed account.
106-
Check::account(&destination)
107-
.lamports(expected_destination_lamports)
108-
.build(),
109-
],
110128
);
111129
}

0 commit comments

Comments
 (0)