use dist_agent_lang::runtime::values::Value;
use dist_agent_lang::runtime::{ReentrancyGuard, SafeMath, StateIsolationManager};
use dist_agent_lang::Runtime;
#[test]
fn test_runtime_initialization() {
let _runtime = Runtime::new();
}
#[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());
drop(result1.unwrap());
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 mut manager = StateIsolationManager::new();
let result = manager.create_contract(
"0x1234".to_string(),
"Contract1".to_string(),
"0xOwner".to_string(),
"hybrid".to_string(),
);
assert!(result.is_ok());
assert!(manager.get_contract("0x1234").is_some());
assert!(manager.get_contract("0x5678").is_none());
}
#[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_ok(), "Different functions in same contract can execute concurrently (function-level reentrancy protection)");
let result3 = guard.enter("function1", Some("contract1"));
assert!(
result3.is_err(),
"Same function should be protected from reentrancy"
);
drop(result1.unwrap());
drop(result2.unwrap());
}
#[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"
);
drop(result1.unwrap());
drop(result2.unwrap());
}
#[test]
fn test_state_isolation_multiple_contracts() {
let mut manager = StateIsolationManager::new();
let result1 = manager.create_contract(
"0x1111".to_string(),
"Contract1".to_string(),
"0xOwner1".to_string(),
"hybrid".to_string(),
);
assert!(result1.is_ok());
let result2 = manager.create_contract(
"0x2222".to_string(),
"Contract2".to_string(),
"0xOwner2".to_string(),
"hybrid".to_string(),
);
assert!(result2.is_ok());
assert!(manager.get_contract("0x1111").is_some());
assert!(manager.get_contract("0x2222").is_some());
assert!(manager.get_contract("0x3333").is_none());
}
#[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);
}
let mul_result = SafeMath::multiply(&a, &b);
assert!(mul_result.is_ok());
if let Ok(Value::Int(product)) = mul_result {
assert_eq!(product, 5000);
}
let div_result = SafeMath::divide(&a, &b);
assert!(div_result.is_ok());
if let Ok(Value::Int(quotient)) = div_result {
assert_eq!(quotient, 2);
}
}
#[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"
);
drop(guard_result.unwrap());
let result = runtime.state_manager.create_contract(
"0x1234".to_string(),
"test_contract".to_string(),
"0xOwner".to_string(),
"hybrid".to_string(),
);
assert!(
result.is_ok(),
"StateIsolationManager should be initialized and functional"
);
assert!(
runtime.state_manager.get_contract("0x1234").is_some(),
"StateIsolationManager should create contracts"
);
let _manager = &runtime.cross_chain_manager;
let _security = &runtime.advanced_security;
}