-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_allow_mint.rs
More file actions
385 lines (303 loc) · 13.7 KB
/
test_allow_mint.rs
File metadata and controls
385 lines (303 loc) · 13.7 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use crate::{
fixtures::{AllowMintFixture, AllowMintSetup},
utils::{
assert_account_exists, assert_allowed_mint_account, assert_escrow_error, assert_instruction_error,
find_allowed_mint_pda, find_noncanonical_program_address, test_missing_signer, test_not_writable,
test_wrong_current_program, test_wrong_system_program, EscrowError, InstructionTestFixture, TestContext,
RANDOM_PUBKEY,
},
};
use escrow_program_client::instructions::{AllowMintBuilder, SetImmutableBuilder};
use solana_sdk::{account::Account, instruction::InstructionError, pubkey::Pubkey, signature::Signer};
use spl_associated_token_account::get_associated_token_address;
use spl_token_2022::extension::ExtensionType;
// ============================================================================
// Error Tests - Using Generic Test Helpers
// ============================================================================
#[test]
fn test_allow_mint_missing_admin_signer() {
let mut ctx = TestContext::new();
test_missing_signer::<AllowMintFixture>(&mut ctx, 1, 0);
}
#[test]
fn test_allow_mint_allowed_mint_not_writable() {
let mut ctx = TestContext::new();
test_not_writable::<AllowMintFixture>(&mut ctx, 5);
}
#[test]
fn test_allow_mint_vault_not_writable() {
let mut ctx = TestContext::new();
// vault is at index 6 in instruction accounts
test_not_writable::<AllowMintFixture>(&mut ctx, 6);
}
#[test]
fn test_allow_mint_wrong_system_program() {
let mut ctx = TestContext::new();
test_wrong_system_program::<AllowMintFixture>(&mut ctx);
}
#[test]
fn test_allow_mint_wrong_current_program() {
let mut ctx = TestContext::new();
test_wrong_current_program::<AllowMintFixture>(&mut ctx);
}
#[test]
fn test_allow_mint_invalid_event_authority() {
let mut ctx = TestContext::new();
let error = AllowMintFixture::build_valid(&mut ctx).with_account_at(10, RANDOM_PUBKEY).send_expect_error(&mut ctx);
assert_escrow_error(error, EscrowError::InvalidEventAuthority);
}
#[test]
fn test_allow_mint_invalid_bump() {
let mut ctx = TestContext::new();
let valid_ix = AllowMintFixture::build_valid(&mut ctx);
let correct_bump = valid_ix.instruction.data[1];
let invalid_bump = correct_bump.wrapping_add(1);
let error = valid_ix.with_data_byte_at(1, invalid_bump).send_expect_error(&mut ctx);
assert_instruction_error(error, InstructionError::InvalidSeeds);
}
#[test]
fn test_allow_mint_noncanonical_pda_rejected() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new(&mut ctx);
let valid_ix = setup.build_instruction(&ctx);
let program_id = valid_ix.instruction.program_id;
let (noncanonical_allowed_mint, noncanonical_bump) = find_noncanonical_program_address(
&[b"allowed_mint", setup.escrow_pda.as_ref(), setup.mint_pubkey.as_ref()],
&program_id,
)
.expect("expected at least one noncanonical bump");
let error = valid_ix
.with_account_at(5, noncanonical_allowed_mint)
.with_data_byte_at(1, noncanonical_bump)
.send_expect_error(&mut ctx);
assert_instruction_error(error, InstructionError::InvalidSeeds);
}
#[test]
fn test_allow_mint_wrong_admin() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new(&mut ctx);
let wrong_admin = ctx.create_funded_keypair();
let instruction = AllowMintBuilder::new()
.payer(ctx.payer.pubkey())
.admin(wrong_admin.pubkey())
.escrow(setup.escrow_pda)
.escrow_extensions(setup.escrow_extensions_pda)
.mint(setup.mint_pubkey)
.allowed_mint(setup.allowed_mint_pda)
.vault(setup.vault)
.token_program(setup.token_program)
.bump(setup.allowed_mint_bump)
.instruction();
let test_ix = crate::utils::TestInstruction { instruction, signers: vec![wrong_admin], name: "AllowMint" };
let error = test_ix.send_expect_error(&mut ctx);
assert_escrow_error(error, EscrowError::InvalidAdmin);
}
#[test]
fn test_allow_mint_wrong_escrow() {
let mut ctx = TestContext::new();
let error = AllowMintFixture::build_valid(&mut ctx).with_account_at(2, RANDOM_PUBKEY).send_expect_error(&mut ctx);
assert_instruction_error(error, InstructionError::InvalidAccountOwner);
}
#[test]
fn test_allow_mint_wrong_mint() {
let mut ctx = TestContext::new();
let error = AllowMintFixture::build_valid(&mut ctx).with_account_at(4, RANDOM_PUBKEY).send_expect_error(&mut ctx);
assert_instruction_error(error, InstructionError::InvalidAccountOwner);
}
#[test]
fn test_allow_mint_wrong_token_program() {
let mut ctx = TestContext::new();
let error = AllowMintFixture::build_valid(&mut ctx).with_account_at(7, RANDOM_PUBKEY).send_expect_error(&mut ctx);
assert_instruction_error(error, InstructionError::IncorrectProgramId);
}
#[test]
fn test_allow_mint_wrong_associated_token_program() {
let mut ctx = TestContext::new();
// associated_token_program is at index 8 in instruction accounts
let error = AllowMintFixture::build_valid(&mut ctx).with_account_at(8, RANDOM_PUBKEY).send_expect_error(&mut ctx);
assert_instruction_error(error, InstructionError::IncorrectProgramId);
}
#[test]
fn test_allow_mint_duplicate() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new(&mut ctx);
let test_ix = setup.build_instruction(&ctx);
test_ix.send_expect_success(&mut ctx);
let duplicate_ix = setup.build_instruction(&ctx);
let error = duplicate_ix.send_expect_error(&mut ctx);
assert!(matches!(error, solana_sdk::transaction::TransactionError::AlreadyProcessed));
}
#[test]
fn test_allow_mint_succeeds_when_escrow_is_immutable() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new(&mut ctx);
let set_immutable_ix =
SetImmutableBuilder::new().admin(setup.admin.pubkey()).escrow(setup.escrow_pda).instruction();
ctx.send_transaction(set_immutable_ix, &[&setup.admin]).unwrap();
let test_ix = setup.build_instruction(&ctx);
test_ix.send_expect_success(&mut ctx);
assert_account_exists(&ctx, &setup.allowed_mint_pda);
assert_allowed_mint_account(&ctx, &setup.allowed_mint_pda, setup.allowed_mint_bump);
}
// ============================================================================
// Happy Path Test
// ============================================================================
#[test]
fn test_allow_mint_success() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new(&mut ctx);
let test_ix = setup.build_instruction(&ctx);
test_ix.send_expect_success(&mut ctx);
assert_account_exists(&ctx, &setup.allowed_mint_pda);
assert_allowed_mint_account(&ctx, &setup.allowed_mint_pda, setup.allowed_mint_bump);
}
#[test]
fn test_allow_mint_prefunded_allowed_mint_pda_succeeds() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new(&mut ctx);
// Simulate griefing by pre-funding the AllowedMint PDA before initialization.
ctx.svm
.set_account(
setup.allowed_mint_pda,
Account { lamports: 1, data: vec![], owner: Pubkey::default(), executable: false, rent_epoch: 0 },
)
.unwrap();
let test_ix = setup.build_instruction(&ctx);
test_ix.send_expect_success(&mut ctx);
assert_account_exists(&ctx, &setup.allowed_mint_pda);
assert_allowed_mint_account(&ctx, &setup.allowed_mint_pda, setup.allowed_mint_bump);
}
#[test]
fn test_allow_mint_multiple_mints() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new(&mut ctx);
let first_ix = setup.build_instruction(&ctx);
first_ix.send_expect_success(&mut ctx);
let second_mint = solana_sdk::signature::Keypair::new();
ctx.create_mint(&second_mint, &ctx.payer.pubkey(), 9);
let (second_allowed_mint_pda, second_bump) = find_allowed_mint_pda(&setup.escrow_pda, &second_mint.pubkey());
let second_vault = get_associated_token_address(&setup.escrow_pda, &second_mint.pubkey());
let instruction = AllowMintBuilder::new()
.payer(ctx.payer.pubkey())
.admin(setup.admin.pubkey())
.escrow(setup.escrow_pda)
.escrow_extensions(setup.escrow_extensions_pda)
.mint(second_mint.pubkey())
.allowed_mint(second_allowed_mint_pda)
.vault(second_vault)
.token_program(setup.token_program)
.bump(second_bump)
.instruction();
let second_ix =
crate::utils::TestInstruction { instruction, signers: vec![setup.admin.insecure_clone()], name: "AllowMint" };
second_ix.send_expect_success(&mut ctx);
assert_account_exists(&ctx, &setup.allowed_mint_pda);
assert_account_exists(&ctx, &second_allowed_mint_pda);
}
// ============================================================================
// Token 2022 Happy Path Tests
// ============================================================================
#[test]
fn test_allow_mint_token_2022_success() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new_token_2022(&mut ctx);
let test_ix = setup.build_instruction(&ctx);
test_ix.send_expect_success(&mut ctx);
assert_account_exists(&ctx, &setup.allowed_mint_pda);
assert_allowed_mint_account(&ctx, &setup.allowed_mint_pda, setup.allowed_mint_bump);
}
// ============================================================================
// Token 2022 Blocked Extension Tests
// ============================================================================
#[test]
fn test_allow_mint_rejects_permanent_delegate() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new_with_extension(&mut ctx, ExtensionType::PermanentDelegate);
let test_ix = setup.build_instruction(&ctx);
let error = test_ix.send_expect_error(&mut ctx);
assert_escrow_error(error, EscrowError::PermanentDelegateNotAllowed);
}
#[test]
fn test_allow_mint_rejects_non_transferable() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new_with_extension(&mut ctx, ExtensionType::NonTransferable);
let test_ix = setup.build_instruction(&ctx);
let error = test_ix.send_expect_error(&mut ctx);
assert_escrow_error(error, EscrowError::NonTransferableNotAllowed);
}
#[test]
fn test_allow_mint_rejects_pausable() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new_with_extension(&mut ctx, ExtensionType::Pausable);
let test_ix = setup.build_instruction(&ctx);
let error = test_ix.send_expect_error(&mut ctx);
assert_escrow_error(error, EscrowError::PausableNotAllowed);
}
#[test]
fn test_allow_mint_rejects_transfer_fee_config() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new_with_extension(&mut ctx, ExtensionType::TransferFeeConfig);
let test_ix = setup.build_instruction(&ctx);
let error = test_ix.send_expect_error(&mut ctx);
assert_escrow_error(error, EscrowError::MintNotAllowed);
}
#[test]
fn test_allow_mint_rejects_mint_close_authority() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new_with_extension(&mut ctx, ExtensionType::MintCloseAuthority);
let test_ix = setup.build_instruction(&ctx);
let error = test_ix.send_expect_error(&mut ctx);
assert_escrow_error(error, EscrowError::MintNotAllowed);
}
// ============================================================================
// Escrow-Specific Blocked Extension Tests
// ============================================================================
#[test]
fn test_allow_mint_rejects_escrow_blocked_extension() {
let mut ctx = TestContext::new();
// Create escrow with MetadataPointer blocked, then try to allow a mint with that extension.
// MetadataPointer is not globally blocked, so rejection should come from escrow-specific blocklist.
let setup = AllowMintSetup::new_with_escrow_blocked_extension(&mut ctx, ExtensionType::MetadataPointer);
let test_ix = setup.build_instruction(&ctx);
let error = test_ix.send_expect_error(&mut ctx);
assert_escrow_error(error, EscrowError::MintNotAllowed);
}
#[test]
fn test_allow_mint_accepts_mint_without_escrow_blocked_extension() {
let mut ctx = TestContext::new();
// Block MetadataPointer, but create a Token-2022 mint without that extension.
let setup = AllowMintSetup::builder(&mut ctx).block_extension(ExtensionType::MetadataPointer).token_2022().build();
let test_ix = setup.build_instruction(&ctx);
test_ix.send_expect_success(&mut ctx);
}
// ============================================================================
// Vault ATA Creation Tests
// ============================================================================
#[test]
fn test_allow_mint_creates_vault_ata() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new(&mut ctx);
// Verify vault ATA does not exist before allow_mint
assert!(ctx.svm.get_account(&setup.vault).is_none());
let test_ix = setup.build_instruction(&ctx);
test_ix.send_expect_success(&mut ctx);
// Verify vault ATA exists after allow_mint
assert_account_exists(&ctx, &setup.vault);
// Verify vault is owned by the token program
let vault_account = ctx.svm.get_account(&setup.vault).unwrap();
assert_eq!(vault_account.owner, setup.token_program);
}
#[test]
fn test_allow_mint_creates_vault_ata_token_2022() {
let mut ctx = TestContext::new();
let setup = AllowMintSetup::new_token_2022(&mut ctx);
// Verify vault ATA does not exist before allow_mint
assert!(ctx.svm.get_account(&setup.vault).is_none());
let test_ix = setup.build_instruction(&ctx);
test_ix.send_expect_success(&mut ctx);
// Verify vault ATA exists after allow_mint
assert_account_exists(&ctx, &setup.vault);
// Verify vault is owned by Token-2022 program
let vault_account = ctx.svm.get_account(&setup.vault).unwrap();
assert_eq!(vault_account.owner, spl_token_2022::ID);
}