Repository navigation
Expand file tree
/
Copy pathtest_svg.py
More file actions
1438 lines (1202 loc) · 56.3 KB
/
Copy pathtest_svg.py
File metadata and controls
1438 lines (1202 loc) · 56.3 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
8000
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for src/retrace/export/svg.py — no ML dependencies."""
from __future__ import annotations
from pathlib import Path
from retrace.core.pipeline import AnalysisResult, Component, Trace
from retrace.export.svg import (
_classify_net,
_edge_point,
_render_zones,
_resolve_image_href,
generate_attack_surface_svg,
generate_bus_topology_svg,
generate_diff_svg,
generate_lineage_svg,
generate_power_tree_svg,
generate_svg,
generate_zones_svg,
save_svg,
)
# ---------------------------------------------------------------------------
# Helpers / fixtures
# ---------------------------------------------------------------------------
def _make_result(
components: list[Component] | None = None,
traces: list[Trace] | None = None,
board_dimensions: tuple[int, int] = (800, 600),
pipeline_version: str = "0.3.0",
) -> AnalysisResult:
return AnalysisResult(
image_path="test_board.jpg",
components=components or [],
traces=traces or [],
board_dimensions=board_dimensions,
pipeline_version=pipeline_version,
)
def _make_component(
cid: str = "C0001",
label: str = "ic",
bbox: tuple[int, int, int, int] = (10, 20, 50, 40),
part_number: str = "",
marking: str = "",
) -> Component:
return Component(
id=cid,
label=label,
confidence=0.9,
bbox=bbox,
part_number=part_number,
marking=marking,
)
def _make_trace(
tid: str = "T00000001",
points: list[tuple[int, int]] | None = None,
) -> Trace:
return Trace(
id=tid,
points=points or [(0, 0), (100, 100)],
width_px=2.0,
)
# ---------------------------------------------------------------------------
# SVG validity
# ---------------------------------------------------------------------------
class TestSvgValidity:
def test_returns_string(self):
result = _make_result()
svg = generate_svg(result)
assert isinstance(svg, str)
def test_contains_opening_svg_tag(self):
result = _make_result()
svg = generate_svg(result)
assert "<svg" in svg
def test_contains_closing_svg_tag(self):
result = _make_result()
svg = generate_svg(result)
assert "</svg>" in svg
def test_svg_namespace(self):
result = _make_result()
svg = generate_svg(result)
assert 'xmlns="http://www.w3.org/2000/svg"' in svg
def test_svg_has_width_and_height(self):
result = _make_result(board_dimensions=(1024, 768))
svg = generate_svg(result)
assert 'width="1024"' in svg
assert 'height="768"' in svg
def test_explicit_width_height_override(self):
result = _make_result(board_dimensions=(800, 600))
svg = generate_svg(result, width=400, height=300)
assert 'width="400"' in svg
assert 'height="300"' in svg
def test_retrace_comment_present(self):
result = _make_result()
svg = generate_svg(result)
assert "re:trace" in svg
def test_legend_present(self):
result = _make_result()
svg = generate_svg(result)
assert 'class="legend"' in svg
# ---------------------------------------------------------------------------
# Component bounding boxes
# ---------------------------------------------------------------------------
class TestComponentAnnotations:
def test_rect_element_present_for_component(self):
comp = _make_component(bbox=(10, 20, 50, 40))
result = _make_result(components=[comp])
svg = generate_svg(result)
assert "<rect" in svg
def test_bbox_coords_in_svg(self):
comp = _make_component(cid="U1", label="ic", bbox=(15, 25, 60, 45))
result = _make_result(components=[comp])
svg = generate_svg(result)
assert 'x="15"' in svg
assert 'y="25"' in s
8000
vg
assert 'width="60"' in svg
assert 'height="45"' in svg
def test_component_group_element(self):
comp = _make_component()
result = _make_result(components=[comp])
svg = generate_svg(result)
assert 'class="component"' in svg
def test_data_id_attribute(self):
comp = _make_component(cid="U99")
result = _make_result(components=[comp])
svg = generate_svg(result)
assert 'data-id="U99"' in svg
def test_data_label_attribute(self):
comp = _make_component(label="capacitor")
result = _make_result(components=[comp])
svg = generate_svg(result)
assert 'data-label="capacitor"' in svg
def test_part_number_shown_in_text(self):
comp = _make_component(part_number="LM1117")
result = _make_result(components=[comp])
svg = generate_svg(result)
assert "LM1117" in svg
def test_marking_shown_when_no_part_number(self):
comp = _make_component(marking="47R", part_number="")
result = _make_result(components=[comp])
svg = generate_svg(result)
assert "47R" in svg
def test_label_shown_as_fallback(self):
comp = _make_component(label="resistor", marking="", part_number="")
result = _make_result(components=[comp])
svg = generate_svg(result)
assert "resistor" in svg
def test_label_text_truncated_at_20_chars(self):
long_label = "A" * 30
comp = _make_component(part_number=long_label)
result = _make_result(components=[comp])
svg = generate_svg(result)
# Only first 20 chars should appear
assert "A" * 20 in svg
assert "A" * 21 not in svg
def test_multiple_components_produce_multiple_rects(self):
comps = [
_make_component("C1", "capacitor", (10, 10, 20, 20)),
_make_component("C2", "resistor", (100, 100, 30, 15)),
_make_component("C3", "ic", (200, 50, 80, 60)),
]
result = _make_result(components=comps)
svg = generate_svg(result)
rect_count = svg.count("<rect")
# At least 3 component rects (plus legend rects)
assert rect_count >= 3
def test_special_chars_in_id_are_escaped(self):
comp = _make_component(cid='U<1>&"2"')
result = _make_result(components=[comp])
svg = generate_svg(result)
# Raw unescaped chars should not appear in data-id
assert '<1>' not in svg
# ---------------------------------------------------------------------------
# Trace paths
# ---------------------------------------------------------------------------
class TestTracePaths:
def test_footer_shows_trace_count(self):
trace = _make_trace(points=[(0, 0), (50, 50), (100, 100)])
result = _make_result(traces=[trace])
svg = generate_svg(result)
assert "1 traces" in svg
def test_no_crash_with_traces_in_result(self):
traces = [_make_trace(f"T{i:08d}") for i in range(5)]
result = _make_result(traces=traces)
svg = generate_svg(result)
assert "<svg" in svg and "</svg>" in svg
def test_summary_footer_counts_match(self):
comps = [_make_component("C1"), _make_component("C2", part_number="LM7805")]
result = _make_result(components=comps)
svg = generate_svg(result)
assert "2 components" in svg
assert "1 identified" in svg
def test_trace_polyline_rendered(self):
trace = _make_trace(points=[(10, 20), (50, 60), (100, 80)])
result = _make_result(traces=[trace])
svg = generate_svg(result)
assert "<polyline" in svg
assert "10,20" in svg
def test_trace_group_element(self):
trace = _make_trace()
result = _make_result(traces=[trace])
svg = generate_svg(result)
assert 'class="traces"' in svg
assert 'class="trace"' in svg
def test_pin_dots_rendered(self):
trace = _make_trace(points=[(10, 20), (100, 100)])
result = _make_result(traces=[trace])
svg = generate_svg(result)
assert svg.count("<circle") >= 2
def test_trace_with_component_endpoints(self):
c1 = _make_component("U1", "ic", (10, 10, 60, 60), marking="VRM")
c2 = _make_component("U2", "ic", (200, 200, 60, 60))
trace = Trace(id="T001", points=[(40, 40), (230, 230)], width_px=3.0,
from_component="U1", to_component="U2")
result = _make_result(components=[c1, c2], traces=[trace])
svg = generate_svg(result)
assert 'data-net="' in svg
def test_show_traces_false_hides_traces(self):
trace = _make_trace()
result = _make_result(traces=[trace])
svg = generate_svg(result, show_traces=False)
assert "<polyline" not in svg
assert 'class="traces"' not in svg
def test_net_type_power_classified(self):
vrm = _make_component("U10", "ic", (10, 10, 50, 50), marking="TPS51611")
cpu = _make_component("U1", "ic", (200, 200, 50, 50))
trace = Trace(id="T001", points=[(35, 35), (225, 225)], width_px=5.0,
from_component="U10", to_component="U1")
result = _make_result(components=[vrm, cpu], traces=[trace])
svg = generate_svg(result)
assert 'data-net="power"' in svg
def test_net_type_debug_classified(self):
jtag = _make_component("J5", "connector", (10, 10, 50, 50), marking="JTAG")
cpu = _make_component("U1", "ic", (200, 200, 50, 50))
trace = Trace(id="T001", points=[(35, 35), (225, 225)], width_px=2.0,
from_component="J5", to_component="U1")
result = _make_result(components=[jtag, cpu], traces=[trace])
svg = generate_svg(result)
assert 'data-net="debug"' in svg
def test_footer_shows_connection_count(self):
c1 = _make_component("U1", "ic", (10, 10, 50, 50))
c2 = _make_component("U2", "ic", (200, 200, 50, 50))
trace = Trace(id="T001", points=[(35, 35), (225, 225)], width_px=2.0,
from_component="U1", to_component="U2")
result = _make_result(components=[c1, c2], traces=[trace])
svg = generate_svg(result)
assert "1 connections" in svg
def test_trace_without_points_uses_component_edges(self):
c1 = _make_component("U1", "ic", (10, 10, 50, 50))
c2 = _make_component("U2", "ic", (200, 200, 50, 50))
trace = Trace(id="T001", points=[], width_px=2.0,
from_component="U1", to_component="U2")
result = _make_result(components=[c1, c2], traces=[trace])
svg = generate_svg(result)
assert "<line" in svg
# ---------------------------------------------------------------------------
# BOM panel
# ---------------------------------------------------------------------------
class TestBomPanel:
def test_bom_panel_rendered(self):
comps = [_make_component("U1", "ic"), _make_component("C1", "capacitor")]
result = _make_result(components=comps)
svg = generate_svg(result)
assert 'class="bom-panel"' in svg
assert "Bill of Materials" in svg
def test_bom_shows_component_types(self):
comps = [
_make_component("U1", "ic"),
_make_component("U2", "ic"),
_make_component("C1", "capacitor"),
]
result = _make_result(components=comps)
svg = generate_svg(result)
assert "ic: 2" in svg
assert "capacitor: 1" in svg
def test_bom_shows_identified_count(self):
comps = [
_make_component("U1", "ic", part_number="LM7805"),
_make_component("U2", "ic"),
]
result = _make_result(components=comps)
svg = generate_svg(result)
assert "1 ID'd" in svg
def test_bom_shows_totals(self):
comps = [_make_component("U1"), _make_component("U2"), _make_component("C1", "capacitor")]
result = _make_result(components=comps)
svg = generate_svg(result)
assert "3 parts" in svg
def test_show_bom_false_hides_panel(self):
comps = [_make_component("U1")]
result = _make_result(components=comps)
svg = generate_svg(result, show_bom=False)
assert 'class="bom-panel"' not in svg
def test_bom_not_shown_for_empty_result(self):
result = _make_result()
svg = generate_svg(result)
assert 'class="bom-panel"' not in svg
# ---------------------------------------------------------------------------
# Net legend
# ---------------------------------------------------------------------------
class TestNetLegend:
def test_net_legend_shows_net_types(self):
result = _make_result()
svg = generate_svg(result)
assert "power" in svg
assert "ground" in svg
assert "signal" in svg
assert "debug" in svg
def test_net_legend_has_line_elements(self):
result = _make_result()
svg = generate_svg(result)
assert "nets:" in svg
# ---------------------------------------------------------------------------
# Empty AnalysisResult
# ---------------------------------------------------------------------------
class TestEmptyResult:
def test_empty_result_returns_valid_svg(self):
result = _make_result()
svg = generate_svg(result)
assert "<svg" in svg
assert "</svg>" in svg
def test_empty_result_default_dimensions(self):
result = AnalysisResult(image_path="x.jpg", board_dimensions=(0, 0))
svg = generate_svg(result)
# Falls back to 800×600
assert 'width="800"' in svg
assert 'height="600"' in svg
def test_empty_result_zero_component_footer(self):
result = _make_result()
svg = generate_svg(result)
assert "0 components" in svg
def test_image_href_embedded(self):
# Non-existent local path is skipped entirely (SVG stays self-contained)
result = _make_result()
svg = generate_svg(result, image_href="/path/to/board.jpg")
assert "<image" not in svg
assert "/path/to/board.jpg" not in svg
def test_no_image_href_no_image_element(self):
result = _make_result()
svg = generate_svg(result, image_href=None)
assert "<image" not in svg
# ---------------------------------------------------------------------------
# Security
# ---------------------------------------------------------------------------
class TestSvgSecurity:
def test_bom_label_xss_escaped(self):
comp = _make_component("X1", '<script>alert(1)</script>')
result = _make_result(components=[comp])
svg = generate_svg(result)
assert "<script>" not in svg
assert "<script>" in svg
def test_component_id_xss_escaped(self):
comp = _make_component(cid='"><script>alert(1)</script>')
result = _make_result(components=[comp])
svg = generate_svg(result)
assert "<script>" not in svg
def test_marking_xss_escaped(self):
comp = _make_component(marking='</text><script>x</script>')
result = _make_result(components=[comp])
svg = generate_svg(result)
assert "<script>" not in svg
def test_javascript_uri_rejected(self):
result = _make_result()
svg = generate_svg(result, image_href="javascript:alert(1)")
assert "<image" not in svg
def test_javascript_uri_case_insensitive(self):
result = _make_result()
svg = generate_svg(result, image_href=" JavaScript:alert(1)")
assert "<image" not in svg
def test_valid_image_href_allowed(self):
result = _make_result()
svg = generate_svg(result, image_href="data:image/png;base64,abc")
assert "<image" in svg
def test_file_path_href_nonexistent_skipped(self):
# Non-existent local file paths are silently dropped so SVG is self-contained
result = _make_result()
svg = generate_svg(result, image_href="/path/to/image.jpg")
assert "<image" not in svg
# ---------------------------------------------------------------------------
# Dark theme & visual features
# ---------------------------------------------------------------------------
class TestDarkTheme:
def test_dark_background_present(self):
result = _make_result()
svg = generate_svg(result)
assert 'fill="#0a0e1a"' in svg
def test_grid_pattern_defined(self):
result = _make_result()
svg = generate_svg(result)
assert 'id="grid"' in svg
assert 'url(#grid)' in svg
def test_glow_filter_defined(self):
result = _make_result()
svg = generate_svg(result)
assert 'id="glow-red"' in svg
assert 'id="glow-purple"' in svg
def test_arrow_marker_defined(self):
result = _make_result()
svg = generate_svg(result)
assert 'id="arrow"' in svg
def test_title_bar_present(self):
result = _make_result()
svg = generate_svg(result)
assert 'class="title-bar"' in svg
def test_title_bar_shows_custom_title(self):
result = _make_result()
svg = generate_svg(result, title="Xbox One X Motherboard")
assert "Xbox One X Motherboard" in svg
def test_title_bar_default_text(self):
result = _make_result()
svg = generate_svg(result)
assert "re:trace" in svg
def test_footer_bar_present(self):
result = _make_result()
svg = generate_svg(result)
assert 'class="footer"' in svg
def test_component_id_inside_box(self):
comp = _make_component(cid="U42", label="ic", bbox=(100, 100, 80, 60))
result = _make_result(components=[comp])
svg = generate_svg(result)
assert "U42" in svg
def test_legend_has_panel_background(self):
result = _make_result()
svg = generate_svg(result)
lines = svg.split('\n')
in_legend = False
has_panel_rect = False
for line in lines:
if 'class="legend"' in line:
in_legend = True
if in_legend and '<rect' in line and 'fill="#111827"' in line:
has_panel_rect = True
break
assert has_panel_rect
class TestSecurityPanel:
def test_jtag_component_gets_glow(self):
jtag = _make_component("J5", "connector", (10, 10, 50, 50), marking="JTAG")
result = _make_result(components=[jtag])
svg = generate_svg(result)
assert 'filter="url(#glow-red)"' in svg
def test_security_panel_rendered_for_jtag(self):
jtag = _make_component("J5", "connector", (10, 10, 50, 50), marking="JTAG")
result = _make_result(components=[jtag])
svg = generate_svg(result)
assert 'class="security-panel"' in svg
assert "Security Findings" in svg
def test_security_panel_shows_cwe(self):
jtag = _make_component("J5", "connector", (10, 10, 50, 50), marking="JTAG")
result = _make_result(components=[jtag])
svg = generate_svg(result)
assert "CWE-1191" in svg
def test_no_security_panel_for_normal_components(self):
comp = _make_component("C1", "capacitor", (10, 10, 50, 50), marking="100nF")
result = _make_result(components=[comp])
svg = generate_svg(result)
assert 'class="security-panel"' not in svg
def test_uart_console_detected(self):
uart = _make_component("J10", "connector", (10, 10, 50, 50), marking="CONSOLE")
result = _make_result(components=[uart])
svg = generate_svg(result)
assert 'class="security-panel"' in svg
assert "CWE-1299" in svg
def test_security_badge_on_component(self):
jtag = _make_component("J5", "connector", (10, 10, 50, 50), marking="JTAG")
result = _make_result(components=[jtag])
svg = generate_svg(result)
assert 'fill="#ef4444" fill-opacity="0.9"' in svg
class TestZoneOverlays:
def test_zones_rendered_when_provided(self):
c1 = _make_component("U1", "ic", (100, 100, 80, 60))
c2 = _make_component("U2", "ic", (200, 100, 80, 60))
result = _make_result(components=[c1, c2])
zones = [("CPU Complex", "cpu", ["U1", "U2"])]
svg = generate_svg(result, zones=zones)
assert 'class="zones"' in svg
assert 'data-zone="cpu"' in svg
assert "CPU COMPLEX" in svg
def test_no_zones_no_zone_group(self):
result = _make_result(components=[_make_component()])
svg = generate_svg(result)
assert 'class="zones"' not in svg
def test_empty_zones_list_no_zone_group(self):
result = _make_result(components=[_make_component()])
svg = generate_svg(result, zones=[])
assert 'class="zones"' not in svg
def test_zone_with_missing_components_skipped(self):
c1 = _make_component("U1", "ic", (100, 100, 80, 60))
result = _make_result(components=[c1])
zones = [("Ghost Zone", "io", ["NONEXISTENT"])]
svg = generate_svg(result, zones=zones)
assert "GHOST ZONE" not in svg
def test_zone_with_partial_components(self):
c1 = _make_component("U1", "ic", (100, 100, 80, 60))
result = _make_result(components=[c1])
zones = [("Partial", "memory", ["U1", "MISSING"])]
svg = generate_svg(result, zones=zones)
assert 'data-zone="memory"' in svg
def test_multiple_zones_rendered(self):
c1 = _make_component("U1", "ic", (50, 50, 80, 60))
c2 = _make_component("U2", "ic", (200, 200, 80, 60))
c3 = _make_component("J1", "connector", (400, 400, 60, 40))
result = _make_result(components=[c1, c2, c3])
zones = [
("CPU", "cpu", ["U1"]),
("Memory", "memory", ["U2"]),
("I/O", "io", ["J1"]),
]
svg = generate_svg(result, zones=zones)
assert 'data-zone="cpu"' in svg
assert 'data-zone="memory"' in svg
assert 'data-zone="io"' in svg
def test_zone_uses_correct_color(self):
c1 = _make_component("U1", "ic", (100, 100, 80, 60))
result = _make_result(components=[c1])
zones = [("Power", "power", ["U1"])]
svg = generate_svg(result, zones=zones)
assert "#f59e0b" in svg
def test_zone_xss_escaped(self):
c1 = _make_component("U1", "ic", (100, 100, 80, 60))
result = _make_result(components=[c1])
zones = [('<script>alert(1)</script>', "cpu", ["U1"])]
svg = generate_svg(result, zones=zones)
assert "<script>" not in svg
assert "<SCRIPT>" in svg
def test_zone_dashed_border(self):
c1 = _make_component("U1", "ic", (100, 100, 80, 60))
result = _make_result(components=[c1])
zones = [("Test", "cpu", ["U1"])]
svg = generate_svg(result, zones=zones)
assert 'stroke-dasharray="6,4"' in svg
def test_zones_rendered_before_traces(self):
c1 = _make_component("U1", "ic", (10, 10, 50, 50))
c2 = _make_component("U2", "ic", (200, 200, 50, 50))
trace = Trace(id="T001", points=[(35, 35), (225, 225)], width_px=2.0,
from_component="U1", to_component="U2")
result = _make_result(components=[c1, c2], traces=[trace])
zones = [("CPU", "cpu", ["U1"])]
svg = generate_svg(result, zones=zones)
zone_pos = svg.index('class="zones"')
trace_pos = svg.index('class="traces"')
assert zone_pos < trace_pos
class TestTraceEnhancements:
def test_debug_trace_has_dash(self):
jtag = _make_component("J5", "connector", (10, 10, 50, 50), marking="JTAG")
cpu = _make_component("U1", "ic", (200, 200, 50, 50))
trace = Trace(id="T001", points=[(35, 35), (225, 225)], width_px=2.0,
from_component="J5", to_component="U1")
result = _make_result(components=[jtag, cpu], traces=[trace])
svg = generate_svg(result)
assert 'stroke-dasharray="6,3"' in svg
def test_connected_trace_has_arrow(self):
c1 = _make_component("U1", "ic", (10, 10, 50, 50))
c2 = _make_component("U2", "ic", (200, 200, 50, 50))
trace = Trace(id="T001", points=[(35, 35), (225, 225)], width_px=2.0,
from_component="U1", to_component="U2")
result = _make_result(components=[c1, c2], traces=[trace])
svg = generate_svg(result)
assert 'marker-end="url(#arrow)"' in svg
def test_net_label_at_midpoint(self):
c1 = _make_component("U1", "ic", (10, 10, 50, 50))
c2 = _make_component("U2", "ic", (200, 200, 50, 50))
trace = Trace(id="T001", points=[(35, 35), (120, 120), (225, 225)], width_px=2.0,
from_component="U1", to_component="U2")
result = _make_result(components=[c1, c2], traces=[trace])
svg = generate_svg(result)
assert "SIGNAL" in svg
def test_power_trace_has_glow(self):
vrm = _make_component("U10", "ic", (10, 10, 50, 50), marking="TPS51611")
cpu = _make_component("U1", "ic", (200, 200, 50, 50))
trace = Trace(id="T001", points=[(35, 35), (225, 225)], width_px=5.0,
from_component="U10", to_component="U1")
result = _make_result(components=[vrm, cpu], traces=[trace])
svg = generate_svg(result)
assert 'filter="url(#glow-red)"' in svg
# ---------------------------------------------------------------------------
# save_svg writes to file
# ---------------------------------------------------------------------------
class TestSaveSvg:
def test_save_svg_creates_file(self, tmp_path: Path):
result = _make_result()
output = tmp_path / "output.svg"
save_svg(result, str(output))
assert output.exists()
def test_save_svg_file_not_empty(self, tmp_path: Path):
result = _make_result(components=[_make_component()])
output = tmp_path / "board.svg"
save_svg(result, str(output))
content = output.read_text(encoding="utf-8")
assert len(content) > 0
def test_save_svg_content_matches_generate(self, tmp_path: Path):
result = _make_result(components=[_make_component()])
output = tmp_path / "board.svg"
save_svg(result, str(output))
expected = generate_svg(result)
actual = output.read_text(encoding="utf-8")
assert actual == expected
def test_save_svg_overwrites_existing(self, tmp_path: Path):
output = tmp_path / "board.svg"
output.write_text("old content", encoding="utf-8")
result = _make_result()
save_svg(result, str(output))
content = output.read_text(encoding="utf-8")
assert "old content" not in content
assert "<svg" in content
def test_save_svg_passes_kwargs(self, tmp_path: Path):
result = _make_result(board_dimensions=(1920, 1080))
output = tmp_path / "wide.svg"
save_svg(result, str(output), width=640, height=480)
content = output.read_text(encoding="utf-8")
assert 'width="640"' in content
assert 'height="480"' in content
def test_save_svg_utf8_encoding(self, tmp_path: Path):
comp = _make_component(marking="100µF")
result = _make_result(components=[comp])
output = tmp_path / "utf8.svg"
save_svg(result, str(output))
content = output.read_bytes()
# µ is a multi-byte UTF-8 character — just verify file is readable
decoded = content.decode("utf-8")
assert "<svg" in decoded
# ---------------------------------------------------------------------------
# Attack Surface SVG
# ---------------------------------------------------------------------------
class TestAttackSurfaceSvg:
def _security_setup(self):
jtag = _make_component("J5", "connector", (10, 800, 100, 40), marking="JTAG")
cpu = _make_component("U1", "ic", (300, 300, 200, 200))
fpga = _make_component("U6", "ic", (600, 300, 150, 150), marking="XC6SLX45")
spi = _make_component("U7", "ic", (700, 100, 80, 40), marking="W25Q128")
cap = _make_component("C1", "capacitor", (100, 100, 20, 10))
result = _make_result(components=[jtag, cpu, fpga, spi, cap])
attack_paths = [
("J5", "U1", "JTAG debug access"),
("U1", "U6", "CPU to FPGA"),
("U7", "U6", "SPI flash to FPGA"),
]
security_refs = ["J5", "U1", "U6", "U7"]
return result, attack_paths, security_refs
def test_returns_valid_svg(self):
result, paths, refs = self._security_setup()
svg = generate_attack_surface_svg(result, paths, refs)
assert "<svg" in svg
assert "</svg>" in svg
def test_contains_attack_paths_group(self):
result, paths, refs = self._security_setup()
svg = generate_attack_surface_svg(result, paths, refs)
assert "attack-path" in svg or "attack_path" in svg or "Attack Surface" in svg
def test_dimmed_components_present(self):
result, paths, refs = self._security_setup()
svg = generate_attack_surface_svg(result, paths, refs)
assert "opacity" in svg
def test_security_refs_highlighted(self):
result, paths, refs = self._security_setup()
svg = generate_attack_surface_svg(result, paths, refs)
assert "glow-red" in svg
def test_attack_path_labels_present(self):
result, paths, refs = self._security_setup()
svg = generate_attack_surface_svg(result, paths, refs)
assert "JTAG debug access" in svg or "JTAG" in svg
def test_title_shown(self):
result, paths, refs = self._security_setup()
svg = generate_attack_surface_svg(result, paths, refs, title="Cisco ASA Attack Surface")
assert "Cisco ASA Attack Surface" in svg or "Attack Surface" in svg
def test_no_external_refs(self):
import re
result, paths, refs = self._security_setup()
svg = generate_attack_surface_svg(result, paths, refs)
ext = re.findall(r'href="(?!data:|#)', svg)
assert len(ext) == 0
def test_empty_attack_paths(self):
result = _make_result(components=[_make_component()])
svg = generate_attack_surface_svg(result, [], [])
assert "<svg" in svg
assert "</svg>" in svg
def test_missing_component_in_path_no_crash(self):
result = _make_result(components=[_make_component("U1")])
svg = generate_attack_surface_svg(
result, [("U1", "NONEXISTENT", "broken path")], ["U1"]
)
assert "<svg" in svg
def test_custom_dimensions(self):
result, paths, refs = self._security_setup()
svg = generate_attack_surface_svg(result, paths, refs, width=1024, height=768)
assert 'width="1024"' in svg
assert 'height="768"' in svg
# ---------------------------------------------------------------------------
# Zones SVG
# ---------------------------------------------------------------------------
class TestZonesSvg:
def _zones_setup(self):
cpu = _make_component("U1", "ic", (300, 300, 200, 200))
mem1 = _make_component("U2", "ic", (100, 100, 120, 70))
mem2 = _make_component("U3", "ic", (100, 250, 120, 70))
pwr = _make_component("VRM1", "ic", (600, 600, 80, 60))
result = _make_result(components=[cpu, mem1, mem2, pwr])
zones = [
("CPU Complex", "cpu", ["U1"]),
("Memory Bank", "memory", ["U2", "U3"]),
("Power", "power", ["VRM1"]),
]
return result, zones
def test_returns_valid_svg(self):
result, zones = self._zones_setup()
svg = generate_zones_svg(result, zones)
assert "<svg" in svg
assert "</svg>" in svg
def test_zone_names_present(self):
result, zones = self._zones_setup()
svg = generate_zones_svg(result, zones)
assert "CPU" in svg
assert "Memory" in svg or "MEMORY" in svg
assert "Power" in svg or "POWER" in svg
def test_zone_rects_present(self):
result, zones = self._zones_setup()
svg = generate_zones_svg(result, zones)
assert 'class="zone"' in svg or 'data-zone=' in svg
def test_components_dimmed(self):
result, zones = self._zones_setup()
svg = generate_zones_svg(result, zones)
assert "opacity" in svg
def test_no_trace_clutter(self):
cpu = _make_component("U1", "ic", (300, 300, 200, 200))
trace = Trace(id="T001", points=[(350, 350), (500, 500)], width_px=2.0,
from_component="U1", to_component="U1")
result = _make_result(components=[cpu], traces=[trace])
zones = [("CPU", "cpu", ["U1"])]
svg = generate_zones_svg(result, zones)
assert 'class="traces"' not in svg
def test_title_shown(self):
result, zones = self._zones_setup()
svg = generate_zones_svg(result, zones, title="Xbox Functional Zones")
assert "Xbox Functional Zones" in svg or "Zone Map" in svg
def test_no_external_refs(self):
import re
result, zones = self._zones_setup()
svg = generate_zones_svg(result, zones)
ext = re.findall(r'href="(?!data:|#)', svg)
assert len(ext) == 0
def test_empty_zones(self):
result = _make_result(components=[_make_component()])
svg = generate_zones_svg(result, [])
assert "<svg" in svg
assert "</svg>" in svg
def test_custom_dimensions(self):
result, zones = self._zones_setup()
svg = generate_zones_svg(result, zones, width=1920, height=1080)
assert 'width="1920"' in svg
assert 'height="1080"' in svg
def test_zone_component_count(self):
result, zones = self._zones_setup()
svg = generate_zones_svg(result, zones)
assert "2" in svg # Memory Bank has 2 components
# ---------------------------------------------------------------------------
# _classify_net branch coverage
# ---------------------------------------------------------------------------
class TestClassifyNet:
"""Cover the remaining _classify_net branches (lines 98-107)."""
def _trace(self, width_px: float = 2.0, from_c: str = "", to_c: str = "") -> Trace:
return Trace(id="T001", points=[(0, 0), (100, 100)],
width_px=width_px, from_component=from_c, to_component=to_c)
def _comp(self, cid: str, marking: str = "", label: str = "ic") -> Component:
return Component(id=cid, label=label, confidence=0.9,
bbox=(10, 10, 50, 50), marking=marking)
def test_wide_trace_classified_as_power(self):
trace = self._trace(width_px=6.0)
net = _classify_net(trace, {})
assert net == "power"
def test_width_exactly_5_is_power(self):
trace = self._trace(width_px=5.0)
net = _classify_net(trace, {})
assert net == "power"
def test_inductor_with_width4_is_power(self):
ind = self._comp("L1", marking="inductor", label="inductor")
trace = self._trace(width_px=4.0, from_c="L1")
net = _classify_net(trace, {"L1": ind})
assert net == "power"
def test_clk_marking_classified_as_clock(self):
clk = self._comp("U1", marking="CLK")
trace = self._trace(width_px=1.0, from_c="U1")
net = _classify_net(trace, {"U1": clk})
assert net == "clock"
def test_ddr_marking_classified_as_data(self):
mem = self._comp("U2", marking="DDR4")
trace = self._trace(width_px=1.0, from_c="U2")
net = _classify_net(trace, {"U2": mem})
assert net == "data"
def test_gnd_marking_classified_as_ground(self):
gnd = self._comp("J1", marking="GND")
trace = self._trace(width_px=1.0, from_c="J1")
net = _classify_net(trace, {"J1": gnd})
assert net == "ground"
# ---------------------------------------------------------------------------
# _edge_point branch coverage
# ---------------------------------------------------------------------------
class TestEdgePoint:
"""Cover the degenerate branches of _edge_point (lines 122-135)."""
def _comp(self, x: int, y: int, w: int, h: int) -> Component:
return Component(id="C1", label="ic", confidence=0.9,
bbox=(x, y, w, h))
def test_near_zero_delta_returns_top_center(self):
# dx < 1 and dy < 1 -> returns (cx, y)
comp = self._comp(100, 200, 50, 40)
cx = 100 + 50 // 2 # 125
cy = 200 + 40 // 2 # 220
# Target is effectively the same as center
result = _edge_point(comp, cx, cy)
assert result == (cx, 200) # (cx, y)
def test_zero_size_component_returns_center(self):
# w=0 h=0 -> returns (cx, cy)
comp = self._comp(100, 200, 0, 0)
result = _edge_point(comp, 200, 300)
assert result == (100, 200) # (cx, cy) when w=0 h=0
# ---------------------------------------------------------------------------
# _render_zones direct call coverage
# ---------------------------------------------------------------------------
class TestRenderZonesDirect:
"""Cover _render_zones with empty list (line 536)."""
def test_empty_zones_returns_empty_string(self):
result = _render_zones([], {})
assert result == ""
def test_zone_with_no_valid_members_skipped(self):
# All comp_ids are missing from comp_map -> members is empty -> continue
comp_map = {}
zones = [("Ghost", "cpu", ["MISSING1", "MISSING2"])]
out = _render_zones(zones, comp_map)
# The outer group is created but no zone rect is added
assert "GHOST" not in out
# ---------------------------------------------------------------------------
# _resolve_image_href coverage (lines 1607-1626)
# ---------------------------------------------------------------------------
class TestResolveImageHref:
"""Cover all branches of _resolve_image_href."""
def test_none_returns_none(self):
assert _resolve_image_href(None) is None