Move ChannelMonitor deserialization to new ser framework
[rust-lightning] / fuzz / fuzz_targets / chanmon_deser_target.rs
index 0bf86177a1885ca74fe79502c1a1ffaadc1fa527..cce12d73f02d16d480919ed6318226e3a6b5224b 100644 (file)
@@ -5,22 +5,25 @@ extern crate lightning;
 
 use lightning::ln::channelmonitor;
 use lightning::util::reset_rng_state;
+use lightning::util::ser::Readable;
+
+use std::io::Cursor;
 
 #[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);
+       if let Ok(monitor) = channelmonitor::ChannelMonitor::read(&mut Cursor::new(data)) {
+               assert!(channelmonitor::ChannelMonitor::read(&mut Cursor::new(&monitor.serialize_for_disk()[..])).unwrap() == monitor);
                monitor.serialize_for_watchtower();
        }
 }
 
 #[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 +38,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());
        }
 }