]> git.bitcoin.ninja Git - rust-lightning/blob - fuzz/fuzz_targets/msg_targets/utils.rs
Merge pull request #185 from TheBlueMatt/2018-09-ser-rework
[rust-lightning] / fuzz / fuzz_targets / msg_targets / utils.rs
1 #![macro_use]
2
3 use lightning::util::ser::Writer;
4 pub struct VecWriter(pub Vec<u8>);
5 impl Writer for VecWriter {
6         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
7                 assert!(self.0.capacity() >= self.0.len() + buf.len());
8                 self.0.extend_from_slice(buf);
9                 Ok(())
10         }
11         fn size_hint(&mut self, size: usize) {
12                 self.0.reserve_exact(size);
13         }
14 }
15
16 #[macro_export]
17 macro_rules! test_msg {
18         ($MsgType: path, $data: ident) => {
19                 {
20                         use lightning::util::ser::{Writeable, Readable};
21                         let mut r = ::std::io::Cursor::new($data);
22                         if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
23                                 let p = r.position() as usize;
24                                 let mut w = VecWriter(Vec::new());
25                                 msg.write(&mut w).unwrap();
26
27                                 assert_eq!(w.0.len(), p);
28                                 assert_eq!(&r.into_inner()[..p], &w.0[..p]);
29                         }
30                 }
31         }
32 }
33
34 #[macro_export]
35 macro_rules! test_msg_simple {
36         ($MsgType: path, $data: ident) => {
37                 {
38                         use lightning::util::ser::{Writeable, Readable};
39                         let mut r = ::std::io::Cursor::new($data);
40                         if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
41                                 let mut w = VecWriter(Vec::new());
42                                 msg.write(&mut w).unwrap();
43                         }
44                 }
45         }
46 }
47
48 #[macro_export]
49 macro_rules! test_msg_exact {
50         ($MsgType: path, $data: ident) => {
51                 {
52                         use lightning::util::ser::{Writeable, Readable};
53                         let mut r = ::std::io::Cursor::new($data);
54                         if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
55                                 let mut w = VecWriter(Vec::new());
56                                 msg.write(&mut w).unwrap();
57
58                                 assert_eq!(&r.into_inner()[..], &w.0[..]);
59                         }
60                 }
61         }
62 }
63
64 #[macro_export]
65 macro_rules! test_msg_hole {
66         ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {
67                 {
68                         use lightning::util::ser::{Writeable, Readable};
69                         let mut r = ::std::io::Cursor::new($data);
70                         if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
71                                 let mut w = VecWriter(Vec::new());
72                                 msg.write(&mut w).unwrap();
73                                 let p = w.0.len() as usize;
74
75                                 assert_eq!(w.0.len(), p);
76                                 assert_eq!(&r.get_ref()[..$hole], &w.0[..$hole]);
77                                 assert_eq!(&r.get_ref()[$hole+$hole_len..p], &w.0[$hole+$hole_len..]);
78                         }
79                 }
80         }
81 }