Skip to content

Commit

Permalink
Test chat sideloading
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Dec 14, 2023
1 parent cdba761 commit 50e4a55
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 4 deletions.
40 changes: 40 additions & 0 deletions integration_tests/src/chat_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ extern "C" {
callback_delivery_confirmation_received: unsafe extern "C" fn(*mut c_void),
callback_read_confirmation_received: unsafe extern "C" fn(*mut c_void),
) -> *mut ClientFFI;
pub fn sideload_chat_client(
config: *mut c_void,
contact_handle: *mut c_void,
error_out: *const c_int,
callback_contact_status_change: unsafe extern "C" fn(*mut c_void),
callback_message_received: unsafe extern "C" fn(*mut c_void),
callback_delivery_confirmation_received: unsafe extern "C" fn(*mut c_void),
callback_read_confirmation_received: unsafe extern "C" fn(*mut c_void),
) -> *mut ClientFFI;
pub fn create_chat_message(receiver: *mut c_void, message: *const c_char, error_out: *const c_int) -> *mut c_void;
pub fn send_chat_message(client: *mut ClientFFI, message: *mut c_void, error_out: *const c_int);
pub fn add_chat_message_metadata(
Expand Down Expand Up @@ -300,6 +309,37 @@ pub async fn spawn_ffi_chat_client(name: &str, seed_peers: Vec<Peer>, base_dir:
}
}

pub async fn sideload_ffi_chat_client(
address: TariAddress,
base_dir: PathBuf,
contacts_handle_ptr: *mut c_void,
) -> ChatFFI {
let mut config = test_config(Multiaddr::empty());
config.chat_client.set_base_path(base_dir);

let config_ptr = Box::into_raw(Box::new(config)) as *mut c_void;

let client_ptr;
let error_out = Box::into_raw(Box::new(0));
unsafe {
*ChatCallback::instance().contact_status_change.lock().unwrap() = 0;

client_ptr = sideload_chat_client(
config_ptr,
contacts_handle_ptr,
error_out,
callback_contact_status_change,
callback_message_received,
callback_delivery_confirmation_received,
callback_read_confirmation_received,
);
}

ChatFFI {
ptr: Arc::new(Mutex::new(PtrWrapper(client_ptr))),
address,
}
}
static mut INSTANCE: Option<ChatCallback> = None;
static START: Once = Once::new();

Expand Down
1 change: 1 addition & 0 deletions integration_tests/src/ffi/ffi_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,5 @@ extern "C" {
error_out: *mut c_int,
) -> c_ulonglong;
pub fn fee_per_gram_stat_destroy(fee_per_gram_stat: *mut TariFeePerGramStat);
pub fn contacts_handle(wallet: *mut TariWallet, error_out: *mut c_int) -> *mut c_void;
}
12 changes: 12 additions & 0 deletions integration_tests/src/ffi/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,16 @@ impl Wallet {
}
FeePerGramStats::from_ptr(ptr)
}

pub fn contacts_handle(&self) -> *mut c_void {
let ptr;
let mut error = 0;
unsafe {
ptr = ffi_import::contacts_handle(self.ptr, &mut error);
if error > 0 {
println!("contacts_handle error {}", error);
}
}
ptr
}
}
14 changes: 11 additions & 3 deletions integration_tests/src/wallet_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ use crate::{
pub struct WalletFFI {
pub name: String,
pub port: u64,
// pub grpc_port: u64,
// pub temp_dir_path: String,
pub base_dir: PathBuf,
pub wallet: Arc<Mutex<ffi::Wallet>>,
}

Expand All @@ -76,7 +75,12 @@ impl WalletFFI {
.unwrap()
.into();
let wallet = ffi::Wallet::create(comms_config, log_path, seed_words_ptr);
Self { name, port, wallet }
Self {
name,
port,
base_dir: base_dir_path,
wallet,
}
}

pub fn identify(&self) -> String {
Expand Down Expand Up @@ -186,6 +190,10 @@ impl WalletFFI {
pub fn get_fee_per_gram_stats(&self, count: u32) -> FeePerGramStats {
self.wallet.lock().unwrap().get_fee_per_gram_stats(count)
}

pub fn contacts_handle(&self) -> *mut c_void {
self.wallet.lock().unwrap().contacts_handle()
}
}

pub fn spawn_wallet_ffi(world: &mut TariWorld, wallet_name: String, seed_words_ptr: *const c_void) {
Expand Down
8 changes: 8 additions & 0 deletions integration_tests/tests/features/ChatFFI.feature
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,11 @@ Feature: Chat FFI messaging
When CHAT_A will have 1 message with CHAT_C
When CHAT_A will have 1 message with CHAT_D
Then CHAT_A will have 3 conversationalists

Scenario: A message is propagated between side loaded chat and client via 3rd party
Given I have a seed node SEED_A
Given I have a ffi wallet WALLET_A connected to base node SEED_A
When I have a sideloaded chat FFI client CHAT_A from WALLET_A
When I have a chat FFI client CHAT_B connected to seed node SEED_A
When I use CHAT_A to send a message 'Hey there' to CHAT_B
Then CHAT_B will have 1 message with CHAT_A
12 changes: 11 additions & 1 deletion integration_tests/tests/steps/chat_ffi_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::time::Duration;
use cucumber::{then, when};
use tari_common_types::tari_address::TariAddress;
use tari_integration_tests::{
chat_ffi::{spawn_ffi_chat_client, ChatCallback},
chat_ffi::{sideload_ffi_chat_client, spawn_ffi_chat_client, ChatCallback},
TariWorld,
};

Expand All @@ -44,6 +44,16 @@ async fn chat_ffi_client_connected_to_base_node(world: &mut TariWorld, name: Str
world.chat_clients.insert(name, Box::new(client));
}

#[when(expr = "I have a sideloaded chat FFI client {word} from {word}")]
async fn sideloaded_chat_ffi_client_connected_to_wallet(world: &mut TariWorld, chat_name: String, wallet_name: String) {
let wallet = world.get_ffi_wallet(&wallet_name).unwrap();
let pubkey = world.get_wallet_address(&wallet_name).await.unwrap();
let address = TariAddress::from_hex(&pubkey).unwrap();

let client = sideload_ffi_chat_client(address, wallet.base_dir.clone(), wallet.contacts_handle()).await;
world.chat_clients.insert(chat_name, Box::new(client));
}

#[then(expr = "there will be a contact status update callback of at least {int}")]
async fn contact_status_update_callback(_world: &mut TariWorld, callback_count: usize) {
let mut count = 0;
Expand Down

0 comments on commit 50e4a55

Please sign in to comment.