From 87ec67f3a3ed76e1b186e324ac3cd3d226cf320a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 15 Mar 2015 17:31:43 -0400 Subject: clean up tests a bit --- tests/lib.rs | 101 +++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 38 deletions(-) (limited to 'tests') diff --git a/tests/lib.rs b/tests/lib.rs index 1761d77..a3e797e 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -7,11 +7,37 @@ use std::fs::File; use serialize::base64::FromBase64; use serialize::hex::FromHex; +fn read_as_hex_lines (filename: &str) -> Vec> { + let fh = File::open(filename).unwrap(); + return std::io::BufStream::new(fh) + .lines() + .map(|line| line.unwrap().from_hex().unwrap()) + .collect(); +} + +fn read_as_base64 (filename: &str) -> Vec { + let fh = File::open(filename).unwrap(); + return std::io::BufStream::new(fh) + .lines() + .map(|line| line.unwrap().from_base64().unwrap()) + .collect::>>() + .concat(); +} + +fn read (filename: &str) -> Vec { + let outfh = File::open(filename).unwrap(); + return outfh.bytes().map(|c| c.unwrap()).collect(); +} + #[test] fn problem_1 () { - let hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"; - let base64 = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"; - assert_eq!(matasano::to_base64(&hex.from_hex().unwrap()[..]), base64); + let hex = "49276d206b696c6c696e6720796f757220627261\ + 696e206c696b65206120706f69736f6e6f757320\ + 6d757368726f6f6d".from_hex().unwrap(); + let base64 = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEg\ + cG9pc29ub3VzIG11c2hyb29t"; + let got = matasano::to_base64(&hex[..]); + assert_eq!(got, base64); } #[test] @@ -19,70 +45,69 @@ fn problem_2 () { let bytes1 = "1c0111001f010100061a024b53535009181c".from_hex().unwrap(); let bytes2 = "686974207468652062756c6c277320657965".from_hex().unwrap(); let expected = "746865206b696420646f6e277420706c6179".from_hex().unwrap(); - assert_eq!(matasano::fixed_xor(&bytes1[..], &bytes2[..]), expected); + let got = matasano::fixed_xor(&bytes1[..], &bytes2[..]); + assert_eq!(got, expected); } #[test] fn problem_3 () { - let encrypted = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736".from_hex().unwrap(); + let ciphertext = "1b37373331363f78151b7f2b783431333d783978\ + 28372d363c78373e783a393b3736".from_hex().unwrap(); let plaintext = b"Cooking MC's like a pound of bacon"; - assert_eq!(matasano::crack_single_byte_xor(&encrypted[..]), plaintext); + let got = matasano::crack_single_byte_xor(&ciphertext[..]); + assert_eq!(got, plaintext); } #[test] fn problem_4 () { - let fh = File::open("data/4.txt").unwrap(); - let possibles = std::io::BufStream::new(fh) - .lines() - .map(|line| line.unwrap().from_hex().unwrap()) - .collect::>>(); - assert_eq!(matasano::find_single_byte_xor_encrypted_string(&possibles[..]), b"Now that the party is jumping\n"); + let possibles = read_as_hex_lines("data/4.txt"); + let plaintext = b"Now that the party is jumping\n"; + let got = matasano::find_single_byte_xor_encrypted_string(&possibles[..]); + assert_eq!(got, plaintext); } #[test] fn problem_5 () { - let plaintext = b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"; + let plaintext = b"Burning 'em, if you ain't quick and nimble\n\ + I go crazy when I hear a cymbal"; let key = b"ICE"; - let ciphertext = "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f".from_hex().unwrap(); - assert_eq!(matasano::repeating_key_xor(plaintext, key), ciphertext); + let ciphertext = "0b3637272a2b2e63622c2e69692a23693a2a3c63\ + 24202d623d63343c2a26226324272765272a282b\ + 2f20430a652e2c652a3124333a653e2b2027630c\ + 692b20283165286326302e27282f".from_hex().unwrap(); + let got = matasano::repeating_key_xor(plaintext, key); + assert_eq!(got, ciphertext); } #[test] fn problem_6 () { - let fh = File::open("data/6.txt").unwrap(); - let ciphertext = std::io::BufStream::new(fh) - .lines() - .map(|line| line.unwrap().from_base64().unwrap()) - .collect::>>() - .concat(); - let outfh = File::open("data/6.out.txt").unwrap(); - let plaintext = outfh.bytes().map(|c| c.unwrap()).collect(); + let ciphertext = read_as_base64("data/6.txt"); + let plaintext = read("data/6.out.txt"); let got = matasano::crack_repeating_key_xor(&ciphertext[..]); assert_eq!(got, plaintext); } #[test] fn problem_7 () { - let fh = File::open("data/7.txt").unwrap(); - let ciphertext = std::io::BufStream::new(fh) - .lines() - .map(|line| line.unwrap().from_base64().unwrap()) - .collect::>>() - .concat(); + let ciphertext = read_as_base64("data/7.txt"); let key = b"YELLOW SUBMARINE"; + let plaintext = read("data/7.out.txt"); let got = matasano::decrypt_aes_128_ecb(&ciphertext[..], key); - let outfh = File::open("data/7.out.txt").unwrap(); - let plaintext = outfh.bytes().map(|c| c.unwrap()).collect(); assert_eq!(got, plaintext); } #[test] fn problem_8 () { - let fh = File::open("data/8.txt").unwrap(); - let possibles = std::io::BufStream::new(fh) - .lines() - .map(|line| line.unwrap().from_hex().unwrap()) - .collect::>>(); - let ecb_encrypted = "d880619740a8a19b7840a8a31c810a3d08649af70dc06f4fd5d2d69c744cd283e2dd052f6b641dbf9d11b0348542bb5708649af70dc06f4fd5d2d69c744cd2839475c9dfdbc1d46597949d9c7e82bf5a08649af70dc06f4fd5d2d69c744cd28397a93eab8d6aecd566489154789a6b0308649af70dc06f4fd5d2d69c744cd283d403180c98c8f6db1f2a3f9c4040deb0ab51b29933f2c123c58386b06fba186a".from_hex().unwrap(); - assert_eq!(matasano::find_aes_128_ecb_encrypted_string(&possibles[..]), ecb_encrypted); + let possibles = read_as_hex_lines("data/8.txt"); + let ciphertext = "d880619740a8a19b7840a8a31c810a3d08649af7\ + 0dc06f4fd5d2d69c744cd283e2dd052f6b641dbf\ + 9d11b0348542bb5708649af70dc06f4fd5d2d69c\ + 744cd2839475c9dfdbc1d46597949d9c7e82bf5a\ + 08649af70dc06f4fd5d2d69c744cd28397a93eab\ + 8d6aecd566489154789a6b0308649af70dc06f4f\ + d5d2d69c744cd283d403180c98c8f6db1f2a3f9c\ + 4040deb0ab51b29933f2c123c58386b06fba186a" + .from_hex().unwrap(); + let got = matasano::find_aes_128_ecb_encrypted_string(&possibles[..]); + assert_eq!(got, ciphertext); } -- cgit v1.2.3-54-g00ecf