]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Migrate fuzz router/channel target to Readable 2018-09-119-rebased
authorYuntai Kyong <yuntai.kyong@gmail.com>
Wed, 12 Sep 2018 10:00:10 +0000 (19:00 +0900)
committerMatt Corallo <git@bluematt.me>
Wed, 12 Sep 2018 14:19:36 +0000 (10:19 -0400)
and be more specific about DecodeError::InvalidValue

fuzz/fuzz_targets/channel_target.rs
fuzz/fuzz_targets/router_target.rs
src/ln/msgs.rs
src/ln/peer_handler.rs

index d2a4fbca94a2aa24cdeaf6beec5e9f7e2c01172c..7eed4fb55d3fa4bf40bc124d78fbb7271b89dd05 100644 (file)
@@ -10,11 +10,12 @@ use bitcoin::network::serialize::{serialize, BitcoinHash};
 use lightning::ln::channel::{Channel, ChannelKeys};
 use lightning::ln::channelmanager::{HTLCFailReason, PendingHTLCStatus};
 use lightning::ln::msgs;
-use lightning::ln::msgs::{MsgDecodable, ErrorAction};
+use lightning::ln::msgs::{ErrorAction};
 use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
 use lightning::chain::transaction::OutPoint;
 use lightning::util::reset_rng_state;
 use lightning::util::logger::Logger;
+use lightning::util::ser::{Readable, Reader};
 
 mod utils;
 
@@ -119,8 +120,9 @@ pub fn do_test(data: &[u8]) {
        }
 
        macro_rules! decode_msg {
-               ($MsgType: path, $len: expr) => {
-                       match <($MsgType)>::decode(get_slice!($len)) {
+               ($MsgType: path, $len: expr) => {{
+                       let mut reader = Reader::new(::std::io::Cursor::new(get_slice!($len)));
+                       match <($MsgType)>::read(&mut reader) {
                                Ok(msg) => msg,
                                Err(e) => match e {
                                        msgs::DecodeError::UnknownRealmByte => return,
@@ -131,11 +133,11 @@ pub fn do_test(data: &[u8]) {
                                        msgs::DecodeError::ExtraAddressesPerType => return,
                                        msgs::DecodeError::BadLengthDescriptor => return,
                                        msgs::DecodeError::ShortRead => panic!("We picked the length..."),
-                                       msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
-                                       msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
+                                       msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"),
+                                       msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
                                }
                        }
-               }
+               }}
        }
 
        macro_rules! decode_msg_with_len16 {
@@ -145,21 +147,7 @@ pub fn do_test(data: &[u8]) {
                                        Some(slice) => slice,
                                        None => return,
                                }[$begin_len..$begin_len + 2]);
-                               match <($MsgType)>::decode(get_slice!($begin_len as usize + 2 + (extra_len as usize)*$factor)) {
-                                       Ok(msg) => msg,
-                                       Err(e) => match e {
-                                               msgs::DecodeError::UnknownRealmByte => return,
-                                               msgs::DecodeError::UnknownRequiredFeature => return,
-                                               msgs::DecodeError::BadPublicKey => return,
-                                               msgs::DecodeError::BadSignature => return,
-                                               msgs::DecodeError::BadText => return,
-                                               msgs::DecodeError::ExtraAddressesPerType => return,
-                                               msgs::DecodeError::BadLengthDescriptor => return,
-                                               msgs::DecodeError::ShortRead => panic!("We picked the length..."),
-                                               msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
-                                               msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
-                                       }
-                               }
+                               decode_msg!($MsgType, $begin_len as usize + 2 + (extra_len as usize)*$factor)
                        }
                }
        }
index 4669f1f8c4afe86be15ba2cacc24be95deacc0d0..fc3e9e9ba9ace20c1de0db547b14fb88360e369b 100644 (file)
@@ -8,10 +8,11 @@ use bitcoin::blockdata::script::{Script, Builder};
 use lightning::chain::chaininterface::{ChainError,ChainWatchInterface, ChainListener};
 use lightning::ln::channelmanager::ChannelDetails;
 use lightning::ln::msgs;
-use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
+use lightning::ln::msgs::{RoutingMessageHandler};
 use lightning::ln::router::{Router, RouteHint};
 use lightning::util::reset_rng_state;
 use lightning::util::logger::Logger;
+use lightning::util::ser::{Reader, Readable};
 
 use secp256k1::key::PublicKey;
 use secp256k1::Secp256k1;
@@ -119,8 +120,9 @@ pub fn do_test(data: &[u8]) {
        }
 
        macro_rules! decode_msg {
-               ($MsgType: path, $len: expr) => {
-                       match <($MsgType)>::decode(get_slice!($len)) {
+               ($MsgType: path, $len: expr) => {{
+                       let mut reader = Reader::new(::std::io::Cursor::new(get_slice!($len)));
+                       match <($MsgType)>::read(&mut reader) {
                                Ok(msg) => msg,
                                Err(e) => match e {
                                        msgs::DecodeError::UnknownRealmByte => return,
@@ -131,11 +133,11 @@ pub fn do_test(data: &[u8]) {
                                        msgs::DecodeError::ExtraAddressesPerType => return,
                                        msgs::DecodeError::BadLengthDescriptor => return,
                                        msgs::DecodeError::ShortRead => panic!("We picked the length..."),
-                                       msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
-                                       msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
+                                       msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"),
+                                       msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
                                }
                        }
-               }
+               }}
        }
 
        macro_rules! decode_msg_with_len16 {
index f2f9fcbd131002b8b6bb8d4b4cca7b67ca6955e9..fc942ab5a2022dbe30eb303c3d4279362c81d881 100644 (file)
@@ -46,7 +46,7 @@ pub enum DecodeError {
        BadLengthDescriptor,
        /// Error from std::io
        Io(::std::io::Error),
-       /// Invalid value found when decoding
+       /// 1 or 0 is not found for boolean value
        InvalidValue,
 }
 pub trait MsgDecodable: Sized {
@@ -525,7 +525,7 @@ impl Error for DecodeError {
                        DecodeError::ExtraAddressesPerType => "More than one address of a single type",
                        DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
                        DecodeError::Io(ref e) => e.description(),
-                       DecodeError::InvalidValue => "Invalid value in the bytes",
+                       DecodeError::InvalidValue => "0 or 1 is not found for boolean",
                }
        }
 }
index b102f947badef2d46cffe3391c6742b25df6a8fa..4e710117bb805037a80457086a434de70a0bb29b 100644 (file)
@@ -364,7 +364,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                                },
                                                                                                msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
                                                                                                msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
-                                                                                               msgs::DecodeError::InvalidValue => return Err(PeerHandleError{ no_connection_possible: false }),
+                                                                                               msgs::DecodeError::InvalidValue => panic!("should not happen with message decoding"),
                                                                                        }
                                                                                }
                                                                        };