Merge pull request #386 from TheBlueMatt/2019-10-useless-lints
[rust-lightning] / fuzz / fuzz_targets / chanmon_deser_target.rs
index 0bf86177a1885ca74fe79502c1a1ffaadc1fa527..f741832580173d3f1529be676c223491e1b46fb2 100644 (file)
@@ -1,26 +1,52 @@
 // 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 bitcoin;
+extern crate bitcoin_hashes;
 extern crate lightning;
 
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+
 use lightning::ln::channelmonitor;
-use lightning::util::reset_rng_state;
+use lightning::util::ser::{ReadableArgs, Writer};
+
+mod utils;
+use utils::test_logger;
+
+use std::io::Cursor;
+use std::sync::Arc;
+
+struct VecWriter(Vec<u8>);
+impl Writer for VecWriter {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               self.0.extend_from_slice(buf);
+               Ok(())
+       }
+       fn size_hint(&mut self, size: usize) {
+               self.0.reserve_exact(size);
+       }
+}
 
 #[inline]
 pub fn do_test(data: &[u8]) {
-       reset_rng_state();
-       if let Some(monitor) = channelmonitor::ChannelMonitor::deserialize(data) {
-               assert!(channelmonitor::ChannelMonitor::deserialize(&monitor.serialize_for_disk()[..]).unwrap() == monitor);
-               monitor.serialize_for_watchtower();
+       let logger = Arc::new(test_logger::TestLogger::new("".to_owned()));
+       if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data), logger.clone()) {
+               let mut w = VecWriter(Vec::new());
+               monitor.write_for_disk(&mut w).unwrap();
+               let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
+               assert!(latest_block_hash == deserialized_copy.0);
+               assert!(monitor == deserialized_copy.1);
+               w.0.clear();
+               monitor.write_for_watchtower(&mut w).unwrap();
        }
 }
 
 #[cfg(feature = "afl")]
-extern crate afl;
+#[macro_use] extern crate afl;
 #[cfg(feature = "afl")]
 fn main() {
-       afl::read_stdio_bytes(|data| {
-               do_test(&data);
+       fuzz!(|data| {
+               do_test(data);
        });
 }
 
@@ -35,29 +61,12 @@ fn main() {
        }
 }
 
+extern crate hex;
 #[cfg(test)]
 mod tests {
-       fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
-               let mut b = 0;
-               for (idx, c) in hex.as_bytes().iter().enumerate() {
-                       b <<= 4;
-                       match *c {
-                               b'A'...b'F' => b |= c - b'A' + 10,
-                               b'a'...b'f' => b |= c - b'a' + 10,
-                               b'0'...b'9' => b |= c - b'0',
-                               _ => panic!("Bad hex"),
-                       }
-                       if (idx & 1) == 1 {
-                               out.push(b);
-                               b = 0;
-                       }
-               }
-       }
 
        #[test]
        fn duplicate_crash() {
-               let mut a = Vec::new();
-               extend_vec_from_hex("00", &mut a);
-               super::do_test(&a);
+               super::do_test(&::hex::decode("00").unwrap());
        }
 }