Abstracted message serialisation test code.

When implementing tests for signing and/or encrypting messages, we can just implement the `MessageSender`/`MessageReceiver` traits when needed then call into this function.

(Those traits may need refactoring if the format of the adapter is not desireable (i.e. `&self` instead of `&mut self` or `self`; we could only blanket-impl those function to `Fn()` closures, not `FnMut()` or `FnOnce()`. (not that we should try this.)), but for now we"ll leave it as it is.)

Fortune for rsh's current commit: Small blessing − 小吉
specialisation
Avril 3 years ago
parent 1f2655a7f2
commit 36f3ed4586
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -373,23 +373,36 @@ impl<V: ?Sized + MessageValue> SerializedMessage<V>
mod tests mod tests
{ {
use super::*; use super::*;
#[test]
fn message_serial() /// Generic message test function
fn message_serial_generic<S,D>(s: S, d: D)
where S: MessageSender,
D: MessageReceiver
{ {
eprintln!("=== Message serialisation with tc, rc: S: {}, D: {}", std::any::type_name::<S>(), std::any::type_name::<D>());
let message = MessageBuilder::new() let message = MessageBuilder::new()
.create(format!("This is a string, and some random data: {:?}", aes::AesKey::generate().unwrap())) .create(format!("This is a string, and some random data: {:?}", aes::AesKey::generate().unwrap()))
.expect("Failed to create message"); .expect("Failed to create message");
println!(">> Created message: {:?}", message); println!(">> Created message: {:?}", message);
let serialised = message.serialise(()).expect("Failed to serialise message"); let serialised = message.serialise(s).expect("Failed to serialise message");
println!(">> Serialised message: {:?}", serialised); println!(">> Serialised message: {:?}", serialised);
let binary = serialised.into_bytes(); let binary = serialised.into_bytes();
println!(">> Written to {} bytes", binary.len()); println!(">> Written to {} bytes", binary.len());
let read = SerializedMessage::from_bytes(&binary).expect("Failed to read serialised message from binary"); let read = SerializedMessage::from_bytes(&binary).expect("Failed to read serialised message from binary");
println!(">> Read from bytes: {:?}", read); println!(">> Read from bytes: {:?}", read);
let deserialised = Message::deserialize(&read, ()).expect("Failed to deserialise message"); let deserialised = Message::deserialize(&read, d).expect("Failed to deserialise message");
println!(">> Deserialised message: {:?}", deserialised); println!(">> Deserialised message: {:?}", deserialised);
assert_eq!(message, deserialised, "Messages not identical"); assert_eq!(message, deserialised, "Messages not identical");
assert_eq!(message.into_value(), deserialised.into_value(), "Message values not identical"); assert_eq!(message.into_value(), deserialised.into_value(), "Message values not identical");
eprintln!("=== Passed (S: {}, D: {}) ===", std::any::type_name::<S>(), std::any::type_name::<D>());
} }
#[test]
fn message_serial_basic()
{
message_serial_generic((), ());
}
//TODO: message_serial_sign(), message_serial_encrypted(), message_serial_encrypted_sign()
} }

Loading…
Cancel
Save