sal/vault/tests/rhai_integration_tests.rs
Mahmoud-Emad 6dead402a2
Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
feat: Remove herodo from monorepo and update dependencies
- Removed the `herodo` binary from the monorepo. This was
  done as part of the monorepo conversion process.
- Updated the `Cargo.toml` file to reflect the removal of
  `herodo` and adjust dependencies accordingly.
- Updated `src/lib.rs` and `src/rhai/mod.rs` to use the new
  `sal-vault` crate for vault functionality.  This improves
  the modularity and maintainability of the project.
2025-06-23 14:56:03 +03:00

228 lines
6.7 KiB
Rust

use rhai::{Engine, EvalAltResult};
use sal_vault::rhai::*;
#[cfg(test)]
mod rhai_integration_tests {
use super::*;
fn create_test_engine() -> Engine {
let mut engine = Engine::new();
register_crypto_module(&mut engine).expect("Failed to register crypto module");
engine
}
#[test]
fn test_rhai_module_registration() {
let engine = create_test_engine();
// Test that the functions are registered by checking if they exist
let script = r#"
// Test that all crypto functions are available
let functions_exist = true;
// We can't actually call these without proper setup, but we can verify they're registered
// by checking that the engine doesn't throw "function not found" errors
functions_exist
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_symmetric_encryption_functions() {
let engine = create_test_engine();
let script = r#"
// Test symmetric encryption functions
let key = generate_key();
let message = "Hello, World!";
let encrypted = encrypt(key, message);
let decrypted = decrypt(key, encrypted);
decrypted == message
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_keyspace_functions() {
let engine = create_test_engine();
let script = r#"
// Test keyspace functions
clear_session();
let created = create_key_space("test_space", "password123");
if !created {
throw "Failed to create key space";
}
let selected = select_keyspace("test_space");
if !selected {
throw "Failed to select keyspace";
}
true
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_keypair_functions() {
let engine = create_test_engine();
let script = r#"
// Test keypair functions
clear_session();
// Create and select keyspace
create_key_space("test_space", "password123");
select_keyspace("test_space");
// Create keypair
let created = create_keypair("test_keypair");
if !created {
throw "Failed to create keypair";
}
// Select keypair
let selected = select_keypair("test_keypair");
if !selected {
throw "Failed to select keypair";
}
// Get public key
let pub_key = keypair_pub_key();
if pub_key == "" {
throw "Failed to get public key";
}
true
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_signing_functions() {
let engine = create_test_engine();
let script = r#"
// Test signing and verification functions
clear_session();
// Setup keyspace and keypair
create_key_space("test_space", "password123");
select_keyspace("test_space");
create_keypair("test_keypair");
select_keypair("test_keypair");
// Test signing and verification
let message = "test message";
let signature = sign(message);
if signature == "" {
throw "Failed to sign message";
}
let is_valid = verify(message, signature);
if !is_valid {
throw "Signature verification failed";
}
// Test with wrong message
let wrong_is_valid = verify("wrong message", signature);
if wrong_is_valid {
throw "Signature should not be valid for wrong message";
}
true
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_session_management() {
let engine = create_test_engine();
let script = r#"
// Test session management
clear_session();
// Create multiple keyspaces
create_key_space("space1", "password1");
create_key_space("space2", "password2");
// Test listing keyspaces
let spaces = list_keyspaces();
if spaces.len() < 2 {
throw "Should have at least 2 keyspaces";
}
// Test selecting different keyspaces
select_keyspace("space1");
create_keypair("keypair1");
select_keyspace("space2");
create_keypair("keypair2");
// Test listing keypairs in current space
let keypairs = list_keypairs();
if keypairs.len() != 1 {
throw "Should have exactly 1 keypair in space2";
}
true
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
if let Err(ref e) = result {
println!("Script error: {}", e);
}
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_error_handling() {
let engine = create_test_engine();
let script = r#"
// Test error handling
clear_session();
// Try to select non-existent keyspace
let selected = select_keyspace("non_existent");
if selected {
throw "Should not be able to select non-existent keyspace";
}
// Try to create keypair without keyspace
let created = create_keypair("test_keypair");
if created {
throw "Should not be able to create keypair without keyspace";
}
true
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
}