-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cybersecurity.py
More file actions
446 lines (340 loc) · 15.8 KB
/
Copy pathtest_cybersecurity.py
File metadata and controls
446 lines (340 loc) · 15.8 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""
Comprehensive test suite for src/cybersecurity.py.
Tests audit chain integrity, STRIDE threat model, and sensor validation.
"""
class TestAuditChain:
"""Tests for SHA-256 hash-chain audit log."""
def test_audit_chain_creation(self, cybersecurity):
"""Should create audit chain without error."""
chain = cybersecurity.AuditChain()
assert chain is not None
assert len(chain.entries) == 0
def test_add_entry_to_chain(self, cybersecurity):
"""Should add entry to chain."""
chain = cybersecurity.AuditChain()
entry = chain.add_entry(
event_type="simulation",
description="Baseline run",
data={"T": 873.15, "P": 5.0e6}
)
assert entry is not None
assert len(chain.entries) == 1
assert chain.entries[0].event_type == "simulation"
def test_entry_has_hash(self, cybersecurity):
"""Added entry should have entry_hash."""
chain = cybersecurity.AuditChain()
entry = chain.add_entry(
event_type="test",
description="Test entry",
data={"key": "value"}
)
assert entry.entry_hash is not None
assert len(entry.entry_hash) == 64 # SHA-256 hex string length
def test_chain_linkage(self, cybersecurity):
"""Each entry should link to previous."""
chain = cybersecurity.AuditChain()
entry1 = chain.add_entry("type1", "First", {"a": 1})
entry2 = chain.add_entry("type2", "Second", {"b": 2})
# Entry2's previous_hash should point to Entry1's entry_hash
assert entry2.previous_hash == entry1.entry_hash
def test_genesis_hash(self, cybersecurity):
"""First entry should link to genesis hash."""
chain = cybersecurity.AuditChain()
entry1 = chain.add_entry("type1", "First", {"a": 1})
# Should link to genesis
assert entry1.previous_hash == chain._genesis_hash
def test_verify_chain_valid(self, cybersecurity):
"""Valid chain should pass verification."""
chain = cybersecurity.AuditChain()
chain.add_entry("type1", "First", {"a": 1})
chain.add_entry("type2", "Second", {"b": 2})
chain.add_entry("type3", "Third", {"c": 3})
assert chain.verify_chain() is True
def test_verify_chain_detects_tampering(self, cybersecurity):
"""Verification should detect entry tampering."""
chain = cybersecurity.AuditChain()
chain.add_entry("type1", "First", {"a": 1})
chain.add_entry("type2", "Second", {"b": 2})
# Tamper with first entry
chain.entries[0].description = "TAMPERED"
assert chain.verify_chain() is False
def test_verify_chain_detects_hash_tampering(self, cybersecurity):
"""Verification should detect hash tampering."""
chain = cybersecurity.AuditChain()
chain.add_entry("type1", "First", {"a": 1})
chain.add_entry("type2", "Second", {"b": 2})
# Tamper with entry hash
chain.entries[0].entry_hash = "0" * 64
assert chain.verify_chain() is False
def test_verify_chain_detects_link_tampering(self, cybersecurity):
"""Verification should detect linkage tampering."""
chain = cybersecurity.AuditChain()
chain.add_entry("type1", "First", {"a": 1})
chain.add_entry("type2", "Second", {"b": 2})
chain.add_entry("type3", "Third", {"c": 3})
# Tamper with link between entries
chain.entries[1].previous_hash = "0" * 64
assert chain.verify_chain() is False
def test_export_log(self, cybersecurity):
"""Should export chain as list of dicts."""
chain = cybersecurity.AuditChain()
chain.add_entry("type1", "First", {"a": 1})
chain.add_entry("type2", "Second", {"b": 2})
log = chain.export_log()
assert isinstance(log, list)
assert len(log) == 2
assert all(isinstance(entry, dict) for entry in log)
assert log[0]["event_type"] == "type1"
assert log[1]["event_type"] == "type2"
def test_multiple_entries_different_data(self, cybersecurity):
"""Different data should produce different hashes."""
chain = cybersecurity.AuditChain()
entry1 = chain.add_entry("simulation", "Run 1", {"stress": 100.0})
chain.entries.clear() # Reset for next entry
chain2 = cybersecurity.AuditChain()
entry2 = chain2.add_entry("simulation", "Run 1", {"stress": 200.0})
# Different data should give different data_hash
assert entry1.data_hash != entry2.data_hash
class TestSTRIDEModel:
"""Tests for STRIDE threat model."""
def test_stride_model_generation(self, cybersecurity):
"""Should generate STRIDE model without error."""
threats = cybersecurity.build_stride_model()
assert threats is not None
assert len(threats) > 0
def test_stride_has_six_categories(self, cybersecurity):
"""Should have all 6 STRIDE categories."""
threats = cybersecurity.build_stride_model()
categories = set(t.category for t in threats)
# At least these categories should be present
assert "Spoofing" in categories
assert "Tampering" in categories
assert "Repudiation" in categories
assert "Information Disclosure" in categories
assert "Denial of Service" in categories
assert "Elevation of Privilege" in categories
def test_each_threat_has_mitigation(self, cybersecurity):
"""Each threat should have mitigation."""
threats = cybersecurity.build_stride_model()
for threat in threats:
assert threat.mitigation is not None
assert len(threat.mitigation) > 0
def test_threat_assessment_fields(self, cybersecurity):
"""Each threat assessment should have required fields."""
threats = cybersecurity.build_stride_model()
for threat in threats:
assert threat.category is not None
assert threat.threat is not None
assert threat.likelihood in ["low", "medium", "high"]
assert threat.impact in ["low", "medium", "high", "critical"]
assert threat.mitigation is not None
assert threat.residual_risk is not None
def test_likelihood_values_valid(self, cybersecurity):
"""Likelihood should be from valid set."""
threats = cybersecurity.build_stride_model()
valid_likelihoods = {"low", "medium", "high"}
for threat in threats:
assert threat.likelihood in valid_likelihoods
def test_impact_values_valid(self, cybersecurity):
"""Impact should be from valid set."""
threats = cybersecurity.build_stride_model()
valid_impacts = {"low", "medium", "high", "critical"}
for threat in threats:
assert threat.impact in valid_impacts
def test_residual_risk_values_valid(self, cybersecurity):
"""Residual risk should be from valid set."""
threats = cybersecurity.build_stride_model()
valid_risks = {"low", "medium", "high"}
for threat in threats:
assert threat.residual_risk in valid_risks
def test_threats_specific_to_heater_monitoring(self, cybersecurity):
"""Threats should be specific to fired heater tube monitoring."""
threats = cybersecurity.build_stride_model()
# Should mention key concepts
assert any("tube" in t.threat.lower() or "TMT" in t.threat
for t in threats)
class TestSensorValidation:
"""Tests for sensor input validation."""
def test_valid_sensor_readings(self, cybersecurity):
"""Should validate correct sensor readings."""
# TMT - outlet = 300K; expected rise = heat_duty * 0.08 = 320K
# So 300 < 320*2 = 640, no alert triggered
result = cybersecurity.validate_sensor_inputs(
tmt_readings=[873.15, 874.0, 872.5],
process_outlet_T=573.15,
heat_duty_kW=4000.0
)
assert result["valid"] is True
assert result["median_tmt"] > 0.0
assert result["spread"] >= 0.0
assert len(result["alerts"]) == 0
def test_detects_low_redundancy(self, cybersecurity):
"""Should detect insufficient sensor redundancy."""
result = cybersecurity.validate_sensor_inputs(
tmt_readings=[873.15], # Only one sensor
process_outlet_T=573.15,
heat_duty_kW=1000.0
)
assert result["valid"] is False
assert len(result["alerts"]) > 0
assert any("redundancy" in alert.lower() for alert in result["alerts"])
def test_detects_sensor_spread(self, cybersecurity):
"""Should detect excessive sensor spread."""
result = cybersecurity.validate_sensor_inputs(
tmt_readings=[873.15, 900.0, 850.0], # Large spread
process_outlet_T=573.15,
heat_duty_kW=1000.0
)
# Spread > 25K should trigger alert
if result["spread"] > 25.0:
assert not result["valid"]
assert any("spread" in alert.lower() for alert in result["alerts"])
def test_detects_physically_inconsistent_readings(self, cybersecurity):
"""Should detect TMT below process outlet (physically impossible)."""
result = cybersecurity.validate_sensor_inputs(
tmt_readings=[573.0, 572.5], # TMT lower than outlet
process_outlet_T=573.15,
heat_duty_kW=1000.0
)
assert not result["valid"]
assert len(result["alerts"]) > 0
assert any("physically" in alert.lower() or "outlet" in alert.lower()
for alert in result["alerts"])
def test_detects_anomalous_tmt_vs_heat_duty(self, cybersecurity):
"""Should detect TMT that doesn't match heat duty."""
result = cybersecurity.validate_sensor_inputs(
tmt_readings=[873.15, 874.0],
process_outlet_T=573.15,
heat_duty_kW=10.0 # Very low heat duty
)
# May or may not alert depending on expected_tmt_rise calculation
# Just verify structure
assert isinstance(result["valid"], bool)
def test_none_readings_handled(self, cybersecurity):
"""Should handle None values in readings."""
result = cybersecurity.validate_sensor_inputs(
tmt_readings=[873.15, None, 874.0],
process_outlet_T=573.15,
heat_duty_kW=1000.0
)
# Should use only valid readings
assert result["median_tmt"] is not None
def test_negative_readings_ignored(self, cybersecurity):
"""Should ignore negative temperature readings."""
result = cybersecurity.validate_sensor_inputs(
tmt_readings=[873.15, -100.0, 874.0], # One negative
process_outlet_T=573.15,
heat_duty_kW=1000.0
)
# Should compute median from valid readings
assert result["median_tmt"] > 0.0
def test_median_tmt_computed_correctly(self, cybersecurity):
"""Median TMT should be computed correctly."""
readings = [870.0, 875.0, 872.0]
result = cybersecurity.validate_sensor_inputs(
tmt_readings=readings,
process_outlet_T=573.15,
heat_duty_kW=1000.0
)
expected_median = 872.0
assert abs(result["median_tmt"] - expected_median) < 0.1
def test_spread_computed_correctly(self, cybersecurity):
"""Spread should be max - min."""
readings = [870.0, 875.0, 872.0]
result = cybersecurity.validate_sensor_inputs(
tmt_readings=readings,
process_outlet_T=573.15,
heat_duty_kW=1000.0
)
expected_spread = 875.0 - 870.0
assert abs(result["spread"] - expected_spread) < 0.1
def test_returns_dict_with_required_keys(self, cybersecurity):
"""Should return dict with all required keys."""
result = cybersecurity.validate_sensor_inputs(
tmt_readings=[873.15, 874.0],
process_outlet_T=573.15,
heat_duty_kW=1000.0
)
required_keys = {"valid", "median_tmt", "spread", "alerts"}
assert set(result.keys()) == required_keys
class TestAuditChainEdgeCases:
"""Edge cases and boundary tests for audit chain."""
def test_empty_chain_verifies(self, cybersecurity):
"""Empty chain should verify as valid."""
chain = cybersecurity.AuditChain()
assert chain.verify_chain() is True
def test_single_entry_verifies(self, cybersecurity):
"""Single entry chain should verify."""
chain = cybersecurity.AuditChain()
chain.add_entry("test", "Single entry", {"data": 1})
assert chain.verify_chain() is True
def test_large_chain_verifies(self, cybersecurity):
"""Large chain should verify without issue."""
chain = cybersecurity.AuditChain()
for i in range(100):
chain.add_entry(f"type_{i}", f"Entry {i}", {"index": i})
assert chain.verify_chain() is True
def test_entry_hash_deterministic(self, cybersecurity):
"""Same entry data should produce same hash."""
entry1 = cybersecurity.AuditEntry(
timestamp=1000.0,
event_type="test",
description="Test",
data_hash="abc123",
previous_hash="def456"
)
entry2 = cybersecurity.AuditEntry(
timestamp=1000.0,
event_type="test",
description="Test",
data_hash="abc123",
previous_hash="def456"
)
assert entry1.compute_hash() == entry2.compute_hash()
def test_chain_export_preserves_data(self, cybersecurity):
"""Exported log should preserve all entry data."""
chain = cybersecurity.AuditChain()
original_entry = chain.add_entry(
"simulation",
"Test simulation",
{"stress": 100.0, "temp": 873.15}
)
log = chain.export_log()
exported_entry = log[0]
assert exported_entry["event_type"] == original_entry.event_type
assert exported_entry["description"] == original_entry.description
assert exported_entry["entry_hash"] == original_entry.entry_hash
class TestSecurityProperties:
"""Tests for security properties of audit system."""
def test_hash_changes_with_data(self, cybersecurity):
"""Changing data should change hash."""
chain1 = cybersecurity.AuditChain()
entry1 = chain1.add_entry("test", "Data A", {"value": 100})
chain2 = cybersecurity.AuditChain()
entry2 = chain2.add_entry("test", "Data B", {"value": 200})
assert entry1.entry_hash != entry2.entry_hash
def test_entry_order_matters(self, cybersecurity):
"""Order of entries should affect verification."""
chain = cybersecurity.AuditChain()
chain.add_entry("type1", "First", {})
chain.add_entry("type2", "Second", {})
# Swap entries
chain.entries[0], chain.entries[1] = chain.entries[1], chain.entries[0]
# Should fail verification
assert chain.verify_chain() is False
def test_cannot_forge_entry_without_private_key(self, cybersecurity):
"""Forging entry requires knowing hash chain."""
# This is implicit - hash chain is cryptographically signed
chain = cybersecurity.AuditChain()
chain.add_entry("type1", "Original", {})
# Try to create fake entry with same properties
fake_entry = cybersecurity.AuditEntry(
timestamp=chain.entries[0].timestamp,
event_type=chain.entries[0].event_type,
description=chain.entries[0].description,
data_hash=chain.entries[0].data_hash,
previous_hash=chain.entries[0].previous_hash
)
fake_entry.entry_hash = fake_entry.compute_hash()
# Should match
assert fake_entry.entry_hash == chain.entries[0].entry_hash