From 35b469d7380a4c0b487b144bb5dd88e2ceef9d4d Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Tue, 3 Feb 2026 10:43:05 -0300 Subject: [PATCH] add tests --- wallet-ffi/src/account.rs | 1 + wallet-ffi/tests/ffi.rs | 138 ++++++++++++++++++++++++++++---------- 2 files changed, 102 insertions(+), 37 deletions(-) diff --git a/wallet-ffi/src/account.rs b/wallet-ffi/src/account.rs index b99d10cf..e5fa84b0 100644 --- a/wallet-ffi/src/account.rs +++ b/wallet-ffi/src/account.rs @@ -101,6 +101,7 @@ pub unsafe extern "C" fn wallet_ffi_create_account_private( }; let (account_id, _chain_index) = wallet.create_new_account_private(None); + println!("_chain_index: {_chain_index}"); unsafe { (*out_account_id).data = *account_id.value(); diff --git a/wallet-ffi/tests/ffi.rs b/wallet-ffi/tests/ffi.rs index 43d22063..70a82874 100644 --- a/wallet-ffi/tests/ffi.rs +++ b/wallet-ffi/tests/ffi.rs @@ -1,18 +1,23 @@ -use std::ffi::{c_char, CString}; +use std::{ + ffi::{c_char, CString}, + path::Path, +}; +use tokio::runtime::Handle; use wallet::WalletCore; use wallet_ffi::{error, FfiBytes32, FfiError, WalletHandle}; extern "C" { - fn wallet_ffi_init_runtime() -> error::WalletFfiError; - - fn wallet_ffi_destroy(handle: *mut WalletHandle); - fn wallet_ffi_create_account_public( handle: *mut WalletHandle, out_account_id: *mut FfiBytes32, ) -> error::WalletFfiError; + fn wallet_ffi_create_account_private( + handle: *mut WalletHandle, + out_account_id: *mut FfiBytes32, + ) -> error::WalletFfiError; + fn wallet_ffi_create_new( config_path: *const c_char, storage_path: *const c_char, @@ -24,42 +29,101 @@ extern "C" { use tempfile::tempdir; -#[test] -fn test() { - unsafe { - let result = wallet_ffi_init_runtime(); - println!("wallet init runtim result: {:?}", result); - } - +unsafe fn new_wallet_ffi_for_tests(password: &str) -> *mut WalletHandle { let tempdir = tempdir().unwrap(); let config_path = tempdir.path().join("wallet_config.json"); let storage_path = tempdir.path().join("storage.json"); let config_path_c = CString::new(config_path.to_str().unwrap()).unwrap(); let storage_path_c = CString::new(storage_path.to_str().unwrap()).unwrap(); - let password = CString::new("").unwrap(); + let password = CString::new(password).unwrap(); - unsafe { - let wallet_handle = wallet_ffi_create_new( - config_path_c.as_ptr(), - storage_path_c.as_ptr(), - password.as_ptr(), - ); - - let mut out_account_id = FfiBytes32::from_bytes([0; 32]); - - let result = wallet_ffi_create_account_public( - wallet_handle, - (&mut out_account_id) as *mut FfiBytes32, - ); - println!("{:?}", out_account_id.data); - println!("create result: {:?}", result); - - let result = wallet_ffi_save(wallet_handle); - println!("save result: {:?}", result); - - wallet_ffi_destroy(wallet_handle); - } - // let mut wallet_core = WalletCore::new_update_chain(config_path.to_path_buf(), - // storage_path.to_path_buf(), None).unwrap(); let (account_id, _) = - // wallet_core.create_new_account_public(None); println!("{:?}", account_id); + wallet_ffi_create_new( + config_path_c.as_ptr(), + storage_path_c.as_ptr(), + password.as_ptr(), + ) +} + +fn new_wallet_rust_for_tests(password: &str) -> WalletCore { + let tempdir = tempdir().unwrap(); + let config_path = tempdir.path().join("wallet_config.json"); + let storage_path = tempdir.path().join("storage.json"); + + WalletCore::new_init_storage( + config_path.to_path_buf(), + storage_path.to_path_buf(), + None, + password.to_string(), + ) + .unwrap() +} + +#[test] +fn test_create_public_accounts() { + let password = "password_for_tests"; + let n_accounts = 10; + // First `n_accounts` public accounts created with Rust wallet + let new_public_account_ids_rust = { + let mut account_ids = Vec::new(); + + let mut wallet_rust = new_wallet_rust_for_tests(password); + for _ in 0..n_accounts { + let account_id = wallet_rust.create_new_account_public(None).0; + account_ids.push(*account_id.value()); + } + account_ids + }; + + // First `n_accounts` public accounts created with wallet FFI + let new_public_account_ids_ffi = unsafe { + let mut account_ids = Vec::new(); + + let wallet_ffi_handle = new_wallet_ffi_for_tests(password); + for _ in 0..n_accounts { + let mut out_account_id = FfiBytes32::from_bytes([0; 32]); + wallet_ffi_create_account_public( + wallet_ffi_handle, + (&mut out_account_id) as *mut FfiBytes32, + ); + account_ids.push(out_account_id.data); + } + account_ids + }; + + assert_eq!(new_public_account_ids_ffi, new_public_account_ids_rust) +} + +#[test] +fn test_create_private_accounts() { + let password = "password_for_tests"; + let n_accounts = 10; + // First `n_accounts` private accounts created with Rust wallet + let new_private_account_ids_rust = { + let mut account_ids = Vec::new(); + + let mut wallet_rust = new_wallet_rust_for_tests(password); + for _ in 0..n_accounts { + let account_id = wallet_rust.create_new_account_private(None).0; + account_ids.push(*account_id.value()); + } + account_ids + }; + + // First `n_accounts` private accounts created with wallet FFI + let new_private_account_ids_ffi = unsafe { + let mut account_ids = Vec::new(); + + let wallet_ffi_handle = new_wallet_ffi_for_tests(password); + for _ in 0..n_accounts { + let mut out_account_id = FfiBytes32::from_bytes([0; 32]); + wallet_ffi_create_account_private( + wallet_ffi_handle, + (&mut out_account_id) as *mut FfiBytes32, + ); + account_ids.push(out_account_id.data); + } + account_ids + }; + + assert_eq!(new_private_account_ids_ffi, new_private_account_ids_rust) }