Merge branch 'tests'

Used the tests endian asserts.

Conflicts:
	src/endian.rs
This commit is contained in:
Jason Travis Smith 2016-01-02 18:29:22 -05:00
commit 7617ddb16a
3 changed files with 111 additions and 1 deletions

View File

@ -5,3 +5,6 @@ authors = ["Jason Travis Smith <Jason@CyberMagesLLC.com>"]
[dependencies.sigils] [dependencies.sigils]
git = "https://gitlab.com/CyberMages/sigils.git" git = "https://gitlab.com/CyberMages/sigils.git"
[dev-dependencies]
rand = "*"

View File

@ -52,7 +52,7 @@ macro_rules! write_bytes
({ ({
use std::$valueType; use std::$valueType;
assert!($valueType::BYTES <= $buffer.len()); assert!($buffer.len() < $value_type::BYTES);
unsafe unsafe
{ {
let size: usize; let size: usize;

107
tests/lib.rs Normal file
View File

@ -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);