1 // This file is Copyright its original authors, visible in version control
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
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);
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.
28 // Tests a message that must survive roundtrip exactly, though may not empty the read buffer
31 macro_rules! test_msg {
32 ($MsgType: path, $data: ident) => {
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();
41 assert_eq!(w.0.len(), p);
42 assert_eq!(msg.serialized_length(), p);
43 assert_eq!(&r.into_inner()[..p], &w.0[..p]);
49 // Tests a message that may lose data on roundtrip, but shoulnd't lose data compared to our
52 macro_rules! test_msg_simple {
53 ($MsgType: path, $data: ident) => {
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());
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[..]);
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.
74 macro_rules! test_msg_exact {
75 ($MsgType: path, $data: ident) => {
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());
89 // Tests a message that must survive roundtrip exactly, modulo one "hole" which may be set to
90 // any value on re-serialization.
92 macro_rules! test_msg_hole {
93 ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {
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);
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..]);