Merge pull request #1054 from ariard/2021-08-check-outbound-feerate
[rust-lightning] / fuzz / src / msg_targets / utils.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 #![macro_use]
11
12 use lightning::util::ser::Writer;
13 pub struct VecWriter(pub Vec<u8>);
14 impl Writer for VecWriter {
15         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
16                 self.0.extend_from_slice(buf);
17                 Ok(())
18         }
19 }
20
21 // We attempt to test the strictest behavior we can for a given message, however, some messages
22 // have different expected behavior. You can see which messages have which behavior in
23 // gen_target.sh, but, in general, the *_announcement messages have to round-trip exactly (as
24 // otherwise we'd invalidate the signatures), most messages just need to round-trip up to the
25 // amount of data we know how to interpret, and some messages we may throw out invalid stuff (eg
26 // if an error message isn't valid UTF-8 we cant String-ize it), so they wont roundtrip correctly.
27
28 // Tests a message that must survive roundtrip exactly, though may not empty the read buffer
29 // entirely
30 #[macro_export]
31 macro_rules! test_msg {
32         ($MsgType: path, $data: ident) => {
33                 {
34                         use lightning::util::ser::{Writeable, Readable};
35                         let mut r = ::std::io::Cursor::new($data);
36                         if let Ok(msg) = <$MsgType as Readable>::read(&mut r) {
37                                 let p = r.position() as usize;
38                                 let mut w = VecWriter(Vec::new());
39                                 msg.write(&mut w).unwrap();
40
41                                 assert_eq!(w.0.len(), p);
42                                 assert_eq!(msg.serialized_length(), p);
43                                 assert_eq!(&r.into_inner()[..p], &w.0[..p]);
44                         }
45                 }
46         }
47 }
48
49 // Tests a message that may lose data on roundtrip, but shoulnd't lose data compared to our
50 // re-serialization.
51 #[macro_export]
52 macro_rules! test_msg_simple {
53         ($MsgType: path, $data: ident) => {
54                 {
55                         use lightning::util::ser::{Writeable, Readable};
56                         let mut r = ::std::io::Cursor::new($data);
57                         if let Ok(msg) = <$MsgType as Readable>::read(&mut r) {
58                                 let mut w = VecWriter(Vec::new());
59                                 msg.write(&mut w).unwrap();
60                                 assert_eq!(msg.serialized_length(), w.0.len());
61
62                                 let msg = <$MsgType as Readable>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap();
63                                 let mut w_two = VecWriter(Vec::new());
64                                 msg.write(&mut w_two).unwrap();
65                                 assert_eq!(&w.0[..], &w_two.0[..]);
66                         }
67                 }
68         }
69 }
70
71 // Tests a message that must survive roundtrip exactly, and must exactly empty the read buffer and
72 // split it back out on re-serialization.
73 #[macro_export]
74 macro_rules! test_msg_exact {
75         ($MsgType: path, $data: ident) => {
76                 {
77                         use lightning::util::ser::{Writeable, Readable};
78                         let mut r = ::std::io::Cursor::new($data);
79                         if let Ok(msg) = <$MsgType as Readable>::read(&mut r) {
80                                 let mut w = VecWriter(Vec::new());
81                                 msg.write(&mut w).unwrap();
82                                 assert_eq!(&r.into_inner()[..], &w.0[..]);
83                                 assert_eq!(msg.serialized_length(), w.0.len());
84                         }
85                 }
86         }
87 }
88
89 // Tests a message that must survive roundtrip exactly, modulo one "hole" which may be set to
90 // any value on re-serialization.
91 #[macro_export]
92 macro_rules! test_msg_hole {
93         ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {
94                 {
95                         use lightning::util::ser::{Writeable, Readable};
96                         let mut r = ::std::io::Cursor::new($data);
97                         if let Ok(msg) = <$MsgType as Readable>::read(&mut r) {
98                                 let mut w = VecWriter(Vec::new());
99                                 msg.write(&mut w).unwrap();
100                                 let p = w.0.len() as usize;
101                                 assert_eq!(msg.serialized_length(), p);
102
103                                 assert_eq!(w.0.len(), p);
104                                 assert_eq!(&r.get_ref()[..$hole], &w.0[..$hole]);
105                                 assert_eq!(&r.get_ref()[$hole+$hole_len..p], &w.0[$hole+$hole_len..]);
106                         }
107                 }
108         }
109 }