use dist_agent_lang::Runtime;
use dist_agent_lang::runtime::{ReentrancyGuard, SafeMath, StateIsolationManager};
use dist_agent_lang::runtime::values::Value;
#[test]
fn test_runtime_initialization() {
let runtime = Runtime::new();
assert!(true);
}
#[test]
fn test_reentrancy_protection() {
let guard = ReentrancyGuard::new();
let result1 = guard.enter("function1", Some("contract1"));
assert!(result1.is_ok());
let result2 = guard.enter("function1", Some("contract1"));
assert!(result2.is_err());
if let Ok(token) = result1 {
guard.exit(&token);
let result3 = guard.enter("function1", Some("contract1"));
assert!(result3.is_ok());
}
}
#[test]
fn test_safe_math_overflow() {
let max_int = Value::Int(i64::MAX);
let one = Value::Int(1);
let result = SafeMath::add(&max_int, &one);
assert!(result.is_err()); }
#[test]
fn test_safe_math_underflow() {
let min_int = Value::Int(i64::MIN);
let one = Value::Int(1);
let result = SafeMath::subtract(&min_int, &one);
assert!(result.is_err()); }
#[test]
fn test_state_isolation() {
let manager = StateIsolationManager::new();
manager.create_isolated_state("contract1", "0x1234");
assert!(manager.can_access("contract1", "0x1234"));
assert!(!manager.can_access("contract2", "0x1234"));
}
#[test]
fn test_runtime_security_features() {
let mut runtime = Runtime::new();
let guard_result = runtime.reentrancy_guard.enter("test_function", Some("test_contract"));
assert!(guard_result.is_ok(), "ReentrancyGuard should be initialized and functional");
if let Ok(token) = guard_result {
runtime.reentrancy_guard.exit(&token);
}
runtime.state_manager.create_isolated_state("test_contract", "0x1234");
assert!(
runtime.state_manager.can_access("test_contract", "0x1234"),
"StateIsolationManager should be initialized and functional"
);
let _manager = &runtime.cross_chain_manager;
assert!(true, "CrossChainSecurityManager should be initialized");
let _security = &runtime.advanced_security;
assert!(true, "AdvancedSecurityManager should be initialized");
}
#[test]
fn test_safe_math_multiplication_overflow() {
let large_int = Value::Int(i64::MAX / 2 + 1);
let two = Value::Int(2);
let result = SafeMath::multiply(&large_int, &two);
assert!(result.is_err()); }
#[test]
fn test_safe_math_division_by_zero() {
let value = Value::Int(100);
let zero = Value::Int(0);
let result = SafeMath::divide(&value, &zero);
assert!(result.is_err()); }
#[test]
fn test_reentrancy_different_functions() {
let guard = ReentrancyGuard::new();
let result1 = guard.enter("function1", Some("contract1"));
assert!(result1.is_ok());
let result2 = guard.enter("function2", Some("contract1"));
assert!(result2.is_err(), "Reentrancy guard should protect across all functions in same contract");
if let Ok(token) = result1 {
guard.exit(&token);
}
}
#[test]
fn test_reentrancy_different_contracts() {
let guard = ReentrancyGuard::new();
let result1 = guard.enter("function1", Some("contract1"));
assert!(result1.is_ok());
let result2 = guard.enter("function1", Some("contract2"));
assert!(result2.is_ok(), "Different contracts should be able to execute concurrently");
if let Ok(token1) = result1 {
guard.exit(&token1);
}
if let Ok(token2) = result2 {
guard.exit(&token2);
}
}
#[test]
fn test_state_isolation_multiple_contracts() {
let manager = StateIsolationManager::new();
manager.create_isolated_state("contract1", "0x1111");
manager.create_isolated_state("contract2", "0x2222");
assert!(manager.can_access("contract1", "0x1111"));
assert!(!manager.can_access("contract1", "0x2222"));
assert!(manager.can_access("contract2", "0x2222"));
assert!(!manager.can_access("contract2", "0x1111"));
}
#[test]
fn test_safe_math_normal_operations() {
let a = Value::Int(100);
let b = Value::Int(50);
let add_result = SafeMath::add(&a, &b);
assert!(add_result.is_ok());
if let Ok(Value::Int(sum)) = add_result {
assert_eq!(sum, 150);
}
let sub_result = SafeMath::subtract(&a, &b);
assert!(sub_result.is_ok());
if let Ok(Value::Int(diff)) = sub_result {
assert_eq!(diff, 50);
}
}