some tests

pull/200/head
Chip Senkbeil 1 year ago
parent 808327eaff
commit 56544b7163
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -252,3 +252,106 @@ fn read_key_eq<'a>(input: &'a [u8], key: &str) -> Result<((), &'a [u8]), &'a [u8
_ => Err(input),
}
}
#[cfg(test)]
mod tests {
use super::*;
mod read_str_bytes {
use super::*;
use test_log::test;
#[test]
fn should_fail_if_input_is_empty() {
let input = read_str_bytes(&[]).unwrap_err();
assert!(input.is_empty());
}
#[test]
fn should_fail_if_input_does_not_start_with_str() {
let input = read_str_bytes(&[0xff, 0xa5, b'h', b'e', b'l', b'l', b'o']).unwrap_err();
assert_eq!(input, [0xff, 0xa5, b'h', b'e', b'l', b'l', b'o']);
}
#[test]
fn should_succeed_if_input_starts_with_str() {
let (s, remaining) =
read_str_bytes(&[0xa5, b'h', b'e', b'l', b'l', b'o', 0xff]).unwrap();
assert_eq!(s, "hello");
assert_eq!(remaining, [0xff]);
}
}
mod read_key_eq {
use super::*;
use test_log::test;
#[test]
fn should_fail_if_input_is_empty() {
let input = read_key_eq(&[], "key").unwrap_err();
assert!(input.is_empty());
}
#[test]
fn should_fail_if_input_does_not_start_with_str() {
let input = &[
0xff,
rmp::Marker::FixStr(5).to_u8(),
b'h',
b'e',
b'l',
b'l',
b'o',
];
let remaining = read_key_eq(input, "key").unwrap_err();
assert_eq!(remaining, input);
}
#[test]
fn should_fail_if_read_key_does_not_match_specified_key() {
let input = &[
rmp::Marker::FixStr(5).to_u8(),
b'h',
b'e',
b'l',
b'l',
b'o',
0xff,
];
let remaining = read_key_eq(input, "key").unwrap_err();
assert_eq!(remaining, input);
}
#[test]
fn should_succeed_if_read_key_matches_specified_key() {
let input = &[
rmp::Marker::FixStr(5).to_u8(),
b'h',
b'e',
b'l',
b'l',
b'o',
0xff,
];
let (_, remaining) = read_key_eq(input, "hello").unwrap();
assert_eq!(remaining, [0xff]);
}
}
mod find_msgpack_byte_len {
use super::*;
use test_log::test;
#[test]
fn should_return_none_if_input_is_empty() {
let len = find_msgpack_byte_len(&[]);
assert_eq!(len, None);
}
#[test]
fn should_return_none_if_input_has_reserved_marker() {
let len = find_msgpack_byte_len(&[rmp::Marker::Reserved.to_u8()]);
assert_eq!(len, None);
}
}
}

@ -12,7 +12,7 @@ use crate::common::utils;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Request<T> {
/// Optional header data to include with request
#[serde(default, flatten, skip_serializing_if = "Header::is_empty")]
#[serde(default, skip_serializing_if = "Header::is_empty")]
pub header: Header,
/// Unique id associated with the request

@ -12,7 +12,7 @@ use crate::common::utils;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Response<T> {
/// Optional header data to include with response
#[serde(default, flatten, skip_serializing_if = "Header::is_empty")]
#[serde(default, skip_serializing_if = "Header::is_empty")]
pub header: Header,
/// Unique id associated with the response
@ -82,11 +82,26 @@ pub enum UntypedResponseParseError {
/// When the bytes do not represent a response
WrongType,
/// When a header should be present, but the key is wrong
InvalidHeaderKey,
/// When a header should be present, but the header bytes are wrong
InvalidHeader,
/// When the key for the id is wrong
InvalidIdKey,
/// When the id is not a valid UTF-8 string
InvalidId,
/// When the key for the origin id is wrong
InvalidOriginIdKey,
/// When the origin id is not a valid UTF-8 string
InvalidOriginId,
/// When the key for the payload is wrong
InvalidPayloadKey,
}
#[inline]
@ -234,11 +249,11 @@ impl<'a> UntypedResponse<'a> {
// Parse the header if we have one
let (header, input) = if has_header {
let (_, input) =
read_key_eq(input, "header").map_err(|_| UntypedResponseParseError::WrongType)?;
let (_, input) = read_key_eq(input, "header")
.map_err(|_| UntypedResponseParseError::InvalidHeaderKey)?;
let (header, input) =
read_header_bytes(input).map_err(|_| UntypedResponseParseError::WrongType)?;
read_header_bytes(input).map_err(|_| UntypedResponseParseError::InvalidHeader)?;
(header, input)
} else {
([0u8; 0].as_slice(), input)
@ -246,23 +261,23 @@ impl<'a> UntypedResponse<'a> {
// Validate that next field is id
let (_, input) =
read_key_eq(input, "id").map_err(|_| UntypedResponseParseError::WrongType)?;
read_key_eq(input, "id").map_err(|_| UntypedResponseParseError::InvalidIdKey)?;
// Get the id itself
let (id, input) =
read_str_bytes(input).map_err(|_| UntypedResponseParseError::InvalidId)?;
// Validate that next field is origin_id
let (_, input) =
read_key_eq(input, "origin_id").map_err(|_| UntypedResponseParseError::WrongType)?;
let (_, input) = read_key_eq(input, "origin_id")
.map_err(|_| UntypedResponseParseError::InvalidOriginIdKey)?;
// Get the origin_id itself
let (origin_id, input) =
read_str_bytes(input).map_err(|_| UntypedResponseParseError::InvalidOriginId)?;
// Validate that final field is payload
let (_, input) =
read_key_eq(input, "payload").map_err(|_| UntypedResponseParseError::WrongType)?;
let (_, input) = read_key_eq(input, "payload")
.map_err(|_| UntypedResponseParseError::InvalidPayloadKey)?;
let header = Cow::Borrowed(header);
let id = Cow::Borrowed(id);
@ -424,7 +439,7 @@ mod tests {
// Missing fields (corrupt data)
assert_eq!(
UntypedResponse::from_slice(&[0x83]),
Err(UntypedResponseParseError::WrongType)
Err(UntypedResponseParseError::InvalidIdKey)
);
// Missing id field (has valid data itself)
@ -442,7 +457,7 @@ mod tests {
.concat()
.as_slice()
),
Err(UntypedResponseParseError::WrongType)
Err(UntypedResponseParseError::InvalidIdKey)
);
// Non-str id field value
@ -496,7 +511,7 @@ mod tests {
.concat()
.as_slice()
),
Err(UntypedResponseParseError::WrongType)
Err(UntypedResponseParseError::InvalidOriginIdKey)
);
// Non-str origin_id field value
@ -550,7 +565,7 @@ mod tests {
.concat()
.as_slice()
),
Err(UntypedResponseParseError::WrongType)
Err(UntypedResponseParseError::InvalidPayloadKey)
);
}
}

Loading…
Cancel
Save