-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_create_escrow.rs
More file actions
174 lines (138 loc) · 6.12 KB
/
test_create_escrow.rs
File metadata and controls
174 lines (138 loc) · 6.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
use crate::{
fixtures::CreateEscrowFixture,
utils::{
assert_escrow_account, assert_escrow_mutability, assert_instruction_error, find_noncanonical_program_address,
test_empty_data, test_missing_signer, test_not_writable, test_wrong_account, test_wrong_current_program,
test_wrong_system_program, InstructionTestFixture, TestContext,
},
};
use escrow_program_client::instructions::CreatesEscrowBuilder;
use solana_sdk::{account::Account, instruction::InstructionError, pubkey::Pubkey, signature::Signer};
// ============================================================================
// Error Tests - Using Generic Test Helpers
// ============================================================================
#[test]
fn test_create_escrow_missing_admin_signer() {
let mut ctx = TestContext::new();
// admin is at account index 1, signer vec index 0 (payer is handled separately)
test_missing_signer::<CreateEscrowFixture>(&mut ctx, 1, 0);
}
#[test]
fn test_create_escrow_missing_escrow_seed_signer() {
let mut ctx = TestContext::new();
// escrow_seed is at account index 2, signer vec index 1
test_missing_signer::<CreateEscrowFixture>(&mut ctx, 2, 1);
}
#[test]
fn test_create_escrow_escrow_not_writable() {
let mut ctx = TestContext::new();
// escrow is at index 3 in instruction accounts
test_not_writable::<CreateEscrowFixture>(&mut ctx, 3);
}
#[test]
fn test_create_escrow_wrong_system_program() {
let mut ctx = TestContext::new();
test_wrong_system_program::<CreateEscrowFixture>(&mut ctx);
}
#[test]
fn test_create_escrow_wrong_current_program() {
let mut ctx = TestContext::new();
test_wrong_current_program::<CreateEscrowFixture>(&mut ctx);
}
#[test]
fn test_create_escrow_invalid_event_authority() {
let mut ctx = TestContext::new();
// event_authority is at index 5 in instruction accounts
// Custom error 2 = InvalidEventAuthority
test_wrong_account::<CreateEscrowFixture>(&mut ctx, 5, InstructionError::Custom(2));
}
#[test]
fn test_create_escrow_invalid_bump() {
let mut ctx = TestContext::new();
// Build a valid instruction first to get the correct bump
let valid_ix = CreateEscrowFixture::build_valid(&mut ctx);
let correct_bump = valid_ix.instruction.data[1];
// Use incorrect bump (correct_bump + 1, wrapping)
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_create_escrow_noncanonical_bump_rejected() {
let mut ctx = TestContext::new();
let valid_ix = CreateEscrowFixture::build_valid(&mut ctx);
let escrow_seed = valid_ix.instruction.accounts[2].pubkey;
let program_id = valid_ix.instruction.program_id;
let (noncanonical_escrow, noncanonical_bump) =
find_noncanonical_program_address(&[b"escrow", escrow_seed.as_ref()], &program_id)
.expect("expected at least one noncanonical bump");
let error = valid_ix
.with_account_at(3, noncanonical_escrow)
.with_data_byte_at(1, noncanonical_bump)
.send_expect_error(&mut ctx);
assert_instruction_error(error, InstructionError::InvalidSeeds);
}
#[test]
fn test_create_escrow_empty_data() {
let mut ctx = TestContext::new();
test_empty_data::<CreateEscrowFixture>(&mut ctx);
}
// ============================================================================
// Happy Path Test
// ============================================================================
#[test]
fn test_create_escrow_success() {
let mut ctx = TestContext::new();
let test_ix = CreateEscrowFixture::build_valid(&mut ctx);
let admin_pubkey = test_ix.signers[0].pubkey();
let escrow_seed_pubkey = test_ix.signers[1].pubkey();
let escrow_pda = test_ix.instruction.accounts[3].pubkey;
let bump = test_ix.instruction.data[1];
test_ix.send_expect_success(&mut ctx);
assert_escrow_account(&ctx, &escrow_pda, &admin_pubkey, bump, &escrow_seed_pubkey);
assert_escrow_mutability(&ctx, &escrow_pda, false);
}
#[test]
fn test_create_escrow_prefunded_pda_succeeds() {
let mut ctx = TestContext::new();
let test_ix = CreateEscrowFixture::build_valid(&mut ctx);
let admin_pubkey = test_ix.signers[0].pubkey();
let escrow_seed_pubkey = test_ix.signers[1].pubkey();
let escrow_pda = test_ix.instruction.accounts[3].pubkey;
let bump = test_ix.instruction.data[1];
// Simulate griefing by pre-funding the PDA before initialization.
ctx.svm
.set_account(
escrow_pda,
Account { lamports: 1, data: vec![], owner: Pubkey::default(), executable: false, rent_epoch: 0 },
)
.unwrap();
test_ix.send_expect_success(&mut ctx);
assert_escrow_account(&ctx, &escrow_pda, &admin_pubkey, bump, &escrow_seed_pubkey);
assert_escrow_mutability(&ctx, &escrow_pda, false);
}
// ============================================================================
// Re-initialization Protection Tests
// ============================================================================
#[test]
fn test_create_escrow_reinitialization_fails() {
let mut ctx = TestContext::new();
let test_ix = CreateEscrowFixture::build_valid(&mut ctx);
let admin = test_ix.signers[0].insecure_clone();
let escrow_seed = test_ix.signers[1].insecure_clone();
let escrow_pda = test_ix.instruction.accounts[3].pubkey;
let bump = test_ix.instruction.data[1];
test_ix.send_expect_success(&mut ctx);
assert_escrow_account(&ctx, &escrow_pda, &admin.pubkey(), bump, &escrow_seed.pubkey());
let attacker = ctx.create_funded_keypair();
let reinit_ix = CreatesEscrowBuilder::new()
.payer(ctx.payer.pubkey())
.admin(attacker.pubkey())
.escrow_seed(escrow_seed.pubkey())
.escrow(escrow_pda)
.bump(bump)
.instruction();
let error = ctx.send_transaction_expect_error(reinit_ix, &[&attacker, &escrow_seed]);
assert_instruction_error(error, InstructionError::AccountAlreadyInitialized);
assert_escrow_account(&ctx, &escrow_pda, &admin.pubkey(), bump, &escrow_seed.pubkey());
}