diff options
Diffstat (limited to 'tests/util.rs')
-rw-r--r-- | tests/util.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/util.rs b/tests/util.rs new file mode 100644 index 0000000..79076fe --- /dev/null +++ b/tests/util.rs @@ -0,0 +1,54 @@ +use std::io::prelude::*; +use std::fs::File; + +use rand::Rng; +use serialize::base64::FromBase64; +use serialize::hex::FromHex; + +pub fn read_as_hex_lines (filename: &str) -> Vec<Vec<u8>> { + let fh = File::open(filename).unwrap(); + return ::std::io::BufStream::new(fh) + .lines() + .map(|line| line.unwrap().from_hex().unwrap()) + .collect(); +} + +pub fn read_as_base64_lines (filename: &str) -> Vec<Vec<u8>> { + let fh = File::open(filename).unwrap(); + return ::std::io::BufStream::new(fh) + .lines() + .map(|line| line.unwrap().from_base64().unwrap()) + .collect(); +} + +pub fn read_as_lines (filename: &str) -> Vec<Vec<u8>> { + let fh = File::open(filename).unwrap(); + return ::std::io::BufStream::new(fh) + .lines() + .map(|line| line.unwrap().as_bytes().to_vec()) + .collect(); +} + +pub fn read_as_base64 (filename: &str) -> Vec<u8> { + let fh = File::open(filename).unwrap(); + return ::std::io::BufStream::new(fh) + .lines() + .map(|line| line.unwrap().from_base64().unwrap()) + .collect::<Vec<Vec<u8>>>() + .concat(); +} + +pub fn read (filename: &str) -> Vec<u8> { + let outfh = File::open(filename).unwrap(); + return outfh.bytes().map(|c| c.unwrap()).collect(); +} + +pub fn random_aes_128_key () -> [u8; 16] { + let mut key = [0; 16]; + ::rand::thread_rng().fill_bytes(&mut key); + return key; +} + +pub fn coinflip () -> bool { + ::rand::thread_rng().gen() +} |