| 1 | #!/usr/bin/env python3 |
| 2 | import importlib.util |
| 3 | import json |
| 4 | from pathlib import Path |
| 5 | import tempfile |
| 6 | import unittest |
| 7 | from unittest.mock import patch |
| 8 | |
| 9 | |
| 10 | spec = importlib.util.spec_from_file_location("watch", Path(__file__).with_name("substation-client-watch.py")) |
| 11 | watch = importlib.util.module_from_spec(spec) |
| 12 | spec.loader.exec_module(watch) |
| 13 | |
| 14 | |
| 15 | class ClientEvidence(unittest.TestCase): |
| 16 | def setUp(self): |
| 17 | clock = patch.object(watch.time, "time", return_value=200) |
| 18 | clock.start() |
| 19 | self.addCleanup(clock.stop) |
| 20 | |
| 21 | def test_capture_skips_expired_metadata_and_still_advances(self): |
| 22 | with tempfile.TemporaryDirectory() as temporary: |
| 23 | path = Path(temporary) / "capture.log" |
| 24 | path.write_text("invalid line\n1.0 IP 192.0.2.5.443 > 192.0.2.1.50000: Flags [.], ack 20, win 40, length 0\n") |
| 25 | rows, offset, _ = watch.capture(path, 0) |
| 26 | self.assertEqual(rows, []) |
| 27 | self.assertEqual(offset, path.stat().st_size) |
| 28 | |
| 29 | def test_capture_keeps_partial_lines_and_excludes_payload(self): |
| 30 | with tempfile.TemporaryDirectory() as temporary: |
| 31 | path = Path(temporary) / "capture.log" |
| 32 | line = "190.123 IP 192.0.2.1.50000 > 192.0.2.5.443: Flags [P.], seq 10:20, ack 30, win 40, length 10 private-payload" |
| 33 | self.assertEqual(watch.capture(path, 0), ([], 0, None)) |
| 34 | path.write_text(line) |
| 35 | self.assertEqual(watch.capture(path, 0), ([], 0, None)) |
| 36 | with path.open("a") as writer: |
| 37 | writer.write("\n") |
| 38 | rows, offset, rotated = watch.capture(path, 0) |
| 39 | self.assertEqual(rows[0]["seq"], "10:20") |
| 40 | self.assertNotIn("private", json.dumps(rows)) |
| 41 | self.assertFalse(rotated) |
| 42 | self.assertEqual(watch.capture(path, offset), ([], offset, None)) |
| 43 | |
| 44 | def test_capture_rotation_preserves_the_open_append_writer(self): |
| 45 | with tempfile.TemporaryDirectory() as temporary: |
| 46 | path = Path(temporary) / "capture.log" |
| 47 | with path.open("a") as writer: |
| 48 | writer.truncate(64 * 1024 * 1024) |
| 49 | writer.write("\n") |
| 50 | writer.flush() |
| 51 | _, offset, rotated = watch.capture(path, 64 * 1024 * 1024) |
| 52 | self.assertTrue(rotated) |
| 53 | self.assertEqual(offset, 0) |
| 54 | writer.write("200.0 IP 192.0.2.5.443 > 192.0.2.1.50000: Flags [.], ack 20, win 40, length 0\n") |
| 55 | writer.flush() |
| 56 | rows, _, rotated = watch.capture(path, offset) |
| 57 | self.assertEqual(rows[0]["ack"], "20") |
| 58 | self.assertFalse(rotated) |
| 59 | |
| 60 | def test_capture_rotates_before_parsing_while_the_writer_remains_active(self): |
| 61 | with tempfile.TemporaryDirectory() as temporary: |
| 62 | path = Path(temporary) / "capture.log" |
| 63 | first = "200.0 IP 192.0.2.5.443 > 192.0.2.1.50000: Flags [.], ack 20, win 40, length 0\n" |
| 64 | second = first.replace("200.0", "201.0") |
| 65 | with path.open("a") as writer: |
| 66 | writer.truncate(64 * 1024 * 1024) |
| 67 | writer.write(first) |
| 68 | writer.flush() |
| 69 | original = watch.packet |
| 70 | |
| 71 | def parse(line): |
| 72 | writer.write(second) |
| 73 | writer.flush() |
| 74 | return original(line) |
| 75 | |
| 76 | with patch.object(watch, "packet", side_effect=parse): |
| 77 | rows, offset, rotated = watch.capture(path, 64 * 1024 * 1024) |
| 78 | self.assertTrue(rotated) |
| 79 | self.assertEqual(rows[0]["ts"], 200.0) |
| 80 | rows, _, _ = watch.capture(path, offset) |
| 81 | self.assertEqual(rows[0]["ts"], 201.0) |
| 82 | |
| 83 | def test_socket_counters_and_pids_exclude_other_destinations_and_program_names(self): |
| 84 | text = "\n".join([ |
| 85 | "tcp4 0 1234 192.0.2.1.50000 198.51.100.1.443 ESTABLISHED 10 20 1000 2000 private-name:42 0 0", |
| 86 | "tcp4 0 0 192.0.2.1.50001 198.51.100.2.443 ESTABLISHED 10 20 1000 2000 private-name:43 0 0", |
| 87 | "tcp4 0 0 192.0.2.1.50002 198.51.100.1.443 ESTABLISHED 30 40 1000 2000 The Knife:44 0 0", |
| 88 | ]) |
| 89 | rows = watch.sockets(text, {"198.51.100.1"}) |
| 90 | self.assertEqual(len(rows), 2) |
| 91 | self.assertEqual(rows[0]["send_q"], 1234) |
| 92 | self.assertEqual(rows[0]["pid"], 42) |
| 93 | self.assertEqual(rows[1]["pid"], 44) |
| 94 | self.assertNotIn("private-name", json.dumps(rows)) |
| 95 | |
| 96 | def test_nettop_retransmissions_windows_and_owner(self): |
| 97 | text = "\n".join([ |
| 98 | ",bytes_in,bytes_out,rx_dupe,rx_ooo,re-tx,rtt_avg,rcvsize,tx_win,", |
| 99 | "2.1.322.42,10,20,0,0,0,,,,", |
| 100 | "tcp4 192.0.2.1:50000<->198.51.100.1:443,10,2000000,0,0,123456,20.5 ms,131072,1000000,", |
| 101 | "tcp4 192.0.2.1:50001<->198.51.100.2:443,10,20,0,0,0,2 ms,131072,1000000,", |
| 102 | ]) |
| 103 | rows = watch.counters(text, {"198.51.100.1"}) |
| 104 | self.assertEqual(len(rows), 1) |
| 105 | self.assertEqual(rows[0]["pid"], 42) |
| 106 | self.assertEqual(rows[0]["re-tx"], "123456") |
| 107 | self.assertEqual(rows[0]["tx_win"], "1000000") |
| 108 | self.assertEqual(watch.address("::ffff:198.51.100.1"), "198.51.100.1") |
| 109 | |
| 110 | def test_stall_retains_unacknowledged_upload_despite_new_buffered_bytes(self): |
| 111 | row = {"local": "192.0.2.1.50000", "peer": "198.51.100.1.443", "pid": 42, "send_q": 100, "bytes_sent": 200} |
| 112 | samples = [{"ts": 0, "connections": [row]}, |
| 113 | {"ts": 5, "connections": [{**row, "send_q": 150, "bytes_sent": 250}]}, |
| 114 | {"ts": 10, "connections": [{**row, "send_q": 200, "bytes_sent": 300}]}] |
| 115 | self.assertEqual(watch.stalls(samples), {(row["local"], row["peer"], 42)}) |
| 116 | self.assertEqual(watch.stalls(samples[:-1]), set()) |
| 117 | samples[-1]["connections"][0]["send_q"] = 190 |
| 118 | self.assertEqual(watch.stalls(samples), set()) |
| 119 | |
| 120 | def test_idle_socket_and_reused_connection_do_not_count_as_stalls(self): |
| 121 | row = {"local": "192.0.2.1.50000", "peer": "198.51.100.1.443", "pid": 42, "send_q": 0, "bytes_sent": 200} |
| 122 | samples = [{"ts": 0, "connections": [row]}, {"ts": 10, "connections": [row]}] |
| 123 | self.assertEqual(watch.stalls(samples), set()) |
| 124 | samples[0]["connections"] = [{**row, "send_q": 100, "pid": 41}] |
| 125 | samples[-1]["connections"] = [{**row, "send_q": 100}] |
| 126 | self.assertEqual(watch.stalls(samples), set()) |
| 127 | samples.insert(1, {"ts": 5, "connections": []}) |
| 128 | self.assertEqual(watch.stalls(samples), set()) |
| 129 | |
| 130 | |
| 131 | if __name__ == "__main__": |
| 132 | unittest.main() |