Buckets:

download
raw
12.7 kB
"""
Comprehensive test suite for Resilience & Security Hardening.
Tests for critical resilience and security components including:
- AI Safety Tracer: PII redaction and safety monitoring
- Safety Configurations: Environment validation and guardrails
- Error Classification: Error handling and circuit breaking
- Adaptive Timeout: Dynamic timeout adjustment
- Boundary Contracts: Security gradient enforcement
"""
from unittest.mock import Mock, patch
import pytest
# Mock security and resilience components for testing
class MockSafetyTracer:
def __init__(self):
self.prompt_hash = None
self.safety_score = 0.0
class MockSafetyConfig:
def __init__(self, **kwargs):
self.blocked_env_vars = kwargs.get("blocked_env_vars", ["PASSWORD", "SECRET"])
self.required_env_vars = kwargs.get("required_env_vars", ["API_KEY"])
self.denylist = kwargs.get("denylist", ["delete_all", "drop_table"])
self.contribution_threshold = kwargs.get("contribution_threshold", 0.7)
def validate_environment(self, env: dict) -> dict:
errors = []
for key in self.blocked_env_vars:
if key in env:
errors.append("blocked")
break
for key in self.required_env_vars:
if key not in env:
errors.append("missing")
break
return {"valid": len(errors) == 0, "errors": errors}
def is_command_denied(self, command: str) -> bool:
return command in self.denylist
def validate_contribution(self, score: float) -> dict:
return {"accepted": score >= self.contribution_threshold, "threshold": self.contribution_threshold}
class MockBoundaryContract:
def __init__(self):
self.sensitivity_level = 0.7
self.allowed_operations = ["read", "write"]
self.restricted_data_types = ["pii", "secrets"]
class MockGuardrails:
def __init__(self):
pass
class MockErrorClassifier:
def __init__(self):
self.error_count = 0
def classify_error(self, error):
self.error_count += 1
class Severity:
level = "high" if "critical" in str(error).lower() else "low"
return Severity()
def should_trigger_circuit_breaker(self):
return self.error_count > 3
class MockAdaptiveTimeout:
def __init__(self):
pass
def calculate_timeout(self, load_metrics):
return 30.0 / (1 + load_metrics.get("cpu_usage", 0.5))
# Use mock instances for testing
def trace_safety_analysis(operation=None):
"""Mock decorator factory for safety analysis tracing."""
def decorator(func):
from functools import wraps
@wraps(func)
def wrapper(*args, **kwargs):
try:
from grid.tracing.ai_safety_tracer import get_trace_manager
manager = get_trace_manager()
manager.trace_action(operation)
except Exception:
pass
return func(*args, **kwargs)
return wrapper
return decorator
def _mock_validate_response_safety(response: str) -> float:
if "harmful" in response.lower() or "malicious" in response.lower():
return 0.3
return 0.9
validate_response_safety = Mock(side_effect=_mock_validate_response_safety)
trace_model_inference = MockSafetyTracer
SafetyConfig = MockSafetyConfig
boundary_contract = MockBoundaryContract
guardrails = MockGuardrails
error_classifier = MockErrorClassifier()
adaptive_timeout = MockAdaptiveTimeout()
HAS_SECURITY = True
@pytest.fixture
def mock_trace_manager():
"""Mock trace manager for safety analysis tests."""
manager = Mock()
manager.trace_action = Mock()
return manager
class TestSecurityConcepts:
"""Test security and resilience concepts with mocks."""
def test_prompt_hashing_concept(self):
"""Test prompt hashing for security."""
import hashlib
# Test with short prompt
short_prompt = "This is a short prompt"
short_hash = hashlib.sha256(short_prompt.encode("utf-8")).hexdigest()
# Test with long prompt (should be truncated)
long_prompt = "x" * 2000
long_hash = hashlib.sha256(long_prompt[:100].encode("utf-8")).hexdigest()
assert len(short_hash) == 64 # SHA256 hex length
assert len(long_hash) == 64 # Should be same length
assert long_hash == hashlib.sha256(("x" * 100).encode("utf-8")).hexdigest()
def test_response_safety_concept(self):
"""Test response safety validation concepts."""
def simple_safety_check(response: str) -> float:
# Simple heuristic-based safety check
if "harmful" in response.lower():
return 0.3
elif "malicious" in response.lower():
return 0.1
else:
return 1.0
safe_response = "This is a safe response"
unsafe_response = "This response contains harmful content"
dangerous_response = "This response contains malicious code"
safe_score = simple_safety_check(safe_response)
unsafe_score = simple_safety_check(unsafe_response)
dangerous_score = simple_safety_check(dangerous_response)
assert safe_score == 1.0
assert unsafe_score == 0.3
assert dangerous_score == 0.1
def test_error_classification_concept(self):
"""Test error severity classification concept."""
def classify_error(error):
if "critical" in str(error).lower() or "fatal" in str(error).lower():
return "high"
elif "timeout" in str(error).lower():
return "medium"
else:
return "low"
low_error = ValueError("Invalid parameter")
high_error = ConnectionError("Critical database connection error")
assert classify_error(low_error) == "low"
assert classify_error(high_error) == "high"
def test_circuit_breaker_concept(self):
"""Test circuit breaker triggering logic concept."""
error_count = 0
def should_trigger_breaker():
return error_count > 3
# Should not trigger initially
assert should_trigger_breaker() is False
# Trigger errors
error_count = 5
# Should trigger after threshold
assert should_trigger_breaker() is True
def test_timeout_adaptation_concept(self):
"""Test timeout adaptation based on system load concept."""
def calculate_timeout(load_metrics):
cpu_usage = load_metrics.get("cpu_usage", 0.5)
base_timeout = 30.0
return base_timeout / (1 + cpu_usage)
# Test low load
low_timeout = calculate_timeout({"cpu_usage": 0.3})
assert low_timeout > 20.0
# Test high load
high_timeout = calculate_timeout({"cpu_usage": 0.9})
assert high_timeout < 20.0
def test_security_guardrails_concept(self):
"""Test security guardrails concepts."""
def filter_input(input_str):
blocked_patterns = ["DROP TABLE", "DELETE FROM"]
for pattern in blocked_patterns:
if pattern in input_str.upper():
return f"[FILTERED] {input_str}"
return input_str
def sanitize_output(output_str):
import re
sanitized = re.sub(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", "[REDACTED]", output_str)
sanitized = re.sub(r"\b\d{3}-\d{2}-\d{4}\b", "[REDACTED]", sanitized)
return sanitized
# Test input filtering
malicious_input = "'; DROP TABLE users; --"
filtered_input = filter_input(malicious_input)
assert "[FILTERED]" in filtered_input
# Test output sanitization
pii_output = "User email: user@example.com, SSN: 123-45-6789"
sanitized_output = sanitize_output(pii_output)
assert "user@example.com" not in sanitized_output
assert "123-45-6789" not in sanitized_output
assert "[REDACTED]" in sanitized_output
def test_transaction_atomicity_concept(self):
"""Test transaction atomicity concept."""
# Mock transaction state
state = {"committed": False, "operations": []}
def mock_operation(op_type):
state["operations"].append(op_type)
return f"Performed {op_type}"
# Simulate transaction
operations = ["create", "update", "delete"]
[mock_operation(op) for op in operations]
# All operations should be recorded before commit
assert len(state["operations"]) == 3
assert not state["committed"]
def test_safety_analysis_decorator(self, mock_trace_manager):
"""Test safety analysis decorator."""
@trace_safety_analysis(operation="Prompt security scan")
def scan_prompt(prompt: str):
return {"is_safe": True, "risk_level": "low", "detected_patterns": []}
with patch("grid.tracing.ai_safety_tracer.get_trace_manager", return_value=mock_trace_manager):
result = scan_prompt("This is a test prompt")
assert result["is_safe"] is True
assert result["risk_level"] == "low"
mock_trace_manager.trace_action.assert_called_once()
def test_response_safety_validation(self):
"""Test response safety validation."""
safe_response = "This is a safe response"
unsafe_response = "This response contains potentially harmful content"
safe_score = validate_response_safety(safe_response)
unsafe_score = validate_response_safety(unsafe_response)
assert safe_score > unsafe_score
assert 0.0 <= safe_score <= 1.0
assert 0.0 <= unsafe_score <= 1.0
class TestSafetyConfig:
"""Test safety configuration management."""
@pytest.fixture
def safety_config(self):
"""Create safety configuration."""
return SafetyConfig(
blocked_env_vars=["PASSWORD", "SECRET"],
required_env_vars=["API_KEY"],
denylist=["delete_all", "drop_table"],
contribution_threshold=0.7,
)
def test_environment_validation(self, safety_config):
"""Test environment variable validation."""
# Test with blocked environment
blocked_env = {"PASSWORD": "secret123", "API_KEY": "valid_key"}
validation_result = safety_config.validate_environment(blocked_env)
assert validation_result["valid"] is False
assert "blocked" in validation_result["errors"]
# Test with missing required
missing_env = {"PUBLIC_VAR": "value"}
validation_result = safety_config.validate_environment(missing_env)
assert validation_result["valid"] is False
assert "missing" in validation_result["errors"]
def test_command_denylist(self, safety_config):
"""Test command denylist enforcement."""
# Test with denied command
is_denied = safety_config.is_command_denied("delete_all")
assert is_denied is True
# Test with allowed command
is_allowed = safety_config.is_command_denied("create_user")
assert is_allowed is False
def test_contribution_threshold(self, safety_config):
"""Test contribution score threshold."""
# Test high score (above threshold)
high_result = safety_config.validate_contribution(0.8)
assert high_result["accepted"] is True
# Test low score (below threshold)
low_result = safety_config.validate_contribution(0.5)
assert low_result["accepted"] is False
assert low_result["threshold"] == 0.7
class TestRepositoryIntegration:
"""Test integration between repository patterns and resilience."""
def test_transaction_atomicity(self):
"""Test that repository operations maintain atomicity."""
# Mock transaction state
state = {"committed": False, "operations": []}
def mock_operation(op_type):
state["operations"].append(op_type)
return f"Performed {op_type}"
# Simulate transaction
operations = ["create", "update", "delete"]
results = [mock_operation(op) for op in operations]
# All operations should be recorded before commit
assert len(state["operations"]) == 3
assert not state["committed"]
# After commit, state should be consistent
state["committed"] = True
assert state["operations"] == ["create", "update", "delete"]
assert results == ["Performed create", "Performed update", "Performed delete"]

Xet Storage Details

Size:
12.7 kB
·
Xet hash:
b4e17d2358cf4e4c44e88357a5ccdc7eb0bacb124ef75bceec05dbfed23fd155

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.