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
|
use monero_c_rust::{WalletManager, NetworkType, WalletError, WalletResult};
use std::fs;
use std::sync::Arc;
use std::time::Instant;
use tempfile::TempDir;
const TEST_WALLET_NAMES: &[&str] = &[
"test_wallet",
"mainnet_wallet",
"testnet_wallet",
"stagenet_wallet",
];
/// Helper function to clean up existing wallet files in a temporary directory.
fn check_and_delete_existing_wallets(temp_dir: &TempDir) -> std::io::Result<()> {
for name in TEST_WALLET_NAMES {
// Construct absolute paths for wallet files.
let wallet_file = temp_dir.path().join(name);
let keys_file = temp_dir.path().join(format!("{}.keys", name));
let address_file = temp_dir.path().join(format!("{}.address.txt", name)); // Added
// Delete wallet file if it exists.
if wallet_file.exists() {
if let Err(e) = fs::remove_file(&wallet_file) {
println!("Warning: Failed to delete wallet file {:?}: {}", wallet_file, e);
} else {
println!("Deleted existing wallet file: {:?}", wallet_file);
}
}
// Delete keys file if it exists.
if keys_file.exists() {
if let Err(e) = fs::remove_file(&keys_file) {
println!("Warning: Failed to delete keys file {:?}: {}", keys_file, e);
} else {
println!("Deleted existing keys file: {:?}", keys_file);
}
}
// Delete address file if it exists.
if address_file.exists() {
if let Err(e) = fs::remove_file(&address_file) {
println!("Warning: Failed to delete address file {:?}: {}", address_file, e);
} else {
println!("Deleted existing address file: {:?}", address_file);
}
}
}
Ok(())
}
/// Sets up the test environment by creating a temporary directory and initializing the WalletManager.
///
/// Returns:
/// - An `Arc` wrapped `WalletManager` instance.
/// - A `TempDir` representing the temporary directory.
fn setup() -> WalletResult<(Arc<WalletManager>, TempDir)> {
println!("Setting up test environment...");
let temp_dir = tempfile::tempdir().expect("Failed to create temporary directory");
check_and_delete_existing_wallets(&temp_dir).expect("Failed to clean up existing wallets");
println!("Creating WalletManager...");
let start = Instant::now();
let manager = WalletManager::new()?;
println!("WalletManager creation took {:?}", start.elapsed());
Ok((manager, temp_dir))
}
/// Tears down the test environment by deleting wallet files.
///
/// Args:
/// - `temp_dir`: Reference to the temporary directory.
///
/// Returns:
/// - `Result<(), std::io::Error>` indicating success or failure.
fn teardown(temp_dir: &TempDir) -> std::io::Result<()> {
println!("Tearing down test environment...");
check_and_delete_existing_wallets(temp_dir)
}
#[test]
fn test_wallet_manager_creation() {
println!("Running test_wallet_manager_creation");
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
// Construct the full path for the wallet within temp_dir.
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet_result = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet);
assert!(wallet_result.is_ok(), "WalletManager creation seems to have failed");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_wallet_creation() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
let wallet_path = temp_dir.path().join("wallet_name");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet);
assert!(wallet.is_ok(), "Failed to create wallet");
let wallet = wallet.unwrap();
assert!(wallet.is_deterministic().is_ok(), "Wallet creation seems to have failed");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_get_seed() {
println!("Running test_get_seed");
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
// Construct the full path for the wallet within temp_dir.
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager
.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
// Test getting seed without offset.
println!("Attempting to get seed without offset...");
let start = Instant::now();
let result = wallet.get_seed(None);
println!("get_seed without offset took {:?}", start.elapsed());
assert!(result.is_ok(), "Failed to get seed: {:?}", result.err());
assert!(!result.unwrap().is_empty(), "Seed is empty");
// Test getting seed with an offset.
println!("Attempting to get seed with offset...");
let start = Instant::now();
let result_with_offset = wallet.get_seed(Some("example_offset"));
println!("get_seed with offset took {:?}", start.elapsed());
assert!(result_with_offset.is_ok(), "Failed to get seed with offset: {:?}", result_with_offset.err());
assert!(!result_with_offset.unwrap().is_empty(), "Seed with offset is empty");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_get_address() {
println!("Running test_get_address");
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
// Construct the full path for the wallet within temp_dir.
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager
.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
println!("Attempting to get address...");
let start = Instant::now();
let result = wallet.get_address(0, 0);
println!("get_address took {:?}", start.elapsed());
assert!(result.is_ok(), "Failed to get address: {:?}", result.err());
assert!(!result.unwrap().is_empty(), "Address is empty");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_is_deterministic() {
println!("Running test_is_deterministic");
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
// Construct the full path for the wallet within temp_dir.
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager
.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
println!("Checking if wallet is deterministic...");
let start = Instant::now();
let result = wallet.is_deterministic();
println!("is_deterministic check took {:?}", start.elapsed());
assert!(result.is_ok(), "Failed to check if wallet is deterministic: {:?}", result.err());
assert!(result.unwrap(), "Wallet should be deterministic");
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_wallet_creation_with_different_networks() {
println!("Running test_wallet_creation_with_different_networks");
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
// Define wallet names and corresponding network types.
let wallets = vec![
("mainnet_wallet", NetworkType::Mainnet),
("testnet_wallet", NetworkType::Testnet),
("stagenet_wallet", NetworkType::Stagenet),
];
for (name, net_type) in wallets {
println!("Creating wallet: {} on network type {:?}", name, net_type);
// Construct the full path for each wallet within temp_dir.
let wallet_path = temp_dir.path().join(name);
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager.create_wallet(wallet_str, "password", "English", net_type);
assert!(wallet.is_ok(), "Failed to create wallet: {}", name);
}
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_multiple_address_generation() {
println!("Running test_multiple_address_generation");
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
// Construct the full path for the wallet within temp_dir.
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager
.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
for i in 0..5 {
println!("Generating address {}...", i);
let start = Instant::now();
let result = wallet.get_address(0, i);
println!("Address generation took {:?}", start.elapsed());
assert!(result.is_ok(), "Failed to get address {}: {:?}", i, result.err());
assert!(!result.unwrap().is_empty(), "Address {} is empty", i);
}
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_wallet_error_display() {
println!("Running test_wallet_error_display");
// Test WalletError::FfiError variant.
let error = WalletError::FfiError("Test error".to_string());
match error {
WalletError::FfiError(msg) => assert_eq!(msg, "Test error"),
_ => panic!("Expected FfiError variant"),
}
// Test WalletError::NullPointer variant.
let error = WalletError::NullPointer;
match error {
WalletError::NullPointer => assert!(true),
_ => panic!("Expected NullPointer variant"),
}
// Test WalletError::WalletErrorCode variant.
let error = WalletError::WalletErrorCode(2, "Sample wallet error".to_string());
match error {
WalletError::WalletErrorCode(code, msg) => {
assert_eq!(code, 2);
assert_eq!(msg, "Sample wallet error");
},
_ => panic!("Expected WalletErrorCode variant"),
}
}
#[test]
fn test_wallet_status_integration() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
// Create a wallet.
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
// Check the status of the wallet.
let status = manager.get_status(wallet.ptr.as_ptr());
assert!(status.is_ok(), "Expected status OK, got error: {:?}", status.err());
// Clean up.
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_open_wallet_integration() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
// Create a wallet.
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager
.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
// Drop the wallet to simulate closing it.
drop(wallet);
// Try opening the wallet.
let open_result = manager.open_wallet(wallet_str, "password", NetworkType::Mainnet);
assert!(open_result.is_ok(), "Failed to open wallet: {:?}", open_result.err());
// Clean up.
teardown(&temp_dir).expect("Failed to clean up after test");
}
#[test]
fn test_get_balance_integration() {
println!("Running test_get_balance_integration");
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
// Construct the full path for the wallet within temp_dir.
let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
// Create the wallet.
let wallet = manager
.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
.expect("Failed to create wallet");
// Fetch the balance.
println!("Fetching wallet balance...");
let start = Instant::now();
let balance_result = wallet.get_balance(0); // Account index 0
println!("Fetching balance took {:?}", start.elapsed());
assert!(balance_result.is_ok(), "Failed to fetch balance: {:?}", balance_result.err());
let balance = balance_result.unwrap();
println!("Balance: {:?}", balance);
// Ensure the balance values make sense.
// assert!(balance.balance >= 0, "Balance should be non-negative");
// assert!(balance.unlocked_balance >= 0, "Unlocked balance should be non-negative");
// These assertions are meaningless with the constraints of the type.
// TODO: Test with scanning integration.
teardown(&temp_dir).expect("Failed to clean up after test");
}
|