Skip to content

Commit

Permalink
add tests for encrypt_string logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dronavallipranav committed Jan 6, 2024
1 parent 577cdaf commit bbc43b1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions labyrinth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit bbc43b1

Please sign in to comment.