Skip to content

Commit

Permalink
Test encode/decode roundtrip of some Messages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jvff committed Jul 6, 2021
1 parent 00c13b6 commit 49cf1cf
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions zebra-network/src/protocol/external/tests/prop.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -66,4 +70,23 @@ proptest! {
prop_assert_eq!(&bytes, &input[..bytes.len()]);
}
}

/// Test if a [`Message::{Inv, GetData}`] is not changed after encoding and decoding it.
// TODO: Update this test to cover all `Message` variants.
#[test]
fn inv_and_getdata_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<Option<Message>, _> = codec.decode(&mut bytes);

prop_assert!(decoded.is_ok());
prop_assert_eq!(decoded.unwrap(), Some(message));
}
}

0 comments on commit 49cf1cf

Please sign in to comment.