#[cfg(test)]
mod tests {
use dusa_collection_utils::{
errors::{ErrorArray, ErrorArrayItem, UnifiedResult as uf},
stringy::Stringy,
};
use crate::encryption::ProgramMode;
fn mock_run(
mode: ProgramMode,
_path: Option<String>,
_owner: Option<String>,
_name: Option<String>,
data: Option<String>,
) -> uf<Option<String>> {
match mode {
ProgramMode::EncryptText => {
let encrypted = format!("encrypted({})", data.unwrap_or_default());
uf::new(Ok(Some(encrypted)))
}
ProgramMode::DecryptText => {
let decrypted = data
.unwrap_or_default()
.replace("encrypted(", "")
.replace(")", "");
uf::new(Ok(Some(decrypted)))
}
_ => uf::new(Err(ErrorArray::new_container())),
}
}
fn encrypt_text_with_mock(data: Stringy) -> Result<Stringy, ErrorArrayItem> {
match mock_run(
ProgramMode::EncryptText,
None,
None,
None,
Some(data.to_string()),
)
.uf_unwrap()
{
Ok(d) => match d {
Some(d) => Ok(Stringy::new(&d)),
None => Err(ErrorArrayItem::new(
dusa_collection_utils::errors::Errors::GeneralError,
String::from("No data received from mock"),
)),
},
Err(mut e) => Err(e.pop()),
}
}
fn decrypt_text_with_mock(data: Stringy) -> Result<Stringy, ErrorArrayItem> {
match mock_run(
ProgramMode::DecryptText,
None,
None,
None,
Some(data.to_string()),
)
.uf_unwrap()
{
Ok(d) => match d {
Some(d) => Ok(Stringy::new(&d)),
None => Err(ErrorArrayItem::new(
dusa_collection_utils::errors::Errors::GeneralError,
String::from("No data received from mock"),
)),
},
Err(mut e) => Err(e.pop()),
}
}
#[test]
fn test_encrypt_text_success() {
let input = Stringy::new("Hello, World!");
let result = encrypt_text_with_mock(input.clone());
match result {
Ok(encrypted) => {
assert!(!encrypted.to_string().is_empty());
assert_ne!(encrypted.to_string(), input.to_string());
assert_eq!(encrypted.to_string(), "encrypted(Hello, World!)");
}
Err(e) => panic!("encrypt_text_with_mock returned an error: {:?}", e),
}
}
#[test]
fn test_encrypt_text_failure() {
let input = Stringy::new("");
let result = encrypt_text_with_mock(input);
match result {
Ok(encrypted) => {
assert_eq!(encrypted.to_string(), "encrypted()");
}
Err(e) => panic!("encrypt_text_with_mock returned an error: {:?}", e),
}
}
#[test]
fn test_decrypt_text_success() {
let encrypted_input = Stringy::new("encrypted(Hello, World!)");
let result = decrypt_text_with_mock(encrypted_input);
match result {
Ok(decrypted) => {
assert_eq!(decrypted.to_string(), "Hello, World!");
}
Err(e) => panic!("decrypt_text_with_mock returned an error: {:?}", e),
}
}
#[test]
fn test_decrypt_text_failure() {
let invalid_encrypted_input = Stringy::new("invalid_encrypted_data");
let result = decrypt_text_with_mock(invalid_encrypted_input);
match result {
Ok(decrypted) => {
assert_eq!(decrypted.to_string(), "invalid_encrypted_data");
}
Err(e) => panic!("decrypt_text_with_mock returned an error: {:?}", e),
}
}
#[test]
fn test_encrypt_then_decrypt() {
let input = Stringy::new("Test Message");
let encrypted_result = encrypt_text_with_mock(input.clone());
match encrypted_result {
Ok(encrypted) => {
let decrypted_result = decrypt_text_with_mock(encrypted);
match decrypted_result {
Ok(decrypted) => {
assert_eq!(decrypted.to_string(), input.to_string());
}
Err(e) => panic!("decrypt_text_with_mock returned an error: {:?}", e),
}
}
Err(e) => panic!("encrypt_text_with_mock returned an error: {:?}", e),
}
}
}