Skip to content

Commit b553a77

Browse files
committed
test: add unit tests for socket diff functionality
Signed-off-by: Hamza Dogar <hxadogar@gmail.com>
1 parent 0fa5031 commit b553a77

File tree

1 file changed

+284
-0
lines changed

1 file changed

+284
-0
lines changed

cmd/diff_test.go

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package cmd
4+
5+
import (
6+
"testing"
7+
)
8+
9+
// compareSockets [empty inputs]
10+
func TestCompareSocketsEmptyInputs(t *testing.T) {
11+
result := compareSockets([]SkNode{}, []SkNode{})
12+
13+
if result == nil {
14+
t.Fatal("Expected non-nil result for empty inputs")
15+
}
16+
17+
if len(result.Added) != 0 {
18+
t.Errorf("Expected 0 added sockets, got %d", len(result.Added))
19+
}
20+
21+
if len(result.Removed) != 0 {
22+
t.Errorf("Expected 0 removed sockets, got %d", len(result.Removed))
23+
}
24+
25+
if result.Unchanged != 0 {
26+
t.Errorf("Expected 0 unchanged sockets, got %d", result.Unchanged)
27+
}
28+
}
29+
30+
func TestCompareSocketsNilInputs(t *testing.T) {
31+
result := compareSockets(nil, nil)
32+
33+
if result == nil {
34+
t.Fatal("Expected non-nil result for nil inputs")
35+
}
36+
37+
if len(result.Added) != 0 {
38+
t.Errorf("Expected 0 added sockets for nil inputs, got %d", len(result.Added))
39+
}
40+
41+
if len(result.Removed) != 0 {
42+
t.Errorf("Expected 0 removed sockets for nil inputs, got %d", len(result.Removed))
43+
}
44+
45+
if result.Unchanged != 0 {
46+
t.Errorf("Expected 0 unchanged sockets for nil inputs, got %d", result.Unchanged)
47+
}
48+
}
49+
50+
// added sockets
51+
func TestCompareSocketsAddedSockets(t *testing.T) {
52+
socketA := SkNode{
53+
PID: 1234,
54+
OpenSockets: []SocketNode{
55+
{
56+
Protocol: "TCP",
57+
Data: SkData{
58+
Type: "TCP",
59+
Source: "0.0.0.0",
60+
SourcePort: 8080,
61+
Dest: "0.0.0.0",
62+
DestPort: 0,
63+
},
64+
},
65+
},
66+
}
67+
68+
socketB := SkNode{
69+
PID: 1234,
70+
OpenSockets: []SocketNode{
71+
{
72+
Protocol: "TCP",
73+
Data: SkData{
74+
Type: "TCP",
75+
Source: "0.0.0.0",
76+
SourcePort: 8080,
77+
Dest: "0.0.0.0",
78+
DestPort: 0,
79+
},
80+
},
81+
{
82+
Protocol: "TCP",
83+
Data: SkData{
84+
Type: "TCP",
85+
Source: "0.0.0.0",
86+
SourcePort: 8081,
87+
Dest: "0.0.0.0",
88+
DestPort: 0,
89+
},
90+
},
91+
},
92+
}
93+
94+
result := compareSockets([]SkNode{socketA}, []SkNode{socketB})
95+
96+
if result == nil {
97+
t.Fatal("Expected non-nil result")
98+
}
99+
100+
if len(result.Added) != 1 {
101+
t.Errorf("Expected 1 added socket, got %d", len(result.Added))
102+
}
103+
104+
if len(result.Removed) != 0 {
105+
t.Errorf("Expected 0 removed sockets, got %d", len(result.Removed))
106+
}
107+
108+
if result.Unchanged != 1 {
109+
t.Errorf("Expected 1 unchanged socket, got %d", result.Unchanged)
110+
}
111+
112+
// verify added socket is the correct one (port 8081)
113+
if result.Added[0].SrcPort != 8081 {
114+
t.Errorf("Expected added socket to have port 8081, got %d", result.Added[0].SrcPort)
115+
}
116+
}
117+
118+
// removed sockets
119+
func TestCompareSocketsRemovedSockets(t *testing.T) {
120+
socketA := SkNode{
121+
PID: 1234,
122+
OpenSockets: []SocketNode{
123+
{
124+
Protocol: "TCP",
125+
Data: SkData{
126+
Type: "TCP",
127+
Source: "0.0.0.0",
128+
SourcePort: 8080,
129+
Dest: "0.0.0.0",
130+
DestPort: 0,
131+
},
132+
},
133+
{
134+
Protocol: "TCP",
135+
Data: SkData{
136+
Type: "TCP",
137+
Source: "0.0.0.0",
138+
SourcePort: 8081,
139+
Dest: "0.0.0.0",
140+
DestPort: 0,
141+
},
142+
},
143+
},
144+
}
145+
146+
socketB := SkNode{
147+
PID: 1234,
148+
OpenSockets: []SocketNode{
149+
{
150+
Protocol: "TCP",
151+
Data: SkData{
152+
Type: "TCP",
153+
Source: "0.0.0.0",
154+
SourcePort: 8081,
155+
Dest: "0.0.0.0",
156+
DestPort: 0,
157+
},
158+
},
159+
},
160+
}
161+
162+
result := compareSockets([]SkNode{socketA}, []SkNode{socketB})
163+
164+
if result == nil {
165+
t.Fatal("Expected non-nil result")
166+
}
167+
168+
if len(result.Added) != 0 {
169+
t.Errorf("Expected 0 added sockets, got %d", len(result.Added))
170+
}
171+
172+
if len(result.Removed) != 1 {
173+
t.Errorf("Expected 1 removed socket, got %d", len(result.Removed))
174+
}
175+
176+
if result.Unchanged != 1 {
177+
t.Errorf("Expected 1 unchanged socket, got %d", result.Unchanged)
178+
}
179+
180+
// verify removed socket is the correct one (port 8080)
181+
if result.Removed[0].SrcPort != 8080 {
182+
t.Errorf("Expected removed socket to have port 8080, got %d", result.Removed[0].SrcPort)
183+
}
184+
}
185+
186+
// comparing sockets from different processes
187+
func TestCompareSocketsMultiplePIDs(t *testing.T) {
188+
socketPID1 := SkNode{
189+
PID: 1,
190+
OpenSockets: []SocketNode{
191+
{
192+
Protocol: "TCP",
193+
Data: SkData{
194+
Type: "TCP",
195+
Source: "0.0.0.0",
196+
SourcePort: 80,
197+
Dest: "0.0.0.0",
198+
DestPort: 0,
199+
},
200+
},
201+
},
202+
}
203+
204+
socketPID2 := SkNode{
205+
PID: 2,
206+
OpenSockets: []SocketNode{
207+
{
208+
Protocol: "TCP",
209+
Data: SkData{
210+
Type: "TCP",
211+
Source: "0.0.0.0",
212+
SourcePort: 443,
213+
Dest: "0.0.0.0",
214+
DestPort: 0,
215+
},
216+
},
217+
},
218+
}
219+
220+
result := compareSockets([]SkNode{socketPID1, socketPID2}, []SkNode{socketPID1})
221+
222+
if result == nil {
223+
t.Fatal("Expected non-nil result")
224+
}
225+
226+
if len(result.Removed) != 1 {
227+
t.Errorf("Expected 1 removed socket (PID 2's socket), got %d", len(result.Removed))
228+
}
229+
230+
if result.Removed[0].PID != 2 {
231+
t.Errorf("Expected removed socket to be from PID 2, got PID %d", result.Removed[0].PID)
232+
}
233+
234+
if result.Removed[0].SrcPort != 443 {
235+
t.Errorf("Expected removed socket to have port 443, got %d", result.Removed[0].SrcPort)
236+
}
237+
}
238+
239+
// summary generation with socket changes | for test generateSummary()
240+
func TestGenerateSummaryWithSocketChanges(t *testing.T) {
241+
result := &DiffResult{
242+
ContainerName: "test-container",
243+
SocketChanges: &SocketDiff{
244+
Added: []SocketInfo{
245+
{PID: 1, Protocol: "TCP", SrcPort: 8081},
246+
{PID: 1, Protocol: "TCP", SrcPort: 8082},
247+
},
248+
Removed: []SocketInfo{
249+
{PID: 1, Protocol: "TCP", SrcPort: 8080},
250+
},
251+
Unchanged: 0,
252+
},
253+
}
254+
255+
summary := generateSummary(result)
256+
257+
if summary == "" {
258+
t.Fatal("Expected non-empty summary")
259+
}
260+
261+
// Check that socket changes are mentioned
262+
if expected := "Sockets: +2 -1"; summary != expected &&
263+
(summary[len(summary)-9:] != "+2 -1" && summary[len(summary)-9:] != "2 -1") {
264+
t.Logf("Summary: %s", summary)
265+
//
266+
if !contains(summary, "Sockets:") {
267+
t.Errorf("Expected summary to contain 'Sockets:', got '%s'", summary)
268+
}
269+
}
270+
}
271+
272+
// helper function to check if string contains substring
273+
func contains(s, substr string) bool {
274+
return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && findSubstring(s, substr))
275+
}
276+
277+
func findSubstring(s, substr string) bool {
278+
for i := 0; i <= len(s)-len(substr); i++ {
279+
if s[i:i+len(substr)] == substr {
280+
return true
281+
}
282+
}
283+
return false
284+
}

0 commit comments

Comments
 (0)