Helper scripts

This commit is contained in:
2026-11-15 17:42:41 +01:00
parent b2f1baaac5
commit 39ba6bd7da
22 changed files with 2233 additions and 0 deletions

135
tests/test_alertsock.py Normal file
View File

@@ -0,0 +1,135 @@
"""Framing tests for the suricata unix_stream listener (suricata is the
client; the controller owns the listening socket). Uses real AF_UNIX
connections, no suricata binary needed."""
import os
import socket
import tempfile
import time
import unittest
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'controller'))
from leaf_controller.alertsock import AlertSocket
class AlertSocketTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.path = os.path.join(self.tmp.name, 'alerts.sock')
self.sock = AlertSocket(self.path, 100)
self.sock.bind()
self.sock.start()
def tearDown(self):
self.sock.close()
self.tmp.cleanup()
def _client(self):
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(self.path)
return s
def _wait(self, n=1, pred=None, timeout=5.0):
"""wait until drain() has >= n records, or pred() is true"""
deadline = time.time() + timeout
recs = []
while time.time() < deadline:
recs = self.sock.drain()
if len(recs) >= n and (pred is None or pred()):
return recs
time.sleep(0.05)
return recs
def _rec(self, n):
return ('{"event_type": "alert", "n": %d}\n' % n).encode()
def test_single_connection_streams_records(self):
s = self._client()
s.sendall(self._rec(1) + self._rec(2))
s.close()
recs = self._wait(2)
self.assertEqual([r['n'] for r in recs], [1, 2])
def test_partial_line_frames_only_on_newline(self):
s = self._client()
s.sendall(b'{"event_type": "alert", "n"')
time.sleep(0.3)
self.assertEqual(self.sock.drain(), [])
s.sendall(b': 7}\n')
recs = self._wait(1)
self.assertEqual([r['n'] for r in recs], [7])
s.close()
def test_non_alert_records_counted_other_never_queued(self):
# ingress gate: the socket output block carries alerts only; a
# stray stats record must never reach policy/state
s = self._client()
s.sendall(b'{"event_type": "stats", "x": 1}\n')
s.close()
self._wait(0, pred=lambda: self.sock.counts['other'] >= 1)
self.assertEqual(self.sock.drain(), [])
self.assertGreaterEqual(self.sock.counts['other'], 1)
def test_reconnect_yields_continuous_stream(self):
self._client().close()
s = self._client()
s.sendall(self._rec(1) + self._rec(2))
s.close()
recs = self._wait(2)
self.assertEqual([r['n'] for r in recs], [1, 2])
self.assertGreaterEqual(self.sock.counts['connections'], 2)
def test_garbage_and_non_object_lines_counted_not_hung_controller(self):
s = self._client()
s.sendall(b'not-json\n[1,2,3]\n' + self._rec(1))
s.close()
# unparseable line -> parse_errors; valid JSON but not an alert
# object -> other gate; the good record must still flow through
recs = self._wait(1, pred=lambda: self.sock.counts['parse_errors'] >= 1
and self.sock.counts['other'] >= 1)
self.assertEqual([r['n'] for r in recs], [1])
self.assertGreaterEqual(self.sock.counts['parse_errors'], 1)
self.assertGreaterEqual(self.sock.counts['other'], 1)
def test_queue_overflow_drops_are_counted(self):
# bounded queue: a stalled consumer forces the eager-drain semantics
# and losses must be visible in counts, never silently dropped
self.sock.close()
self.sock = AlertSocket(self.path, 1)
self.sock.bind()
self.sock.start()
s = self._client()
s.sendall(self._rec(1) + self._rec(2) + self._rec(3))
s.close()
deadline = time.time() + 5.0
while time.time() < deadline and self.sock.counts['queue_drops'] < 1:
time.sleep(0.05)
self.assertGreaterEqual(self.sock.counts['queue_drops'], 1)
recs = self.sock.drain()
self.assertEqual(len(recs), 1) # exactly the survivor of the burst
def test_close_removes_socket_file(self):
self.assertTrue(os.path.exists(self.path))
self.sock.close()
self.assertFalse(os.path.exists(self.path))
def test_bind_replaces_stale_socket_path(self):
# An unowned socket whose listener has exited can be reclaimed.
# Active listeners and separately locked socket paths are protected.
self.sock.close()
stale = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
stale.bind(self.path)
stale.close()
self.sock = AlertSocket(self.path, 10)
self.sock.bind()
self.sock.start()
s = self._client()
s.sendall(self._rec(1))
s.close()
recs = self._wait(1)
self.assertEqual([r['n'] for r in recs], [1])
if __name__ == '__main__':
unittest.main()