fb8f0bf6f120f49d6225168b18f4a9aaf55b340e
[rust-lightning] / fuzz / fuzz_targets / chanmon_deser_target.rs
1 // This file is auto-generated by gen_target.sh based on msg_target_template.txt
2 // To modify it, modify msg_target_template.txt and run gen_target.sh instead.
3
4 extern crate bitcoin;
5 extern crate lightning;
6
7 use bitcoin::util::hash::Sha256dHash;
8
9 use lightning::ln::channelmonitor;
10 use lightning::util::reset_rng_state;
11 use lightning::util::ser::{ReadableArgs, Writer};
12
13 mod utils;
14 use utils::test_logger;
15
16 use std::io::Cursor;
17 use std::sync::Arc;
18
19 struct VecWriter(Vec<u8>);
20 impl Writer for VecWriter {
21         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
22                 self.0.extend_from_slice(buf);
23                 Ok(())
24         }
25         fn size_hint(&mut self, size: usize) {
26                 self.0.reserve_exact(size);
27         }
28 }
29
30 #[inline]
31 pub fn do_test(data: &[u8]) {
32         reset_rng_state();
33         let logger = Arc::new(test_logger::TestLogger::new("".to_owned()));
34         if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data), logger.clone()) {
35                 let mut w = VecWriter(Vec::new());
36                 monitor.write_for_disk(&mut w).unwrap();
37                 let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
38                 assert!(latest_block_hash == deserialized_copy.0);
39                 assert!(monitor == deserialized_copy.1);
40                 w.0.clear();
41                 monitor.write_for_watchtower(&mut w).unwrap();
42         }
43 }
44
45 #[cfg(feature = "afl")]
46 #[macro_use] extern crate afl;
47 #[cfg(feature = "afl")]
48 fn main() {
49         fuzz!(|data| {
50                 do_test(data);
51         });
52 }
53
54 #[cfg(feature = "honggfuzz")]
55 #[macro_use] extern crate honggfuzz;
56 #[cfg(feature = "honggfuzz")]
57 fn main() {
58         loop {
59                 fuzz!(|data| {
60                         do_test(data);
61                 });
62         }
63 }
64
65 extern crate hex;
66 #[cfg(test)]
67 mod tests {
68
69         #[test]
70         fn duplicate_crash() {
71                 super::do_test(&::hex::decode("00").unwrap());
72         }
73 }