summaryrefslogtreecommitdiffstats
path: root/tests/util.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2015-04-06 02:11:07 -0400
committerJesse Luehrs <doy@tozt.net>2015-04-06 02:11:07 -0400
commit3d08e9986ed006cdf1c6f37969c9d11c53918105 (patch)
tree90baeb7e7fbbaefd36d1db2670705b986e07bcab /tests/util.rs
parentcf5f54073a9494fbd0c44c54f2b1a29b1b87e642 (diff)
downloadmatasano-3d08e9986ed006cdf1c6f37969c9d11c53918105.tar.gz
matasano-3d08e9986ed006cdf1c6f37969c9d11c53918105.zip
rearrange some test code
Diffstat (limited to 'tests/util.rs')
-rw-r--r--tests/util.rs54
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()
+}