]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add fuzz targets for Writeable messages/migrate fuzz test
authorYuntai Kyong <yuntai.kyong@gmail.com>
Sat, 1 Sep 2018 07:21:39 +0000 (16:21 +0900)
committerMatt Corallo <git@bluematt.me>
Wed, 12 Sep 2018 14:19:36 +0000 (10:19 -0400)
31 files changed:
fuzz/Cargo.toml
fuzz/fuzz_targets/msg_ping_target.rs [deleted file]
fuzz/fuzz_targets/msg_pong_target.rs [deleted file]
fuzz/fuzz_targets/msg_targets/gen_target.sh
fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs
fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs
fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs
fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs
fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs
fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs
fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs
fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs
fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs
fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs
fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs
fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs
fuzz/fuzz_targets/msg_targets/msg_init_target.rs
fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs
fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs
fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs
fuzz/fuzz_targets/msg_targets/msg_ping_target.rs [new file with mode: 0644]
fuzz/fuzz_targets/msg_targets/msg_pong_target.rs [new file with mode: 0644]
fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs
fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs
fuzz/fuzz_targets/msg_targets/msg_target_template.txt
fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs
fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs
fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs
fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs
fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs
fuzz/fuzz_targets/msg_targets/utils.rs

index 3cd249ea8218d7ef115054c5afe37d77014662ee..01311f206776d29725f7e4433612262d4c2bd639 100644 (file)
@@ -54,11 +54,11 @@ path = "fuzz_targets/chanmon_deser_target.rs"
 # message fuzz targets
 [[bin]]
 name = "msg_ping_target"
-path = "fuzz_targets/msg_ping_target.rs"
+path = "fuzz_targets/msg_targets/msg_ping_target.rs"
 
 [[bin]]
 name = "msg_pong_target"
-path = "fuzz_targets/msg_pong_target.rs"
+path = "fuzz_targets/msg_targets/msg_pong_target.rs"
 
 [[bin]]
 name = "msg_error_message_target"
diff --git a/fuzz/fuzz_targets/msg_ping_target.rs b/fuzz/fuzz_targets/msg_ping_target.rs
deleted file mode 100644 (file)
index c9fb341..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::util::reset_rng_state;
-
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable, Ping};
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       reset_rng_state();
-       if let Ok(msg) = Ping::decode(data) {
-               let _ = msg.encode();
-       }
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
diff --git a/fuzz/fuzz_targets/msg_pong_target.rs b/fuzz/fuzz_targets/msg_pong_target.rs
deleted file mode 100644 (file)
index d4572d3..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-// This file is auto-generated by gen_target.sh based on msg_target_template.txt
-// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
-
-extern crate lightning;
-
-use lightning::util::reset_rng_state;
-
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable, Pong};
-
-#[inline]
-pub fn do_test(data: &[u8]) {
-       reset_rng_state();
-       if let Ok(msg) = Pong::decode(data) {
-               let _ = msg.encode();
-       }
-}
-
-#[cfg(feature = "afl")]
-#[macro_use] extern crate afl;
-#[cfg(feature = "afl")]
-fn main() {
-       fuzz!(|data| {
-               do_test(data);
-       });
-}
-
-#[cfg(feature = "honggfuzz")]
-#[macro_use] extern crate honggfuzz;
-#[cfg(feature = "honggfuzz")]
-fn main() {
-       loop {
-               fuzz!(|data| {
-                       do_test(data);
-               });
-       }
-}
-
-extern crate hex;
-#[cfg(test)]
-mod tests {
-       #[test]
-       fn duplicate_crash() {
-               super::do_test(&::hex::decode("00").unwrap());
-       }
-}
index 32f071e85cd5cfb568e118ba57551a434bab7811..cfd100f3dfe73a5b48a1434b4497304d179893e6 100755 (executable)
@@ -31,3 +31,6 @@ GEN_TEST NodeAnnouncement test_msg_exact ""
 GEN_TEST UpdateAddHTLC test_msg_hole ", 85, 33"
 GEN_TEST ErrorMessage test_msg_hole ", 32, 2"
 GEN_TEST OnionHopData test_msg_hole ", 1+8+8+4, 12"
