]> git.bitcoin.ninja Git - rust-lightning/commitdiff
fix fuzz stuff 2021-11-fix-1054
authorMatt Corallo <git@bluematt.me>
Tue, 9 Nov 2021 16:32:56 +0000 (16:32 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 9 Nov 2021 16:32:56 +0000 (16:32 +0000)
fuzz/src/chanmon_consistency.rs
fuzz/src/utils/test_logger.rs

index 4995faba6569ebc6ea1cc99c86e047bc17ffb798..e0f527dd2486df86a82416607f5ca3d9c0a04dfd 100644 (file)
@@ -50,8 +50,7 @@ use lightning::util::events::MessageSendEventsProvider;
 use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
 use lightning::routing::router::{Route, RouteHop};
 
-
-use utils::test_logger;
+use utils::test_logger::{self, Output};
 use utils::test_persister::TestPersister;
 
 use bitcoin::secp256k1::key::{PublicKey,SecretKey};
@@ -337,7 +336,8 @@ fn send_hop_payment(source: &ChanMan, middle: &ChanMan, middle_chan_id: u64, des
 }
 
 #[inline]
-pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
+pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
+       let out = SearchingOutput::new(underlying_out);
        let broadcast = Arc::new(TestBroadcaster{});
 
        macro_rules! make_node {
@@ -732,7 +732,8 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
                                                        // force-close which we should detect as an error).
                                                        assert_eq!(msg.contents.flags & 2, 0);
                                                },
-                                               _ => if out.locked_contains(&"Outbound update_fee HTLC buffer overflow - counterparty should force-close this channel") {
+                                               _ => if out.may_fail.load(atomic::Ordering::Acquire) {
+                                                       return;
                                                } else {
                                                        panic!("Unhandled message event {:?}", event)
                                                },
@@ -767,7 +768,8 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
                                                        events::MessageSendEvent::SendChannelUpdate { ref msg, .. } => {
                                                                assert_eq!(msg.contents.flags & 2, 0); // The disable bit must never be set!
                                                        },
-                                                       _ => if out.locked_contains(&"Outbound update_fee HTLC buffer overflow - counterparty should force-close this channel") {
+                                                       _ => if out.may_fail.load(atomic::Ordering::Acquire) {
+                                                               return;
                                                        } else {
                                                                panic!("Unhandled message event")
                                                        },
@@ -787,7 +789,8 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
                                                        events::MessageSendEvent::SendChannelUpdate { ref msg, .. } => {
                                                                assert_eq!(msg.contents.flags & 2, 0); // The disable bit must never be set!
                                                        },
-                                                       _ => if out.locked_contains(&"Outbound update_fee HTLC buffer overflow - counterparty should force-close this channel") {
+                                                       _ => if out.may_fail.load(atomic::Ordering::Acquire) {
+                                                               return;
                                                        } else {
                                                                panic!("Unhandled message event")
                                                        },
@@ -841,7 +844,8 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
                                                events::Event::PendingHTLCsForwardable { .. } => {
                                                        nodes[$node].process_pending_htlc_forwards();
                                                },
-                                               _ => if out.locked_contains(&"Outbound update_fee HTLC buffer overflow - counterparty should force-close this channel") {
+                                               _ => if out.may_fail.load(atomic::Ordering::Acquire) {
+                                                       return;
                                                } else {
                                                        panic!("Unhandled event")
                                                },
@@ -1129,7 +1133,8 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
 
                                // Finally, make sure that at least one end of each channel can make a substantial payment.
                                // Unless we expected to hit a limitation of LN state machine (see comment in `send_update_fee_and_commit`)
-                               if out.locked_contains(&"Outbound update_fee HTLC buffer overflow - counterparty should force-close this channel") {
+                               if out.may_fail.load(atomic::Ordering::Acquire) {
+                                       return;
                                } else {
                                        assert!(
                                                send_payment(&nodes[0], &nodes[1], chan_a, 10_000_000, &mut payment_id) ||
@@ -1158,7 +1163,28 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
        }
 }
 
-pub fn chanmon_consistency_test<Out: test_logger::Output>(data: &[u8], out: Out) {
+/// We actually have different behavior based on if a certain log string has been seen, so we have
+/// to do a bit more tracking.
+#[derive(Clone)]
+struct SearchingOutput<O: Output> {
+       output: O,
+       may_fail: Arc<atomic::AtomicBool>,
+}
+impl<O: Output> Output for SearchingOutput<O> {
+       fn locked_write(&self, data: &[u8]) {
+               if std::str::from_utf8(data).unwrap().contains("Outbound update_fee HTLC buffer overflow - counterparty should force-close this channel") {
+                       self.may_fail.store(true, atomic::Ordering::Release);
+               }
+               self.output.locked_write(data)
+       }
+}
+impl<O: Output> SearchingOutput<O> {
+       pub fn new(output: O) -> Self {
+               Self { output, may_fail: Arc::new(atomic::AtomicBool::new(false)) }
+       }
+}
+
+pub fn chanmon_consistency_test<Out: Output>(data: &[u8], out: Out) {
        do_test(data, out);
 }
 
index ef6344ed4b4c5d3bee894c577a71b46ece685636..f8c96f99bd1c8ddb9c1f9b28d8d7b5fbe67cc27b 100644 (file)
@@ -13,16 +13,12 @@ use std::io::Write;
 
 pub trait Output : Clone  + 'static {
        fn locked_write(&self, data: &[u8]);
-       fn locked_contains(&self, pattern: &str) -> bool;
 }
 
 #[derive(Clone)]
 pub struct DevNull {}
 impl Output for DevNull {
        fn locked_write(&self, _data: &[u8]) {}
-       fn locked_contains(&self, _pattern: &str) -> bool {
-               false
-       }
 }
 #[derive(Clone)]
 pub struct StringBuffer(Arc<Mutex<String>>);
@@ -30,9 +26,6 @@ impl Output for StringBuffer {
        fn locked_write(&self, data: &[u8]) {
                self.0.lock().unwrap().push_str(&String::from_utf8(data.to_vec()).unwrap());
        }
-       fn locked_contains(&self, pattern: &str) -> bool {
-               self.0.lock().unwrap().contains(pattern)
-       }
 }
 impl StringBuffer {
        pub fn new() -> Self {