Add more tests

next
Sayan Nandan 2 years ago
parent 71dcfb7efc
commit 3b918ff828
No known key found for this signature in database
GPG Key ID: 8BC07A0A4D41DD52

@ -164,27 +164,27 @@ impl UnsafeSlice {
} }
} }
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq)]
#[repr(u8)] #[repr(u8)]
/// # Parser Errors /// # Parser Errors
/// ///
/// Several errors can arise during parsing and this enum accounts for them /// Several errors can arise during parsing and this enum accounts for them
pub enum ParseError { pub enum ParseError {
/// Didn't get the number of expected bytes /// Didn't get the number of expected bytes
NotEnough = 0, NotEnough = 0u8,
/// The packet simply contains invalid data /// The packet simply contains invalid data
BadPacket = 1, BadPacket = 1u8,
/// The query contains an unexpected byte /// The query contains an unexpected byte
UnexpectedByte = 2, UnexpectedByte = 2u8,
/// A data type was given but the parser failed to serialize it into this type /// A data type was given but the parser failed to serialize it into this type
/// ///
/// This can happen not just for elements but can also happen for their sizes ([`Self::parse_into_u64`]) /// This can happen not just for elements but can also happen for their sizes ([`Self::parse_into_u64`])
DatatypeParseFailure = 3, DatatypeParseFailure = 3u8,
/// A data type that the server doesn't know was passed into the query /// A data type that the server doesn't know was passed into the query
/// ///
/// This is a frequent problem that can arise between different server editions as more data types /// This is a frequent problem that can arise between different server editions as more data types
/// can be added with changing server versions /// can be added with changing server versions
UnknownDatatype = 4, UnknownDatatype = 4u8,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]

@ -28,10 +28,11 @@
use crate::corestore::heap_array::HeapArray; use crate::corestore::heap_array::HeapArray;
use crate::protocol::{ParseError, ParseResult, UnsafeSlice}; use crate::protocol::{ParseError, ParseResult, UnsafeSlice};
use core::marker::PhantomData; use core::{marker::PhantomData, mem::transmute};
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
#[derive(Debug)]
pub struct Query { pub struct Query {
forward: usize, forward: usize,
data: QueryType, data: QueryType,
@ -43,11 +44,13 @@ impl Query {
} }
} }
#[derive(Debug)]
pub enum QueryType { pub enum QueryType {
Simple(SimpleQuery), Simple(SimpleQuery),
Pipelined(PipelinedQuery), Pipelined(PipelinedQuery),
} }
#[derive(Debug)]
pub struct SimpleQuery { pub struct SimpleQuery {
data: HeapArray<UnsafeSlice>, data: HeapArray<UnsafeSlice>,
} }
@ -70,6 +73,7 @@ struct OwnedSimpleQuery {
data: Vec<Vec<u8>>, data: Vec<Vec<u8>>,
} }
#[derive(Debug)]
pub struct PipelinedQuery { pub struct PipelinedQuery {
data: HeapArray<HeapArray<UnsafeSlice>>, data: HeapArray<HeapArray<UnsafeSlice>>,
} }
@ -207,7 +211,7 @@ impl<'a> Parser<'a> {
Ok(UnsafeSlice::new(start_ptr, len)) Ok(UnsafeSlice::new(start_ptr, len))
} else { } else {
// just some silly hackery // just some silly hackery
Err(*(&(1 & has_lf as u8) as *const u8 as *const ParseError)) Err(transmute(has_lf))
} }
} }
} }
@ -312,7 +316,8 @@ impl<'a> Parser<'a> {
unsafe { unsafe {
let mut queries = HeapArray::with_capacity(query_count); let mut queries = HeapArray::with_capacity(query_count);
for i in 0..query_count { for i in 0..query_count {
queries.write_to_index(i, self._next_simple_query()?); let sq = self._next_simple_query()?;
queries.write_to_index(i, sq);
} }
Ok(PipelinedQuery { data: queries }) Ok(PipelinedQuery { data: queries })
} }

@ -563,6 +563,15 @@ fn read_usize_fail() {
); );
} }
#[test]
fn parse_fail_because_unknown_query_scheme() {
let body = v!(b"?3\n3\nSET1\nx3\n100");
assert_eq!(
Parser::parse(&body).unwrap_err(),
ParseError::UnexpectedByte
)
}
#[test] #[test]
fn simple_query_okay() { fn simple_query_okay() {
let body = v!(b"*3\n3\nSET1\nx3\n100"); let body = v!(b"*3\n3\nSET1\nx3\n100");
@ -571,6 +580,21 @@ fn simple_query_okay() {
let query = simple_query(ret); let query = simple_query(ret);
assert_eq!(query.into_owned().data, v!["SET", "x", "100"]); assert_eq!(query.into_owned().data, v!["SET", "x", "100"]);
} }
#[test]
fn parse_fail_because_not_enough() {
let full_payload = b"*3\n3\nSET1\nx3\n100";
let samples: Vec<Vec<u8>> = (0..full_payload.len() - 1)
.map(|i| full_payload.iter().take(i).cloned().collect())
.collect();
for body in samples {
assert_eq!(
Parser::parse(&body).unwrap_err(),
ParseError::NotEnough,
"Failed with body len: {}",
body.len()
)
}
}
#[test] #[test]
fn pipelined_query_okay() { fn pipelined_query_okay() {
@ -583,3 +607,15 @@ fn pipelined_query_okay() {
vec![v!["SET", "x", "100"], v!["GET", "x"]] vec![v!["SET", "x", "100"], v!["GET", "x"]]
) )
} }
#[test]
fn pipelined_query_fail_because_not_enough() {
let full_payload = v!(b"$2\n3\n3\nSET1\nx3\n1002\n3\nGET1\nx");
let samples: Vec<Vec<u8>> = (0..full_payload.len() - 1)
.map(|i| full_payload.iter().cloned().take(i).collect())
.collect();
for body in samples {
let ret = Parser::parse(&body).unwrap_err();
assert_eq!(ret, ParseError::NotEnough)
}
}

Loading…
Cancel
Save