`rustfmt`: `fuzz/src/fromstr_to_netaddress.rs`
[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::ser::{ReadableArgs, Writeable, Writer};
8 use lightning::util::test_channel_signer::TestChannelSigner;
9 use lightning::util::test_utils::OnlyReadsKeysInterface;
10
11 use crate::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 }
22
23 #[inline]
24 pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
25         if let Ok((latest_block_hash, monitor)) =
26                 <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
27                         &mut Cursor::new(data),
28                         (&OnlyReadsKeysInterface {}, &OnlyReadsKeysInterface {}),
29                 ) {
30                 let mut w = VecWriter(Vec::new());
31                 monitor.write(&mut w).unwrap();
32                 let deserialized_copy =
33                         <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
34                                 &mut Cursor::new(&w.0),
35                                 (&OnlyReadsKeysInterface {}, &OnlyReadsKeysInterface {}),
36                         )
37                         .unwrap();
38                 assert!(latest_block_hash == deserialized_copy.0);
39                 assert!(monitor == deserialized_copy.1);
40         }
41 }
42
43 pub fn chanmon_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
44         do_test(data, out);
45 }
46
47 #[no_mangle]
48 pub extern "C" fn chanmon_deser_run(data: *const u8, datalen: usize) {
49         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
50 }