e7dbf3ed8d6a588aef5e7529d7295999018d01ca
[rust-lightning] / fuzz / src / chanmon_deser.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 use bitcoin::hash_types::BlockHash;
5
6 use lightning::chain::channelmonitor;
7 use lightning::util::enforcing_trait_impls::EnforcingSigner;
8 use lightning::util::ser::{ReadableArgs, Writer, Writeable};
9 use lightning::util::test_utils::OnlyReadsKeysInterface;
10
11 use utils::test_logger;
12
13 use std::io::Cursor;
14
15 struct VecWriter(Vec<u8>);
16 impl Writer for VecWriter {
17         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
18                 self.0.extend_from_slice(buf);
19                 Ok(())
20         }
21         fn size_hint(&mut self, size: usize) {
22                 self.0.reserve_exact(size);
23         }
24 }
25
26 #[inline]
27 pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
28         if let Ok((Some(latest_block_hash), monitor)) = <(Option<BlockHash>, channelmonitor::ChannelMonitor<EnforcingSigner>)>::read(&mut Cursor::new(data), &OnlyReadsKeysInterface {}) {
29                 let mut w = VecWriter(Vec::new());
30                 monitor.write(&mut w).unwrap();
31                 let deserialized_copy = <(Option<BlockHash>, channelmonitor::ChannelMonitor<EnforcingSigner>)>::read(&mut Cursor::new(&w.0), &OnlyReadsKeysInterface {}).unwrap();
32                 if let Some(deserialized) = deserialized_copy.0 {
33                         assert!(latest_block_hash == deserialized);
34                 }
35                 assert!(monitor == deserialized_copy.1);
36         }
37 }
38
39 pub fn chanmon_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
40         do_test(data, out);
41 }
42
43 #[no_mangle]
44 pub extern "C" fn chanmon_deser_run(data: *const u8, datalen: usize) {
45         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull{});
46 }