diff --git a/bindings/src/ffi_test_utils.rs b/bindings/src/ffi_test_utils.rs index fa4e7d4523f..d5e4a8241e3 100644 --- a/bindings/src/ffi_test_utils.rs +++ b/bindings/src/ffi_test_utils.rs @@ -1,7 +1,17 @@ use crate::error::FFIResult; +use lightning::util::events::Event; +use lightning::chain::transaction::OutPoint; +use bitcoin::hash_types::Txid; +use hex; +use crate::adaptors::primitives::FFIEvents; +use crate::utils::into_fixed_buffer; +use crate::Out; +use lightning::ln::channelmanager::{PaymentHash, PaymentSecret, PaymentPreimage}; +use bitcoin_hashes::core::time::Duration; +use lightning::chain::keysinterface::SpendableOutputDescriptor; +use bitcoin::TxOut; -// These tests should be used for asserting that the wrapper code can see the expected -// error messages when it fails (or succeeds). +// These tests should be used for asserting that the wrapper can receive expected items from rust. ffi! { fn ffi_test_error() -> FFIResult { use std::io; @@ -12,5 +22,48 @@ ffi! { fn ffi_test_ok() -> FFIResult { FFIResult::ok() } -} + fn test_event_serialization(buf_out: Out, buf_len: usize, actual_len: Out) -> FFIResult { + let mut events = Vec::with_capacity(5); + + let txid = bitcoin::consensus::deserialize(&hex::decode("4141414141414141414141414141414141414141414141414141414141414141").unwrap()).unwrap(); + let funding_txo= OutPoint::new(txid, 0); + let user_channel_id = 1111; + events.push(Event::FundingBroadcastSafe {funding_txo, user_channel_id} ); + + let payment_hash = PaymentHash([2;32]); + let payment_secret = Some(PaymentSecret([3; 32])); + let amt = 50000; + events.push(Event::PaymentReceived {payment_secret, payment_hash, amt}); + + + let payment_preimage = PaymentPreimage([4;32]); + events.push(Event::PaymentSent {payment_preimage}); + + let payment_hash = PaymentHash([5;32]); + let rejected_by_dest = true; + events.push(Event::PaymentFailed {payment_hash, rejected_by_dest}); + + let time_forwardable = Duration::from_millis(100); + events.push(Event::PendingHTLCsForwardable {time_forwardable}); + + let outpoint = bitcoin::blockdata::transaction::OutPoint::new(txid, 0); + let output = TxOut {value: 255, script_pubkey: bitcoin::blockdata::script::Script::new() }; + let static_output = SpendableOutputDescriptor::StaticOutput {outpoint, output: output.clone()}; + + let key = bitcoin::secp256k1::key::SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()).unwrap(); + let dynamic_output_p2wsh = SpendableOutputDescriptor::DynamicOutputP2WSH { + outpoint, + key, + witness_script: bitcoin::blockdata::script::Script::new(), + to_self_delay: 144, + output + }; + let outputs = vec![static_output, dynamic_output_p2wsh]; + events.push(Event::SpendableOutputs {outputs}); + + let mut e = FFIEvents{ events }; + let buf = unsafe_block!("The buffer lives as long as this function, the length is within the buffer and the buffer won't be read before initialization" => buf_out.as_uninit_bytes_mut(buf_len)); + into_fixed_buffer(&mut e, buf, &mut actual_len) + } +} diff --git a/bindings/src/test_utils.rs b/bindings/src/test_utils.rs index f9c40c6bc56..3e986508f0f 100644 --- a/bindings/src/test_utils.rs +++ b/bindings/src/test_utils.rs @@ -1,3 +1,15 @@ +macro_rules! assert_match { + ($bind:pat = $bind_from:expr) => { + assert_match!($bind = $bind_from => ()) + }; + ($bind:pat = $bind_from:expr => $with:expr) => { + match $bind_from { + $bind => $with, + _ => panic!("assertion failed: unexpected value `{:?}`", $bind_from), + } + }; +} + pub mod static_assert { use std::panic::UnwindSafe;