1#!/usr/bin/env python3
2import importlib.util
3import json
4from pathlib import Path
5import tempfile
6import unittest
7
8
9spec = importlib.util.spec_from_file_location("watch", Path(__file__).with_name("substation-watch.py"))
10watch = importlib.util.module_from_spec(spec)
11spec.loader.exec_module(watch)
12
13
14class TransportEvidence(unittest.TestCase):
15 def test_filters_private_headers_bodies_and_other_services(self):
16 original = {"service": "substation", "status": 504, "request_id": "fixture", "content_length": "100", "bytes_read": 40,
17 "client": "192.0.2.1:1234", "request": {"headers": {"X-Api-Key": "private"}}, "body": "private"}
18 edge, client = watch.event({"MESSAGE": json.dumps(original)}, "substation.example")
19 self.assertEqual(client, "192.0.2.1:1234")
20 self.assertNotIn("private", json.dumps(edge))
21 self.assertEqual(watch.event({"MESSAGE": json.dumps({"service": "another"})}, "substation.example"), (None, None))
22 self.assertEqual(watch.event({"MESSAGE": "not json"}, "substation.example"), (None, None))
23 peer = {"request": {"host": "substation.example", "remote_ip": "2001:db8::1", "remote_port": "44300", "headers": {"Authorization": "private"}}}
24 self.assertEqual(watch.event({"MESSAGE": json.dumps(peer)}, "substation.example"), (None, "[2001:db8::1]:44300"))
25
26 def test_connection_selection_and_counters(self):
27 output = "\n".join([
28 "0 100 127.0.0.1:24000 127.0.0.1:45000 cubic bytes_received:40000 rtt:0.1/0.2 snd_wnd:0 ignored:private",
29 "0 0 192.0.2.5:443 192.0.2.1:1234 cubic bytes_received:41000 lastrcv:60000",
30 "0 0 192.0.2.5:443 192.0.2.2:1234 cubic bytes_received:99999",
31 "0 0 [::ffff:192.0.2.5]:443 [::ffff:192.0.2.1]:5678 cubic bytes_received:42000",
32 ])
33 rows = watch.connections(output, {24000}, {"192.0.2.1"})
34 self.assertEqual(len(rows), 3)
35 self.assertEqual(rows[0]["recv_q"], 0)
36 self.assertEqual(rows[0]["send_q"], 100)
37 self.assertEqual(rows[0]["snd_wnd"], "0")
38 self.assertEqual(rows[1]["lastrcv"], "60000")
39 self.assertNotIn("private", json.dumps(rows))
40 self.assertEqual(watch.endpoint("[2001:db8::1]:1234"), ("2001:db8::1", 1234))
41 self.assertEqual(watch.endpoint("[::ffff:192.0.2.1]:1234"), ("192.0.2.1", 1234))
42
43 def test_completion_and_window(self):
44 edge = {"ts": 200, "duration": 60, "content_length": "100", "bytes_read": 40}
45 samples = [{"ts": 130}, {"ts": 140}, {"ts": 195}, {"ts": 210}]
46 record = watch.incident(edge, samples, [])
47 self.assertFalse(record["upload_complete"])
48 self.assertEqual(record["tcp"], [{"ts": 140}, {"ts": 195}])
49 edge["bytes_read"] = 100
50 self.assertTrue(watch.incident(edge, samples, [])["upload_complete"])
51 edge["content_length"] = ""
52 self.assertIsNone(watch.incident(edge, samples, [])["upload_complete"])
53
54 def test_packet_metadata_excludes_payload_and_other_connections(self):
55 row = watch.packet("190.123 lo In IP 192.0.2.1.50000 > 192.0.2.5.443: Flags [R.], seq 10:20, ack 30, win 40, options [sack 1 {50:60}], length 10 private-payload")
56 self.assertEqual(row["flags"], "R.")
57 self.assertEqual(row["sack"], ["50:60"])
58 self.assertNotIn("private", json.dumps(row))
59 mac = watch.packet("190.123 IP 192.0.2.1.50000 > 192.0.2.5.443: Flags [R.], seq 10:20, ack 30, win 40, options [sack 1 {50:60}], length 10")
60 self.assertEqual(mac, row)
61 ipv6 = watch.packet("190.123 IP6 2001:db8::1.50000 > 2001:db8::5.443: Flags [.], ack 30, win 40, length 0")
62 self.assertEqual(ipv6["source"], "[2001:db8::1]:50000")
63 edge = {"ts": 200, "duration": 60, "client": "192.0.2.1:50000"}
64 self.assertEqual(watch.incident(edge, [], [row])["packets"], [row])
65 edge["client"] = "192.0.2.1:50001"
66 self.assertEqual(watch.incident(edge, [], [row])["packets"], [])
67 self.assertIsNone(watch.packet("private application payload"))
68
69 def test_private_atomic_state_survives_reload(self):
70 with tempfile.TemporaryDirectory() as temporary:
71 path = Path(temporary) / "state.json"
72 watch.save(path, {"cursor": "first"})
73 watch.save(path, {"cursor": "second"})
74 self.assertEqual(json.loads(path.read_text()), {"cursor": "second"})
75 self.assertEqual(path.stat().st_mode & 0o777, 0o600)
76 self.assertFalse(path.with_suffix(".pending").exists())
77
78
79if __name__ == "__main__":
80 unittest.main()