-
Notifications
You must be signed in to change notification settings - Fork 0
/
restartable_test.go
407 lines (368 loc) · 9.99 KB
/
restartable_test.go
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
package main
import (
"fmt"
"io/fs"
"os"
"slices"
"testing"
"testing/fstest"
)
// Test using real /proc files
func TestRealProcPid(t *testing.T) {
pid := os.Getpid()
fs, err := OpenProcPid("/proc", pid)
if err != nil {
t.Fatalf("failed to open /proc/%d: %v", pid, err)
}
defer fs.Close()
// Test PID method
t.Run("PID", func(t *testing.T) {
got := fs.PID()
if got != pid {
t.Errorf("PID() = %d, want %d", got, pid)
}
})
// Test ReadFile with real data and compare to os.ReadFile
filesToTest := []string{"cmdline"}
for _, file := range filesToTest {
t.Run(file, func(t *testing.T) {
// Custom ReadFile method
data, err := fs.ReadFile(file)
if err != nil {
t.Errorf("failed to read /proc/%d/%s: %v", pid, file, err)
}
// Real ReadFile method (os.ReadFile)
realData, err := os.ReadFile(fmt.Sprintf("/proc/%d/%s", pid, file))
if err != nil {
t.Errorf("failed to read /proc/%d/%s using os.ReadFile: %v", pid, file, err)
}
// Compare the data returned by both methods
if string(data) != string(realData) {
t.Errorf("ReadFile data mismatch for %s: expected %s, got %s", file, string(realData), string(data))
}
})
}
// Test ReadLink with real data and compare to os.ReadLink
t.Run("exe", func(t *testing.T) {
link, err := fs.ReadLink("exe")
if err != nil {
t.Errorf("failed to read /proc/%d/exe: %v", pid, err)
}
realLink, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
if err != nil {
t.Errorf("failed to read /proc/%d/exe using os.Readlink: %v", pid, err)
}
if link != realLink {
t.Errorf("ReadLink data mismatch for exe: expected %s, got %s", realLink, link)
}
})
}
// Test getDeleted
func TestGetDeleted(t *testing.T) {
tests := []struct {
name string
maps string
expected []string
}{
{
name: "Test with deleted file",
maps: "00400000-0040b000 r-xp 00000000 08:01 1234 /path/to/executable (deleted)\n",
expected: []string{"/path/to/executable"},
},
{
name: "Test with no deleted files",
maps: "00400000-0040b000 r-xp 00000000 08:01 1234 /path/to/executable\n",
expected: []string{},
},
{
name: "Test with permission error",
maps: "",
expected: []string{},
},
{
name: "Test with invalid mapping",
maps: "00400000-0040b000 r-xp 00000000 08:01 1234 /path/to/someotherfile (deleted)\n",
expected: []string{"/path/to/someotherfile"},
},
{
name: "Test with multiple deleted files",
maps: "00400000-0040b000 r-xp 00000000 08:01 1234 /path/to/executable1 (deleted)\n" +
"00410000-0041b000 r-xp 00000000 08:01 1234 /path/to/executable2 (deleted)\n",
expected: []string{"/path/to/executable1", "/path/to/executable2"},
},
{
name: "Test with ignored files",
maps: "00400000-0040b000 r-xp 00000000 08:01 1234 /memfd:shm (deleted)\n",
expected: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
files := getDeleted(tt.maps)
if !slices.Equal(files, tt.expected) {
t.Errorf("expected files: %v, got: %v", tt.expected, files)
}
})
}
}
// Test getService
func TestGetService(t *testing.T) {
tests := []struct {
name string
userService bool
cgroup string
expected string
}{
{
name: "Test Systemd User Slice",
userService: true,
cgroup: "/user.slice/my.service",
expected: "my",
},
{
name: "Test Systemd System Slice",
userService: false,
cgroup: "/system.slice/my.service",
expected: "my",
},
{
name: "Test OpenRC Service",
userService: false,
cgroup: ":name=openrc:/myservice",
expected: "myservice",
},
{
name: "Test Invalid Cgroup Format",
userService: false,
cgroup: "/invalid/format",
expected: "-",
},
{
name: "Test No Service in Cgroup",
userService: false,
cgroup: "/system.slice/",
expected: "-",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := getService(tt.cgroup, tt.userService)
if cmd != tt.expected {
t.Errorf("expected command: %v, got: %v", tt.expected, cmd)
}
})
}
}
// Test getCommand
func TestGetCommand(t *testing.T) {
tests := []struct {
name string
fullPath bool
statusName string
cmdline string
exe string
expected string
}{
{
name: "Test Full Path with Non-Zombie Process",
fullPath: true,
statusName: "cmdline data",
cmdline: "cmd --flag=\"value\"",
exe: "/path/to/executable",
expected: "cmd --flag=\"value\"",
},
{
name: "Test Full Path with Zombie Process",
fullPath: true,
statusName: "cmdline data",
cmdline: "cmd --flag=\"value\"",
exe: "",
expected: "cmd --flag=\"value\"",
},
{
name: "Test Short Path (cmdline exists)",
fullPath: false,
statusName: "cmdline data",
cmdline: "cmdline",
exe: "",
expected: "cmdline",
},
{
name: "Test Command Truncated in Status",
fullPath: false,
statusName: "cmdline data",
cmdline: "cmdline",
exe: "",
expected: "cmdline",
},
{
name: "Test Command 'none' for Kernel Helper",
fullPath: false,
statusName: "none",
cmdline: "cmdline",
exe: "",
expected: "cmdline",
},
{
name: "Test Empty Command",
fullPath: false,
statusName: "",
cmdline: "",
exe: "",
expected: "-",
},
{
name: "Test Non-Zombie Process with Matching Executable Path",
fullPath: true,
statusName: "cmdline data",
cmdline: "cmd --flag=\"value\"",
exe: "/path/to/executable",
expected: "cmd --flag=\"value\"",
},
{
name: "Test Non-Zombie Process with Mismatched Executable Path",
fullPath: true,
statusName: "cmdline data",
cmdline: "cmd --flag=\"value\"",
exe: "/different/path/to/executable",
expected: "cmd --flag=\"value\"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := getCommand([]byte(tt.cmdline), tt.exe, tt.fullPath, tt.statusName)
if cmd != tt.expected {
t.Errorf("expected command: %v, got: %v", tt.expected, cmd)
}
})
}
}
// Test parseStatusField
func TestParseStatusField(t *testing.T) {
tests := []struct {
name string
data string
field string
expected string
}{
{"Valid Uid Field", "Name:\tbash\nUid:\t1000\t1001\n", "Uid", "1000\t1001"},
{"Valid Name Field", "Name:\tbash\nUid:\t1000\n", "Name", "bash"},
{"Missing Field", "Name:\tbash\nUid:\t1000\n", "Gid", ""},
{"Empty Data", "", "Uid", ""},
{"Malformed Data", "Name\nUid:\t1000\n", "Uid", "1000"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
field := parseStatusField(tt.data, tt.field)
if field != tt.expected {
t.Errorf("Expected '%s', got '%s'", tt.expected, field)
}
})
}
}
type MockProcPidFS struct {
fs fs.FS
pid int
}
func (p *MockProcPidFS) ReadFile(path string) ([]byte, error) {
return fs.ReadFile(p.fs, path)
}
// Workaround for https://github.com/golang/go/issues/49580
func (p *MockProcPidFS) ReadLink(path string) (string, error) {
file, err := p.fs.Open(path)
if err != nil {
return "", fmt.Errorf("failed to open path: %w", err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return "", fmt.Errorf("failed to stat path: %w", err)
}
if stat.Mode()&fs.ModeSymlink == 0 {
return "", fmt.Errorf("not a symlink")
}
if mapFile, ok := p.fs.(fstest.MapFS)[path]; ok {
return string(mapFile.Data), nil
}
return "", fmt.Errorf("symlink target not found")
}
func (p *MockProcPidFS) PID() int {
return p.pid
}
func (p *MockProcPidFS) Close() error {
return nil
}
func mockProcFS(pid int, files map[string]string, symlinks map[string]string) *MockProcPidFS {
mockFS := fstest.MapFS{}
for path, content := range files {
mockFS[path] = &fstest.MapFile{Data: []byte(content)}
}
for path, target := range symlinks {
mockFS[path] = &fstest.MapFile{
Mode: fs.ModeSymlink,
Data: []byte(target),
}
}
return &MockProcPidFS{fs: mockFS, pid: pid}
}
// Test getProcessInfo
func TestGetProcessInfo(t *testing.T) {
procFS := mockProcFS(1234, map[string]string{
"cgroup": "/proc/777/cgroup:0::/system.slice/sshd.service\n",
"cmdline": "/bin/bash\x00--version\x00",
"maps": "00400000-00452000 r-xp 00000000 fd:00 12345 /bin/bash (deleted)\n",
"status": "Name:\tbash\nPPid:\t1\nUid:\t1000\n",
}, nil)
info, err := getProcessInfo(procFS, true, false)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if info.Pid != 1234 {
t.Errorf("Expected PID 1234, got %d", info.Pid)
}
if info.Ppid != 1 {
t.Errorf("Expected PPID 1, got %d", info.Ppid)
}
if info.Uid != 1000 {
t.Errorf("Expected UID 1000, got %d", info.Uid)
}
if info.Command != "/bin/bash --version" {
t.Errorf("Expected command '/bin/bash --version', got '%s'", info.Command)
}
if len(info.Deleted) != 1 || info.Deleted[0] != "/bin/bash" {
t.Errorf("Expected Deleted ['/bin/bash'], got %v", info.Deleted)
}
}
// Test getUser
func TestGetUser(t *testing.T) {
if getUser(0) != "root" {
t.Errorf("Expected 'root', got '%s'", getUser(0))
}
if getUser(99999) != "-" {
t.Errorf("Expected '-', got '%s'", getUser(99999))
}
}
// Test quoteString
func TestQuoteString(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"Empty String", "", ""},
{"No Special Characters", "hello", "hello"},
{"String With Spaces", "hello world", "hello world"},
{"String With Special Characters", "hello\tworld", "hello\\tworld"},
{"String With Quotes", `cmd --flag="value"`, `cmd --flag=\"value\"`},
{"String With Newline", "hello\nworld", "hello\\nworld"},
{"String With Mixed Characters", "cmd --flag=\"value\"\nnext line", `cmd --flag=\"value\"\nnext line`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := quoteString(tt.input)
if result != tt.expected {
t.Errorf("For input '%s': expected '%s', got '%s'", tt.input, tt.expected, result)
}
})
}
}