-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtests.py
More file actions
364 lines (287 loc) · 12 KB
/
Copy pathtests.py
File metadata and controls
364 lines (287 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# ShadowWall AI Testing Framework
import unittest
import asyncio
from unittest.mock import Mock, patch, AsyncMock
import tempfile
import os
from datetime import datetime
# Test imports
from src.core.config.settings import load_config
from src.core.ml.threat_detector import ThreatDetector
from src.core.ml.behavioral_analyzer import BehavioralAnalyzer
from src.core.honeypots.manager import HoneypotManager
from src.core.network.monitor import NetworkMonitor
from src.core.deception.controller import DeceptionController
from src.core.intelligence.threat_intel import ThreatIntelligence
from src.core.sandbox.emulator import SandboxEmulator
class TestConfiguration(unittest.TestCase):
"""Test configuration loading and validation"""
def setUp(self):
self.test_config = {
'database': {'url': 'sqlite:///test.db'},
'logging': {'level': 'INFO'},
'network': {'interface': 'eth0'},
'ml': {'model_update_interval': 3600},
'honeypots': {'enabled': True},
'dashboard': {'port': 8080},
'sandbox': {'enabled': False}
}
def test_config_loading(self):
"""Test configuration loading from dict"""
# This would normally test file loading, but we'll test the structure
self.assertIn('database', self.test_config)
self.assertIn('logging', self.test_config)
self.assertIn('network', self.test_config)
self.assertIn('ml', self.test_config)
def test_config_validation(self):
"""Test configuration validation"""
# Test required fields are present
required_sections = ['database', 'logging', 'network', 'ml']
for section in required_sections:
self.assertIn(section, self.test_config)
class TestThreatDetector(unittest.TestCase):
"""Test ML threat detection"""
def setUp(self):
self.config = {
'models_dir': tempfile.mkdtemp(),
'update_interval': 3600,
'anomaly_threshold': 0.95,
'ensemble_weights': [0.4, 0.3, 0.3]
}
self.detector = ThreatDetector(self.config)
def test_initialization(self):
"""Test threat detector initialization"""
self.assertIsNotNone(self.detector)
self.assertEqual(self.detector.config, self.config)
@patch('src.core.ml.threat_detector.IsolationForest')
@patch('src.core.ml.threat_detector.RandomForestClassifier')
def test_model_training(self, mock_rf, mock_iso):
"""Test model training with synthetic data"""
# Mock the models
mock_iso_instance = Mock()
mock_rf_instance = Mock()
mock_iso.return_value = mock_iso_instance
mock_rf.return_value = mock_rf_instance
# Test training
asyncio.run(self.detector._train_models())
# Verify models were created and trained
mock_iso.assert_called_once()
mock_rf.assert_called_once()
def test_threat_analysis(self):
"""Test threat analysis logic"""
# Test with sample features
features = [0.5, 0.3, 0.8, 0.2, 0.9]
# Mock trained models
self.detector.isolation_forest = Mock()
self.detector.random_forest = Mock()
self.detector.isolation_forest.decision_function.return_value = [-0.1]
self.detector.random_forest.predict_proba.return_value = [[0.8, 0.2]]
result = self.detector.analyze_threat(features)
self.assertIsInstance(result, dict)
self.assertIn('threat_score', result)
self.assertIn('threat_type', result)
class TestBehavioralAnalyzer(unittest.TestCase):
"""Test behavioral analysis"""
def setUp(self):
self.config = {
'learning_rate': 0.1,
'baseline_period': 3600,
'anomaly_threshold': 2.0
}
self.analyzer = BehavioralAnalyzer(self.config)
def test_initialization(self):
"""Test behavioral analyzer initialization"""
self.assertIsNotNone(self.analyzer)
self.assertEqual(len(self.analyzer.entity_profiles), 0)
def test_entity_tracking(self):
"""Test entity behavior tracking"""
entity_id = "user_123"
behavior_data = {
'login_time': datetime.now(),
'source_ip': '192.168.1.100',
'actions': ['login', 'file_access']
}
result = asyncio.run(self.analyzer.track_behavior(entity_id, behavior_data))
self.assertIsInstance(result, dict)
self.assertIn('anomaly_score', result)
self.assertIn(entity_id, self.analyzer.entity_profiles)
class TestHoneypotManager(unittest.TestCase):
"""Test honeypot management"""
def setUp(self):
self.config = {
'ssh_port': 2222,
'http_port': 8080,
'ftp_port': 2121,
'deployment_strategy': 'adaptive',
'max_honeypots': 10
}
self.manager = HoneypotManager(self.config)
def test_initialization(self):
"""Test honeypot manager initialization"""
self.assertIsNotNone(self.manager)
self.assertEqual(len(self.manager.active_honeypots), 0)
@patch('asyncio.start_server')
async def test_honeypot_deployment(self, mock_server):
"""Test honeypot deployment"""
mock_server.return_value = Mock()
honeypot_id = await self.manager.deploy_honeypot('ssh', '0.0.0.0', 2222)
self.assertIsNotNone(honeypot_id)
self.assertIn(honeypot_id, self.manager.active_honeypots)
def test_interaction_recording(self):
"""Test interaction recording"""
interaction_data = {
'source_ip': '192.168.1.100',
'honeypot_type': 'ssh',
'timestamp': datetime.now(),
'data': 'login attempt'
}
asyncio.run(self.manager.record_interaction(interaction_data))
# Verify interaction was recorded
self.assertTrue(len(self.manager.interactions) > 0)
class TestNetworkMonitor(unittest.TestCase):
"""Test network monitoring"""
def setUp(self):
self.config = {
'interface': 'lo', # Use loopback for testing
'capture_filter': 'tcp',
'simulation_mode': True # Use simulation for testing
}
self.monitor = NetworkMonitor(self.config)
def test_initialization(self):
"""Test network monitor initialization"""
self.assertIsNotNone(self.monitor)
self.assertEqual(self.monitor.interface, 'lo')
@patch('scapy.all.AsyncSniffer')
def test_packet_capture_start(self, mock_sniffer):
"""Test packet capture start"""
mock_sniffer_instance = Mock()
mock_sniffer.return_value = mock_sniffer_instance
asyncio.run(self.monitor.start())
if not self.config['simulation_mode']:
mock_sniffer.assert_called_once()
class TestDeceptionController(unittest.TestCase):
"""Test deception strategies"""
def setUp(self):
self.config = {
'strategy_update_interval': 300,
'effectiveness_threshold': 0.7
}
# Mock dependencies
self.honeypot_manager = Mock()
self.threat_detector = Mock()
self.controller = DeceptionController(self.config, self.honeypot_manager, self.threat_detector)
def test_initialization(self):
"""Test deception controller initialization"""
self.assertIsNotNone(self.controller)
self.assertTrue(len(self.controller.available_strategies) > 0)
def test_strategy_selection(self):
"""Test strategy selection logic"""
threat_context = {
'threat_type': 'reconnaissance',
'source_ip': '192.168.1.100',
'confidence': 0.8
}
strategy = asyncio.run(self.controller.select_strategy(threat_context))
self.assertIsNotNone(strategy)
self.assertIn('name', strategy)
self.assertIn('actions', strategy)
class TestSandboxEmulator(unittest.TestCase):
"""Test sandbox emulation"""
def setUp(self):
self.config = {
'enabled': True,
'max_concurrent_sessions': 5
}
self.emulator = SandboxEmulator(self.config)
def test_initialization(self):
"""Test sandbox emulator initialization"""
self.assertIsNotNone(self.emulator)
self.assertTrue(len(self.emulator.environments) > 0)
@patch('docker.from_env')
def test_session_creation(self, mock_docker):
"""Test sandbox session creation"""
# Mock Docker client
mock_docker.return_value = Mock()
session_id = asyncio.run(
self.emulator.create_session('test_user', 'basic_network', 60)
)
# In simulation mode, this should still work
self.assertTrue(session_id is None or isinstance(session_id, str))
def test_environment_listing(self):
"""Test available environments listing"""
environments = self.emulator.get_available_environments()
self.assertIsInstance(environments, dict)
self.assertIn('basic_network', environments)
self.assertIn('corporate_sim', environments)
class TestIntegration(unittest.TestCase):
"""Integration tests"""
def setUp(self):
self.config = {
'database': {'url': 'sqlite:///:memory:'},
'logging': {'level': 'INFO'},
'network': {'interface': 'lo', 'simulation_mode': True},
'ml': {'model_update_interval': 3600},
'honeypots': {'enabled': True, 'ssh_port': 2222},
'dashboard': {'port': 8080},
'sandbox': {'enabled': False},
'deception': {'strategy_update_interval': 300},
'threat_intel': {'enabled': True, 'update_interval': 3600}
}
def test_component_initialization(self):
"""Test that all components can be initialized together"""
try:
# Initialize core components
threat_detector = ThreatDetector(self.config['ml'])
behavioral_analyzer = BehavioralAnalyzer(self.config['ml'])
honeypot_manager = HoneypotManager(self.config['honeypots'])
network_monitor = NetworkMonitor(self.config['network'])
# Verify initialization
self.assertIsNotNone(threat_detector)
self.assertIsNotNone(behavioral_analyzer)
self.assertIsNotNone(honeypot_manager)
self.assertIsNotNone(network_monitor)
except Exception as e:
self.fail(f"Component initialization failed: {e}")
def run_tests():
"""Run all tests"""
print("=" * 60)
print("ShadowWall AI - Running Test Suite")
print("=" * 60)
# Create test suite
test_suite = unittest.TestSuite()
# Add test cases
test_classes = [
TestConfiguration,
TestThreatDetector,
TestBehavioralAnalyzer,
TestHoneypotManager,
TestNetworkMonitor,
TestDeceptionController,
TestSandboxEmulator,
TestIntegration
]
for test_class in test_classes:
tests = unittest.TestLoader().loadTestsFromTestCase(test_class)
test_suite.addTests(tests)
# Run tests
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(test_suite)
# Print summary
print("\n" + "=" * 60)
print(f"Tests run: {result.testsRun}")
print(f"Failures: {len(result.failures)}")
print(f"Errors: {len(result.errors)}")
if result.failures:
print("\nFailures:")
for test, traceback in result.failures:
print(f" - {test}: {traceback}")
if result.errors:
print("\nErrors:")
for test, traceback in result.errors:
print(f" - {test}: {traceback}")
success = len(result.failures) == 0 and len(result.errors) == 0
print(f"\nResult: {'PASSED' if success else 'FAILED'}")
print("=" * 60)
return success
if __name__ == '__main__':
run_tests()