Skip to content

Commit 8212f9b

Browse files
authored
Merge pull request #20 from SpecterOps/analysis-work
BED-7147 - Analysis Tooling Work
2 parents e8df0ab + 8d38856 commit 8212f9b

19 files changed

Lines changed: 2093 additions & 433 deletions

algo/scc.go

Lines changed: 263 additions & 98 deletions
Large diffs are not rendered by default.

algo/scc_test.go

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package algo
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/specterops/dawgs/container"
8+
"github.com/specterops/dawgs/graph"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// Test a simple chain of verticies. In this example, each vertex is its own SCC.
13+
//
14+
// Graph:
15+
// 0 -> 1 -> 2 -> 3
16+
func TestSCC_Chain(t *testing.T) {
17+
var (
18+
digraph = container.BuildCSRGraph(map[uint64][]uint64{
19+
0: {1},
20+
1: {2},
21+
2: {3},
22+
})
23+
24+
ctx = context.Background()
25+
sccs, nodeToSCCIndex = StronglyConnectedComponents(ctx, digraph)
26+
expectedNodeToComponent = map[uint64]uint64{
27+
0: 3,
28+
1: 2,
29+
2: 1,
30+
3: 0,
31+
}
32+
)
33+
34+
require.Equalf(t, 4, len(sccs), "expected 4 SCCs, got %d", len(sccs))
35+
36+
// Each SCC must contain exactly one node
37+
for component, members := range sccs {
38+
require.Equalf(t, uint64(1), members.Cardinality(), "SCC %d expected size 1, got %d", component, members.Cardinality())
39+
}
40+
41+
require.Equal(t, expectedNodeToComponent, nodeToSCCIndex)
42+
}
43+
44+
// Test a simple cycle. In this example, there should be two disconnected components.
45+
//
46+
// Graph:
47+
// 0 -> 0
48+
// 1
49+
func TestSCC_SimpleCycle(t *testing.T) {
50+
var (
51+
digraph = container.BuildCSRGraph(map[uint64][]uint64{
52+
0: {0}, // self‑loop component
53+
1: {}, // isolated vertex – must be present as a key!
54+
})
55+
56+
ctx = context.Background()
57+
sccs, nodeToIdx = StronglyConnectedComponents(ctx, digraph)
58+
)
59+
60+
require.Equalf(t, 2, len(sccs), "expected 2 components, got %d", len(sccs))
61+
62+
// The self‑loop should be a component of size 1 (Tarjan treats it as strongly connected)
63+
require.Equalf(t, uint64(1), sccs[nodeToIdx[0]].Cardinality(), "self‑loop node not alone in its component")
64+
require.Equalf(t, uint64(1), sccs[nodeToIdx[1]].Cardinality(), "isolated node not alone in its component")
65+
}
66+
67+
// Test two cycles with a bridge (a “figure‑8” graph). There should be one component that contains
68+
// all verticies.
69+
//
70+
// Graph:
71+
// 0 -> 1 -> 2 -> 3
72+
// 1 -> 3
73+
// 2 -> 3
74+
// 3 -> 1
75+
func TestSCC_FigureEight(t *testing.T) {
76+
var (
77+
digraph = container.BuildCSRGraph(map[uint64][]uint64{
78+
0: {1},
79+
1: {2, 3},
80+
2: {0},
81+
3: {1},
82+
})
83+
84+
ctx = context.Background()
85+
sccs, nodeToIdx = StronglyConnectedComponents(ctx, digraph)
86+
)
87+
88+
// The whole graph is one SCC with 4 members
89+
require.Equalf(t, 1, len(sccs), "expected 1 SCC, got %d", len(sccs))
90+
require.Equalf(t, uint64(4), sccs[0].Cardinality(), "expected component size 4, got %d", sccs[0].Cardinality())
91+
92+
// All nodes must map to component 0.
93+
digraph.EachNode(func(node uint64) bool {
94+
if component, ok := nodeToIdx[node]; !ok || component != 0 {
95+
t.Fatalf("node %d expected in component 0, got %d (exists=%v)", node, component, ok)
96+
}
97+
98+
return true
99+
})
100+
}
101+
102+
// Test component graph construction including edge deduplication and directionality.
103+
//
104+
// Graph:
105+
// 0 → 1 → 2 → 0 (cycle A)
106+
// 3 → 4 → 5 → 3 (cycle B)
107+
// 2 → 3 (bridge from A to B)
108+
func TestComponentGraph_EdgeDeduplication(t *testing.T) {
109+
var (
110+
digraph = container.BuildCSRGraph(map[uint64][]uint64{
111+
0: {1},
112+
1: {2},
113+
2: {0, 3},
114+
3: {4},
115+
4: {5},
116+
5: {3},
117+
})
118+
119+
ctx = context.Background()
120+
componentGraph = NewComponentGraph(ctx, digraph)
121+
)
122+
123+
// Expect exactly two components
124+
if len(componentGraph.componentMembers) != 2 {
125+
t.Fatalf("expected 2 components, got %d", len(componentGraph.componentMembers))
126+
}
127+
128+
// Component IDs are 0 and 1 (order depends on traversal)
129+
var outEdges []uint64
130+
131+
// Verify that the component digraph contains a *single* edge from component A to B
132+
componentGraph.Digraph().EachAdjacentNode(0, graph.DirectionOutbound, func(v uint64) bool {
133+
outEdges = append(outEdges, v)
134+
return true
135+
})
136+
137+
if len(outEdges) == 0 {
138+
// The edge could be reversed depending on which component got ID 0, so test both possibilities
139+
componentGraph.Digraph().EachAdjacentNode(0, graph.DirectionInbound, func(v uint64) bool {
140+
outEdges = append(outEdges, v)
141+
return true
142+
})
143+
}
144+
145+
require.Equalf(t, 1, len(outEdges), "expected exactly one inter‑component edge, got %v", outEdges)
146+
require.Equalf(t, uint64(1), outEdges[0], "expected exactly one inter‑component edge, got %v", outEdges)
147+
148+
var (
149+
startComp, _ = componentGraph.ContainingComponent(0)
150+
endComp, _ = componentGraph.ContainingComponent(5)
151+
)
152+
153+
// The component containing origin vertex 0 should reach component containing origin vertex 5
154+
require.Truef(t, componentGraph.ComponentReachable(startComp, endComp, graph.DirectionOutbound), "expected component %d to reach component %d", startComp, endComp)
155+
}
156+
157+
// Test histogram counting.
158+
//
159+
// Graph:
160+
// 0 -> 1 -> 2 -> 0
161+
// 3 -> 4 -> 5 -> 3
162+
// 6 -> 7 -> 8 -> 6
163+
func TestComponentHistogram(t *testing.T) {
164+
var (
165+
digraph = container.BuildCSRGraph(map[uint64][]uint64{
166+
0: {1},
167+
1: {2},
168+
2: {0},
169+
3: {4},
170+
4: {5},
171+
5: {3},
172+
6: {7},
173+
7: {8},
174+
8: {6},
175+
})
176+
177+
ctx = context.Background()
178+
componentGraph = NewComponentGraph(ctx, digraph)
179+
componentHistogram = componentGraph.ComponentHistogram([]uint64{0, 1, 2, 3, 4, 5, 6, 7, 8})
180+
)
181+
182+
require.Equalf(t, 3, len(componentHistogram), "expected 3 components, got %d", len(componentHistogram))
183+
184+
for _, count := range componentHistogram {
185+
require.Equalf(t, uint64(3), count, "each component should have count 3, got %d", count)
186+
}
187+
}

