diff --git a/Cargo.toml b/Cargo.toml index 6905525..ffa054f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,6 @@ authors = ["Jason Travis Smith "] [dependencies.sigils] git = "https://gitlab.com/CyberMages/sigils.git" + +[dev-dependencies] +rand = "*" diff --git a/src/endian.rs b/src/endian.rs index 35f9267..a35eb6c 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -52,7 +52,7 @@ macro_rules! write_bytes ({ use std::$valueType; - assert!($valueType::BYTES <= $buffer.len()); + assert!($buffer.len() < $value_type::BYTES); unsafe { let size: usize; diff --git a/tests/lib.rs b/tests/lib.rs new file mode 100644 index 0000000..ae69ef7 --- /dev/null +++ b/tests/lib.rs @@ -0,0 +1,107 @@ +#![feature(zero_one)] +#![feature(num_bits_bytes)] + +extern crate rand; + +extern crate alchemy; +extern crate sigils; + + + +use rand::thread_rng; + + + +/// This should only be called by the macro below. +macro_rules! overflow_impl +{ + ($numBytes: expr, $T: ident, + $read_func: ident, $write_func: ident) => + { + use std::num::Zero; + + use alchemy::{BigEndian, LittleEndian, Transmutable}; + + + + #[test] + #[should_panic] + fn read_big_endian() + { + let buffer: [u8; $numBytes]; + + buffer = [0u8; $numBytes]; + BigEndian::$read_func(&buffer); + } + + #[test] + #[should_panic] + fn read_little_endian() + { + let buffer: [u8; $numBytes]; + + buffer = [0u8; $numBytes]; + LittleEndian::$read_func(&buffer); + } + + + #[test] + #[should_panic] + fn write_big_endian() + { + let mut buffer: [u8; $numBytes]; + + buffer = [0u8; $numBytes]; + BigEndian::$write_func(&mut buffer, $T::zero()); + } + + #[test] + #[should_panic] + fn write_little_endian() + { + let mut buffer: [u8; $numBytes]; + + buffer = [0u8; $numBytes]; + LittleEndian::$write_func(&mut buffer, $T::zero()); + } + }; +} + +/// This macro tries to test for buffer overflow happening. +macro_rules! test_buffer_overflow +{ + ($mod_name: ident, $T: ident, + $read_func: ident, $write_func: ident) => + { + mod $mod_name + { + use std::$T; + overflow_impl!($T::BYTES, $T, $read_func, $write_func); + } + }; + + ($mod_name: ident, $T: ident, + $read_func: ident, $write_func: ident, $numBytes: expr) => + { + mod $mod_name + { + overflow_impl!($numBytes, $T, $read_func, $write_func); + } + }; +} + + + +// Test the different data types for buffer overflow. +test_buffer_overflow!(overflow_u16, u16, bytes_to_u16, u16_to_bytes); +test_buffer_overflow!(overflow_u32, u32, bytes_to_u32, u32_to_bytes); +test_buffer_overflow!(overflow_u64, u64, bytes_to_u64, u64_to_bytes); +//test_buffer_overflow!(overflow_usize, usize, bytes_to_usize, usize_to_bytes); + +test_buffer_overflow!(overflow_i16, i16, bytes_to_i16, i16_to_bytes); +test_buffer_overflow!(overflow_i32, i32, bytes_to_i32, i32_to_bytes); +test_buffer_overflow!(overflow_i64, i64, bytes_to_i64, i64_to_bytes); +//test_buffer_overflow!(overflow_isize, isize, bytes_to_isize, isize_to_bytes); + +test_buffer_overflow!(overflow_f32, f32, bytes_to_f32, f32_to_bytes, 4); +test_buffer_overflow!(overflow_f64, f64, bytes_to_f64, f64_to_bytes, 8);