test_ldap_lab.py
Validation tests for the independent LDAP sample scripts.
1 import csv
2 import importlib.util
3 from pathlib import Path
4 import unittest
5
6 ROOT = Path(__file__).resolve().parents[1]
7 LAB = ROOT / 'dist/labs/ldap'
8 if not LAB.is_dir():
9 LAB = ROOT # Standalone download bundle
10
11
12 def load(name):
13 spec = importlib.util.spec_from_file_location(name, LAB / (name + '.py'))
14 module = importlib.util.module_from_spec(spec)
15 spec.loader.exec_module(module)
16 return module
17
18
19 queries = load('detect_query_bursts')
20 sysmon = load('detect_sysmon_ldap')
21
22
23 class LabTests(unittest.TestCase):
24 def setUp(self):
25 with (LAB / 'sample-searches.csv').open() as f:
26 self.rows = list(csv.DictReader(f))
27 self.xml = (LAB / 'sample-sysmon.xml').read_text()
28
29 def test_sample_and_out_of_order(self):
30 alerts = queries.detect(list(reversed(self.rows)), 5, 60)
31 self.assertEqual(len(alerts), 1)
32 self.assertEqual(alerts[0]['source_ip'], '192.0.2.25')
33 self.assertEqual(alerts[0]['observed_count'], 5)
34
35 def test_duplicates_do_not_inflate(self):
36 self.assertEqual(queries.detect(self.rows * 2, 6, 60), [])
37
38 def test_window_boundary_and_group_isolation(self):
39 rows = [dict(r) for r in self.rows[:2]]
40 rows[1]['timestamp'] = '2026-01-01T12:01:00Z'
41 self.assertEqual(len(queries.detect(rows, 2, 60)), 1)
42 rows[1]['timestamp'] = '2026-01-01T12:01:01Z'
43 self.assertEqual(queries.detect(rows, 2, 60), [])
44 rows[1]['timestamp'] = rows[0]['timestamp']
45 rows[1]['dc_ip'] = '192.0.2.11'
46 self.assertEqual(queries.detect(rows, 2, 60), [])
47
48 def test_conflicting_id_and_missing_timezone_rejected(self):
49 bad = dict(self.rows[0], source_ip='192.0.2.99')
50 with self.assertRaises(ValueError):
51 queries.detect(self.rows + [bad])
52 with self.assertRaises(ValueError):
53 queries.detect([dict(self.rows[0], timestamp='2026-01-01T12:00:00')])
54
55 def test_connection_rows_and_invalid_threshold_rejected(self):
56 with self.assertRaises(ValueError):
57 queries.detect([dict(self.rows[0], event_type='connection')])
58 with self.assertRaises(ValueError):
59 queries.detect(self.rows, 0)
60
61 def test_sustained_burst_alerts_once_then_rearms(self):
62 rows = [dict(self.rows[0], record_id=str(i), timestamp=f'2026-01-01T12:00:{i:02d}Z') for i in range(6)]
63 rows += [dict(self.rows[0], record_id=f'later-{i}', timestamp=f'2026-01-01T12:02:{i:02d}Z') for i in range(3)]
64 self.assertEqual(len(queries.detect(rows, 3, 60)), 2)
65
66 def test_sysmon_sample(self):
67 alerts = sysmon.detect(self.xml, ['192.0.2.10'])
68 self.assertEqual(len(alerts), 1)
69 self.assertEqual(alerts[0]['record_id'], '1')
70
71 def test_sysmon_scope_and_provider(self):
72 self.assertEqual(sysmon.detect(self.xml, ['192.0.2.50']), [])
73 self.assertEqual(sysmon.detect(self.xml.replace('Microsoft-Windows-Sysmon', 'Other'), ['192.0.2.10']), [])
74 self.assertEqual(sysmon.detect(self.xml.replace('>true<', '>false<'), ['192.0.2.10']), [])
75 self.assertEqual(sysmon.detect(self.xml.replace('>389<', '>443<'), ['192.0.2.10']), [])
76
77 def test_sysmon_missing_fields_and_entities(self):
78 with self.assertRaises(ValueError):
79 sysmon.detect(self.xml.replace('Name="ProcessGuid"', 'Name="Other"'), ['192.0.2.10'])
80 with self.assertRaises(ValueError):
81 sysmon.detect('<!DOCTYPE x><Events/>', ['192.0.2.10'])
82
83
84 if __name__ == '__main__':
85 unittest.main()