Move tests under src

This commit is contained in:
Franck Royer 2021-01-21 14:15:19 +11:00
parent 91fe18a796
commit f24f77fcd1
No known key found for this signature in database
GPG key ID: A82ED75A8DFC50A4
11 changed files with 65 additions and 52 deletions

View file

@ -23,6 +23,8 @@ pub mod monero;
pub mod network; pub mod network;
pub mod protocol; pub mod protocol;
pub mod seed; pub mod seed;
#[cfg(test)]
mod tests;
pub mod trace; pub mod trace;
mod fs; mod fs;

View file

@ -33,6 +33,8 @@ pub mod monero;
pub mod network; pub mod network;
pub mod protocol; pub mod protocol;
pub mod seed; pub mod seed;
#[cfg(test)]
mod tests;
pub mod trace; pub mod trace;
#[macro_use] #[macro_use]

View file

@ -1,13 +1,14 @@
pub mod testutils; use crate::{
protocol::{alice, bob},
use swap::protocol::{alice, bob}; tests::setup_test,
};
use tokio::join; use tokio::join;
/// Run the following tests with RUST_MIN_STACK=10000000 /// Run the following tests with RUST_MIN_STACK=10000000
#[tokio::test] #[tokio::test]
async fn happy_path() { 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 alice_swap = ctx.new_swap_as_alice().await;
let bob_swap = ctx.new_swap_as_bob().await; let bob_swap = ctx.new_swap_as_bob().await;

View file

@ -1,11 +1,11 @@
pub mod testutils; use crate::{
protocol::{alice, alice::AliceState, bob},
use swap::protocol::{alice, alice::AliceState, bob}; tests::{alice_run_until::is_encsig_learned, setup_test},
use testutils::alice_run_until::is_encsig_learned; };
#[tokio::test] #[tokio::test]
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { 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 alice_swap = ctx.new_swap_as_alice().await;
let bob_swap = ctx.new_swap_as_bob().await; let bob_swap = ctx.new_swap_as_bob().await;

View file

@ -1,11 +1,11 @@
pub mod testutils; use crate::{
protocol::{alice, bob, bob::BobState},
use swap::protocol::{alice, bob, bob::BobState}; tests::{bob_run_until::is_encsig_sent, setup_test},
use testutils::bob_run_until::is_encsig_sent; };
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { 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 alice_swap = ctx.new_swap_as_alice().await;
let bob_swap = ctx.new_swap_as_bob().await; let bob_swap = ctx.new_swap_as_bob().await;

View file

@ -1,11 +1,11 @@
pub mod testutils; use crate::{
protocol::{alice, bob, bob::BobState},
use swap::protocol::{alice, bob, bob::BobState}; tests::{bob_run_until::is_lock_proof_received, setup_test},
use testutils::bob_run_until::is_lock_proof_received; };
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_lock_proof_received_resume_swap() { 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 alice_swap = ctx.new_swap_as_alice().await;
let bob_swap = ctx.new_swap_as_bob().await; let bob_swap = ctx.new_swap_as_bob().await;

View file

@ -1,11 +1,11 @@
pub mod testutils; use crate::{
protocol::{alice, bob, bob::BobState},
use swap::protocol::{alice, bob, bob::BobState}; tests::{bob_run_until::is_xmr_locked, setup_test},
use testutils::bob_run_until::is_xmr_locked; };
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { 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 alice_swap = ctx.new_swap_as_alice().await;
let bob_swap = ctx.new_swap_as_bob().await; let bob_swap = ctx.new_swap_as_bob().await;

View file

@ -1,23 +1,31 @@
use crate::testutils; use crate::{
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::{
bitcoin, bitcoin,
config::Config, config::Config,
monero, monero,
protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts}, protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts},
seed::Seed, 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 tempfile::tempdir;
use testcontainers::{clients::Cli, Container}; use testcontainers::{clients::Cli, Container};
use tracing_core::dispatcher::DefaultGuard; use tracing_core::dispatcher::DefaultGuard;
use tracing_log::LogTracer; use tracing_log::LogTracer;
use uuid::Uuid; 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)] #[derive(Debug, Clone)]
pub struct StartingBalances { pub struct StartingBalances {
pub xmr: monero::Amount, pub xmr: monero::Amount,
@ -297,7 +305,7 @@ where
let _guard = init_tracing(); let _guard = init_tracing();
let (monero, containers) = testutils::init_containers(&cli).await; let (monero, containers) = init_containers(&cli).await;
let swap_amounts = SwapAmounts { let swap_amounts = SwapAmounts {
btc: bitcoin::Amount::from_sat(1_000_000), btc: bitcoin::Amount::from_sat(1_000_000),
@ -398,13 +406,13 @@ async fn init_wallets(
.await .await
.unwrap(); .unwrap();
let xmr_wallet = Arc::new(swap::monero::Wallet { let xmr_wallet = Arc::new(monero::Wallet {
inner: monero.wallet(name).unwrap().client(), inner: monero.wallet(name).unwrap().client(),
network: config.monero_network, network: config.monero_network,
}); });
let btc_wallet = Arc::new( 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 .await
.unwrap(), .unwrap(),
); );
@ -461,7 +469,7 @@ fn init_tracing() -> DefaultGuard {
} }
pub mod alice_run_until { pub mod alice_run_until {
use swap::protocol::alice::AliceState; use crate::protocol::alice::AliceState;
pub fn is_xmr_locked(state: &AliceState) -> bool { pub fn is_xmr_locked(state: &AliceState) -> bool {
matches!( matches!(
@ -479,7 +487,7 @@ pub mod alice_run_until {
} }
pub mod bob_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 { pub fn is_btc_locked(state: &BobState) -> bool {
matches!(state, BobState::BtcLocked(..)) matches!(state, BobState::BtcLocked(..))

View file

@ -1,13 +1,13 @@
pub mod testutils; use crate::{
protocol::{alice, bob, bob::BobState},
use swap::protocol::{alice, bob, bob::BobState}; tests::{bob_run_until::is_btc_locked, setup_test},
use testutils::bob_run_until::is_btc_locked; };
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice /// 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. /// the encsig and fail to refund or redeem. Alice punishes.
#[tokio::test] #[tokio::test]
async fn alice_punishes_if_bob_never_acts_after_fund() { 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 alice_swap = ctx.new_swap_as_alice().await;
let bob_swap = ctx.new_swap_as_bob().await; let bob_swap = ctx.new_swap_as_bob().await;

View file

@ -1,13 +1,13 @@
pub mod testutils; use crate::{
protocol::{alice, alice::AliceState, bob},
use swap::protocol::{alice, alice::AliceState, bob}; tests::{alice_run_until::is_xmr_locked, setup_test},
use testutils::alice_run_until::is_xmr_locked; };
/// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
/// then also refunds. /// then also refunds.
#[tokio::test] #[tokio::test]
async fn given_alice_restarts_after_xmr_is_locked_refund_swap() { 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 alice_swap = ctx.new_swap_as_alice().await;
let bob_swap = ctx.new_swap_as_bob().await; let bob_swap = ctx.new_swap_as_bob().await;

View file

@ -1,14 +1,14 @@
pub mod testutils; use crate::{
protocol::{alice, alice::AliceState, bob},
use swap::protocol::{alice, alice::AliceState, bob}; tests::{alice_run_until::is_encsig_learned, setup_test},
use testutils::alice_run_until::is_encsig_learned; };
/// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// 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 /// is forced to refund even though she learned the secret and would be able to
/// redeem had the timelock not expired. /// redeem had the timelock not expired.
#[tokio::test] #[tokio::test]
async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_refund_swap() { 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 alice_swap = ctx.new_swap_as_alice().await;
let bob_swap = ctx.new_swap_as_bob().await; let bob_swap = ctx.new_swap_as_bob().await;