diff --git a/set1/challenge1_hex2base64/src/main.rs b/set1/challenge1_hex2base64/src/main.rs index a6e182e..25f104b 100644 --- a/set1/challenge1_hex2base64/src/main.rs +++ b/set1/challenge1_hex2base64/src/main.rs @@ -5,12 +5,23 @@ use std::io::{BufRead, stdin}; fn main() { env_logger::init(); - let stdin = stdin(); - for line in stdin.lock().lines().filter_map(|x| x.ok()) { + for line in stdin().lock().lines().filter_map(|x| x.ok()) { match line.from_hex() { Ok(v) => println!("{}", v.to_base64(STANDARD)), Err(e) => error!("{}", e) } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn challenge1_hex2base64() { + let v = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d".from_hex().unwrap(); + let r = v.to_base64(STANDARD); + assert_eq!(r, "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"); + } +} diff --git a/set1/challenge2_fixed_xor/src/main.rs b/set1/challenge2_fixed_xor/src/main.rs index 6fcf1cf..8f79338 100644 --- a/set1/challenge2_fixed_xor/src/main.rs +++ b/set1/challenge2_fixed_xor/src/main.rs @@ -19,9 +19,8 @@ impl Iterator for Pairwise where T: Iterator { fn main() { env_logger::init(); - let stdin = stdin(); - for (line1, line2) in Pairwise(stdin.lock().lines().filter_map(|x| x.ok())) { + for (line1, line2) in Pairwise(stdin().lock().lines().filter_map(|x| x.ok())) { match (line1.from_hex(), line2.from_hex()) { (Ok(v1), Ok(v2)) => println!("{}", fixed_xor_cipher(&v1, &mut v2.into_iter()).to_hex()), (Err(e), _) => error!("{}", e), @@ -29,3 +28,16 @@ fn main() { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn challenge2_fixed_xor() { + let v1 = "1c0111001f010100061a024b53535009181c".from_hex().unwrap(); + let v2 = "686974207468652062756c6c277320657965".from_hex().unwrap(); + let r = fixed_xor_cipher(&v1, &mut v2.into_iter()).to_hex(); + assert_eq!(r, "746865206b696420646f6e277420706c6179"); + } +} diff --git a/set1/challenge3_xor_cipher/src/main.rs b/set1/challenge3_xor_cipher/src/main.rs index 854696a..5caff28 100644 --- a/set1/challenge3_xor_cipher/src/main.rs +++ b/set1/challenge3_xor_cipher/src/main.rs @@ -5,9 +5,8 @@ use std::io::{BufRead, stdin}; fn main() { env_logger::init(); - let stdin = stdin(); - for line in stdin.lock().lines().filter_map(|x| x.ok()) { + for line in stdin().lock().lines().filter_map(|x| x.ok()) { match line.from_hex() { Ok(ciphertext) => { let (decrypted, _) = break_xor_cipher(&ciphertext); @@ -20,3 +19,15 @@ fn main() { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn challenge3_xor_cipher() { + let ciphertext = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736".from_hex().unwrap(); + let (decrypted, _) = break_xor_cipher(&ciphertext); + assert_eq!(String::from_utf8(decrypted).unwrap(), "Cooking MC's like a pound of bacon"); + } +} diff --git a/set1/challenge4_detect_xor/src/main.rs b/set1/challenge4_detect_xor/src/main.rs index 1b33b7d..e931c4b 100644 --- a/set1/challenge4_detect_xor/src/main.rs +++ b/set1/challenge4_detect_xor/src/main.rs @@ -1,28 +1,44 @@ use common::break_xor_cipher; use log::error; use rustc_serialize::hex::FromHex; +use std::error::Error; use std::io::{BufRead, stdin}; -fn main() { - env_logger::init(); - let stdin = stdin(); +fn detect_xor(input: &mut dyn BufRead) -> Result> { let mut maxscore = 0; let mut decrypted: Vec = vec![]; - for line in stdin.lock().lines().filter_map(|x| x.ok()) { - match line.from_hex() { - Ok(ciphertext) => { - let (plaintext, score) = break_xor_cipher(&ciphertext); - if score > maxscore { - maxscore = score; - decrypted = plaintext; - } - } - Err(e) => error!("{}", e) + for line in input.lines() { + let ciphertext = line?.from_hex()?; + let (plaintext, score) = break_xor_cipher(&ciphertext); + if score > maxscore { + maxscore = score; + decrypted = plaintext; } } - match String::from_utf8(decrypted) { + Ok(String::from_utf8(decrypted)?) +} + +fn main() { + env_logger::init(); + + match detect_xor(&mut stdin().lock()) { Ok(s) => println!("{}", s), Err(e) => error!("{}", e) } } + +#[cfg(test)] +mod tests { + use std::fs::File; + use std::io::BufReader; + use std::path::Path; + use super::*; + + #[test] + fn challenge4_detect_xor() { + let cargo_manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); + let testinput = File::open(cargo_manifest_dir.join("4.txt")).unwrap(); + assert_eq!(detect_xor(&mut BufReader::new(testinput)).unwrap(), "Now that the party is jumping\n"); + } +}