Merge pull request #118 from TheBlueMatt/2018-08-103-rebased
[rust-lightning] / fuzz / fuzz_targets / full_stack_target.rs
index 40336124a5575bb9ce56744ebb50fdbcd33521d9..9ce82aeb02dae9d07856aa0330c43062c0200aa1 100644 (file)
@@ -174,7 +174,7 @@ pub fn do_test(data: &[u8]) {
                Err(_) => return,
        };
 
-       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new());
+       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger{});
        let monitor = Arc::new(TestChannelMonitor{});
        let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
        let broadcast = Arc::new(TestBroadcaster{});
@@ -257,7 +257,8 @@ pub fn do_test(data: &[u8]) {
                                if !peers.borrow()[peer_id as usize] { return; }
                                let their_key = get_pubkey!();
                                let chan_value = slice_to_be24(get_slice!(3)) as u64;
-                               if channelmanager.create_channel(their_key, chan_value, 0).is_err() { return; }
+                               let push_msat_value = slice_to_be24(get_slice!(3)) as u64;
+                               if channelmanager.create_channel(their_key, chan_value, push_msat_value, 0).is_err() { return; }
                        },
                        6 => {
                                let mut channels = channelmanager.list_channels();
@@ -275,14 +276,17 @@ pub fn do_test(data: &[u8]) {
                        },
                        8 => {
                                for payment in payments_received.drain(..) {
-                                       let mut payment_preimage = [0; 32];
-                                       payment_preimage[0] = payment[0];
-                                       let mut sha = Sha256::new();
-                                       sha.input(&payment_preimage);
-                                       let mut payment_hash_check = [0; 32];
-                                       sha.result(&mut payment_hash_check);
-                                       assert!(payment_hash_check == payment);
-                                       channelmanager.claim_funds(payment_preimage);
+                                       // SHA256 is defined as XOR of all input bytes placed in the first byte, and 0s
+                                       // for the remaining bytes. Thus, if not all remaining bytes are 0s we cannot
+                                       // fulfill this HTLC, but if they are, we can just take the first byte and
+                                       // place that anywhere in our preimage.
+                                       if &payment[1..] != &[0; 31] {
+                                               channelmanager.fail_htlc_backwards(&payment);
+                                       } else {
+                                               let mut payment_preimage = [0; 32];
+                                               payment_preimage[0] = payment[0];
+                                               channelmanager.claim_funds(payment_preimage);
+                                       }
                                }
                        },
                        9 => {