diff --git a/swap/src/lib.rs b/swap/src/lib.rs index d85355a7..240cb68f 100644 --- a/swap/src/lib.rs +++ b/swap/src/lib.rs @@ -23,6 +23,8 @@ pub mod monero; pub mod network; pub mod protocol; pub mod seed; +#[cfg(test)] +mod tests; pub mod trace; mod fs; diff --git a/swap/src/main.rs b/swap/src/main.rs index 06fd7c00..8c674752 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -33,6 +33,8 @@ pub mod monero; pub mod network; pub mod protocol; pub mod seed; +#[cfg(test)] +mod tests; pub mod trace; #[macro_use] diff --git a/swap/tests/happy_path.rs b/swap/src/tests/happy_path.rs similarity index 82% rename from swap/tests/happy_path.rs rename to swap/src/tests/happy_path.rs index 435edfa8..87455aa5 100644 --- a/swap/tests/happy_path.rs +++ b/swap/src/tests/happy_path.rs @@ -1,13 +1,14 @@ -pub mod testutils; - -use swap::protocol::{alice, bob}; +use crate::{ + protocol::{alice, bob}, + tests::setup_test, +}; use tokio::join; /// Run the following tests with RUST_MIN_STACK=10000000 #[tokio::test] async fn happy_path() { - testutils::setup_test(|mut ctx| async move { + setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/src/tests/happy_path_restart_alice.rs similarity index 83% rename from swap/tests/happy_path_restart_alice.rs rename to swap/src/tests/happy_path_restart_alice.rs index afc918d0..b5179bf5 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/src/tests/happy_path_restart_alice.rs @@ -1,11 +1,11 @@ -pub mod testutils; - -use swap::protocol::{alice, alice::AliceState, bob}; -use testutils::alice_run_until::is_encsig_learned; +use crate::{ + protocol::{alice, alice::AliceState, bob}, + tests::{alice_run_until::is_encsig_learned, setup_test}, +}; #[tokio::test] async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { - testutils::setup_test(|mut ctx| async move { + setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/src/tests/happy_path_restart_bob_after_comm.rs similarity index 83% rename from swap/tests/happy_path_restart_bob_after_comm.rs rename to swap/src/tests/happy_path_restart_bob_after_comm.rs index fa1c14ff..8d907fbd 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/src/tests/happy_path_restart_bob_after_comm.rs @@ -1,11 +1,11 @@ -pub mod testutils; - -use swap::protocol::{alice, bob, bob::BobState}; -use testutils::bob_run_until::is_encsig_sent; +use crate::{ + protocol::{alice, bob, bob::BobState}, + tests::{bob_run_until::is_encsig_sent, setup_test}, +}; #[tokio::test] async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { - testutils::setup_test(|mut ctx| async move { + setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs b/swap/src/tests/happy_path_restart_bob_after_lock_proof_received.rs similarity index 83% rename from swap/tests/happy_path_restart_bob_after_lock_proof_received.rs rename to swap/src/tests/happy_path_restart_bob_after_lock_proof_received.rs index 4d5509c2..56a30491 100644 --- a/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs +++ b/swap/src/tests/happy_path_restart_bob_after_lock_proof_received.rs @@ -1,11 +1,11 @@ -pub mod testutils; - -use swap::protocol::{alice, bob, bob::BobState}; -use testutils::bob_run_until::is_lock_proof_received; +use crate::{ + protocol::{alice, bob, bob::BobState}, + tests::{bob_run_until::is_lock_proof_received, setup_test}, +}; #[tokio::test] async fn given_bob_restarts_after_lock_proof_received_resume_swap() { - testutils::setup_test(|mut ctx| async move { + setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_before_comm.rs b/swap/src/tests/happy_path_restart_bob_before_comm.rs similarity index 83% rename from swap/tests/happy_path_restart_bob_before_comm.rs rename to swap/src/tests/happy_path_restart_bob_before_comm.rs index e3aa8b77..5601fb72 100644 --- a/swap/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/src/tests/happy_path_restart_bob_before_comm.rs @@ -1,11 +1,11 @@ -pub mod testutils; - -use swap::protocol::{alice, bob, bob::BobState}; -use testutils::bob_run_until::is_xmr_locked; +use crate::{ + protocol::{alice, bob, bob::BobState}, + tests::{bob_run_until::is_xmr_locked, setup_test}, +}; #[tokio::test] async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { - testutils::setup_test(|mut ctx| async move { + setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/testutils/mod.rs b/swap/src/tests/mod.rs similarity index 96% rename from swap/tests/testutils/mod.rs rename to swap/src/tests/mod.rs index e21aa1a4..07959d47 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/src/tests/mod.rs @@ -1,23 +1,31 @@ -use crate::testutils; -use bitcoin_harness::Bitcoind; -use futures::Future; -use get_port::get_port; -use libp2p::{core::Multiaddr, PeerId}; -use monero_harness::{image, Monero}; -use std::{path::PathBuf, sync::Arc}; -use swap::{ +use crate::{ bitcoin, config::Config, monero, protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts}, seed::Seed, }; +use bitcoin_harness::Bitcoind; +use futures::Future; +use get_port::get_port; +use libp2p::{core::Multiaddr, PeerId}; +use monero_harness::{image, Monero}; +use std::{path::PathBuf, sync::Arc}; use tempfile::tempdir; use testcontainers::{clients::Cli, Container}; use tracing_core::dispatcher::DefaultGuard; use tracing_log::LogTracer; use uuid::Uuid; +mod happy_path; +mod happy_path_restart_alice; +mod happy_path_restart_bob_after_comm; +mod happy_path_restart_bob_after_lock_proof_received; +mod happy_path_restart_bob_before_comm; +mod punish; +mod refund_restart_alice; +mod refund_restart_alice_cancelled; + #[derive(Debug, Clone)] pub struct StartingBalances { pub xmr: monero::Amount, @@ -297,7 +305,7 @@ where let _guard = init_tracing(); - let (monero, containers) = testutils::init_containers(&cli).await; + let (monero, containers) = init_containers(&cli).await; let swap_amounts = SwapAmounts { btc: bitcoin::Amount::from_sat(1_000_000), @@ -398,13 +406,13 @@ async fn init_wallets( .await .unwrap(); - let xmr_wallet = Arc::new(swap::monero::Wallet { + let xmr_wallet = Arc::new(monero::Wallet { inner: monero.wallet(name).unwrap().client(), network: config.monero_network, }); let btc_wallet = Arc::new( - swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), config.bitcoin_network) + bitcoin::Wallet::new(name, bitcoind.node_url.clone(), config.bitcoin_network) .await .unwrap(), ); @@ -461,7 +469,7 @@ fn init_tracing() -> DefaultGuard { } pub mod alice_run_until { - use swap::protocol::alice::AliceState; + use crate::protocol::alice::AliceState; pub fn is_xmr_locked(state: &AliceState) -> bool { matches!( @@ -479,7 +487,7 @@ pub mod alice_run_until { } pub mod bob_run_until { - use swap::protocol::bob::BobState; + use crate::protocol::bob::BobState; pub fn is_btc_locked(state: &BobState) -> bool { matches!(state, BobState::BtcLocked(..)) diff --git a/swap/tests/punish.rs b/swap/src/tests/punish.rs similarity index 86% rename from swap/tests/punish.rs rename to swap/src/tests/punish.rs index 51487c7f..3d3285ff 100644 --- a/swap/tests/punish.rs +++ b/swap/src/tests/punish.rs @@ -1,13 +1,13 @@ -pub mod testutils; - -use swap::protocol::{alice, bob, bob::BobState}; -use testutils::bob_run_until::is_btc_locked; +use crate::{ + protocol::{alice, bob, bob::BobState}, + tests::{bob_run_until::is_btc_locked, setup_test}, +}; /// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice /// the encsig and fail to refund or redeem. Alice punishes. #[tokio::test] async fn alice_punishes_if_bob_never_acts_after_fund() { - testutils::setup_test(|mut ctx| async move { + setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/refund_restart_alice.rs b/swap/src/tests/refund_restart_alice.rs similarity index 85% rename from swap/tests/refund_restart_alice.rs rename to swap/src/tests/refund_restart_alice.rs index 1b147863..a17c2f98 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/src/tests/refund_restart_alice.rs @@ -1,13 +1,13 @@ -pub mod testutils; - -use swap::protocol::{alice, alice::AliceState, bob}; -use testutils::alice_run_until::is_xmr_locked; +use crate::{ + protocol::{alice, alice::AliceState, bob}, + tests::{alice_run_until::is_xmr_locked, setup_test}, +}; /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// then also refunds. #[tokio::test] async fn given_alice_restarts_after_xmr_is_locked_refund_swap() { - testutils::setup_test(|mut ctx| async move { + setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await; diff --git a/swap/tests/refund_restart_alice_cancelled.rs b/swap/src/tests/refund_restart_alice_cancelled.rs similarity index 87% rename from swap/tests/refund_restart_alice_cancelled.rs rename to swap/src/tests/refund_restart_alice_cancelled.rs index 24f5415f..9b5d0fc3 100644 --- a/swap/tests/refund_restart_alice_cancelled.rs +++ b/swap/src/tests/refund_restart_alice_cancelled.rs @@ -1,14 +1,14 @@ -pub mod testutils; - -use swap::protocol::{alice, alice::AliceState, bob}; -use testutils::alice_run_until::is_encsig_learned; +use crate::{ + protocol::{alice, alice::AliceState, bob}, + tests::{alice_run_until::is_encsig_learned, setup_test}, +}; /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// is forced to refund even though she learned the secret and would be able to /// redeem had the timelock not expired. #[tokio::test] async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_refund_swap() { - testutils::setup_test(|mut ctx| async move { + setup_test(|mut ctx| async move { let alice_swap = ctx.new_swap_as_alice().await; let bob_swap = ctx.new_swap_as_bob().await;