-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_test.go
219 lines (193 loc) · 5.21 KB
/
process_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
// Copyright 2021 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ps_test
import (
"bytes"
"errors"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/tklauser/ps"
)
var startTime = time.Now()
func getExeName() string {
if runtime.GOOS == "windows" {
return "ps.test.exe"
}
return "ps.test"
}
func logProcess(t *testing.T, p ps.Process) {
t.Helper()
t.Logf("process %d (%s) ppid %d uid %d gid %d ctime %v",
p.PID(), p.Command(), p.PPID(), p.UID(), p.GID(), p.CreationTime())
exeArgs := ""
if args := p.ExecutableArgs(); len(args) > 1 {
exeArgs = " " + strings.Join(args[1:], " ")
}
t.Logf(" $ %s%s", p.ExecutablePath(), exeArgs)
}
func checkOwnProcess(t *testing.T, p ps.Process) {
t.Helper()
logProcess(t, p)
if got, want := p.PID(), os.Getpid(); got != want {
t.Errorf("PID: got %v, want %v", got, want)
}
if got, want := p.PPID(), os.Getppid(); got != want {
t.Errorf("PPID: got %v, want %v", got, want)
}
if got, want := p.UID(), os.Getuid(); got != want {
t.Errorf("UID: got %v, want %v", got, want)
}
if got, want := p.GID(), os.Getgid(); got != want {
t.Errorf("GID: got %v, want %v", got, want)
}
if got, want := p.Command(), getExeName(); got != want {
t.Errorf("Command: got %v, want %v", got, want)
}
if got, want := filepath.Base(p.ExecutablePath()), getExeName(); got != want {
t.Errorf("ExecutablePath: got command %q, want %q", got, want)
}
switch runtime.GOOS {
case "darwin", "dragonfly", "freebsd", "linux":
if got, want := p.ExecutablePath(), os.Args[0]; got != want {
t.Errorf("ExecutablePath: got %q, want %q", got, want)
}
exeArgs := p.ExecutableArgs()
if got, want := len(exeArgs), len(os.Args); got != want {
t.Errorf("ExecutableArgs: got %d args, want %d", got, want)
} else {
for i, a := range exeArgs {
if oa := os.Args[i]; a != oa {
t.Errorf("ExecutableArgs: got argument %d %q, want %q", i, a, oa)
}
}
}
case "windows":
if got, want := p.ExecutablePath(), os.Args[0]; got != want {
t.Errorf("ExecutablePath: got %q, want %q", got, want)
}
default:
t.Logf("ExecutableArgs: not yet supported on %s", runtime.GOOS)
}
slack := 2 * time.Minute
if diff := p.CreationTime().Sub(startTime); diff > slack {
t.Errorf("process created %v after tests started", diff)
} else if diff < -slack {
t.Errorf("process created %v before tests started", -diff)
}
}
func TestProcesses(t *testing.T) {
procs, err := ps.Processes()
if err != nil {
t.Fatalf("Processes: %v", err)
}
if len(procs) == 0 {
t.Errorf("no processes returned")
}
pid := os.Getpid()
for _, p := range procs {
if p.PID() == pid {
checkOwnProcess(t, p)
return
}
}
t.Errorf("didn't find process with command name %q", getExeName())
}
func getInitName() string {
switch runtime.GOOS {
case "darwin":
return "launchd"
case "dragonfly", "freebsd", "netbsd", "openbsd",
"illumos", "solaris":
return "init"
case "linux":
// might be systemd, sysv init, openrc, ...
b, err := os.ReadFile("/proc/1/comm")
if err == nil {
return string(bytes.Trim(b, " \r\n"))
}
case "windows":
return "System"
}
return ""
}
func TestFindProcessOwn(t *testing.T) {
proc, err := ps.FindProcess(-1)
if err == nil {
t.Fatal("FindProcess: got nil error, want an error")
}
if proc != nil {
t.Fatalf("FindProcess: got process %v, want nil", proc)
}
proc, err = ps.FindProcess(os.Getpid())
if err != nil {
t.Fatalf("FindProcess: %v", err)
}
if proc == nil {
t.Fatal("FindProcess: got nil process")
}
checkOwnProcess(t, proc)
}
func TestFindProcessInit(t *testing.T) {
initPID := 1
wantUID, wantGID := 0, 0
if runtime.GOOS == "windows" {
// see https://devblogs.microsoft.com/oldnewthing/?p=23283
initPID = 4
wantUID, wantGID = -1, -1
}
proc, err := ps.FindProcess(initPID)
if errors.Is(err, fs.ErrPermission) {
t.Skipf("no permission to read init process")
} else if err != nil {
t.Fatalf("FindProcess(%d): %v", initPID, err)
}
logProcess(t, proc)
if got, want := proc.PID(), initPID; got != want {
t.Errorf("PID: got %v, want %v", got, want)
}
if got, want := proc.PPID(), 0; got != want {
t.Errorf("Parent PID: got %v, want %v", got, want)
}
if uid := proc.UID(); uid != wantUID {
t.Errorf("UID: got %v, want %v", uid, wantUID)
}
if gid := proc.GID(); gid != wantGID {
t.Errorf("GID: got %v, want %v", gid, wantGID)
}
if initName := getInitName(); initName != "" {
cmd := proc.Command()
if (runtime.GOOS == "illumos" || runtime.GOOS == "solaris") && cmd == "" {
t.Skipf("command: empty; might lack permissions to read /proc/1 on %s, skipping", runtime.GOOS)
} else if cmd != initName {
t.Errorf("command: got %q, want %q", cmd, initName)
}
} else {
t.Skipf("init process name not defined on %s", runtime.GOOS)
}
}
func BenchmarkProcesses(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := ps.Processes()
if err != nil {
b.Fatalf("Processes: %v", err)
}
}
}
func BenchmarkFindProcess(b *testing.B) {
pid := os.Getpid()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := ps.FindProcess(pid)
if err != nil {
b.Fatalf("FindProcess: %v", err)
}
}
}