forked from Northgate-Systems/RemitX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rs
More file actions
282 lines (244 loc) · 8.86 KB
/
Copy pathtest.rs
File metadata and controls
282 lines (244 loc) · 8.86 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
//! Unit tests for the RemitX Escrow Contract.
//!
//! Deploys a simple mock token contract to satisfy the token transfers
//! performed by deposit()/release()/refund(), then tests all contract
//! behaviors end to end.
#![cfg(test)]
use super::*;
use soroban_sdk::testutils::{Address as _, Ledger, Register};
use soroban_sdk::{contract, contractimpl, Address, BytesN, Env};
// ------------------------------------------------------------------------
// Minimal mock token contract (test-only) implementing the SEP-41 transfer
// surface needed by the escrow contract (transfer / balance / mint).
// ------------------------------------------------------------------------
#[derive(Clone, Debug, Eq, PartialEq)]
#[soroban_sdk::contracttype]
pub enum MockTokenDataKey {
Balance(Address),
}
#[contract]
pub struct MockToken;
#[contractimpl]
impl MockToken {
/// Mint `amount` of the token to `to`.
pub fn mint(env: Env, to: Address, amount: i128) {
let cur: i128 = env
.storage()
.instance()
.get(&MockTokenDataKey::Balance(to.clone()))
.unwrap_or(0);
env.storage()
.instance()
.set(&MockTokenDataKey::Balance(to), &(cur + amount));
}
/// Return the balance of `id`.
pub fn balance(env: Env, id: Address) -> i128 {
env.storage()
.instance()
.get(&MockTokenDataKey::Balance(id))
.unwrap_or(0)
}
/// Transfer `amount` from `from` to `to`.
pub fn transfer(env: Env, from: Address, to: Address, amount: i128) {
let from_bal: i128 = env
.storage()
.instance()
.get(&MockTokenDataKey::Balance(from.clone()))
.unwrap_or(0);
if from_bal < amount {
panic!("insufficient balance");
}
let to_bal: i128 = env
.storage()
.instance()
.get(&MockTokenDataKey::Balance(to.clone()))
.unwrap_or(0);
env.storage()
.instance()
.set(&MockTokenDataKey::Balance(from), &(from_bal - amount));
env.storage()
.instance()
.set(&MockTokenDataKey::Balance(to), &(to_bal + amount));
}
}
// ----------------------------------------------------------------------------
// Test helpers
// ----------------------------------------------------------------------------
struct TestHarness<'a> {
_env: Env,
escrow: EscrowContractClient<'a>,
token: Address,
sender: Address,
recipient: Address,
}
/// Deploy mock token + escrow, mint funds, and create a funded escrow.
fn setup(env: &Env, expires_in: u64) -> (TestHarness<'_>, BytesN<32>) {
env.mock_all_auths();
// Deploy mock token.
let token = MockToken.register(env, None, ());
let token_client = MockTokenClient::new(env, &token);
// Fund two addresses.
let sender = Address::generate(env);
let recipient = Address::generate(env);
token_client.mint(&sender, &10_000_000i128);
token_client.mint(&recipient, &5_000_000i128);
// Deploy escrow and create the escrow.
let escrow = EscrowContractClient::new(env, &EscrowContract.register(env, None, ()));
let amount: i128 = 1_000_000;
let expires_at = env.ledger().timestamp() + expires_in;
let escrow_id = escrow.deposit(&sender, &recipient, &amount, &token, &expires_at);
let h = TestHarness {
_env: env.clone(),
escrow,
token,
sender,
recipient,
};
(h, escrow_id)
}
// ----------------------------------------------------------------------------
// deposit() tests
// ----------------------------------------------------------------------------
#[test]
fn test_deposit_happy_path() {
let env = Env::default();
let (h, id) = setup(&env, 3600);
let state = h.escrow.get_escrow(&id);
assert_eq!(state.sender, h.sender);
assert_eq!(state.recipient, h.recipient);
assert_eq!(state.amount, 1_000_000i128);
assert_eq!(state.asset, h.token);
assert_eq!(state.status, EscrowStatus::Locked);
assert!(state.expires_at > env.ledger().timestamp());
}
#[test]
fn test_deposit_increments_escrow_count() {
let env = Env::default();
let (h, _) = setup(&env, 3600);
assert_eq!(h.escrow.get_escrow_count(), 1u32);
// Create a second escrow.
let expires_at = env.ledger().timestamp() + 7200;
let id2 = h
.escrow
.deposit(&h.sender, &h.recipient, &500_000i128, &h.token, &expires_at);
assert_eq!(h.escrow.get_escrow_count(), 2u32);
assert_ne!(id2, BytesN::from_array(&env, &[0u8; 32]));
// Third.
let expires_at = env.ledger().timestamp() + 10_800;
h.escrow
.deposit(&h.recipient, &h.sender, &200_000i128, &h.token, &expires_at);
assert_eq!(h.escrow.get_escrow_count(), 3u32);
}
#[test]
#[should_panic(expected = "amount must be greater than zero")]
fn test_deposit_rejects_zero_amount() {
let env = Env::default();
let (h, _) = setup(&env, 3600);
let expires_at = env.ledger().timestamp() + 3600;
h.escrow.deposit(&h.sender, &h.recipient, &0i128, &h.token, &expires_at);
}
#[test]
#[should_panic(expected = "amount must be greater than zero")]
fn test_deposit_rejects_negative_amount() {
let env = Env::default();
let (h, _) = setup(&env, 3600);
let expires_at = env.ledger().timestamp() + 3600;
h.escrow
.deposit(&h.sender, &h.recipient, &(-100i128), &h.token, &expires_at);
}
#[test]
#[should_panic(expected = "expires_at must be in the future")]
fn test_deposit_rejects_past_expiry() {
let env = Env::default();
env.ledger().set_timestamp(1_000_000);
let (h, _) = setup(&env, 3600);
let expires_at = env.ledger().timestamp() - 100; // already expired
h.escrow
.deposit(&h.sender, &h.recipient, &1_000_000i128, &h.token, &expires_at);
}
// ----------------------------------------------------------------------------
// release() tests
// ----------------------------------------------------------------------------
#[test]
fn test_release_before_expiry() {
let env = Env::default();
let (h, id) = setup(&env, 3600);
// Recipient has 5,000,000 before release.
h.escrow.release(&id);
let state = h.escrow.get_escrow(&id);
assert_eq!(state.status, EscrowStatus::Released);
// Funds moved to recipient.
let token_client = MockTokenClient::new(&env, &h.token);
let rec_bal = token_client.balance(&h.recipient);
assert_eq!(rec_bal, 5_000_000i128 + 1_000_000i128);
}
#[test]
#[should_panic(expected = "escrow has expired")]
fn test_release_after_expiry_panics() {
let env = Env::default();
let (h, id) = setup(&env, 3600);
// Advance the ledger past the expiry.
env.ledger().set_timestamp(env.ledger().timestamp() + 7200);
h.escrow.release(&id);
}
// ----------------------------------------------------------------------------
// refund() tests
// ----------------------------------------------------------------------------
#[test]
fn test_refund_after_expiry() {
let env = Env::default();
let (h, id) = setup(&env, 3600);
// Advance the ledger past the expiry.
env.ledger().set_timestamp(env.ledger().timestamp() + 7200);
h.escrow.refund(&id);
let state = h.escrow.get_escrow(&id);
assert_eq!(state.status, EscrowStatus::Refunded);
// Funds returned to sender.
let token_client = MockTokenClient::new(&env, &h.token);
let send_bal = token_client.balance(&h.sender);
assert_eq!(send_bal, 10_000_000i128); // sent 1,000,000 out, got it back
}
#[test]
#[should_panic(expected = "escrow has not expired yet")]
fn test_refund_before_expiry_panics() {
let env = Env::default();
let (h, id) = setup(&env, 3600);
h.escrow.refund(&id);
}
// ----------------------------------------------------------------------------
// Guard tests (double actions + non-existent escrows)
// ----------------------------------------------------------------------------
#[test]
#[should_panic(expected = "escrow is not in Locked status")]
fn test_double_release_prevented() {
let env = Env::default();
let (h, id) = setup(&env, 3600);
h.escrow.release(&id);
h.escrow.release(&id);
}
#[test]
#[should_panic(expected = "escrow is not in Locked status")]
fn test_double_refund_prevented() {
let env = Env::default();
let (h, id) = setup(&env, 3600);
// Advance the ledger past the expiry.
env.ledger().set_timestamp(env.ledger().timestamp() + 7200);
h.escrow.refund(&id);
h.escrow.refund(&id);
}
#[test]
#[should_panic(expected = "Escrow not found")]
fn test_release_nonexistent_escrow() {
let env = Env::default();
let (h, _) = setup(&env, 3600);
let fake_id = BytesN::from_array(&env, &[0u8; 32]);
h.escrow.release(&fake_id);
}
#[test]
#[should_panic(expected = "Escrow not found")]
fn test_refund_nonexistent_escrow() {
let env = Env::default();
let (h, _) = setup(&env, 3600);
let fake_id = BytesN::from_array(&env, &[0u8; 32]);
h.escrow.refund(&fake_id);
}