-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathutxo_tests.rs
More file actions
578 lines (497 loc) · 19.9 KB
/
utxo_tests.rs
File metadata and controls
578 lines (497 loc) · 19.9 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#[cfg(test)]
mod utxo_tests {
use super::super::*;
use crate::error::{FFIError, FFIErrorCode};
use key_wallet::managed_account::managed_account_type::ManagedAccountType;
use key_wallet::Utxo;
use std::ffi::CStr;
use std::ptr;
#[test]
fn test_ffi_utxo_new() {
let txid = [1u8; 32];
let vout = 0;
let amount = 100000;
let address = "yXdxAYfK7KGx7gNpVHUfRsQMNpMj5cAadG".to_string();
let script = vec![0x76, 0xa9, 0x14]; // Sample script
let height = 12345;
let confirmations = 10;
let utxo = FFIUTXO::new(
txid,
vout,
amount,
address.clone(),
script.clone(),
height,
confirmations,
);
assert_eq!(utxo.txid, txid);
assert_eq!(utxo.vout, vout);
assert_eq!(utxo.amount, amount);
assert!(!utxo.address.is_null());
assert!(!utxo.script_pubkey.is_null());
assert_eq!(utxo.script_len, script.len());
assert_eq!(utxo.height, height);
assert_eq!(utxo.confirmations, confirmations);
// Verify address
let addr_str = unsafe { CStr::from_ptr(utxo.address).to_str().unwrap() };
assert_eq!(addr_str, address);
// Clean up
unsafe {
let mut utxo = utxo;
utxo.free();
}
}
#[test]
fn test_ffi_utxo_new_empty_script() {
let txid = [2u8; 32];
let utxo = FFIUTXO::new(
txid,
1,
50000,
"yYNrYTYsV8xCTMAz5wXmKzn7eqUe5p5V8V".to_string(),
vec![],
100,
5,
);
assert_eq!(utxo.txid, txid);
assert!(utxo.script_pubkey.is_null());
assert_eq!(utxo.script_len, 0);
// Clean up
unsafe {
let mut utxo = utxo;
utxo.free();
}
}
#[test]
fn test_deprecated_wallet_get_utxos() {
let mut error = FFIError::success();
let error = &mut error as *mut FFIError;
let mut utxos_out: *mut FFIUTXO = ptr::null_mut();
let mut count_out: usize = 0;
// The deprecated function should always return an empty list
let success = unsafe {
#[allow(deprecated)]
wallet_get_utxos(ptr::null(), &mut utxos_out, &mut count_out, error)
};
assert!(success);
assert_eq!(count_out, 0);
assert!(utxos_out.is_null());
}
#[test]
fn test_managed_wallet_get_utxos_null() {
let mut error = FFIError::success();
let error = &mut error as *mut FFIError;
let mut utxos_out: *mut FFIUTXO = ptr::null_mut();
let mut count_out: usize = 0;
// Test with null managed_info
let result =
unsafe { managed_wallet_get_utxos(ptr::null(), &mut utxos_out, &mut count_out, error) };
assert!(!result);
assert_eq!(unsafe { (*error).code }, FFIErrorCode::InvalidInput);
unsafe { (*error).free_message() };
}
#[test]
fn test_managed_wallet_get_utxos_empty() {
use crate::managed_wallet::FFIManagedWalletInfo;
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
use key_wallet::Network;
let mut error = FFIError::success();
let error = &mut error as *mut FFIError;
let mut utxos_out: *mut FFIUTXO = ptr::null_mut();
let mut count_out: usize = 0;
// Create an empty managed wallet info heap-allocated like C would do
let managed_info = ManagedWalletInfo::new(Network::Testnet, [0u8; 32]);
let ffi_managed_info = Box::into_raw(Box::new(FFIManagedWalletInfo::new(managed_info)));
let result = unsafe {
managed_wallet_get_utxos(&*ffi_managed_info, &mut utxos_out, &mut count_out, error)
};
assert!(result);
assert_eq!(unsafe { (*error).code }, FFIErrorCode::Success);
assert_eq!(count_out, 0);
assert!(utxos_out.is_null());
unsafe { crate::managed_wallet::managed_wallet_free(ffi_managed_info) };
}
// Note: There's no individual utxo_free function, only utxo_array_free
#[test]
fn test_utxo_array_free() {
// Create some test UTXOs in the same format as managed_wallet_get_utxos returns
let mut utxos = Vec::new();
for i in 0..3 {
let utxo = FFIUTXO::new(
[i as u8; 32],
i as u32,
(i as u64 + 1) * 10000,
format!("address_{}", i),
vec![0x76, 0xa9, i as u8],
i as u32 * 100,
i as u32,
);
utxos.push(utxo);
}
// Convert to boxed slice and get raw pointer (same as in managed_wallet_get_utxos)
let count = utxos.len();
let mut boxed_utxos = utxos.into_boxed_slice();
let utxos_ptr = boxed_utxos.as_mut_ptr();
std::mem::forget(boxed_utxos);
// Free the UTXOs
unsafe {
utxo_array_free(utxos_ptr, count);
}
}
#[test]
fn test_utxo_array_free_null() {
// Should handle null gracefully
unsafe {
utxo_array_free(ptr::null_mut(), 0);
}
}
#[test]
fn test_managed_wallet_get_utxos_with_data() {
use crate::managed_wallet::FFIManagedWalletInfo;
use dashcore::blockdata::script::ScriptBuf;
use dashcore::{Address, OutPoint, TxOut, Txid};
use key_wallet::account::account_type::StandardAccountType;
use key_wallet::managed_account::ManagedCoreAccount;
use key_wallet::utxo::Utxo;
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
use key_wallet::Network;
let mut error = FFIError::success();
let error = &mut error as *mut FFIError;
let mut utxos_out: *mut FFIUTXO = ptr::null_mut();
let mut count_out: usize = 0;
// Create a managed wallet info with some UTXOs
let mut managed_info = ManagedWalletInfo::new(Network::Testnet, [1u8; 32]);
// Create a BIP44 account with UTXOs
let mut bip44_account = ManagedCoreAccount::new(
ManagedAccountType::Standard {
index: 0,
standard_account_type: StandardAccountType::BIP44Account,
external_addresses:
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
.base_path(key_wallet::DerivationPath::from(vec![]))
.build()
.unwrap(),
internal_addresses:
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
.base_path(key_wallet::DerivationPath::from(vec![]))
.build()
.unwrap(),
},
Network::Testnet,
false,
);
// Add multiple UTXOs
for i in 0..3 {
let mut txid_bytes = [0u8; 32];
txid_bytes[0] = i as u8;
let outpoint = OutPoint {
txid: Txid::from(txid_bytes),
vout: i as u32,
};
let txout = TxOut {
value: (i as u64 + 1) * 50000,
script_pubkey: ScriptBuf::from(vec![]),
};
// Create a dummy P2PKH address
let dummy_pubkey_hash = dashcore::PubkeyHash::from([0u8; 20]);
let script = ScriptBuf::new_p2pkh(&dummy_pubkey_hash);
let address = Address::from_script(&script, Network::Testnet).unwrap();
let mut utxo = Utxo::new(outpoint, txout, address, 100 + i as u32, false);
utxo.is_confirmed = true;
bip44_account.utxos.insert(outpoint, utxo);
}
managed_info.accounts.insert(bip44_account);
let ffi_managed_info = Box::into_raw(Box::new(FFIManagedWalletInfo::new(managed_info)));
unsafe { (*ffi_managed_info).inner_mut() }.update_synced_height(300);
let result = unsafe {
managed_wallet_get_utxos(&*ffi_managed_info, &mut utxos_out, &mut count_out, error)
};
assert!(result);
assert_eq!(unsafe { (*error).code }, FFIErrorCode::Success);
assert_eq!(count_out, 3);
assert!(!utxos_out.is_null());
// Verify UTXO data
unsafe {
let utxos = std::slice::from_raw_parts(utxos_out, count_out);
// Check first UTXO
assert_eq!(utxos[0].txid[0], 0);
assert_eq!(utxos[0].vout, 0);
assert_eq!(utxos[0].amount, 50000);
assert_eq!(utxos[0].height, 100);
assert_eq!(utxos[0].confirmations, 201);
// Check second UTXO
assert_eq!(utxos[1].txid[0], 1);
assert_eq!(utxos[1].vout, 1);
assert_eq!(utxos[1].amount, 100000);
assert_eq!(utxos[1].height, 101);
assert_eq!(utxos[1].confirmations, 200);
// Check third UTXO
assert_eq!(utxos[2].txid[0], 2);
assert_eq!(utxos[2].vout, 2);
assert_eq!(utxos[2].amount, 150000);
assert_eq!(utxos[2].height, 102);
assert_eq!(utxos[2].confirmations, 199);
}
// Clean up
unsafe {
utxo_array_free(utxos_out, count_out);
crate::managed_wallet::managed_wallet_free(ffi_managed_info);
}
}
#[test]
fn test_managed_wallet_get_utxos_multiple_accounts() {
use crate::managed_wallet::FFIManagedWalletInfo;
use key_wallet::account::account_type::StandardAccountType;
use key_wallet::managed_account::ManagedCoreAccount;
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
use key_wallet::Network;
let mut error = FFIError::success();
let error = &mut error as *mut FFIError;
let mut utxos_out: *mut FFIUTXO = ptr::null_mut();
let mut count_out: usize = 0;
let mut managed_info = ManagedWalletInfo::new(Network::Testnet, [2u8; 32]);
// Create BIP44 account with 2 UTXOs
let mut bip44_account = ManagedCoreAccount::new(
ManagedAccountType::Standard {
index: 0,
standard_account_type: StandardAccountType::BIP44Account,
external_addresses:
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
.base_path(key_wallet::DerivationPath::from(vec![]))
.build()
.unwrap(),
internal_addresses:
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
.base_path(key_wallet::DerivationPath::from(vec![]))
.build()
.unwrap(),
},
Network::Testnet,
false,
);
let utxos = Utxo::new_test_batch(0..2, 10000, 100, false, false);
for utxo in utxos {
bip44_account.utxos.insert(utxo.outpoint, utxo);
}
managed_info.accounts.insert(bip44_account);
// Create BIP32 account with 1 UTXO
let mut bip32_account = ManagedCoreAccount::new(
ManagedAccountType::Standard {
index: 0,
standard_account_type: StandardAccountType::BIP32Account,
external_addresses:
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
.base_path(key_wallet::DerivationPath::from(vec![]))
.build()
.unwrap(),
internal_addresses:
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
.base_path(key_wallet::DerivationPath::from(vec![]))
.build()
.unwrap(),
},
Network::Testnet,
false,
);
let utxos = Utxo::new_test_batch(10..11, 20000, 200, false, false);
for utxo in utxos {
bip32_account.utxos.insert(utxo.outpoint, utxo);
}
managed_info.accounts.insert(bip32_account);
// Create CoinJoin account with 2 UTXOs
let mut coinjoin_account = ManagedCoreAccount::new(
ManagedAccountType::CoinJoin {
index: 0,
addresses: key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
.base_path(key_wallet::DerivationPath::from(vec![]))
.build()
.unwrap(),
},
Network::Testnet,
false,
);
let utxos = Utxo::new_test_batch(20..22, 30000, 300, false, false);
for utxo in utxos {
coinjoin_account.utxos.insert(utxo.outpoint, utxo);
}
managed_info.accounts.insert(coinjoin_account);
let ffi_managed_info = Box::into_raw(Box::new(FFIManagedWalletInfo::new(managed_info)));
let result = unsafe {
managed_wallet_get_utxos(&*ffi_managed_info, &mut utxos_out, &mut count_out, error)
};
assert!(result);
assert_eq!(count_out, 5); // 2 from BIP44, 1 from BIP32, 2 from CoinJoin
assert!(!utxos_out.is_null());
// Clean up
unsafe {
utxo_array_free(utxos_out, count_out);
crate::managed_wallet::managed_wallet_free(ffi_managed_info);
}
}
#[test]
fn test_managed_wallet_get_utxos() {
use crate::managed_wallet::FFIManagedWalletInfo;
use key_wallet::account::account_type::StandardAccountType;
use key_wallet::managed_account::ManagedCoreAccount;
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
use key_wallet::Network;
let mut error = FFIError::success();
let error = &mut error as *mut FFIError;
let mut utxos_out: *mut FFIUTXO = ptr::null_mut();
let mut count_out: usize = 0;
// Create managed wallet info for testnet
let mut managed_info = ManagedWalletInfo::new(Network::Testnet, [3u8; 32]);
// Add a UTXO to Testnet account
let mut testnet_account = ManagedCoreAccount::new(
ManagedAccountType::Standard {
index: 0,
standard_account_type: StandardAccountType::BIP44Account,
external_addresses:
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
.base_path(key_wallet::DerivationPath::from(vec![]))
.build()
.unwrap(),
internal_addresses:
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
.base_path(key_wallet::DerivationPath::from(vec![]))
.build()
.unwrap(),
},
Network::Testnet,
false,
);
let utxos = Utxo::new_test_batch(1..2, 10000, 100, false, false);
for utxo in utxos {
testnet_account.utxos.insert(utxo.outpoint, utxo);
}
managed_info.accounts.insert(testnet_account);
let ffi_managed_info = Box::into_raw(Box::new(FFIManagedWalletInfo::new(managed_info)));
// Get UTXOs
let result = unsafe {
managed_wallet_get_utxos(&*ffi_managed_info, &mut utxos_out, &mut count_out, error)
};
assert!(result);
assert_eq!(count_out, 1);
unsafe {
utxo_array_free(utxos_out, count_out);
crate::managed_wallet::managed_wallet_free(ffi_managed_info);
}
}
#[test]
fn test_ffi_utxo_with_large_script() {
let txid = [0xAAu8; 32];
let vout = 42;
let amount = 1000000;
let address = "yXdxAYfK7KGx7gNpVHUfRsQMNpMj5cAadG".to_string();
let script = vec![0x76; 1000]; // Large script
let height = 654321;
let confirmations = 100;
let utxo = FFIUTXO::new(
txid,
vout,
amount,
address.clone(),
script.clone(),
height,
confirmations,
);
assert_eq!(utxo.txid, txid);
assert_eq!(utxo.vout, vout);
assert_eq!(utxo.amount, amount);
assert_eq!(utxo.script_len, 1000);
assert_eq!(utxo.height, height);
assert_eq!(utxo.confirmations, confirmations);
// Verify script content
unsafe {
let script_slice = std::slice::from_raw_parts(utxo.script_pubkey, utxo.script_len);
assert!(script_slice.iter().all(|&b| b == 0x76));
let mut utxo = utxo;
utxo.free();
}
}
#[test]
fn test_ffi_utxo_edge_values() {
// Test with maximum values
let txid = [0xFFu8; 32];
let vout = u32::MAX;
let amount = u64::MAX;
let address = "x".repeat(100); // Long address
let script = vec![0x00];
let height = u32::MAX;
let confirmations = u32::MAX;
let utxo = FFIUTXO::new(txid, vout, amount, address.clone(), script, height, confirmations);
assert_eq!(utxo.vout, u32::MAX);
assert_eq!(utxo.amount, u64::MAX);
assert_eq!(utxo.height, u32::MAX);
assert_eq!(utxo.confirmations, u32::MAX);
// Clean up
unsafe {
let mut utxo = utxo;
utxo.free();
}
}
#[test]
fn test_utxo_array_free_with_mixed_content() {
// Create UTXOs with different properties
let utxos = vec![
// UTXO with normal values
FFIUTXO::new([0x01u8; 32], 0, 10000, "address1".to_string(), vec![0x76, 0xa9], 100, 10),
// UTXO with empty script
FFIUTXO::new([0x02u8; 32], 1, 20000, "address2".to_string(), vec![], 200, 20),
// UTXO with large script
FFIUTXO::new([0x03u8; 32], 2, 30000, "address3".to_string(), vec![0xAB; 500], 300, 30),
];
let count = utxos.len();
let mut boxed_utxos = utxos.into_boxed_slice();
let utxos_ptr = boxed_utxos.as_mut_ptr();
std::mem::forget(boxed_utxos);
// Free should handle all different UTXO types
unsafe {
utxo_array_free(utxos_ptr, count);
}
}
#[test]
fn test_managed_wallet_get_utxos_null_outputs() {
use crate::managed_wallet::FFIManagedWalletInfo;
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
use key_wallet::Network;
let mut error = FFIError::success();
let error = &mut error as *mut FFIError;
let mut count_out: usize = 0;
let managed_info = ManagedWalletInfo::new(Network::Testnet, [4u8; 32]);
let ffi_managed_info = Box::into_raw(Box::new(FFIManagedWalletInfo::new(managed_info)));
// Test with null utxos_out
let result = unsafe {
managed_wallet_get_utxos(&*ffi_managed_info, ptr::null_mut(), &mut count_out, error)
};
assert!(!result);
assert_eq!(unsafe { (*error).code }, FFIErrorCode::InvalidInput);
// Test with null count_out
let mut utxos_out: *mut FFIUTXO = ptr::null_mut();
let result = unsafe {
managed_wallet_get_utxos(&*ffi_managed_info, &mut utxos_out, ptr::null_mut(), error)
};
assert!(!result);
assert_eq!(unsafe { (*error).code }, FFIErrorCode::InvalidInput);
unsafe {
crate::managed_wallet::managed_wallet_free(ffi_managed_info);
(*error).free_message();
}
}
#[test]
fn test_ffi_utxo_free_idempotent() {
let utxo =
FFIUTXO::new([0x05u8; 32], 0, 10000, "test_address".to_string(), vec![0x76], 100, 1);
unsafe {
let mut utxo = utxo;
// First free
utxo.free();
// After free, pointers should be null
assert!(utxo.address.is_null());
assert!(utxo.script_pubkey.is_null());
assert_eq!(utxo.script_len, 0);
// Second free should be safe (no-op)
utxo.free();
}
}
}