cardinality/commutative.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cardinality
2+
3+
type CommutativeDuplex64 struct {
4+
duplexes []Duplex[uint64]
5+
}
6+
7+
func NewCommutativeDuplex64() *CommutativeDuplex64 {
8+
return &CommutativeDuplex64{}
9+
}
10+
11+
func (s *CommutativeDuplex64) orAll() Duplex[uint64] {
12+
tempBitmap := NewBitmap64()
13+
14+
for _, nextDuplex := range s.duplexes {
15+
tempBitmap.Or(nextDuplex)
16+
}
17+
18+
return tempBitmap
19+
}
20+
21+
func (s *CommutativeDuplex64) Or(duplex ...Duplex[uint64]) {
22+
s.duplexes = append(s.duplexes, duplex...)
23+
}
24+
25+
func (s *CommutativeDuplex64) OrInto(duplex Duplex[uint64]) {
26+
for _, internalDuplex := range s.duplexes {
27+
duplex.Or(internalDuplex)
28+
}
29+
}
30+
31+
func (s *CommutativeDuplex64) AndInto(duplex Duplex[uint64]) {
32+
for _, internalDuplex := range s.duplexes {
33+
duplex.And(internalDuplex)
34+
}
35+
}
36+
37+
func (s *CommutativeDuplex64) OrAll(other *CommutativeDuplex64) {
38+
s.duplexes = append(s.duplexes, other.duplexes...)
39+
}
40+
41+
func (s *CommutativeDuplex64) Cardinality() uint64 {
42+
return s.orAll().Cardinality()
43+
}
44+
45+
func (s *CommutativeDuplex64) Slice() []uint64 {
46+
return s.orAll().Slice()
47+
}
48+
49+
func (s *CommutativeDuplex64) Each(delegate func(value uint64) bool) {
50+
s.orAll().Each(delegate)
51+
}
52+
53+
func (s *CommutativeDuplex64) Contains(value uint64) bool {
54+
for _, nextDuplex := range s.duplexes {
55+
if nextDuplex.Contains(value) {
56+
return true
57+
}
58+
}
59+
60+
return false
61+
}

0 commit comments

Comments
 (0)