Put test utilities behind a feature flag.
authorValentine Wallace <vwallace@protonmail.com>
Wed, 16 Sep 2020 21:27:13 +0000 (17:27 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Fri, 16 Oct 2020 15:30:33 +0000 (11:30 -0400)
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
lightning/src/chain/channelmonitor.rs
lightning/src/lib.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/mod.rs
lightning/src/util/mod.rs

index 3b12468e9c4a964e353dc6df9978719312b10366..b5ec64d99b0aab3824ef407162aba5a5acee770d 100644 (file)
@@ -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"]
index d8433ef5ffaf423fbed900bbde1a017274c89c18..315b2afec495cf6895ee2999b6472608ffdd7559 100644 (file)
@@ -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<ChanSigner: ChannelKeys> {
        secp_ctx: Secp256k1<secp256k1::All>, //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<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
index dbd89a2ccdf182f88a6ee32b3ff311456b37b0aa..c5d369268cc6836f462ddb69d4c1b58c2a2047c5 100644 (file)
@@ -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;
index 379fe19300938127f0602d2c323b532f8604e97c..b43c98c840392c7b983de276e0867ba5e3baf795 100644 (file)
@@ -405,9 +405,9 @@ pub struct ChannelManager<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref,
        last_block_hash: Mutex<BlockHash>,
        secp_ctx: Secp256k1<secp256k1::All>,
 
-       #[cfg(test)]
+       #[cfg(any(test, feature = "_test_utils"))]
        pub(super) channel_state: Mutex<ChannelHolder<ChanSigner>>,
-       #[cfg(not(test))]
+       #[cfg(not(any(test, feature = "_test_utils")))]
        channel_state: Mutex<ChannelHolder<ChanSigner>>,
        our_network_key: SecretKey,
 
index 7d54b6a92166d9c3378e4f0bf3431d157c28d37d..871854ce63e06ae405c0c859f286214c8adde245 100644 (file)
@@ -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<Node<'a, 'b,
        }
 }
 
+#[cfg(test)]
 macro_rules! get_channel_value_stat {
        ($node: expr, $channel_id: expr) => {{
                let chan_lock = $node.node.channel_state.lock().unwrap();
index cd959a74dc53cc0e176a25e175735011aea6a8ec..1afcb3530fe16b995a4671c563da98d8e8e8d115 100644 (file)
@@ -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;
index 84c8e6c14daa841c2d01772b3cbe15f69bbe0ea7..a3cfaa9804e6e9f58c3521e6ab2d07c3862fd0b6 100644 (file)
@@ -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;