#[cfg(test)]
mod tests {
use kasl::libs::secret::Secret;
use serial_test::serial;
use std::fs;
use tempfile::TempDir;
use test_context::{TestContext, test_context};
struct SecretTestContext {
_temp_dir: TempDir,
_test_password: String,
test_prompt: String,
secret_file_name: String,
}
impl TestContext for SecretTestContext {
fn setup() -> Self {
let temp_dir = tempfile::tempdir().unwrap();
unsafe {
std::env::set_var("HOME", temp_dir.path());
}
unsafe {
std::env::set_var("LOCALAPPDATA", temp_dir.path());
}
SecretTestContext {
_temp_dir: temp_dir,
_test_password: "test_password_123".to_string(),
test_prompt: "Enter test password".to_string(),
secret_file_name: ".test_secret".to_string(),
}
}
}
#[test_context(SecretTestContext)]
#[serial]
#[test]
fn test_secret_creation(ctx: &mut SecretTestContext) {
let _secret = Secret::new(&ctx.secret_file_name, &ctx.test_prompt);
assert!(!ctx.secret_file_name.is_empty());
}
#[test_context(SecretTestContext)]
#[serial]
#[test]
fn test_encrypt_decrypt_roundtrip(ctx: &mut SecretTestContext) {
let _secret = Secret::new(&ctx.secret_file_name, &ctx.test_prompt);
}
#[test_context(SecretTestContext)]
#[serial]
#[test]
fn test_secret_file_path_resolution(ctx: &mut SecretTestContext) {
let _secret1 = Secret::new(&ctx.secret_file_name, &ctx.test_prompt);
let _secret2 = Secret::new(&ctx.secret_file_name, &ctx.test_prompt);
}
#[test_context(SecretTestContext)]
#[serial]
#[test]
fn test_nonexistent_secret_file(ctx: &mut SecretTestContext) {
let _secret = Secret::new("nonexistent_secret", &ctx.test_prompt);
}
#[test_context(SecretTestContext)]
#[serial]
#[test]
fn test_corrupted_secret_file(ctx: &mut SecretTestContext) {
let _secret = Secret::new(&ctx.secret_file_name, &ctx.test_prompt);
let secret_path = kasl::libs::data_storage::DataStorage::new().get_path(&ctx.secret_file_name).unwrap();
fs::create_dir_all(secret_path.parent().unwrap()).unwrap();
fs::write(&secret_path, "invalid_base64_content!@#$").unwrap();
assert!(secret_path.exists());
}
#[test_context(SecretTestContext)]
#[serial]
#[test]
fn test_secret_file_permissions(ctx: &mut SecretTestContext) {
let _secret = Secret::new(&ctx.secret_file_name, &ctx.test_prompt);
let secret_path = kasl::libs::data_storage::DataStorage::new().get_path(&ctx.secret_file_name).unwrap();
if let Some(parent) = secret_path.parent() {
fs::create_dir_all(parent).unwrap();
}
fs::write(&secret_path, "dGVzdF9lbmNyeXB0ZWRfZGF0YQ==").unwrap();
assert!(secret_path.exists());
let content = fs::read_to_string(&secret_path).unwrap();
assert!(!content.is_empty());
}
#[test_context(SecretTestContext)]
#[serial]
#[test]
fn test_multiple_secret_instances(ctx: &mut SecretTestContext) {
let _secret1 = Secret::new(&ctx.secret_file_name, &ctx.test_prompt);
let _secret2 = Secret::new(&ctx.secret_file_name, "Different prompt");
}
#[test_context(SecretTestContext)]
#[serial]
#[test]
fn test_secret_with_special_characters(ctx: &mut SecretTestContext) {
let special_filename = ".secret_with_спец符号";
let _secret = Secret::new(special_filename, &ctx.test_prompt);
}
}