fix test payloads

This commit is contained in:
NikVolf
2018-02-16 00:00:04 +03:00
parent cc0cd414aa
commit f868b7b371
3 changed files with 12 additions and 35 deletions

View File

@ -40,16 +40,10 @@ pub use self::name_section::{
LocalNameSection, LocalNameSection,
}; };
/// Generic buffer error for deserializing
pub trait BufferError : Sized {
/// Return buffer error instance
fn buffer() -> Self;
}
/// Deserialization from serial i/o /// Deserialization from serial i/o
pub trait Deserialize : Sized { pub trait Deserialize : Sized {
/// Serialization error produced by deserialization routine. /// Serialization error produced by deserialization routine.
type Error: BufferError; type Error: From<io::Error>;
/// Deserialize type from serial i/o /// Deserialize type from serial i/o
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error>; fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error>;
} }
@ -57,7 +51,7 @@ pub trait Deserialize : Sized {
/// Serialization to serial i/o /// Serialization to serial i/o
pub trait Serialize { pub trait Serialize {
/// Serialization error produced by serialization routine. /// Serialization error produced by serialization routine.
type Error; type Error: From<io::Error>;
/// Serialize type to serial i/o /// Serialize type to serial i/o
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error>; fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error>;
} }
@ -108,14 +102,6 @@ pub enum Error {
InconsistentMetadata, InconsistentMetadata,
/// Invalid section id /// Invalid section id
InvalidSectionId(u8), InvalidSectionId(u8),
/// There is still data left in the buffer
BufferUnderflow,
}
impl BufferError for Error {
fn buffer() -> Self {
Error::BufferUnderflow
}
} }
impl fmt::Display for Error { impl fmt::Display for Error {
@ -142,7 +128,6 @@ impl fmt::Display for Error {
Error::InvalidVarUint64 => write!(f, "Not an unsigned 64-bit integer"), Error::InvalidVarUint64 => write!(f, "Not an unsigned 64-bit integer"),
Error::InconsistentMetadata => write!(f, "Inconsistent metadata"), Error::InconsistentMetadata => write!(f, "Inconsistent metadata"),
Error::InvalidSectionId(ref id) => write!(f, "Invalid section id: {}", id), Error::InvalidSectionId(ref id) => write!(f, "Invalid section id: {}", id),
Error::BufferUnderflow => write!(f, "Buffer underflow"),
} }
} }
} }
@ -169,7 +154,6 @@ impl error::Error for Error {
Error::InvalidVarUint64 => "Not an unsigned 64-bit integer", Error::InvalidVarUint64 => "Not an unsigned 64-bit integer",
Error::InconsistentMetadata => "Inconsistent metadata", Error::InconsistentMetadata => "Inconsistent metadata",
Error::InvalidSectionId(_) => "Invalid section id", Error::InvalidSectionId(_) => "Invalid section id",
Error::BufferUnderflow => "Buffer underflow",
} }
} }
} }
@ -214,8 +198,9 @@ pub fn deserialize_file<P: AsRef<::std::path::Path>>(p: P) -> Result<Module, Err
pub fn deserialize_buffer<T: Deserialize>(contents: &[u8]) -> Result<T, T::Error> { pub fn deserialize_buffer<T: Deserialize>(contents: &[u8]) -> Result<T, T::Error> {
let mut reader = io::Cursor::new(contents); let mut reader = io::Cursor::new(contents);
let result = T::deserialize(&mut reader)?; let result = T::deserialize(&mut reader)?;
println!("position: {}, content.len: {}", reader.position(), contents.len());
if reader.position() != contents.len() as u64 { if reader.position() != contents.len() as u64 {
return Err(T::Error::buffer()) return Err(io::Error::from(io::ErrorKind::InvalidData).into())
} }
Ok(result) Ok(result)
} }

View File

@ -468,15 +468,6 @@ mod integration_tests {
assert_eq!(Module::default().magic, module2.magic); assert_eq!(Module::default().magic, module2.magic);
} }
#[test]
fn inconsistent_meta() {
let result = deserialize_file("./res/cases/v1/payload_len.wasm");
// should be error, not panic
if let Err(Error::InconsistentMetadata) = result {}
else { panic!("Should return inconsistent metadata error"); }
}
#[test] #[test]
fn names() { fn names() {
use super::super::name_section::NameSection; use super::super::name_section::NameSection;

View File

@ -873,14 +873,14 @@ mod tests {
fn types_test_payload() -> &'static [u8] { fn types_test_payload() -> &'static [u8] {
&[ &[
// section length // section length
148u8, 0x80, 0x80, 0x80, 0x0, 11,
// 2 functions // 2 functions
130u8, 0x80, 0x80, 0x80, 0x0, 2,
// func 1, form =1 // func 1, form =1
0x01, 0x01,
// param_count=1 // param_count=1
129u8, 0x80, 0x80, 0x80, 0x0, 1,
// first param // first param
0x7e, // i64 0x7e, // i64
// no return params // no return params
@ -889,7 +889,7 @@ mod tests {
// func 2, form=1 // func 2, form=1
0x01, 0x01,
// param_count=1 // param_count=1
130u8, 0x80, 0x80, 0x80, 0x0, 2,
// first param // first param
0x7e, 0x7e,
// second param // second param
@ -925,9 +925,9 @@ mod tests {
// section id // section id
0x07, 0x07,
// section length // section length
148u8, 0x80, 0x80, 0x80, 0x0, 28,
// 6 entries // 6 entries
134u8, 0x80, 0x80, 0x80, 0x0, 6,
// func "A", index 6 // func "A", index 6
// [name_len(1-5 bytes), name_bytes(name_len, internal_kind(1byte), internal_index(1-5 bytes)]) // [name_len(1-5 bytes), name_bytes(name_len, internal_kind(1byte), internal_index(1-5 bytes)])
0x01, 0x41, 0x01, 0x86, 0x80, 0x00, 0x01, 0x41, 0x01, 0x86, 0x80, 0x00,
@ -1005,10 +1005,11 @@ mod tests {
fn data_payload() -> &'static [u8] { fn data_payload() -> &'static [u8] {
&[ &[
0x0bu8, // section id 0x0bu8, // section id
19, // 19 bytes overall 20, // 19 bytes overall
0x01, // number of segments 0x01, // number of segments
0x00, // index 0x00, // index
0x0b, // just `end` op 0x0b, // just `end` op
0x10,
// 16x 0x00 // 16x 0x00
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,