From bbc43b1bab965acbbf613a4753ff87c53e0b8d63 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli Date: Fri, 5 Jan 2024 19:36:28 -0600 Subject: [PATCH] add tests for encrypt_string logic --- labyrinth/src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/labyrinth/src/lib.rs b/labyrinth/src/lib.rs index 6105480..a12458e 100644 --- a/labyrinth/src/lib.rs +++ b/labyrinth/src/lib.rs @@ -30,3 +30,36 @@ fn xor_cipher(input: &str, key: &str) -> String { .map(|(input_char, key_char)| { ((input_char as u8) ^ (key_char as u8)) as char }) .collect() } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_xor_cipher_and_decrypt() { + let key = "xnasff3wcedj"; + let test_strings = ["Hello", "World", "1234", "!@#$%^&*()"]; + + for &original in &test_strings { + let encrypted = xor_cipher(original, &key); + let decrypted = labyrinth_helper::decrypt_string(&encrypted, &key); + assert_eq!(original, decrypted, "Failed for string: {}", original); + } + } + #[test] + fn test_xor_cipher_and_decrypt_customkey() { + //set key + std::env::set_var("LABYRINTH_KEY", "testkey"); + //test loc from encrypt_string meant to extract key + let key = env::var("LABYRINTH_KEY").unwrap_or_else(|_| "xnasff3wcedj".to_string()); + assert_eq!(key, "testkey"); + + let test_strings = ["Hello", "World", "1234", "!@#$%^&*()"]; + + for &original in &test_strings { + let encrypted = xor_cipher(original, &key); + let decrypted = labyrinth_helper::decrypt_string(&encrypted, &key); + assert_eq!(original, decrypted, "Failed for string: {}", original); + } + } +}