Merge pull request #191 from TheBlueMatt/2018-09-chanmon-ser-framework
[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 lightning;
5
6 use lightning::ln::channelmonitor;
7 use lightning::util::reset_rng_state;
8 use lightning::util::ser::{Readable, Writer};
9
10 use std::io::Cursor;
11
12 struct VecWriter(Vec<u8>);
13 impl Writer for VecWriter {
14         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
15                 self.0.extend_from_slice(buf);
16                 Ok(())
17         }
18         fn size_hint(&mut self, size: usize) {
19                 self.0.reserve_exact(size);
20         }
21 }
22
23 #[inline]
24 pub fn do_test(data: &[u8]) {
25         reset_rng_state();
26         if let Ok(monitor) = channelmonitor::ChannelMonitor::read(&mut Cursor::new(data)) {
27                 let mut w = VecWriter(Vec::new());
28                 monitor.write_for_disk(&mut w).unwrap();
29                 assert!(channelmonitor::ChannelMonitor::read(&mut Cursor::new(&w.0)).unwrap() == monitor);
30                 w.0.clear();
31                 monitor.write_for_watchtower(&mut w).unwrap();
32         }
33 }
34
35 #[cfg(feature = "afl")]
36 #[macro_use] extern crate afl;
37 #[cfg(feature = "afl")]
38 fn main() {
39         fuzz!(|data| {
40                 do_test(data);
41         });
42 }
43
44 #[cfg(feature = "honggfuzz")]
45 #[macro_use] extern crate honggfuzz;
46 #[cfg(feature = "honggfuzz")]
47 fn main() {
48         loop {
49                 fuzz!(|data| {
50                         do_test(data);
51                 });
52         }
53 }
54
55 extern crate hex;
56 #[cfg(test)]
57 mod tests {
58
59         #[test]
60         fn duplicate_crash() {
61                 super::do_test(&::hex::decode("00").unwrap());
62         }
63 }