From f48b8ba98ba88200b29ca8c1bbf2fb40cf2809da Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Wed, 30 Dec 2015 17:14:48 -0500 Subject: [PATCH 1/2] Creating the tests for the Alchemy library. --- tests/lib.rs | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/lib.rs diff --git a/tests/lib.rs b/tests/lib.rs new file mode 100644 index 0000000..335eaf9 --- /dev/null +++ b/tests/lib.rs @@ -0,0 +1 @@ +extern crate alchemy; From cdd72603e515696c356665a5b193ae8ce9baea0a Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Sat, 2 Jan 2016 18:27:06 -0500 Subject: [PATCH 2/2] Create a buffer overflow test. This tests buffer overflow for the basic types. It does not currently test the platform int and uint until the final function definitions are determined. --- Cargo.toml | 3 ++ src/endian.rs | 4 +- tests/lib.rs | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) 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 fbeae5d..e562a0d 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -36,7 +36,7 @@ macro_rules! read_bytes ({ use std::$return_type; - assert!($return_type::BYTES <= $buffer.len()); + assert!($buffer.len() < $return_type::BYTES); unsafe { (*($buffer.as_ptr() as *const $return_type)).$convert_func() @@ -52,7 +52,7 @@ macro_rules! write_bytes ({ use std::$value_type; - assert!($value_type::BYTES <= $buffer.len()); + assert!($buffer.len() < $value_type::BYTES); unsafe { let size: usize; diff --git a/tests/lib.rs b/tests/lib.rs index 335eaf9..ae69ef7 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1 +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);