Skip to content

Commit 8f383d9

Browse files
committed
Expose functional tests under _externalize_tests feature flag
Also, introduce TestSignerFactory, a factory for dynamic signers and ext-functional-test-demo crate for testing this machinery.
1 parent a8dbede commit 8f383d9

32 files changed

+567
-499
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ lightning-rapid-gossip-sync/res/full_graph.lngossip
1313
lightning-custom-message/target
1414
lightning-transaction-sync/target
1515
lightning-dns-resolver/target
16+
ext-functional-test-demo/target
1617
no-std-check/target
1718
msrv-no-dev-deps-check/target

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ members = [
2121

2222
exclude = [
2323
"lightning-transaction-sync",
24+
"ext-functional-test-demo",
2425
"no-std-check",
2526
"msrv-no-dev-deps-check",
2627
"bench",

ci/ci-tests.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ cargo check --verbose --color always
112112
[ "$CI_MINIMIZE_DISK_USAGE" != "" ] && cargo clean
113113
popd
114114

115+
echo -e "\n\Running functional tests from outside the workspace"
116+
pushd ext-functional-test-demo
117+
[ "$RUSTC_MINOR_VERSION" -lt 65 ] && cargo update -p regex --precise "1.9.6" --verbose
118+
cargo test --color always
119+
[ "$CI_MINIMIZE_DISK_USAGE" != "" ] && cargo clean
120+
popd
121+
115122
# Test that we can build downstream code with only the "release pins".
116123
pushd msrv-no-dev-deps-check
117124
PIN_RELEASE_DEPS

ext-functional-test-demo/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "ext-functional-tester"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
lightning = { path = "../lightning", features = ["_externalize_tests"] }

ext-functional-test-demo/src/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
fn main() {
2+
println!("{} tests were exported", lightning::get_xtests().len());
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use lightning::util::dyn_signer::{DynKeysInterfaceTrait, DynSigner};
8+
use lightning::util::test_utils::{TestSignerFactory, SIGNER_FACTORY};
9+
use std::panic::catch_unwind;
10+
use std::sync::Arc;
11+
use std::time::Duration;
12+
13+
struct BrokenSignerFactory();
14+
15+
impl TestSignerFactory for BrokenSignerFactory {
16+
fn make_signer(
17+
&self, _seed: &[u8; 32], _now: Duration,
18+
) -> Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>> {
19+
panic!()
20+
}
21+
}
22+
23+
#[test]
24+
fn test_functional() {
25+
lightning::ln::functional_tests::test_insane_channel_opens();
26+
lightning::ln::functional_tests::fake_network_test();
27+
28+
SIGNER_FACTORY.set(Arc::new(BrokenSignerFactory()));
29+
catch_unwind(|| lightning::ln::functional_tests::fake_network_test()).unwrap_err();
30+
}
31+
}

fuzz/src/chanmon_consistency.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ use bitcoin::secp256k1::schnorr;
8181
use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey};
8282

8383
use lightning::io::Cursor;
84+
use lightning::util::dyn_signer::DynSigner;
8485
use std::cmp::{self, Ordering};
8586
use std::mem;
8687
use std::sync::atomic;
@@ -382,6 +383,7 @@ impl SignerProvider for KeyProvider {
382383
channel_keys_id,
383384
);
384385
let revoked_commitment = self.make_enforcement_state_cell(keys.commitment_seed);
386+
let keys = DynSigner::new(keys);
385387
TestChannelSigner::new_with_revoked(keys, revoked_commitment, false)
386388
}
387389

@@ -390,6 +392,7 @@ impl SignerProvider for KeyProvider {
390392

391393
let inner: InMemorySigner = ReadableArgs::read(&mut reader, self)?;
392394
let state = self.make_enforcement_state_cell(inner.commitment_seed);
395+
let inner = DynSigner::new(inner);
393396

394397
Ok(TestChannelSigner::new_with_revoked(inner, state, false))
395398
}

