Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /unit /test_security_integrated.py
| """ | |
| Tests for core.integrated_security module. | |
| Split from test_core_remaining.py | |
| """ | |
| import asyncio | |
| import gc | |
| import hashlib | |
| import hmac | |
| import json | |
| import logging | |
| import os | |
| import sys | |
| import tempfile | |
| import threading | |
| import time | |
| from datetime import datetime, timedelta, timezone | |
| from pathlib import Path | |
| from unittest.mock import AsyncMock, MagicMock, Mock, mock_open, patch | |
| import pytest | |
| # Mock aiortc before importing integrated_security | |
| sys.modules["aiortc"] = MagicMock() | |
| sys.modules["aiortc.mediastreams"] = MagicMock() | |
| sys.modules["p2p"] = MagicMock() | |
| sys.modules["p2p.connection"] = MagicMock() | |
| sys.modules["p2p.channel"] = MagicMock() | |
| sys.modules["core.credential_lifecycle"] = MagicMock() | |
| sys.modules["core.vault_manager"] = MagicMock() | |
| sys.modules["core.ssh_key_manager"] = MagicMock() | |
| sys.modules["kimi_auth_bridge"] = MagicMock() | |
| # Integrated Security Tests | |
| # ============================================================================= | |
| from core.integrated_security import IntegratedSecurityManager | |
| from core.integrated_security import SecurityError as ISSecurityError | |
| from core.integrated_security import SecurityStatus, get_security_manager | |
| from core.kill_switch import KillSwitchEngine | |
| class TestSecurityStatus: | |
| """Test SecurityStatus dataclass.""" | |
| def test_creation(self): | |
| """Test creation.""" | |
| status = SecurityStatus( | |
| kill_switch_armed=True, | |
| p2p_connected=True, | |
| vault_unlocked=False, | |
| active_credentials=3, | |
| last_heartbeat=30.0, | |
| session_duration="1h 30m", | |
| ) | |
| assert status.kill_switch_armed is True | |
| assert status.p2p_connected is True | |
| assert status.vault_unlocked is False | |
| assert status.active_credentials == 3 | |
| assert status.last_heartbeat == 30.0 | |
| assert status.session_duration == "1h 30m" | |
| class TestIntegratedSecurityManager: | |
| """Test IntegratedSecurityManager class.""" | |
| def test_initialization(self): | |
| """Test manager initialization.""" | |
| manager = IntegratedSecurityManager() | |
| assert isinstance(manager.kill_switch, KillSwitchEngine) | |
| assert manager.p2p is None | |
| def test_setup_kill_switch_callbacks(self): | |
| """Test kill switch callbacks setup.""" | |
| manager = IntegratedSecurityManager() | |
| # Callback should be registered | |
| assert len(manager.kill_switch._callbacks) == 1 | |
| async def test_start(self): | |
| """Test starting security system.""" | |
| manager = IntegratedSecurityManager() | |
| with patch.object(manager.kill_switch, "start_monitoring", new_callable=AsyncMock): | |
| with patch.object(manager.temporal_ca, "start", new_callable=AsyncMock): | |
| await manager.start() | |
| manager.kill_switch.start_monitoring.assert_called_once() | |
| manager.temporal_ca.start.assert_called_once() | |
| async def test_stop(self): | |
| """Test stopping security system.""" | |
| manager = IntegratedSecurityManager() | |
| with patch.object(manager.kill_switch, "trigger_kill_switch", new_callable=AsyncMock): | |
| with patch.object(manager.temporal_ca, "stop", new_callable=AsyncMock): | |
| with patch.object( | |
| manager.kill_switch, | |
| "stop_monitoring", | |
| new_callable=AsyncMock, | |
| ): | |
| await manager.stop() | |
| manager.kill_switch.trigger_kill_switch.assert_called_once() | |
| manager.temporal_ca.stop.assert_called_once() | |
| async def test_on_p2p_disconnect(self): | |
| """Test P2P disconnect handler.""" | |
| manager = IntegratedSecurityManager() | |
| with patch.object(manager.kill_switch, "trigger_kill_switch", new_callable=AsyncMock): | |
| await manager._on_p2p_disconnect() | |
| manager.kill_switch.trigger_kill_switch.assert_called_once() | |
| args = manager.kill_switch.trigger_kill_switch.call_args[0] | |
| assert args[0].value == "tunnel_down" | |
| def test_get_status(self): | |
| """Test getting security status.""" | |
| manager = IntegratedSecurityManager() | |
| with patch.object( | |
| manager.kill_switch, | |
| "get_status", | |
| return_value={ | |
| "last_heartbeat": {"seconds_ago": 30}, | |
| "session": {"age_hours": 1.5}, | |
| }, | |
| ): | |
| with patch.object(manager.vault_manager, "is_unlocked", return_value=False): | |
| status = manager.get_status() | |
| assert isinstance(status, SecurityStatus) | |
| assert status.kill_switch_armed is True | |
| assert status.vault_unlocked is False | |
| assert status.last_heartbeat == 30 | |
| async def test_emergency_lockdown(self): | |
| """Test emergency lockdown.""" | |
| manager = IntegratedSecurityManager() | |
| with patch.object(manager.temporal_ca, "revoke_all", new_callable=AsyncMock): | |
| with patch.object(manager.vault_manager, "encrypt_vault", new_callable=AsyncMock): | |
| await manager.emergency_lockdown() | |
| manager.temporal_ca.revoke_all.assert_called_once() | |
| manager.vault_manager.encrypt_vault.assert_called_once() | |
| async def test_rotate_all_credentials(self): | |
| """Test rotating all credentials.""" | |
| manager = IntegratedSecurityManager() | |
| mock_credential = MagicMock() | |
| mock_credential.id = "cred1" | |
| with patch.object( | |
| manager.temporal_ca, | |
| "get_active", | |
| new_callable=AsyncMock, | |
| return_value=[mock_credential], | |
| ): | |
| with patch.object( | |
| manager.temporal_ca, | |
| "rotate_credential", | |
| new_callable=AsyncMock, | |
| ): | |
| await manager.rotate_all_credentials() | |
| manager.temporal_ca.rotate_credential.assert_called_once_with("cred1") | |
| class TestGetSecurityManager: | |
| """Test get_security_manager function.""" | |
| def test_returns_singleton(self): | |
| """Test that function returns singleton.""" | |
| sm1 = get_security_manager() | |
| sm2 = get_security_manager() | |
| assert sm1 is sm2 | |
| def test_returns_integrated_security_manager(self): | |
| """Test that function returns correct type.""" | |
| sm = get_security_manager() | |
| assert isinstance(sm, IntegratedSecurityManager) | |
| # ============================================================================= | |
Xet Storage Details
- Size:
- 6.65 kB
- Xet hash:
- 615b0ee7420928b20f2132516394822e93cb47feb026c4de1ae6d4d49098c7f5
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.