Merge branch '2022-02-bal-panic' into 2022-02-0.0.105-sec
[rust-lightning] / fuzz / src / msg_targets / utils.rs
index 6125e5afd611d3dde51a8713b7b3c4f4cd097edf..6325f3bf10d07bab3469b018ab106c7c732ec1d7 100644 (file)
@@ -1,16 +1,21 @@
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
 #![macro_use]
 
 use lightning::util::ser::Writer;
 pub struct VecWriter(pub Vec<u8>);
 impl Writer for VecWriter {
        fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
-               assert!(self.0.capacity() >= self.0.len() + buf.len());
                self.0.extend_from_slice(buf);
                Ok(())
        }
-       fn size_hint(&mut self, size: usize) {
-               self.0.reserve_exact(size);
-       }
 }
 
 // We attempt to test the strictest behavior we can for a given message, however, some messages
@@ -34,6 +39,7 @@ macro_rules! test_msg {
                                msg.write(&mut w).unwrap();
 
                                assert_eq!(w.0.len(), p);
+                               assert_eq!(msg.serialized_length(), p);
                                assert_eq!(&r.into_inner()[..p], &w.0[..p]);
                        }
                }
@@ -51,6 +57,7 @@ macro_rules! test_msg_simple {
                        if let Ok(msg) = <$MsgType as Readable>::read(&mut r) {
                                let mut w = VecWriter(Vec::new());
                                msg.write(&mut w).unwrap();
+                               assert_eq!(msg.serialized_length(), w.0.len());
 
                                let msg = <$MsgType as Readable>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap();
                                let mut w_two = VecWriter(Vec::new());
@@ -73,13 +80,14 @@ macro_rules! test_msg_exact {
                                let mut w = VecWriter(Vec::new());
                                msg.write(&mut w).unwrap();
                                assert_eq!(&r.into_inner()[..], &w.0[..]);
+                               assert_eq!(msg.serialized_length(), w.0.len());
                        }
                }
        }
 }
 
-// Tests a message that must survive roundtrip exactly, modulo one "hole" which may be set to 0s on
-// re-serialization.
+// Tests a message that must survive roundtrip exactly, modulo one "hole" which may be set to
+// any value on re-serialization.
 #[macro_export]
 macro_rules! test_msg_hole {
        ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {
@@ -90,6 +98,7 @@ macro_rules! test_msg_hole {
                                let mut w = VecWriter(Vec::new());
                                msg.write(&mut w).unwrap();
                                let p = w.0.len() as usize;
+                               assert_eq!(msg.serialized_length(), p);
 
                                assert_eq!(w.0.len(), p);
                                assert_eq!(&r.get_ref()[..$hole], &w.0[..$hole]);