fuzz/src/full_stack.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
7676
use bitcoin::secp256k1::schnorr;
7777
use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey};
7878

79+
use lightning::util::dyn_signer::DynSigner;
7980
use std::cell::RefCell;
8081
use std::cmp;
8182
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
@@ -444,7 +445,7 @@ impl SignerProvider for KeyProvider {
444445
let ctr = channel_keys_id[0];
445446
let (inbound, state) = self.signer_state.borrow().get(&ctr).unwrap().clone();
446447
TestChannelSigner::new_with_revoked(
447-
if inbound {
448+
DynSigner::new(if inbound {
448449
InMemorySigner::new(
449450
&secp_ctx,
450451
SecretKey::from_slice(&[
@@ -516,7 +517,7 @@ impl SignerProvider for KeyProvider {
516517
channel_keys_id,
517518
channel_keys_id,
518519
)
519-
},
520+
}),
520521
state,
521522
false,
522523
)
@@ -525,6 +526,7 @@ impl SignerProvider for KeyProvider {
525526
fn read_chan_signer(&self, mut data: &[u8]) -> Result<TestChannelSigner, DecodeError> {
526527
let inner: InMemorySigner = ReadableArgs::read(&mut data, self)?;
527528
let state = Arc::new(Mutex::new(EnforcementState::new()));
529+
let inner = DynSigner::new(inner);
528530

529531
Ok(TestChannelSigner::new_with_revoked(inner, state, false))
530532
}

lightning-background-processor/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,8 @@ mod tests {
25502550
failure: PathFailure::OnPath { network_update: None },
25512551
path: path.clone(),
25522552
short_channel_id: Some(scored_scid),
2553+
error_code: None,
2554+
error_data: None,
25532555
});
25542556
let event = $receive.expect("PaymentPathFailed not handled within deadline");
25552557
match event {
@@ -2567,6 +2569,8 @@ mod tests {
25672569
failure: PathFailure::OnPath { network_update: None },
25682570
path: path.clone(),
25692571
short_channel_id: None,
2572+
error_code: None,
2573+
error_data: None,
25702574
});
25712575
let event = $receive.expect("PaymentPathFailed not handled within deadline");
25722576
match event {

lightning-macros/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ pub fn drop_legacy_field_definition(expr: TokenStream) -> TokenStream {
306306
///
307307
/// fn f1() {}
308308
///
309-
/// #[xtest(feature = "_test_utils")]
309+
/// #[xtest(feature = "_externalize_tests")]
310310
/// pub fn test_f1() {
311311
/// f1();
312312
/// }
@@ -317,7 +317,7 @@ pub fn drop_legacy_field_definition(expr: TokenStream) -> TokenStream {
317317
/// ```rust
318318
/// use lightning_macros::xtest;
319319
///
320-
/// #[xtest(feature = "_test_utils")]
320+
/// #[xtest(feature = "_externalize_tests")]
321321
/// pub mod tests {
322322
/// use super::*;
323323
///

lightning/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1818
[features]
1919
# Internal test utilities exposed to other repo crates
2020
_test_utils = ["regex", "bitcoin/bitcoinconsensus", "lightning-types/_test_utils"]
21-
21+
_externalize_tests = ["inventory", "_test_utils"]
2222
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
2323
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
2424
unsafe_revoked_tx_signing = []
@@ -48,10 +48,12 @@ regex = { version = "1.5.6", optional = true }
4848
backtrace = { version = "0.3", optional = true }
4949

5050
libm = { version = "0.2", default-features = false }
51+
inventory = { version = "0.3", optional = true }
5152

5253
[dev-dependencies]
5354
regex = "1.5.6"
5455
lightning-types = { version = "0.3.0", path = "../lightning-types", features = ["_test_utils"] }
56+
lightning-macros = { path = "../lightning-macros" }
5557

5658
[dev-dependencies.bitcoin]
5759
version = "0.32.2"

0 commit comments

Comments
 (0)