From 4350cc615cb4764e75e8d8d993071b1bb5bdb404 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Wed, 16 Sep 2020 17:27:13 -0400 Subject: [PATCH] Put test utilities behind a feature flag. The utilities will still be part of cfg(test). The purpose of this is to enable other crates in the workspace to use the test utilities. --- lightning/Cargo.toml | 4 ++++ lightning/src/chain/channelmonitor.rs | 6 +++--- lightning/src/lib.rs | 6 +++--- lightning/src/ln/channelmanager.rs | 4 ++-- lightning/src/ln/functional_test_utils.rs | 11 +++++++++++ lightning/src/ln/mod.rs | 4 ++-- lightning/src/util/mod.rs | 6 +++--- 7 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lightning/Cargo.toml b/lightning/Cargo.toml index 3b12468e9..b5ec64d99 100644 --- a/lightning/Cargo.toml +++ b/lightning/Cargo.toml @@ -12,6 +12,7 @@ Still missing tons of error-handling. See GitHub issues for suggested projects i [features] fuzztarget = ["bitcoin/fuzztarget"] +_test_utils = ["hex", "regex"] # Unlog messages superior at targeted level. max_level_off = [] max_level_error = [] @@ -25,6 +26,9 @@ unsafe_revoked_tx_signing = [] [dependencies] bitcoin = "0.24" +hex = { version = "0.3", optional = true } +regex = { version = "0.1.80", optional = true } + [dev-dependencies.bitcoin] version = "0.24" features = ["bitcoinconsensus"] diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index d8433ef5f..315b2afec 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -57,7 +57,7 @@ use std::io::Error; /// An update generated by the underlying Channel itself which contains some new information the /// ChannelMonitor should be made aware of. -#[cfg_attr(test, derive(PartialEq))] +#[cfg_attr(any(test, feature = "_test_utils"), derive(PartialEq))] #[derive(Clone)] #[must_use] pub struct ChannelMonitorUpdate { @@ -470,7 +470,7 @@ enum OnchainEvent { const SERIALIZATION_VERSION: u8 = 1; const MIN_SERIALIZATION_VERSION: u8 = 1; -#[cfg_attr(test, derive(PartialEq))] +#[cfg_attr(any(test, feature = "_test_utils"), derive(PartialEq))] #[derive(Clone)] pub(crate) enum ChannelMonitorUpdateStep { LatestHolderCommitmentTXInfo { @@ -696,7 +696,7 @@ pub struct ChannelMonitor { secp_ctx: Secp256k1, //TODO: dedup this a bit... } -#[cfg(any(test, feature = "fuzztarget"))] +#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] /// Used only in testing and fuzztarget to check serialization roundtrips don't change the /// underlying object impl PartialEq for ChannelMonitor { diff --git a/lightning/src/lib.rs b/lightning/src/lib.rs index dbd89a2cc..c5d369268 100644 --- a/lightning/src/lib.rs +++ b/lightning/src/lib.rs @@ -18,7 +18,7 @@ //! generated/etc. This makes it a good candidate for tight integration into an existing wallet //! instead of having a rather-separate lightning appendage to a wallet. -#![cfg_attr(not(feature = "fuzztarget"), deny(missing_docs))] +#![cfg_attr(not(any(feature = "fuzztarget", feature = "_test_utils")), deny(missing_docs))] #![forbid(unsafe_code)] // In general, rust is absolutely horrid at supporting users doing things like, @@ -28,8 +28,8 @@ #![allow(ellipsis_inclusive_range_patterns)] extern crate bitcoin; -#[cfg(test)] extern crate hex; -#[cfg(test)] extern crate regex; +#[cfg(any(test, feature = "_test_utils"))] extern crate hex; +#[cfg(any(test, feature = "_test_utils"))] extern crate regex; #[macro_use] pub mod util; diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 379fe1930..b43c98c84 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -405,9 +405,9 @@ pub struct ChannelManager, secp_ctx: Secp256k1, - #[cfg(test)] + #[cfg(any(test, feature = "_test_utils"))] pub(super) channel_state: Mutex>, - #[cfg(not(test))] + #[cfg(not(any(test, feature = "_test_utils")))] channel_state: Mutex>, our_network_key: SecretKey, diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 7d54b6a92..871854ce6 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -263,6 +263,7 @@ macro_rules! get_event_msg { } } +#[cfg(test)] macro_rules! get_htlc_update_msgs { ($node: expr, $node_id: expr) => { { @@ -279,6 +280,7 @@ macro_rules! get_htlc_update_msgs { } } +#[cfg(test)] macro_rules! get_feerate { ($node: expr, $channel_id: expr) => { { @@ -289,6 +291,7 @@ macro_rules! get_feerate { } } +#[cfg(test)] macro_rules! get_local_commitment_txn { ($node: expr, $channel_id: expr) => { { @@ -327,6 +330,8 @@ macro_rules! unwrap_send_err { } } +/// Check whether N channel monitor(s) have been added. +#[macro_export] macro_rules! check_added_monitors { ($node: expr, $count: expr) => { { @@ -553,6 +558,9 @@ macro_rules! get_closing_signed_broadcast { } } +/// Check that a channel's closing channel update has been broadcasted, and optionally +/// check whether an error message event has occurred. +#[macro_export] macro_rules! check_closed_broadcast { ($node: expr, $with_error_msg: expr) => {{ let events = $node.node.get_and_clear_pending_msg_events(); @@ -779,6 +787,7 @@ macro_rules! expect_pending_htlcs_forwardable { }} } +#[cfg(test)] macro_rules! expect_payment_received { ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => { let events = $node.node.get_and_clear_pending_events(); @@ -807,6 +816,7 @@ macro_rules! expect_payment_sent { } } +#[cfg(test)] macro_rules! expect_payment_failed { ($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr $(, $expected_error_code: expr, $expected_error_data: expr)*) => { let events = $node.node.get_and_clear_pending_events(); @@ -1284,6 +1294,7 @@ pub fn get_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec {{ let chan_lock = $node.node.channel_state.lock().unwrap(); diff --git a/lightning/src/ln/mod.rs b/lightning/src/ln/mod.rs index cd959a74d..1afcb3530 100644 --- a/lightning/src/ln/mod.rs +++ b/lightning/src/ln/mod.rs @@ -38,9 +38,9 @@ mod wire; // without the node parameter being mut. This is incorrect, and thus newer rustcs will complain // about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below. -#[cfg(test)] +#[cfg(any(test, feature = "_test_utils"))] #[macro_use] -pub(crate) mod functional_test_utils; +pub mod functional_test_utils; #[cfg(test)] #[allow(unused_mut)] mod functional_tests; diff --git a/lightning/src/util/mod.rs b/lightning/src/util/mod.rs index 84c8e6c14..a3cfaa980 100644 --- a/lightning/src/util/mod.rs +++ b/lightning/src/util/mod.rs @@ -32,10 +32,10 @@ pub(crate) mod macro_logger; pub mod logger; pub mod config; -#[cfg(test)] -pub(crate) mod test_utils; +#[cfg(any(test, feature = "_test_utils"))] +pub mod test_utils; /// impls of traits that add exra enforcement on the way they're called. Useful for detecting state /// machine errors and used in fuzz targets and tests. -#[cfg(any(test, feature = "fuzztarget"))] +#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] pub mod enforcing_trait_impls; -- 2.39.5