Improve msgs::message coverage

This commit is contained in:
Joseph Birr-Pixton 2020-06-07 17:19:29 +01:00
parent 39175e7252
commit 1803e8e7ad
2 changed files with 35 additions and 6 deletions

View File

@ -105,7 +105,6 @@ impl Codec for Message {
}
}
#[derive(Debug)]
pub enum MessageError {
TooShortForHeader,
TooShortForLength,
@ -162,10 +161,6 @@ impl Message {
pub fn is_handshake_type(&self, hstyp: HandshakeType) -> bool {
// Bit of a layering violation, but OK.
if !self.is_content_type(ContentType::Handshake) {
return false;
}
if let MessagePayload::Handshake(ref hsp) = self.payload {
hsp.typ == hstyp
} else {
@ -257,7 +252,6 @@ impl<'a> Message {
///
/// This type also cannot decode its internals and
/// is not a `Codec` type, only `Message` can do that.
#[derive(Debug)]
pub struct BorrowMessage<'a> {
pub typ: ContentType,
pub version: ProtocolVersion,

View File

@ -1,6 +1,11 @@
use super::codec::Reader;
use super::codec::Codec;
use super::message::Message;
use super::enums::{
AlertDescription,
AlertLevel,
HandshakeType
};
use std::fs;
use std::io::Read;
@ -60,3 +65,33 @@ fn can_read_safari_client_hello() {
println!("m = {:?}", m);
assert_eq!(m.decode_payload(), false);
}
#[test]
fn alert_is_not_handshake() {
let m = Message::build_alert(AlertLevel::Fatal, AlertDescription::DecodeError);
assert_eq!(false, m.is_handshake_type(HandshakeType::ClientHello));
}
#[test]
fn alert_is_not_opaque() {
let mut m = Message::build_alert(AlertLevel::Fatal, AlertDescription::DecodeError);
assert_eq!(None, m.take_opaque_payload());
assert_eq!(false, m.decode_payload());
}
#[test]
fn construct_all_types() {
let samples = [
&b"\x14\x03\x04\x00\x01\x01"[..],
&b"\x15\x03\x04\x00\x02\x01\x16"[..],
&b"\x16\x03\x04\x00\x05\x18\x00\x00\x01\x00"[..],
&b"\x17\x03\x04\x00\x04\x11\x22\x33\x44"[..],
&b"\x18\x03\x04\x00\x04\x11\x22\x33\x44"[..]
];
for bytes in samples.iter() {
let mut m = Message::read_bytes(bytes).unwrap();
println!("m = {:?}", m);
m.decode_payload();
println!("m' = {:?}", m);
}
}