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.
This commit is contained in:
@ -5,3 +5,6 @@ authors = ["Jason Travis Smith <Jason@CyberMagesLLC.com>"]
|
||||
|
||||
[dependencies.sigils]
|
||||
git = "https://gitlab.com/CyberMages/sigils.git"
|
||||
|
||||
[dev-dependencies]
|
||||
rand = "*"
|
||||
|
@ -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;
|
||||
|
106
tests/lib.rs
106
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);
|
||||
|
Reference in New Issue
Block a user