AsyncPaymentsMessageHandler trait for OnionMessenger
[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                 use lightning::util::ser::{Readable, Writeable};
34                 let mut r = ::std::io::Cursor::new($data);
35                 if let Ok(msg) = <$MsgType as Readable>::read(&mut r) {
36                         let p = r.position() as usize;
37                         let mut w = VecWriter(Vec::new());
38                         msg.write(&mut w).unwrap();
39
40                         assert_eq!(w.0.len(), p);
41                         assert_eq!(msg.serialized_length(), p);
42                         assert_eq!(&r.into_inner()[..p], &w.0[..p]);
43                 }
44         }};
45 }
46
47 // Tests a message that may lose data on roundtrip, but shoulnd't lose data compared to our
48 // re-serialization.
49 #[macro_export]
50 macro_rules! test_msg_simple {
51         ($MsgType: path, $data: ident) => {{
52                 use lightning::util::ser::{Readable, Writeable};
53                 let mut r = ::std::io::Cursor::new($data);
54                 if let Ok(msg) = <$MsgType as Readable>::read(&mut r) {
55                         let mut w = VecWriter(Vec::new());
56                         msg.write(&mut w).unwrap();
57                         assert_eq!(msg.serialized_length(), w.0.len());
58
59                         let msg = <$MsgType as Readable>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap();
60                         let mut w_two = VecWriter(Vec::new());
61                         msg.write(&mut w_two).unwrap();
62                         assert_eq!(&w.0[..], &w_two.0[..]);
63                 }
64         }};
65 }
66
67 // Tests a message that must survive roundtrip exactly, and must exactly empty the read buffer and
68 // split it back out on re-serialization.
69 #[macro_export]
70 macro_rules! test_msg_exact {
71         ($MsgType: path, $data: ident) => {{
72                 use lightning::util::ser::{Readable, Writeable};
73                 let mut r = ::std::io::Cursor::new($data);
74                 if let Ok(msg) = <$MsgType as Readable>::read(&mut r) {
75                         let mut w = VecWriter(Vec::new());
76                         msg.write(&mut w).unwrap();
77                         assert_eq!(&r.into_inner()[..], &w.0[..]);
78                         assert_eq!(msg.serialized_length(), w.0.len());
79                 }
80         }};
81 }
82
83 // Tests a message that must survive roundtrip exactly, modulo one "hole" which may be set to
84 // any value on re-serialization.
85 #[macro_export]
86 macro_rules! test_msg_hole {
87         ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {{
88                 use lightning::util::ser::{Readable, Writeable};
89                 let mut r = ::std::io::Cursor::new($data);
90                 if let Ok(msg) = <$MsgType as Readable>::read(&mut r) {
91                         let mut w = VecWriter(Vec::new());
92                         msg.write(&mut w).unwrap();
93                         let p = w.0.len() as usize;
94                         assert_eq!(msg.serialized_length(), p);
95
96                         assert_eq!(w.0.len(), p);
97                         assert_eq!(&r.get_ref()[..$hole], &w.0[..$hole]);
98                         assert_eq!(&r.get_ref()[$hole + $hole_len..p], &w.0[$hole + $hole_len..]);
99                 }
100         }};
101 }