From 8c26e9183fd23874566b1129071f0e5f5edad6cd Mon Sep 17 00:00:00 2001 From: Janito Vaqueiro Ferreira Filho Date: Mon, 5 Jul 2021 20:16:20 +0000 Subject: [PATCH] Test encode/decode roundtrip of `Message`s Create a `Message` instance, encode it and then decode it using a `Codec` instance and check that the result is the same as the initial `Message`. For now, this only tests `Message::Inv` and `Message::GetData`, because these are the variants that are related to the scope of the current set of changes to support parsing the `MSG_WTX` inventory type. Even so, the test relies on being able to serialize an `InventoryHash::Wtx`, which is currently not implemented. Therefore the test was marked as `should_panic` until the serialization code is implemented. --- .../src/protocol/external/tests/prop.rs | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/zebra-network/src/protocol/external/tests/prop.rs b/zebra-network/src/protocol/external/tests/prop.rs index 09ae86bfaba..7f430c8a509 100644 --- a/zebra-network/src/protocol/external/tests/prop.rs +++ b/zebra-network/src/protocol/external/tests/prop.rs @@ -1,8 +1,12 @@ +use bytes::BytesMut; use proptest::{collection::vec, prelude::*}; +use tokio_util::codec::{Decoder, Encoder}; -use zebra_chain::serialization::{SerializationError, ZcashDeserializeInto, ZcashSerialize}; +use zebra_chain::serialization::{ + SerializationError, ZcashDeserializeInto, ZcashSerialize, MAX_PROTOCOL_MESSAGE_LEN, +}; -use super::super::InventoryHash; +use super::super::{Codec, InventoryHash, Message}; /// Maximum number of random input bytes to try to deserialize an [`InventoryHash`] from. /// @@ -55,4 +59,22 @@ proptest! { prop_assert!(deserialized.is_ok()); } } + + /// Test if a [`Message`] is not changed after encoding and decoding it. + #[test] + fn message_roundtrip( + message in prop_oneof!(Message::inv_strategy(), Message::get_data_strategy()), + ) { + let mut codec = Codec::builder().finish(); + let mut bytes = BytesMut::with_capacity(MAX_PROTOCOL_MESSAGE_LEN); + + let encoding_result = codec.encode(message.clone(), &mut bytes); + + prop_assert!(encoding_result.is_ok()); + + let decoded: Result, _> = codec.decode(&mut bytes); + + prop_assert!(decoded.is_ok()); + prop_assert_eq!(decoded.unwrap(), Some(message)); + } }