Skip to content

Commit

Permalink
add message deserialization tests + fix minor oopsie
Browse files Browse the repository at this point in the history
  • Loading branch information
antonilol committed Nov 13, 2023
1 parent ba99268 commit 4345783
Showing 1 changed file with 74 additions and 2 deletions.
76 changes: 74 additions & 2 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ impl Message {
let topic = match self {
Self::HashBlock(..) => "hashblock",
Self::HashTx(..) => "hashtx",
Self::Block(..) => "block",
Self::Tx(..) => "tx",
Self::Block(..) => "rawblock",
Self::Tx(..) => "rawtx",
Self::Sequence(..) => "sequence",
};

Expand Down Expand Up @@ -186,3 +186,75 @@ impl fmt::Display for Message {
}
}
}

#[cfg(test)]
mod tests {
use crate::{Error, Message};
use bitcoin::{consensus::serialize, constants::genesis_block, hashes::Hash, Network};

#[test]
fn test_deserialize_rawtx() {
let genesis_block = genesis_block(Network::Bitcoin);

let tx = &genesis_block.txdata[0];
let tx_bytes = serialize(tx);
let txid = tx.txid();
let mut txid_bytes = txid.to_byte_array();
txid_bytes.reverse();

let to_deserialize = [
b"rawtx" as &[u8],
&tx_bytes,
&[0x03, 0x00, 0x00, 0x00],
b"garbage",
];

let msg = Message::from_multipart(&to_deserialize[..3]).unwrap();

assert_eq!(msg, Message::Tx(tx.clone(), 3));

assert_eq!(msg.topic_str(), "rawtx");
assert_eq!(msg.serialize_data_to_vec(), tx_bytes);
assert_eq!(msg.sequence(), 3);

assert_eq!(msg.serialize_to_vecs(), to_deserialize[0..3]);

assert!(matches!(
Message::from_multipart(&to_deserialize[..0]),
Err(Error::InvalidMutlipartLength(0))
));
assert!(matches!(
Message::from_multipart(&to_deserialize[..1]),
Err(Error::InvalidMutlipartLength(1))
));
assert!(matches!(
Message::from_multipart(&to_deserialize[..2]),
Err(Error::InvalidMutlipartLength(2))
));
assert!(matches!(
Message::from_multipart(&to_deserialize[..4]),
Err(Error::InvalidMutlipartLength(4))
));
}

#[test]
fn test_deserialize_hashtx() {
let genesis_block = genesis_block(Network::Bitcoin);

let txid = genesis_block.txdata[0].txid();
let mut txid_bytes = txid.to_byte_array();
txid_bytes.reverse();

let to_deserialize = [b"hashtx" as &[u8], &txid_bytes, &[0x04, 0x00, 0x00, 0x00]];

let msg = Message::from_multipart(&to_deserialize).unwrap();

assert_eq!(msg, Message::HashTx(txid, 4));

assert_eq!(msg.topic_str(), "hashtx");
assert_eq!(msg.serialize_data_to_vec(), txid_bytes);
assert_eq!(msg.sequence(), 4);

assert_eq!(msg.serialize_to_vecs(), to_deserialize);
}
}

0 comments on commit 4345783

Please sign in to comment.