+
+GEN_TEST Ping test_msg_simple ""
+GEN_TEST Pong test_msg_simple ""
index d7f0a88138009085b93f2324d1b26936eba36f60..ef3ae4cdf4e1f6848c6b2be4b3555970dd09188c 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 33c52b33e9eccbee036f4862db0fc5b418f9d9ef..f32aad1a198b2b6c4a63535de4e5a0b771724562 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 1252cfe79dec0492dece71c40bc5b3ad18b50619..e814c4fae52028365ae0589b486d465a2c272d7a 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 38f9160450f8a3c1c38d00d6fb66494d90425093..8532ac120504c04efb6ebe515eceb26871059400 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index c488fb6da978e56dfcc93c66f4f67382c3db6928..8968ee33ea0c1df0a13c0d302ec6c0f1bf394fa1 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 504e1e37633ff867595bdeac632e7266d1350e8f..70d969209403c77f95a43751ea5d0258844fd87f 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 701dd1fb9e2085c10084814505b5f23f1c19514d..c70437b8409e3f9756137b84bc5b7cf24521bb68 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 56a5fb9f676203b8b8b0b45a5cff53105ae04456..173f13d3fdeb6b9c40536fc91b425605eab4c60b 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 97e15c3c4b76299aeaf782abdba43b3d0e7595df..f565e80f9195973c9b46e188a28b1a83bcf719fd 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 7289267c23b126852f57efea3d0493aaff6cf1be..b9c67ae33504b27e7820cb0b17095d73200d2264 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index bfafdf48979c6678acbf723cc43e2bf262966365..499be0049dc4e0783056a6d8cd6aacb5cf2e982a 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 6a2b6ac3a7dfa7610f740676eac106c4ae82a320..2f63bd0bd166f1dcdc5045058f6cb7ed968261cf 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 8a5ee76f5b5a34f150d1b4385744fa5b486c0495..2013a540650763ed5bdb08cdfd017c49be6f64d5 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 54b9cb684b5b7ee6dd6e845dcc501078e46038c6..1a0c9e062f24492d81c71387900ac6fbcac3ffec 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 70849c18981e08701cad7b66171f670a79b64cdd..398dd2886bfd74698c0ca6320e517bf694e5cca4 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 737bc2f0c63b40905e8937c199f422dc40ee3188..01082d8b77ac4e4931c9bf2e9e9ddcbb82d9274d 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
diff --git a/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs b/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs
new file mode 100644 (file)
index 0000000..4a792f6
--- /dev/null
@@ -0,0 +1,44 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+use lightning::util::reset_rng_state;
+
+mod utils;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       reset_rng_state();
+       test_msg_simple!(msgs::Ping, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
diff --git a/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs b/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs
new file mode 100644 (file)
index 0000000..ae5d22a
--- /dev/null
@@ -0,0 +1,44 @@
+// This file is auto-generated by gen_target.sh based on msg_target_template.txt
+// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
+
+extern crate lightning;
+
+use lightning::ln::msgs;
+use lightning::util::reset_rng_state;
+
+mod utils;
+
+#[inline]
+pub fn do_test(data: &[u8]) {
+       reset_rng_state();
+       test_msg_simple!(msgs::Pong, data);
+}
+
+#[cfg(feature = "afl")]
+#[macro_use] extern crate afl;
+#[cfg(feature = "afl")]
+fn main() {
+       fuzz!(|data| {
+               do_test(data);
+       });
+}
+
+#[cfg(feature = "honggfuzz")]
+#[macro_use] extern crate honggfuzz;
+#[cfg(feature = "honggfuzz")]
+fn main() {
+       loop {
+               fuzz!(|data| {
+                       do_test(data);
+               });
+       }
+}
+
+extern crate hex;
+#[cfg(test)]
+mod tests {
+       #[test]
+       fn duplicate_crash() {
+               super::do_test(&::hex::decode("00").unwrap());
+       }
+}
index 6086d279cb261d7ca3a09af20521952dfa813753..804e4ba4b2f194169bc37126c31dff32859a6bb0 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 29243dab6ae27c48c05f2a556aa9d7b666a7cd51..4cc58f28d594845747bcbb28d462073599620a06 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 6053c041cf72c64462cd0fd5047e14a7d36a70ee..d3bda1aa33595038806eed0ff18ad60a67643556 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 64806f20fa4395dff7fa572a4e71ef7f76944e49..cfccc21a3f472c8b763b0934786c363bff5336e9 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 27eaf123e7f6a263a636171892f5cb8020ae1b39..90827f31c95ff7935eef112d046e820d8fc3b701 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index eb6dc94aa1de00944b44deeedafabeeead6ab571..a045dea6038de130b5dd097478e33e34eb41fcbf 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index eec1d2624598947c36c589c7e113faa2dd9fca17..6f5ff3e47a98867da17d63f5bee4c24ccbae5aeb 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 3dd87b30ca5387e4acb5ebc0c52611747981017e..c5dd32356570bdeceba462839e8bc8a9ae9533d0 100644 (file)
@@ -6,8 +6,6 @@ extern crate lightning;
 use lightning::ln::msgs;
 use lightning::util::reset_rng_state;
 
-use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
-
 mod utils;
 
 #[inline]
index 64bfd89326b4e8d3aa1e12c2bdd6f0323d929b8e..c1bcd8f361887003d4e2823d2e5445fc4ce16496 100644 (file)
@@ -4,9 +4,29 @@
 macro_rules! test_msg {
        ($MsgType: path, $data: ident) => {
                {
-                       if let Ok(msg) = <$MsgType as MsgDecodable>::decode($data){
-                               let enc = msg.encode();
-                               assert_eq!(&$data[..enc.len()], &enc[..]);
+                       use lightning::util::ser::{Writer, Reader, Writeable, Readable};
+                       let mut r = Reader::new(::std::io::Cursor::new($data));
+                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
+                               let p = r.get_ref().position() as usize;
+                               let mut w = Writer::new(::std::io::Cursor::new(vec![]));
+                               msg.write(&mut w).unwrap();
+
+                               let buf = w.into_inner().into_inner();
+                               assert_eq!(buf.len(), p);
+                               assert_eq!(&r.into_inner().into_inner()[..p], &buf[..p]);
+                       }
+               }
+       }
+}
+
+#[macro_export]
+macro_rules! test_msg_simple {
+       ($MsgType: path, $data: ident) => {
+               {
+                       use lightning::util::ser::{Writer, Reader, Writeable, Readable};
+                       let mut r = Reader::new(::std::io::Cursor::new($data));
+                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
+                               msg.write(&mut Writer::new(::std::io::Cursor::new(vec![]))).unwrap();
                        }
                }
        }
@@ -16,9 +36,14 @@ macro_rules! test_msg {
 macro_rules! test_msg_exact {
        ($MsgType: path, $data: ident) => {
                {
-                       if let Ok(msg) = <$MsgType as MsgDecodable>::decode($data){
-                               let enc = msg.encode();
-                               assert_eq!(&$data[..], &enc[..]);
+                       use lightning::util::ser::{Writer, Reader, Writeable, Readable};
+                       let mut r = Reader::new(::std::io::Cursor::new($data));
+                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
+                               let mut w = Writer::new(::std::io::Cursor::new(vec![]));
+                               msg.write(&mut w).unwrap();
+
+                               let buf = w.into_inner().into_inner();
+                               assert_eq!(&r.into_inner().into_inner()[..], &buf[..]);
                        }
                }
        }
@@ -28,10 +53,17 @@ macro_rules! test_msg_exact {
 macro_rules! test_msg_hole {
        ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {
                {
-                       if let Ok(msg) = <$MsgType as MsgDecodable>::decode($data){
-                               let enc = msg.encode();
-                               assert_eq!(&$data[..$hole], &enc[..$hole]);
-                               assert_eq!(&$data[$hole + $hole_len..enc.len()], &enc[$hole + $hole_len..]);
+                       use lightning::util::ser::{Writer, Reader, Writeable, Readable};
+                       let mut r = Reader::new(::std::io::Cursor::new($data));
+                       if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
+                               let mut w = Writer::new(::std::io::Cursor::new(vec![]));
+                               msg.write(&mut w).unwrap();
+                               let p = w.get_ref().position() as usize;
+
+                               let buf = w.into_inner().into_inner();
+                               assert_eq!(buf.len(),p);
+                               assert_eq!(&r.get_ref().get_ref()[..$hole], &buf[..$hole]);
+                               assert_eq!(&r.get_ref().get_ref()[$hole+$hole_len..p], &buf[$hole+$hole_len..]);
                        }
                }
        }