add tests

This commit is contained in:
Thomas Lindner 2023-12-17 02:27:12 +01:00
parent 25b1621892
commit 3b0283df56
4 changed files with 70 additions and 20 deletions

View file

@ -5,12 +5,23 @@ use std::io::{BufRead, stdin};
fn main() { fn main() {
env_logger::init(); 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() { match line.from_hex() {
Ok(v) => println!("{}", v.to_base64(STANDARD)), Ok(v) => println!("{}", v.to_base64(STANDARD)),
Err(e) => error!("{}", e) 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");
}
}

View file

@ -19,9 +19,8 @@ impl<T> Iterator for Pairwise<T> where T: Iterator {
fn main() { fn main() {
env_logger::init(); 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()) { match (line1.from_hex(), line2.from_hex()) {
(Ok(v1), Ok(v2)) => println!("{}", fixed_xor_cipher(&v1, &mut v2.into_iter()).to_hex()), (Ok(v1), Ok(v2)) => println!("{}", fixed_xor_cipher(&v1, &mut v2.into_iter()).to_hex()),
(Err(e), _) => error!("{}", e), (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");
}
}

View file

@ -5,9 +5,8 @@ use std::io::{BufRead, stdin};
fn main() { fn main() {
env_logger::init(); 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() { match line.from_hex() {
Ok(ciphertext) => { Ok(ciphertext) => {
let (decrypted, _) = break_xor_cipher(&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");
}
}

View file

@ -1,28 +1,44 @@
use common::break_xor_cipher; use common::break_xor_cipher;
use log::error; use log::error;
use rustc_serialize::hex::FromHex; use rustc_serialize::hex::FromHex;
use std::error::Error;
use std::io::{BufRead, stdin}; use std::io::{BufRead, stdin};
fn main() { fn detect_xor(input: &mut dyn BufRead) -> Result<String, Box<dyn Error>> {
env_logger::init();
let stdin = stdin();
let mut maxscore = 0; let mut maxscore = 0;
let mut decrypted: Vec<u8> = vec![]; let mut decrypted: Vec<u8> = vec![];
for line in stdin.lock().lines().filter_map(|x| x.ok()) { for line in input.lines() {
match line.from_hex() { let ciphertext = line?.from_hex()?;
Ok(ciphertext) => {
let (plaintext, score) = break_xor_cipher(&ciphertext); let (plaintext, score) = break_xor_cipher(&ciphertext);
if score > maxscore { if score > maxscore {
maxscore = score; maxscore = score;
decrypted = plaintext; decrypted = plaintext;
} }
} }
Err(e) => error!("{}", e) Ok(String::from_utf8(decrypted)?)
} }
}
match String::from_utf8(decrypted) { fn main() {
env_logger::init();
match detect_xor(&mut stdin().lock()) {
Ok(s) => println!("{}", s), Ok(s) => println!("{}", s),
Err(e) => error!("{}", e) 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");
}
}