ChannelManager+Router++ Logger Arc --> Deref
[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::util::enforcing_trait_impls::EnforcingChannelKeys;
7 use lightning::ln::channelmonitor;
8 use lightning::util::ser::{Readable, Writer};
9
10 use utils::test_logger;
11
12 use std::io::Cursor;
13
14 struct VecWriter(Vec<u8>);
15 impl Writer for VecWriter {
16         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
17                 self.0.extend_from_slice(buf);
18                 Ok(())
19         }
20         fn size_hint(&mut self, size: usize) {
21                 self.0.reserve_exact(size);
22         }
23 }
24
25 #[inline]
26 pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
27         if let Ok((latest_block_hash, monitor)) = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(data)) {
28                 let mut w = VecWriter(Vec::new());
29                 monitor.write_for_disk(&mut w).unwrap();
30                 let deserialized_copy = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&w.0)).unwrap();
31                 assert!(latest_block_hash == deserialized_copy.0);
32                 assert!(monitor == deserialized_copy.1);
33         }
34 }
35
36 pub fn chanmon_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
37         do_test(data, out);
38 }
39
40 #[no_mangle]
41 pub extern "C" fn chanmon_deser_run(data: *const u8, datalen: usize) {
42         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull{});
43 }