-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathtest.rs
More file actions
135 lines (109 loc) · 3.79 KB
/
test.rs
File metadata and controls
135 lines (109 loc) · 3.79 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
use borsh::BorshDeserialize;
use counter_solana_native::Counter;
use litesvm::LiteSVM;
use solana_instruction::{AccountMeta, Instruction};
use solana_keypair::{Keypair, Signer};
use solana_native_token::LAMPORTS_PER_SOL;
use solana_pubkey::Pubkey;
use solana_rent::Rent;
use solana_system_interface::instruction::create_account;
use solana_transaction::Transaction;
#[test]
fn test_counter() {
let program_id = Pubkey::new_unique();
let program_bytes = include_bytes!("../../tests/fixtures/counter_solana_native.so");
let mut svm = LiteSVM::new();
svm.add_program(program_id, program_bytes).unwrap();
let payer = Keypair::new();
let counter_account = Keypair::new();
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
let counter_account_size = std::mem::size_of::<Counter>();
let create_ix = create_account(
&payer.pubkey(),
&counter_account.pubkey(),
Rent::default().minimum_balance(counter_account_size),
counter_account_size as u64,
&program_id,
);
let tx = Transaction::new_signed_with_payer(
&[create_ix],
Some(&payer.pubkey()),
&[&payer, &counter_account],
svm.latest_blockhash(),
);
assert!(svm.send_transaction(tx).is_ok());
let ix = Instruction {
program_id,
accounts: vec![AccountMeta::new(counter_account.pubkey(), false)],
data: vec![0],
};
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&payer.pubkey()),
&[payer],
svm.latest_blockhash(),
);
assert!(svm.send_transaction(tx).is_ok());
let counter_account_data = svm.get_account(&counter_account.pubkey()).unwrap().data;
let counter = Counter::try_from_slice(&counter_account_data).unwrap();
assert_eq!(counter.count, 1);
}
#[test]
fn test_unknown_instruction_fails() {
let program_id = Pubkey::new_unique();
let program_bytes = include_bytes!("../../tests/fixtures/counter_solana_native.so");
let mut svm = LiteSVM::new();
svm.add_program(program_id, program_bytes).unwrap();
let payer = Keypair::new();
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL).unwrap();
let ix = Instruction {
program_id,
accounts: vec![],
data: vec![99],
};
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&payer.pubkey()),
&[payer],
svm.latest_blockhash(),
);
assert!(svm.send_transaction(tx).is_err());
}
#[test]
fn test_readonly_counter_fails() {
let program_id = Pubkey::new_unique();
let program_bytes = include_bytes!("../../tests/fixtures/counter_solana_native.so");
let mut svm = LiteSVM::new();
svm.add_program(program_id, program_bytes).unwrap();
let payer = Keypair::new();
let counter_account = Keypair::new();
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
let counter_account_size = std::mem::size_of::<Counter>();
let create_ix = create_account(
&payer.pubkey(),
&counter_account.pubkey(),
Rent::default().minimum_balance(counter_account_size),
counter_account_size as u64,
&program_id,
);
let tx = Transaction::new_signed_with_payer(
&[create_ix],
Some(&payer.pubkey()),
&[&payer, &counter_account],
svm.latest_blockhash(),
);
assert!(svm.send_transaction(tx).is_ok());
// Mark counter account as readonly; program should return an error (not panic).
let ix = Instruction {
program_id,
accounts: vec![AccountMeta::new_readonly(counter_account.pubkey(), false)],
data: vec![0],
};
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&payer.pubkey()),
&[payer],
svm.latest_blockhash(),
);
assert!(svm.send_transaction(tx).is_err());
}