Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /test_audit_logger.py
| """Comprehensive unit tests for core/audit_logger.py. | |
| This module provides security audit logging functionality for agent decisions. | |
| Targets 90%+ coverage for audit_logger.py. | |
| """ | |
| import json | |
| import logging | |
| import os | |
| import sys | |
| from datetime import datetime | |
| from io import StringIO | |
| from unittest.mock import MagicMock, Mock, mock_open, patch | |
| import pytest | |
| # Ensure imports work | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from core.audit_logger import AuditLogger | |
| # ============================================================================ | |
| # Fixtures | |
| # ============================================================================ | |
| def reset_logging_state(): | |
| """Reset logging state before each test to avoid handler duplication.""" | |
| # Remove all handlers from the 'audit' logger before each test | |
| logger = logging.getLogger("audit") | |
| for handler in logger.handlers[:]: | |
| logger.removeHandler(handler) | |
| yield | |
| # Cleanup after test | |
| for handler in logger.handlers[:]: | |
| logger.removeHandler(handler) | |
| def temp_log_file(tmp_path): | |
| """Create a temporary log file path.""" | |
| return str(tmp_path / "test_audit.log") | |
| def mock_file_handler(): | |
| """Create a mock FileHandler for testing.""" | |
| handler = MagicMock() | |
| handler.setFormatter = MagicMock() | |
| handler.setLevel = MagicMock() | |
| return handler | |
| def sample_context(): | |
| """Create a sample context dictionary for testing.""" | |
| return { | |
| "scan_id": "scan_123", | |
| "target": "example.com", | |
| "severity": "high", | |
| "details": {"port": 443, "service": "https"}, | |
| } | |
| def sample_result(): | |
| """Create a sample result dictionary for testing.""" | |
| return { | |
| "status": "completed", | |
| "findings": [ | |
| {"id": 1, "type": "vulnerability"}, | |
| {"id": 2, "type": "misconfiguration"}, | |
| ], | |
| "duration": 45.5, | |
| } | |
| # ============================================================================ | |
| # Test AuditLogger Initialization | |
| # ============================================================================ | |
| class TestAuditLoggerInitialization: | |
| """Test AuditLogger initialization.""" | |
| def test_init_default_log_file(self): | |
| """Test initialization with default log file path.""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger() | |
| assert logger.log_file == "./logs/audit.log" | |
| def test_init_custom_log_file(self, temp_log_file): | |
| """Test initialization with custom log file path.""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| assert logger.log_file == temp_log_file | |
| def test_init_logger_instance(self, temp_log_file): | |
| """Test that logger is properly initialized.""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| assert isinstance(logger.logger, logging.Logger) | |
| assert logger.logger.name == "audit" | |
| def test_setup_logger_called(self, temp_log_file): | |
| """Test that _setup_logger is called during initialization.""" | |
| with patch.object(AuditLogger, "_setup_logger") as mock_setup: | |
| logger = AuditLogger(log_file=temp_log_file) | |
| mock_setup.assert_called_once() | |
| # ============================================================================ | |
| # Test Logger Setup | |
| # ============================================================================ | |
| class TestLoggerSetup: | |
| """Test _setup_logger method.""" | |
| def test_setup_logger_file_handler_created(self, temp_log_file): | |
| """Test that FileHandler is created with correct log file.""" | |
| with patch("logging.FileHandler") as mock_handler_class: | |
| mock_handler = MagicMock() | |
| mock_handler_class.return_value = mock_handler | |
| AuditLogger(log_file=temp_log_file) | |
| mock_handler_class.assert_called_once_with(temp_log_file) | |
| def test_setup_logger_formatter(self, temp_log_file): | |
| """Test that formatter is properly configured.""" | |
| with patch("logging.FileHandler") as mock_handler_class: | |
| mock_handler = MagicMock() | |
| mock_handler_class.return_value = mock_handler | |
| AuditLogger(log_file=temp_log_file) | |
| # Check that setFormatter was called | |
| assert mock_handler.setFormatter.called | |
| # Get the formatter that was passed | |
| formatter_call = mock_handler.setFormatter.call_args | |
| formatter = formatter_call[0][0] | |
| assert isinstance(formatter, logging.Formatter) | |
| assert "%(asctime)s" in formatter._fmt | |
| assert "%(name)s" in formatter._fmt | |
| assert "%(levelname)s" in formatter._fmt | |
| assert "%(message)s" in formatter._fmt | |
| def test_setup_logger_level(self, temp_log_file): | |
| """Test that logger level is set to INFO.""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| assert logger.logger.level == logging.INFO | |
| def test_setup_logger_handler_added(self, temp_log_file): | |
| """Test that handler is added to logger.""" | |
| with patch("logging.FileHandler") as mock_handler_class: | |
| mock_handler = MagicMock() | |
| mock_handler_class.return_value = mock_handler | |
| logger = AuditLogger(log_file=temp_log_file) | |
| assert mock_handler in logger.logger.handlers | |
| # ============================================================================ | |
| # Test Log Agent Decision | |
| # ============================================================================ | |
| class TestLogAgentDecision: | |
| """Test log_agent_decision method.""" | |
| def test_log_agent_decision_structure(self, temp_log_file, sample_context): | |
| """Test that log entry has correct structure.""" | |
| mock_logger = MagicMock() | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("agent_1", "start_scan", sample_context) | |
| # Check that info was called | |
| assert mock_logger.info.called | |
| # Get the logged message | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["type"] == "agent_decision" | |
| assert log_entry["agent_id"] == "agent_1" | |
| assert log_entry["decision"] == "start_scan" | |
| assert log_entry["context"] == sample_context | |
| assert "timestamp" in log_entry | |
| def test_log_agent_decision_timestamp(self, temp_log_file): | |
| """Test that timestamp is included and is valid ISO format.""" | |
| mock_logger = MagicMock() | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| before = datetime.now() | |
| logger.log_agent_decision("agent_1", "test", {}) | |
| after = datetime.now() | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| # Parse timestamp and verify it's between before and after | |
| timestamp = datetime.fromisoformat(log_entry["timestamp"]) | |
| assert before <= timestamp <= after | |
| def test_log_agent_decision_empty_context(self, temp_log_file): | |
| """Test logging with empty context.""" | |
| mock_logger = MagicMock() | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("agent_1", "test", {}) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["context"] == {} | |
| def test_log_agent_decision_nested_context(self, temp_log_file): | |
| """Test logging with nested context data.""" | |
| mock_logger = MagicMock() | |
| nested_context = { | |
| "level1": {"level2": {"level3": "deep_value"}}, | |
| "list": [1, 2, 3], | |
| "null": None, | |
| "bool": True, | |
| } | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("agent_1", "test", nested_context) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["context"] == nested_context | |
| def test_log_agent_decision_unicode(self, temp_log_file): | |
| """Test logging with unicode characters.""" | |
| mock_logger = MagicMock() | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("агент_1", "тест", {"ключ": "значение"}) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["agent_id"] == "агент_1" | |
| assert log_entry["decision"] == "тест" | |
| assert log_entry["context"]["ключ"] == "значение" | |
| def test_log_agent_decision_special_characters(self, temp_log_file): | |
| """Test logging with special characters.""" | |
| mock_logger = MagicMock() | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision( | |
| 'agent_1"quoted"', | |
| "decision\nwith\ttabs", | |
| {"key": 'value\r\n"escaped"'}, | |
| ) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["agent_id"] == 'agent_1"quoted"' | |
| assert log_entry["decision"] == "decision\nwith\ttabs" | |
| assert log_entry["context"]["key"] == 'value\r\n"escaped"' | |
| # ============================================================================ | |
| # Test Log Tool Execution | |
| # ============================================================================ | |
| class TestLogToolExecution: | |
| """Test log_tool_execution method.""" | |
| def test_log_tool_execution_structure(self, temp_log_file, sample_result): | |
| """Test that log entry has correct structure.""" | |
| mock_logger = MagicMock() | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_tool_execution("nmap", "example.com", sample_result) | |
| # Check that info was called | |
| assert mock_logger.info.called | |
| # Get the logged message | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["type"] == "tool_execution" | |
| assert log_entry["tool"] == "nmap" | |
| assert log_entry["target"] == "example.com" | |
| assert log_entry["status"] == "completed" | |
| assert log_entry["findings_count"] == 2 | |
| assert "timestamp" in log_entry | |
| def test_log_tool_execution_no_findings(self, temp_log_file): | |
| """Test logging tool execution with no findings.""" | |
| mock_logger = MagicMock() | |
| result = {"status": "completed", "findings": []} | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_tool_execution("nmap", "example.com", result) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["findings_count"] == 0 | |
| def test_log_tool_execution_missing_status(self, temp_log_file): | |
| """Test logging tool execution with missing status.""" | |
| mock_logger = MagicMock() | |
| result = {"findings": []} # No status | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_tool_execution("nmap", "example.com", result) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["status"] is None | |
| def test_log_tool_execution_missing_findings(self, temp_log_file): | |
| """Test logging tool execution with missing findings key.""" | |
| mock_logger = MagicMock() | |
| result = {"status": "completed"} # No findings key | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_tool_execution("nmap", "example.com", result) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["findings_count"] == 0 | |
| def test_log_tool_execution_many_findings(self, temp_log_file): | |
| """Test logging tool execution with many findings.""" | |
| mock_logger = MagicMock() | |
| result = { | |
| "status": "completed", | |
| "findings": [{"id": i} for i in range(100)], | |
| } | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_tool_execution("nmap", "example.com", result) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["findings_count"] == 100 | |
| # ============================================================================ | |
| # Test Get Audit Trail | |
| # ============================================================================ | |
| class TestGetAuditTrail: | |
| """Test get_audit_trail method.""" | |
| def test_get_audit_trail_found(self, temp_log_file): | |
| """Test retrieving audit trail for existing scan.""" | |
| log_content = """2024-01-01 10:00:00 - audit - INFO - {"scan_id": "scan_123", "type": "agent_decision"} | |
| 2024-01-01 10:00:01 - audit - INFO - {"scan_id": "scan_456", "type": "agent_decision"} | |
| 2024-01-01 10:00:02 - audit - INFO - {"scan_id": "scan_123", "type": "tool_execution"}""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| with patch("builtins.open", mock_open(read_data=log_content)): | |
| entries = logger.get_audit_trail("scan_123") | |
| assert len(entries) == 2 | |
| assert all("scan_123" in entry for entry in entries) | |
| def test_get_audit_trail_not_found(self, temp_log_file): | |
| """Test retrieving audit trail for non-existent scan.""" | |
| log_content = """2024-01-01 10:00:00 - audit - INFO - {"scan_id": "scan_123"} | |
| 2024-01-01 10:00:01 - audit - INFO - {"scan_id": "scan_456"}""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| with patch("builtins.open", mock_open(read_data=log_content)): | |
| entries = logger.get_audit_trail("scan_999") | |
| assert len(entries) == 0 | |
| def test_get_audit_trail_empty_file(self, temp_log_file): | |
| """Test retrieving audit trail from empty file.""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| with patch("builtins.open", mock_open(read_data="")): | |
| entries = logger.get_audit_trail("scan_123") | |
| assert len(entries) == 0 | |
| def test_get_audit_trail_file_not_found(self, temp_log_file): | |
| """Test retrieving audit trail when file doesn't exist.""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| with patch("builtins.open", side_effect=FileNotFoundError()): | |
| entries = logger.get_audit_trail("scan_123") | |
| assert len(entries) == 0 | |
| def test_get_audit_trail_strips_newlines(self, temp_log_file): | |
| """Test that entries have newlines stripped.""" | |
| log_content = "2024-01-01 10:00:00 - audit - INFO - scan_123 entry\n" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| with patch("builtins.open", mock_open(read_data=log_content)): | |
| entries = logger.get_audit_trail("scan_123") | |
| assert len(entries) == 1 | |
| assert not entries[0].endswith("\n") | |
| def test_get_audit_trail_partial_match(self, temp_log_file): | |
| """Test that partial matches are included.""" | |
| log_content = """2024-01-01 10:00:00 - audit - INFO - {"id": "scan_123_456"} | |
| 2024-01-01 10:00:01 - audit - INFO - {"id": "scan_123"} | |
| 2024-01-01 10:00:02 - audit - INFO - {"id": "other_scan_123"}""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| with patch("builtins.open", mock_open(read_data=log_content)): | |
| entries = logger.get_audit_trail("scan_123") | |
| # All three lines contain "scan_123" as substring | |
| assert len(entries) == 3 | |
| # ============================================================================ | |
| # Test File Output (Real File Operations) | |
| # ============================================================================ | |
| class TestFileOutput: | |
| """Test actual file output operations.""" | |
| def test_real_file_creation(self, tmp_path): | |
| """Test that log file is actually created.""" | |
| log_file = tmp_path / "real_test.log" | |
| logger = AuditLogger(log_file=str(log_file)) | |
| logger.log_agent_decision("agent_1", "test", {"key": "value"}) | |
| assert log_file.exists() | |
| def test_real_file_content(self, tmp_path): | |
| """Test that logged content is written to file.""" | |
| log_file = tmp_path / "real_test.log" | |
| logger = AuditLogger(log_file=str(log_file)) | |
| logger.log_agent_decision("agent_1", "test_decision", {"test": "context"}) | |
| content = log_file.read_text() | |
| assert "agent_1" in content | |
| assert "test_decision" in content | |
| assert "agent_decision" in content | |
| def test_multiple_entries_in_file(self, tmp_path): | |
| """Test that multiple entries are written to file.""" | |
| log_file = tmp_path / "real_test.log" | |
| logger = AuditLogger(log_file=str(log_file)) | |
| logger.log_agent_decision("agent_1", "decision_1", {}) | |
| logger.log_agent_decision("agent_2", "decision_2", {}) | |
| logger.log_tool_execution("nmap", "target", {"status": "done", "findings": []}) | |
| content = log_file.read_text() | |
| lines = content.strip().split("\n") | |
| assert len(lines) == 3 | |
| def test_get_audit_trail_from_real_file(self, tmp_path): | |
| """Test retrieving audit trail from actual log file.""" | |
| log_file = tmp_path / "real_test.log" | |
| logger = AuditLogger(log_file=str(log_file)) | |
| logger.log_agent_decision("agent_1", "start", {"scan_id": "scan_abc"}) | |
| logger.log_tool_execution("nmap", "target", {"status": "done", "findings": [1, 2]}) | |
| logger.log_agent_decision("agent_1", "end", {"scan_id": "scan_abc"}) | |
| entries = logger.get_audit_trail("scan_abc") | |
| assert len(entries) == 2 # Two entries with scan_abc | |
| def test_log_directory_creation(self, tmp_path): | |
| """Test that log directory is created if needed.""" | |
| log_dir = tmp_path / "logs" / "subdir" | |
| log_file = log_dir / "test.log" | |
| # Ensure directory exists before creating logger (FileHandler requirement) | |
| log_dir.mkdir(parents=True) | |
| logger = AuditLogger(log_file=str(log_file)) | |
| logger.log_agent_decision("agent_1", "test", {}) | |
| assert log_file.exists() | |
| # ============================================================================ | |
| # Test Error Handling | |
| # ============================================================================ | |
| class TestErrorHandling: | |
| """Test error handling scenarios.""" | |
| def test_json_encoding_error(self, temp_log_file): | |
| """Test handling of non-JSON-serializable objects.""" | |
| mock_logger = MagicMock() | |
| # Context with non-serializable object | |
| context = {"datetime": datetime.now()} # datetime is not JSON serializable by default | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| # This should raise a TypeError due to JSON serialization failure | |
| with pytest.raises(TypeError): | |
| logger.log_agent_decision("agent_1", "test", context) | |
| def test_file_handler_exception(self, temp_log_file): | |
| """Test handling when FileHandler fails.""" | |
| with patch("logging.FileHandler", side_effect=IOError("Cannot create file")): | |
| with pytest.raises(IOError): | |
| AuditLogger(log_file=temp_log_file) | |
| def test_permission_error_on_write(self, temp_log_file): | |
| """Test handling permission error during write.""" | |
| mock_handler = MagicMock() | |
| mock_handler.emit = MagicMock(side_effect=PermissionError("Permission denied")) | |
| mock_handler.level = logging.INFO # Set proper level attribute | |
| mock_handler.filters = [] # Empty filters list | |
| with patch("logging.FileHandler", return_value=mock_handler): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| # This should not raise but the error might be handled by logging framework | |
| logger.log_agent_decision("agent_1", "test", {}) | |
| def test_get_audit_trail_permission_error(self, temp_log_file): | |
| """Test handling permission error when reading log file.""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| with patch("builtins.open", side_effect=PermissionError("Permission denied")): | |
| with pytest.raises(PermissionError): | |
| logger.get_audit_trail("scan_123") | |
| def test_get_audit_trail_is_a_directory(self, temp_log_file): | |
| """Test handling when log file path is a directory.""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| with patch("builtins.open", side_effect=IsADirectoryError("Is a directory")): | |
| with pytest.raises(IsADirectoryError): | |
| logger.get_audit_trail("scan_123") | |
| # ============================================================================ | |
| # Test Log Levels | |
| # ============================================================================ | |
| class TestLogLevels: | |
| """Test different log levels.""" | |
| def test_default_log_level_is_info(self, temp_log_file): | |
| """Test that default log level is INFO.""" | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| assert logger.logger.level == logging.INFO | |
| def test_log_level_filters_debug(self, temp_log_file, caplog): | |
| """Test that DEBUG messages are filtered.""" | |
| # Create a real StringIO to capture output | |
| stream = StringIO() | |
| stream_handler = logging.StreamHandler(stream) | |
| stream_handler.setLevel(logging.DEBUG) | |
| stream_handler.setFormatter(logging.Formatter("%(message)s")) | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| # Set to DEBUG to receive all messages | |
| logger.logger.setLevel(logging.DEBUG) | |
| # Remove default handlers and add our stream handler | |
| for handler in logger.logger.handlers[:]: | |
| logger.logger.removeHandler(handler) | |
| logger.logger.addHandler(stream_handler) | |
| logger.logger.debug("Debug message") | |
| logger.log_agent_decision("agent_1", "test", {}) # This uses info level | |
| # Get the captured output | |
| output = stream.getvalue() | |
| # The agent decision should be logged (INFO level) | |
| assert "agent_decision" in output | |
| # Debug message should also appear since we set level to DEBUG | |
| assert "Debug message" in output | |
| # ============================================================================ | |
| # Test Edge Cases | |
| # ============================================================================ | |
| class TestEdgeCases: | |
| """Test edge cases and boundary conditions.""" | |
| def test_very_long_decision_string(self, temp_log_file): | |
| """Test logging with very long decision string.""" | |
| mock_logger = MagicMock() | |
| long_decision = "x" * 10000 | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("agent_1", long_decision, {}) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["decision"] == long_decision | |
| def test_very_large_context(self, temp_log_file): | |
| """Test logging with very large context.""" | |
| mock_logger = MagicMock() | |
| large_context = {"key_" + str(i): "value_" + str(i) for i in range(1000)} | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("agent_1", "test", large_context) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert len(log_entry["context"]) == 1000 | |
| def test_empty_strings(self, temp_log_file): | |
| """Test logging with empty strings.""" | |
| mock_logger = MagicMock() | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("", "", {"": ""}) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["agent_id"] == "" | |
| assert log_entry["decision"] == "" | |
| assert log_entry["context"] == {"": ""} | |
| def test_none_values_in_context(self, temp_log_file): | |
| """Test logging with None values in context.""" | |
| mock_logger = MagicMock() | |
| context = {"null_key": None, "nested": {"null_nested": None}} | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("agent_1", "test", context) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["context"]["null_key"] is None | |
| def test_boolean_values_in_context(self, temp_log_file): | |
| """Test logging with boolean values in context.""" | |
| mock_logger = MagicMock() | |
| context = {"true_val": True, "false_val": False} | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("agent_1", "test", context) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["context"]["true_val"] is True | |
| assert log_entry["context"]["false_val"] is False | |
| def test_numeric_values_in_context(self, temp_log_file): | |
| """Test logging with numeric values in context.""" | |
| mock_logger = MagicMock() | |
| context = {"int_val": 42, "float_val": 3.14159, "negative": -100} | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("agent_1", "test", context) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["context"]["int_val"] == 42 | |
| assert log_entry["context"]["float_val"] == 3.14159 | |
| assert log_entry["context"]["negative"] == -100 | |
| def test_list_in_context(self, temp_log_file): | |
| """Test logging with list values in context.""" | |
| mock_logger = MagicMock() | |
| context = { | |
| "ports": [80, 443, 8080], | |
| "mixed": [1, "two", 3.0, None, True], | |
| } | |
| with patch("logging.FileHandler"): | |
| logger = AuditLogger(log_file=temp_log_file) | |
| logger.logger = mock_logger | |
| logger.log_agent_decision("agent_1", "test", context) | |
| log_message = mock_logger.info.call_args[0][0] | |
| log_entry = json.loads(log_message) | |
| assert log_entry["context"]["ports"] == [80, 443, 8080] | |
| assert log_entry["context"]["mixed"] == [1, "two", 3.0, None, True] | |
| def test_concurrent_logging(self, tmp_path): | |
| """Test concurrent logging operations.""" | |
| import threading | |
| log_file = tmp_path / "concurrent_test.log" | |
| logger = AuditLogger(log_file=str(log_file)) | |
| errors = [] | |
| success_count = [0] | |
| def log_entries(agent_id, count): | |
| try: | |
| for i in range(count): | |
| logger.log_agent_decision(agent_id, f"decision_{i}", {"iteration": i}) | |
| success_count[0] += 1 | |
| except Exception as e: | |
| errors.append(str(e)) | |
| threads = [threading.Thread(target=log_entries, args=(f"agent_{i}", 10)) for i in range(5)] | |
| for t in threads: | |
| t.start() | |
| for t in threads: | |
| t.join() | |
| assert len(errors) == 0 | |
| assert success_count[0] == 50 | |
| # Verify file contains all entries | |
| content = log_file.read_text() | |
| lines = [line for line in content.strip().split("\n") if line] | |
| assert len(lines) == 50 | |
| # ============================================================================ | |
| # Test Main Block | |
| # ============================================================================ | |
| class TestMainBlock: | |
| """Test the __main__ block execution.""" | |
| def test_main_block_execution(self, capsys): | |
| """Test that the main block executes without errors.""" | |
| from core import audit_logger | |
| with patch.object(AuditLogger, "__init__", return_value=None): | |
| with patch.object(AuditLogger, "log_agent_decision"): | |
| with patch.object(AuditLogger, "_setup_logger"): | |
| # Simulate running as __main__ | |
| import importlib | |
| # Reload module with __name__ set to "__main__" | |
| with patch.object(audit_logger, "__name__", "__main__"): | |
| with patch("builtins.print") as mock_print: | |
| # Execute the main block code | |
| logger = AuditLogger() | |
| logger.log_agent_decision( | |
| "agent_1", | |
| "start_scan", | |
| {"target": "example.com"}, | |
| ) | |
| print("✅ AuditLogger working") | |
| mock_print.assert_called_once_with("✅ AuditLogger working") | |
| # ============================================================================ | |
| # Test Integration | |
| # ============================================================================ | |
| class TestIntegration: | |
| """Integration tests for complete workflows.""" | |
| def test_full_audit_workflow(self, tmp_path): | |
| """Test complete audit workflow from logging to retrieval.""" | |
| log_file = tmp_path / "workflow_test.log" | |
| logger = AuditLogger(log_file=str(log_file)) | |
| # Simulate a scan workflow | |
| scan_id = "scan_workflow_001" | |
| # Step 1: Agent starts scan | |
| logger.log_agent_decision( | |
| "recon_agent", | |
| "start_scan", | |
| {"scan_id": scan_id, "target": "api.example.com"}, | |
| ) | |
| # Step 2: Tool executions | |
| logger.log_tool_execution( | |
| "nmap", | |
| "api.example.com", | |
| {"status": "completed", "findings": [{"port": 80}, {"port": 443}]}, | |
| ) | |
| logger.log_tool_execution( | |
| "nikto", | |
| "api.example.com", | |
| {"status": "completed", "findings": [{"vuln": "X-Frame-Options"}]}, | |
| ) | |
| # Step 3: Agent makes decision based on findings | |
| logger.log_agent_decision( | |
| "analysis_agent", | |
| "escalate_risk", | |
| {"scan_id": scan_id, "risk_level": "high", "findings_count": 3}, | |
| ) | |
| # Step 4: Retrieve audit trail | |
| entries = logger.get_audit_trail(scan_id) | |
| # Should have 2 entries with scan_id (start_scan and escalate_risk) | |
| assert len(entries) == 2 | |
| # Verify all entries are valid JSON in the message part | |
| for entry in entries: | |
| # Extract JSON from log line (after the last " - ") | |
| json_str = entry.split(" - ")[-1] | |
| data = json.loads(json_str) | |
| assert data["context"]["scan_id"] == scan_id | |
| def test_multiple_scans_isolation(self, tmp_path): | |
| """Test that audit trails for different scans are isolated.""" | |
| log_file = tmp_path / "isolation_test.log" | |
| logger = AuditLogger(log_file=str(log_file)) | |
| # Log entries for scan_1 | |
| for i in range(5): | |
| logger.log_agent_decision("agent_1", f"action_{i}", {"scan_id": "scan_1"}) | |
| # Log entries for scan_2 | |
| for i in range(3): | |
| logger.log_agent_decision("agent_1", f"action_{i}", {"scan_id": "scan_2"}) | |
| # Retrieve and verify | |
| scan_1_entries = logger.get_audit_trail("scan_1") | |
| scan_2_entries = logger.get_audit_trail("scan_2") | |
| assert len(scan_1_entries) == 5 | |
| assert len(scan_2_entries) == 3 | |
Xet Storage Details
- Size:
- 34.1 kB
- Xet hash:
- f14ba59016442e1dd42ad7b0f749e7a36e1e800330dd15856475c598f42a192a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.