|
2 | 2 |
|
3 | 3 | use key_wallet_ffi::error::{FFIError, FFIErrorCode}; |
4 | 4 |
|
| 5 | +/// Helper to test an FFIError conversion and clean up the message |
| 6 | +fn assert_ffi_error_code(mut ffi_err: FFIError, expected: FFIErrorCode) { |
| 7 | + assert_eq!(ffi_err.code, expected); |
| 8 | + unsafe { ffi_err.free_message() }; |
| 9 | +} |
| 10 | + |
5 | 11 | #[test] |
6 | 12 | fn test_key_wallet_error_to_ffi_error() { |
7 | 13 | use key_wallet::Error as KeyWalletError; |
8 | 14 |
|
9 | 15 | // Test InvalidMnemonic conversion |
10 | 16 | let err = KeyWalletError::InvalidMnemonic("bad mnemonic".to_string()); |
11 | | - let ffi_err: FFIError = err.into(); |
12 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidMnemonic); |
| 17 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidMnemonic); |
13 | 18 |
|
14 | 19 | // Test InvalidNetwork conversion |
15 | 20 | let err = KeyWalletError::InvalidNetwork; |
16 | | - let ffi_err: FFIError = err.into(); |
17 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidNetwork); |
| 21 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidNetwork); |
18 | 22 |
|
19 | 23 | // Test InvalidAddress conversion |
20 | 24 | let err = KeyWalletError::InvalidAddress("bad address".to_string()); |
21 | | - let ffi_err: FFIError = err.into(); |
22 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidAddress); |
| 25 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidAddress); |
23 | 26 |
|
24 | 27 | // Test InvalidDerivationPath conversion |
25 | 28 | let err = KeyWalletError::InvalidDerivationPath("bad path".to_string()); |
26 | | - let ffi_err: FFIError = err.into(); |
27 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidDerivationPath); |
| 29 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidDerivationPath); |
28 | 30 |
|
29 | 31 | // Test InvalidParameter conversion |
30 | 32 | let err = KeyWalletError::InvalidParameter("bad param".to_string()); |
31 | | - let ffi_err: FFIError = err.into(); |
32 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidInput); |
| 33 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidInput); |
33 | 34 |
|
34 | 35 | // Test Serialization conversion |
35 | 36 | let err = KeyWalletError::Serialization("serialization failed".to_string()); |
36 | | - let ffi_err: FFIError = err.into(); |
37 | | - assert_eq!(ffi_err.code, FFIErrorCode::SerializationError); |
| 37 | + assert_ffi_error_code(err.into(), FFIErrorCode::SerializationError); |
38 | 38 |
|
39 | 39 | // Test WatchOnly conversion |
40 | 40 | let err = KeyWalletError::WatchOnly; |
41 | | - let ffi_err: FFIError = err.into(); |
42 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidState); |
| 41 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidState); |
43 | 42 |
|
44 | 43 | // Test CoinJoinNotEnabled conversion |
45 | 44 | let err = KeyWalletError::CoinJoinNotEnabled; |
46 | | - let ffi_err: FFIError = err.into(); |
47 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidState); |
| 45 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidState); |
48 | 46 |
|
49 | 47 | // Test KeyError conversion (should map to WalletError) |
50 | 48 | let err = KeyWalletError::KeyError("key error".to_string()); |
51 | | - let ffi_err: FFIError = err.into(); |
52 | | - assert_eq!(ffi_err.code, FFIErrorCode::WalletError); |
| 49 | + assert_ffi_error_code(err.into(), FFIErrorCode::WalletError); |
53 | 50 |
|
54 | 51 | // Test Base58 conversion (should map to WalletError) |
55 | 52 | let err = KeyWalletError::Base58; |
56 | | - let ffi_err: FFIError = err.into(); |
57 | | - assert_eq!(ffi_err.code, FFIErrorCode::WalletError); |
| 53 | + assert_ffi_error_code(err.into(), FFIErrorCode::WalletError); |
58 | 54 | } |
59 | 55 |
|
60 | 56 | #[test] |
61 | 57 | fn test_wallet_manager_error_to_ffi_error() { |
62 | 58 | use key_wallet_manager::wallet_manager::WalletError; |
63 | 59 |
|
64 | | - // Test WalletNotFound conversion |
65 | 60 | let wallet_id = [0u8; 32]; |
| 61 | + |
| 62 | + // Test WalletNotFound conversion |
66 | 63 | let err = WalletError::WalletNotFound(wallet_id); |
67 | | - let ffi_err: FFIError = err.into(); |
68 | | - assert_eq!(ffi_err.code, FFIErrorCode::NotFound); |
| 64 | + assert_ffi_error_code(err.into(), FFIErrorCode::NotFound); |
69 | 65 |
|
70 | 66 | // Test InvalidMnemonic conversion |
71 | 67 | let err = WalletError::InvalidMnemonic("bad mnemonic".to_string()); |
72 | | - let ffi_err: FFIError = err.into(); |
73 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidMnemonic); |
| 68 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidMnemonic); |
74 | 69 |
|
75 | 70 | // Test InvalidNetwork conversion |
76 | 71 | let err = WalletError::InvalidNetwork; |
77 | | - let ffi_err: FFIError = err.into(); |
78 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidNetwork); |
| 72 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidNetwork); |
79 | 73 |
|
80 | 74 | // Test AccountNotFound conversion |
81 | 75 | let err = WalletError::AccountNotFound(0); |
82 | | - let ffi_err: FFIError = err.into(); |
83 | | - assert_eq!(ffi_err.code, FFIErrorCode::NotFound); |
| 76 | + assert_ffi_error_code(err.into(), FFIErrorCode::NotFound); |
84 | 77 |
|
85 | 78 | // Test AddressGeneration conversion |
86 | 79 | let err = WalletError::AddressGeneration("failed to generate".to_string()); |
87 | | - let ffi_err: FFIError = err.into(); |
88 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidAddress); |
| 80 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidAddress); |
89 | 81 |
|
90 | 82 | // Test InvalidParameter conversion |
91 | 83 | let err = WalletError::InvalidParameter("bad param".to_string()); |
92 | | - let ffi_err: FFIError = err.into(); |
93 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidInput); |
| 84 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidInput); |
94 | 85 |
|
95 | 86 | // Test TransactionBuild conversion |
96 | 87 | let err = WalletError::TransactionBuild("tx build failed".to_string()); |
97 | | - let ffi_err: FFIError = err.into(); |
98 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidTransaction); |
| 88 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidTransaction); |
99 | 89 |
|
100 | 90 | // Test InsufficientFunds conversion |
101 | 91 | let err = WalletError::InsufficientFunds; |
102 | | - let ffi_err: FFIError = err.into(); |
103 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidState); |
| 92 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidState); |
104 | 93 |
|
105 | 94 | // Test WalletCreation conversion |
106 | 95 | let err = WalletError::WalletCreation("creation failed".to_string()); |
107 | | - let ffi_err: FFIError = err.into(); |
108 | | - assert_eq!(ffi_err.code, FFIErrorCode::WalletError); |
| 96 | + assert_ffi_error_code(err.into(), FFIErrorCode::WalletError); |
109 | 97 |
|
110 | 98 | // Test WalletExists conversion |
111 | 99 | let err = WalletError::WalletExists(wallet_id); |
112 | | - let ffi_err: FFIError = err.into(); |
113 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidState); |
| 100 | + assert_ffi_error_code(err.into(), FFIErrorCode::InvalidState); |
114 | 101 |
|
115 | 102 | // Test AccountCreation conversion |
116 | 103 | let err = WalletError::AccountCreation("account creation failed".to_string()); |
117 | | - let ffi_err: FFIError = err.into(); |
118 | | - assert_eq!(ffi_err.code, FFIErrorCode::WalletError); |
| 104 | + assert_ffi_error_code(err.into(), FFIErrorCode::WalletError); |
119 | 105 | } |
120 | 106 |
|
121 | 107 | #[test] |
@@ -201,9 +187,7 @@ fn test_error_message_consistency() { |
201 | 187 |
|
202 | 188 | // Convert to FFIError |
203 | 189 | let ffi_err: FFIError = key_err.into(); |
204 | | - // Note: We can't easily check the message in FFIError since it's a raw pointer |
205 | | - // but we know it should contain the original message |
206 | | - assert_eq!(ffi_err.code, FFIErrorCode::InvalidMnemonic); |
| 190 | + assert_ffi_error_code(ffi_err, FFIErrorCode::InvalidMnemonic); |
207 | 191 | } |
208 | 192 |
|
209 | 193 | #[test] |
|
0 commit comments