-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
322 lines (261 loc) · 8.02 KB
/
Copy pathmain_test.go
File metadata and controls
322 lines (261 loc) · 8.02 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
package main
import (
"os"
"testing"
"time"
)
// TestDetectCgroupVersion tests cgroup version detection
func TestDetectCgroupVersion(t *testing.T) {
version, basePath, err := detectCgroupVersion()
if err != nil {
t.Fatalf("Failed to detect cgroup version: %v", err)
}
if version != 1 && version != 2 {
t.Errorf("Invalid cgroup version detected: %d", version)
}
if basePath == "" {
t.Error("Base path should not be empty")
}
t.Logf("Detected cgroups v%d at %s", version, basePath)
}
// TestDetectFirewallTool tests firewall tool detection
func TestDetectFirewallTool(t *testing.T) {
// Skip test if not running as root
if os.Geteuid() != 0 {
t.Skip("Skipping firewall detection test: requires root privileges")
}
tool, err := detectFirewallTool()
if err != nil {
t.Fatalf("Failed to detect firewall tool: %v", err)
}
if tool != "nftables" && tool != "iptables" {
t.Errorf("Invalid firewall tool detected: %s", tool)
}
t.Logf("Detected firewall tool: %s", tool)
}
// TestProcessExists tests the process existence check function
func TestProcessExists(t *testing.T) {
// Test with current process (must exist)
currentPID := os.Getpid()
if !processExists(currentPID) {
t.Errorf("Current process %d should exist", currentPID)
}
// Test with non-existent PID (very unlikely to exist)
if processExists(999999) {
t.Log("Warning: PID 999999 exists, this is unexpected but not necessarily an error")
}
}
// TestGetProcessName tests retrieving a process name
func TestGetProcessName(t *testing.T) {
currentPID := os.Getpid()
name := getProcessName(currentPID)
if name == "" {
t.Error("Process name should not be empty")
}
t.Logf("Current process name: %s", name)
}
// TestGetProcessCgroup tests retrieving a process cgroup
func TestGetProcessCgroup(t *testing.T) {
currentPID := os.Getpid()
cgroup, err := getProcessCgroup(currentPID)
if err != nil {
t.Fatalf("Failed to get cgroup for current process: %v", err)
}
if cgroup == "" {
t.Error("Cgroup should not be empty")
}
t.Logf("Current process cgroup: %s", cgroup)
}
// TestJailerStateCreation tests jailer state creation
func TestJailerStateCreation(t *testing.T) {
state := NewJailerState()
if state == nil {
t.Fatal("JailerState should not be nil")
}
if state.ActiveJails == nil {
t.Error("ActiveJails map should be initialized")
}
if len(state.ActiveJails) != 0 {
t.Error("ActiveJails should be empty initially")
}
// Test that cgroup paths are set after initialization
if err := initializeCgroup(state); err != nil && os.Geteuid() == 0 {
t.Errorf("Failed to initialize cgroups: %v", err)
}
if state.NetworkCgroupPath == "" && os.Geteuid() == 0 {
t.Error("NetworkCgroupPath should be set after initialization")
}
}
// TestValidateProcessAccess tests process access validation
func TestValidateProcessAccess(t *testing.T) {
currentPID := os.Getpid()
if err := validateProcessAccess(currentPID); err != nil {
t.Errorf("Should be able to access current process: %v", err)
}
// Test with non-existent PID
if err := validateProcessAccess(999999); err == nil {
t.Log("Warning: PID 999999 seems to be accessible, this is unexpected")
}
}
// TestCleanupDeadProcesses tests dead process cleanup
func TestCleanupDeadProcesses(t *testing.T) {
state := NewJailerState()
// Create fake jail with non-existent PID
state.ActiveJails[999999] = &Jail{
PID: 999999,
OriginalCgroup: "/",
JailTypes: []string{"network"},
Timestamp: time.Now(),
Children: []int{999998},
}
// Cleanup should remove non-existent process
cleanupDeadProcesses(state)
if len(state.ActiveJails) != 0 {
t.Error("Dead processes should have been cleaned up")
}
}
// TestJailMethods tests the Jail struct methods
func TestJailMethods(t *testing.T) {
jail := &Jail{
PID: 1234,
OriginalCgroup: "/",
JailTypes: []string{"network"},
Timestamp: time.Now(),
Children: []int{},
}
// Test HasJailType
if !jail.HasJailType("network") {
t.Error("Should have network jail type")
}
if jail.HasJailType("cpu") {
t.Error("Should not have cpu jail type")
}
// Test AddJailType
jail.AddJailType("cpu")
if !jail.HasJailType("cpu") {
t.Error("Should have cpu jail type after adding")
}
if len(jail.JailTypes) != 2 {
t.Errorf("Should have 2 jail types, got %d", len(jail.JailTypes))
}
// Test GetJailTypesString
jailTypesStr := jail.GetJailTypesString()
if jailTypesStr != "network,cpu" && jailTypesStr != "cpu,network" {
t.Errorf("Unexpected jail types string: %s", jailTypesStr)
}
// Test RemoveJailType
jail.RemoveJailType("network")
if jail.HasJailType("network") {
t.Error("Should not have network jail type after removal")
}
if len(jail.JailTypes) != 1 {
t.Errorf("Should have 1 jail type, got %d", len(jail.JailTypes))
}
}
// TestMultipleJailTypes tests multiple jail type functionality
func TestMultipleJailTypes(t *testing.T) {
jail := &Jail{
PID: 1234,
OriginalCgroup: "/",
JailTypes: []string{},
Timestamp: time.Now(),
Children: []int{},
}
// Test adding multiple jail types
jail.AddJailType("network")
jail.AddJailType("cpu")
if len(jail.JailTypes) != 2 {
t.Errorf("Should have 2 jail types, got %d", len(jail.JailTypes))
}
// Test that adding the same type twice doesn't duplicate
jail.AddJailType("network")
if len(jail.JailTypes) != 2 {
t.Errorf("Should still have 2 jail types after duplicate add, got %d", len(jail.JailTypes))
}
// Test removing one type
jail.RemoveJailType("cpu")
if len(jail.JailTypes) != 1 {
t.Errorf("Should have 1 jail type after removal, got %d", len(jail.JailTypes))
}
if !jail.HasJailType("network") {
t.Error("Should still have network jail type")
}
if jail.HasJailType("cpu") {
t.Error("Should not have cpu jail type after removal")
}
}
// TestCommandExists tests command existence check
func TestCommandExists(t *testing.T) {
// Test with a command that certainly exists
if !commandExists("ls") {
t.Error("ls command should exist on Unix systems")
}
// Test with a command that doesn't exist
if commandExists("this_command_does_not_exist_12345") {
t.Error("Non-existent command should not be found")
}
}
// TestCgroupInitialization tests cgroup initialization (requires root)
func TestCgroupInitialization(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("Skipping cgroup initialization test: requires root privileges")
}
state := NewJailerState()
err := initializeCgroup(state)
if err != nil {
t.Fatalf("Failed to initialize cgroups: %v", err)
}
// Check that paths are set
if state.NetworkCgroupPath == "" {
t.Error("NetworkCgroupPath should be set")
}
if state.CpuCgroupPath == "" {
t.Error("CpuCgroupPath should be set")
}
if state.NetworkCpuCgroupPath == "" {
t.Error("NetworkCpuCgroupPath should be set")
}
if state.CgroupVersion != 1 && state.CgroupVersion != 2 {
t.Errorf("Invalid cgroup version: %d", state.CgroupVersion)
}
t.Logf("Initialized cgroups v%d", state.CgroupVersion)
t.Logf("Network cgroup path: %s", state.NetworkCgroupPath)
t.Logf("CPU cgroup path: %s", state.CpuCgroupPath)
t.Logf("Combined cgroup path: %s", state.NetworkCpuCgroupPath)
}
// BenchmarkGetProcessChildren benchmark for retrieving child processes
func BenchmarkGetProcessChildren(b *testing.B) {
currentPID := os.Getpid()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := getProcessChildren(currentPID)
if err != nil {
b.Fatalf("Failed to get process children: %v", err)
}
}
}
// BenchmarkProcessExists benchmark for process existence check
func BenchmarkProcessExists(b *testing.B) {
currentPID := os.Getpid()
b.ResetTimer()
for i := 0; i < b.N; i++ {
processExists(currentPID)
}
}
// BenchmarkJailMethods benchmark for jail type operations
func BenchmarkJailMethods(b *testing.B) {
jail := &Jail{
PID: 1234,
OriginalCgroup: "/",
JailTypes: []string{"network"},
Timestamp: time.Now(),
Children: []int{},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
jail.AddJailType("cpu")
jail.HasJailType("cpu")
jail.RemoveJailType("cpu")
jail.GetJailTypesString()
}
}