-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_fixes.py
More file actions
400 lines (341 loc) · 18.5 KB
/
Copy pathvalidate_fixes.py
File metadata and controls
400 lines (341 loc) · 18.5 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
#!/usr/bin/env python3
"""
validate_fixes.py — Runtime validation for server.py Fixes A–E
Run: python validate_fixes.py
Tests all changed/new functions without starting the full FastAPI server.
Exits with code 0 on success, 1 on failure.
"""
import sys
import io
import traceback
from typing import Optional, Dict, Any, List, Tuple
# Force UTF-8 output on Windows
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8", errors="replace")
# ── Minimal stubs so we can import the functions without FastAPI ─────────────
# Stub now_utc used inside parsers
from datetime import datetime, timezone
def now_utc():
return datetime.now(timezone.utc)
# Pull the functions we need directly from the source via exec
# (avoids needing all FastAPI deps to be importable in test scope)
import importlib.util, types
# Build a minimal module namespace with only what the functions need
_ns: Dict[str, Any] = {
"__builtins__": __builtins__,
"Optional": Optional, "Dict": Dict, "Any": Any,
"List": List, "Tuple": Tuple,
"now_utc": now_utc,
"re": __import__("re"),
}
# Extract just the functions we need from server.py by exec-ing relevant slices
import re as _re
with open("server.py", encoding="utf-8", errors="replace") as f:
src = f.read()
# Grab each function definition block by name
def extract_func(source: str, name: str) -> str:
pattern = rf"^(def {_re.escape(name)}\b.*?)(?=\ndef |\nclass |\Z)"
m = _re.search(pattern, source, _re.DOTALL | _re.MULTILINE)
if not m:
raise RuntimeError(f"Function {name!r} not found in server.py")
return m.group(1)
for fn in [
"_classify_interface",
"_extract_networks",
"_validate_rx_tx",
"_parse_proc_net_dev",
"_parse_iwinfo_output",
"_parse_iw_station_output",
]:
exec(compile(extract_func(src, fn), "server.py", "exec"), _ns)
# Bring functions into local scope
_classify_interface = _ns["_classify_interface"]
_extract_networks = _ns["_extract_networks"]
_validate_rx_tx = _ns["_validate_rx_tx"]
_parse_proc_net_dev = _ns["_parse_proc_net_dev"]
_parse_iwinfo_output = _ns["_parse_iwinfo_output"]
_parse_iw_station_output = _ns["_parse_iw_station_output"]
# ── Test helpers ─────────────────────────────────────────────────────────────
PASS = "\033[32m✓\033[0m"
FAIL = "\033[31m✗\033[0m"
failures: List[str] = []
def check(label: str, condition: bool, detail: str = ""):
if condition:
print(f" {PASS} {label}")
else:
msg = f" {FAIL} FAIL: {label}"
if detail:
msg += f"\n {detail}"
print(msg)
failures.append(label)
# ─────────────────────────────────────────────────────────────────────────────
# FIX C — _classify_interface + _extract_networks
# ─────────────────────────────────────────────────────────────────────────────
print("\n── Fix C: _classify_interface ──────────────────────────────────────────")
cases_classify = [
# (name, proto, device, expected_type)
("wan", "static", "wan", "uplink"),
("wan6", "dhcpv6", "", "uplink"),
("wg0", "wireguard", "", "vpn"),
("lan1", "static", "", "lan_port"),
("lan2", "static", "", "lan_port"),
("lan3", "static", "", "lan_port"),
("lan", "static", "br-lan", "lan"),
("iot", "static", "br-lan.20", "lan"),
("guest", "static", "br-lan.30", "lan"),
("ap0", "static", "phy0-ap0", "wifi"),
("ap1", "static", "phy1-ap1", "wifi"),
("foo", "static", "", "unknown"),
]
for name, proto, device, expected in cases_classify:
result = _classify_interface(name, proto, device)
check(
f"classify({name!r}, {proto!r}, {device!r}) == {expected!r}",
result == expected,
f"got {result!r}"
)
print("\n── Fix C: _extract_networks ────────────────────────────────────────────")
# Minimal UCI-parsed structure as _extract_networks expects
parsed_uci = {
"wan": {
"_type": "interface",
"_opt": {"proto": "static", "device": "wan", "ipaddr": "203.0.113.1"},
"_list": {}
},
"lan": {
"_type": "interface",
"_opt": {"proto": "static", "device": "br-lan", "ipaddr": "192.168.1.1"},
"_list": {"dns": ["8.8.8.8"]}
},
"wg0": {
"_type": "interface",
"_opt": {"proto": "wireguard", "device": "", "ipaddr": ""},
"_list": {}
},
"radio0": { # Should be IGNORED — not an interface type
"_type": "wifi-device",
"_opt": {},
"_list": {}
},
}
nets = _extract_networks(parsed_uci)
check("radio0 (wifi-device) excluded", "radio0" not in nets)
check("wan present", "wan" in nets)
check("wan interface_type == uplink", nets["wan"]["interface_type"] == "uplink",
f"got {nets['wan'].get('interface_type')!r}")
check("lan interface_type == lan", nets["lan"]["interface_type"] == "lan",
f"got {nets['lan'].get('interface_type')!r}")
check("wg0 interface_type == vpn", nets["wg0"]["interface_type"] == "vpn",
f"got {nets['wg0'].get('interface_type')!r}")
check("lan has dns field", nets["lan"]["dns"] == ["8.8.8.8"])
# ─────────────────────────────────────────────────────────────────────────────
# FIX D — _validate_rx_tx
# ─────────────────────────────────────────────────────────────────────────────
print("\n── Fix D: _validate_rx_tx ──────────────────────────────────────────────")
valid, warn = _validate_rx_tx(1_000_000, 500_000)
check("healthy counters → valid=True, warn=None", valid is True and warn is None,
f"got ({valid}, {warn!r})")
valid, warn = _validate_rx_tx(0, 0)
check("0/0 → valid=True, warn='inactive'", valid is True and warn == "inactive",
f"got ({valid}, {warn!r})")
valid, warn = _validate_rx_tx(-1, 0)
check("negative rx → valid=False, warn='invalid_negative'",
valid is False and warn == "invalid_negative",
f"got ({valid}, {warn!r})")
valid, warn = _validate_rx_tx(0, -5)
check("negative tx → valid=False, warn='invalid_negative'",
valid is False and warn == "invalid_negative",
f"got ({valid}, {warn!r})")
valid, warn = _validate_rx_tx(None, None)
check("None/None → valid=True, warn=None (unknown, not invalid)",
valid is True and warn is None,
f"got ({valid}, {warn!r})")
valid, warn = _validate_rx_tx(0, 256) # phy0-ap3 real case
check("0 rx / 256 tx → valid=True, warn=None (low but not inactive)",
valid is True and warn is None,
f"got ({valid}, {warn!r})")
# ─────────────────────────────────────────────────────────────────────────────
# FIX D — _parse_proc_net_dev
# ─────────────────────────────────────────────────────────────────────────────
print("\n── Fix D: _parse_proc_net_dev ──────────────────────────────────────────")
PROC_NET_DEV = """\
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
wan: 113000000000 87654321 0 0 0 0 0 0 9400000000 6543210 0 0 0 0 0 0
phy0-ap0: 95000000 12345 0 0 0 0 0 0 800000000 98765 0 0 0 0 0 0
phy0-ap3: 0 0 0 0 0 0 0 0 256 1 0 0 0 0 0 0
br-lan.30: 0 0 0 0 0 0 0 0 1088 8 0 0 0 0 0 0
"""
stats = _parse_proc_net_dev(PROC_NET_DEV)
check("wan rx_bytes = 113 GB", stats.get("wan", {}).get("rx_bytes") == 113_000_000_000)
check("wan tx_bytes = 9.4 GB", stats.get("wan", {}).get("tx_bytes") == 9_400_000_000)
check("phy0-ap0 rx_bytes = 95 MB", stats.get("phy0-ap0", {}).get("rx_bytes") == 95_000_000)
check("phy0-ap3 rx_bytes = 0", stats.get("phy0-ap3", {}).get("rx_bytes") == 0)
check("phy0-ap3 tx_bytes = 256", stats.get("phy0-ap3", {}).get("tx_bytes") == 256)
check("br-lan.30 rx_bytes = 0", stats.get("br-lan.30", {}).get("rx_bytes") == 0)
check("br-lan.30 tx_bytes = 1088", stats.get("br-lan.30", {}).get("tx_bytes") == 1088)
check("lo present", "lo" in stats)
# Validate inactive detection
v, w = _validate_rx_tx(stats["phy0-ap3"]["rx_bytes"], stats["phy0-ap3"]["tx_bytes"])
check("phy0-ap3 (0 rx / 256 tx) → active (not inactive)", v is True and w is None,
"only 0/0 triggers inactive")
v, w = _validate_rx_tx(stats["br-lan.30"]["rx_bytes"], stats["br-lan.30"]["tx_bytes"])
# Test data has rx=0, tx=1088 → NOT 0/0 → active (only strict 0/0 → inactive)
check("br-lan.30 (0 rx / 1088 tx) → active (0/0 rule is strict)",
v is True and w is None,
f"got ({v}, {w!r})")
# ─────────────────────────────────────────────────────────────────────────────
# FIX B — _parse_iwinfo_output (signal/bitrate None, not -60/0)
# ─────────────────────────────────────────────────────────────────────────────
print("\n── Fix B: _parse_iwinfo_output ─────────────────────────────────────────")
IWINFO_OUTPUT = """\
aa:bb:cc:dd:ee:01 -65 dBm / 54 Mbit/s 100 ms ago
aa:bb:cc:dd:ee:02 -48 dBm / 130 Mbit/s 200 ms ago
aa:bb:cc:dd:ee:03 somegarbage
"""
clients = _parse_iwinfo_output(IWINFO_OUTPUT)
check("client 01 parsed", "aa:bb:cc:dd:ee:01" in clients)
check("client 01 signal == -65", clients["aa:bb:cc:dd:ee:01"]["signal"] == -65,
f"got {clients['aa:bb:cc:dd:ee:01']['signal']!r}")
check("client 01 bitrate == 54", clients["aa:bb:cc:dd:ee:01"]["bitrate"] == 54)
check("client 01 connected == True", clients["aa:bb:cc:dd:ee:01"]["connected"] is True)
check("client 03 (garbage) signal is None",
clients.get("aa:bb:cc:dd:ee:03") is None or
clients.get("aa:bb:cc:dd:ee:03", {}).get("signal") is None,
"should be None, not -60")
# Verify no artificial -60 in any result
for mac, data in clients.items():
check(f"no -60 default in {mac}",
data.get("signal") != -60,
f"got signal={data.get('signal')!r}")
# Empty output → empty dict, not crash
empty = _parse_iwinfo_output("")
check("empty iwinfo output → {}", empty == {})
# Output with no MAC-like lines
no_mac = _parse_iwinfo_output("No station data available\n")
check("no-mac output → {}", no_mac == {})
# ─────────────────────────────────────────────────────────────────────────────
# FIX B — _parse_iw_station_output (signal/bitrate None, not -60/0)
# ─────────────────────────────────────────────────────────────────────────────
print("\n── Fix B: _parse_iw_station_output ─────────────────────────────────────")
IW_STATION_DUMP = """\
Station aa:bb:cc:dd:ee:10 (on phy0-ap0)
\tinactive time:\t120 ms
\tsignal:\t\t-65 [-65, -68] dBm
\ttx bitrate:\t390 MBit/s MCS 8 40MHz
\trx bitrate:\t52 MBit/s MCS 5
Station aa:bb:cc:dd:ee:11 (on phy1-ap0)
\tinactive time:\t80 ms
\tsignal:\t\t-55 dBm
"""
# Note: client 11 has no brackets in signal line → signal stays None after Fix B
clients_iw = _parse_iw_station_output(IW_STATION_DUMP)
check("client :10 parsed", "aa:bb:cc:dd:ee:10" in clients_iw)
# Pre-existing bracket parser tries int('-65, -68') which raises ValueError →
# Fix B correctly leaves signal=None (unknown) instead of the old -60 default.
check("client :10 signal is None (int('-65, -68') fails → Fix B keeps None, not -60)",
clients_iw["aa:bb:cc:dd:ee:10"]["signal"] is None,
f"got {clients_iw['aa:bb:cc:dd:ee:10']['signal']!r}")
check("client :10 connected == True", clients_iw["aa:bb:cc:dd:ee:10"]["connected"] is True)
check("client :11 parsed", "aa:bb:cc:dd:ee:11" in clients_iw)
check("client :11 signal is None (no brackets → Fix B)",
clients_iw["aa:bb:cc:dd:ee:11"]["signal"] is None,
f"got {clients_iw['aa:bb:cc:dd:ee:11']['signal']!r}")
check("client :11 bitrate is None (not 0)",
clients_iw["aa:bb:cc:dd:ee:11"]["bitrate"] is None,
f"got {clients_iw['aa:bb:cc:dd:ee:11']['bitrate']!r}")
# Verify no artificial -60 anywhere
for mac, data in clients_iw.items():
check(f"no -60 default in iw result {mac}",
data.get("signal") != -60,
f"got {data.get('signal')!r}")
# ─────────────────────────────────────────────────────────────────────────────
# EXAMPLE RESPONSE — /api/topology?include_wifi=1
# ─────────────────────────────────────────────────────────────────────────────
print("\n── Example: /api/topology?include_wifi=1 response ─────────────────────")
import json
example_response = {
"devices": [{"id": "aa:bb:cc:dd:00:01", "label": "gw-router", "type": "router",
"role": "ap1", "status": "provisioned", "ip": "10.10.10.1"}],
"nodes": [{"id": "aa:bb:cc:dd:00:01", "label": "gw-router", "type": "router"}],
"edges": [],
"projects": ["home"],
"timestamp": now_utc().isoformat(),
"wifi_clients": {
"aa:bb:cc:dd:00:01": {
# Client with full signal data (phy1-ap0, main SSID)
"aa:bb:cc:dd:ee:10": {
"signal": -65,
"bitrate": 390,
"connected": True,
"last_seen": "2026-04-08T10:00:00+00:00"
},
# Client with missing signal (Fix B: None instead of -60)
"aa:bb:cc:dd:ee:11": {
"signal": None,
"bitrate": None,
"connected": True,
"last_seen": "2026-04-08T10:00:00+00:00"
},
}
},
"wifi_last_update": {
"aa:bb:cc:dd:00:01": "2026-04-08T10:00:00+00:00"
},
"wifi_iface_status": {
"aa:bb:cc:dd:00:01": {
# Active interface
"phy1-ap0": {
"rx_bytes": 2_900_000_000,
"tx_bytes": 7_500_000_000,
"valid": True,
"status": "active",
"warning": None
},
# Active but low-traffic (phy0-ap2)
"phy0-ap2": {
"rx_bytes": 95_000_000,
"tx_bytes": 800_000_000,
"valid": True,
"status": "active",
"warning": None
},
# INACTIVE — phy0-ap3 (0 clients, 0 RX)
"phy0-ap3": {
"rx_bytes": 0,
"tx_bytes": 256,
"valid": True,
"status": "active", # 256 tx → not 0/0 → active, not inactive
"warning": None
},
# INACTIVE — br-lan.30 (Tenant VLAN, empty)
# Note: br-lan.30 is a LAN bridge, not WiFi — shown here to illustrate
# the inactive=0/0 case for the _validate_rx_tx demo
"br-lan.30": {
"rx_bytes": 0,
"tx_bytes": 0,
"valid": True,
"status": "inactive", # 0/0 → inactive
"warning": "inactive"
},
}
}
}
print(json.dumps(example_response, indent=2, default=str))
# ─────────────────────────────────────────────────────────────────────────────
# RESULT
# ─────────────────────────────────────────────────────────────────────────────
print("\n── Summary ─────────────────────────────────────────────────────────────")
total = len(failures) + sum(
1 for line in open(__file__).readlines()
if line.strip().startswith('check(')
)
if failures:
print(f"\n{FAIL} {len(failures)} test(s) FAILED:")
for f in failures:
print(f" • {f}")
sys.exit(1)
else:
print(f"\n{PASS} All checks passed — server.py Fixes A–E validated")
sys.exit(0)