2020-12-19 19:31:09 +01:00
|
|
|
package procmon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
myPid = os.Getpid()
|
2023-09-30 18:31:19 +02:00
|
|
|
proc = NewProcessEmpty(myPid, "fakeComm")
|
2020-12-19 19:31:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewProcess(t *testing.T) {
|
|
|
|
if proc.ID != myPid {
|
|
|
|
t.Error("NewProcess PID not equal to ", myPid)
|
|
|
|
}
|
2022-06-24 01:09:45 +02:00
|
|
|
if proc.Comm != "fakeComm" {
|
|
|
|
t.Error("NewProcess Comm not equal to fakeComm")
|
2020-12-19 19:31:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcPath(t *testing.T) {
|
2022-06-24 01:09:45 +02:00
|
|
|
if err := proc.ReadPath(); err != nil {
|
2020-12-19 19:31:09 +01:00
|
|
|
t.Error("Proc path error:", err)
|
|
|
|
}
|
|
|
|
if proc.Path == "/fake/path" {
|
|
|
|
t.Error("Proc path equal to /fake/path, should be different:", proc.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcCwd(t *testing.T) {
|
2022-06-24 01:09:45 +02:00
|
|
|
err := proc.ReadCwd()
|
2020-12-19 19:31:09 +01:00
|
|
|
|
|
|
|
if proc.CWD == "" {
|
|
|
|
t.Error("Proc readCwd() not read:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcCmdline(t *testing.T) {
|
2022-06-24 01:09:45 +02:00
|
|
|
proc.ReadCmdline()
|
2020-12-19 19:31:09 +01:00
|
|
|
|
|
|
|
if len(proc.Args) == 0 {
|
|
|
|
t.Error("Proc Args should not be empty:", proc.Args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcDescriptors(t *testing.T) {
|
|
|
|
proc.readDescriptors()
|
|
|
|
|
|
|
|
if len(proc.Descriptors) == 0 {
|
|
|
|
t.Error("Proc Descriptors should not be empty:", proc.Descriptors)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcEnv(t *testing.T) {
|
2024-10-18 01:08:43 +02:00
|
|
|
proc.pathEnviron = "testdata/proc-environ"
|
2022-06-24 01:09:45 +02:00
|
|
|
proc.ReadEnv()
|
2020-12-19 19:31:09 +01:00
|
|
|
|
2024-10-18 01:08:43 +02:00
|
|
|
expected := map[string]string{
|
|
|
|
"EMPTY": "",
|
|
|
|
"TEST1": "xxx=123",
|
|
|
|
"TEST2": "xxx=123==456",
|
|
|
|
"SSH_AGENT_PID": "4873",
|
|
|
|
"XDG_CURRENT_DESKTOP": "i3",
|
|
|
|
"USER": "opensnitch",
|
|
|
|
"HOME": "/tmp",
|
|
|
|
"XDG_DATA_DIRS": "/usr/share/gnome:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share",
|
|
|
|
"DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/1000/bus",
|
|
|
|
// Test latest var
|
|
|
|
"LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;",
|
|
|
|
|
|
|
|
//"LAST": "",
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range expected {
|
|
|
|
if env, found := proc.Env[k]; !found || env != v {
|
|
|
|
t.Error("Proc Env error, expected", ":", v, "got:", env, "(", k, ")")
|
|
|
|
}
|
2020-12-19 19:31:09 +01:00
|
|
|
}
|
2024-10-18 01:08:43 +02:00
|
|
|
|
2020-12-19 19:31:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcIOStats(t *testing.T) {
|
2024-01-05 15:31:00 +01:00
|
|
|
err := proc.readIOStats()
|
2020-12-19 19:31:09 +01:00
|
|
|
|
2024-01-05 15:31:00 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("error reading proc IOStats:", err)
|
2020-12-19 19:31:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcStatus(t *testing.T) {
|
|
|
|
proc.readStatus()
|
|
|
|
|
|
|
|
if proc.Status == "" {
|
|
|
|
t.Error("Proc Status should not be empty:", proc)
|
|
|
|
}
|
|
|
|
if proc.Stat == "" {
|
|
|
|
t.Error("Proc Stat should not be empty:", proc)
|
|
|
|
}
|
|
|
|
/*if proc.Stack == "" {
|
|
|
|
t.Error("Proc Stack should not be empty:", proc)
|
|
|
|
}*/
|
|
|
|
if proc.Maps == "" {
|
|
|
|
t.Error("Proc Maps should not be empty:", proc)
|
|
|
|
}
|
|
|
|
if proc.Statm.Size == 0 {
|
|
|
|
t.Error("Proc Statm Size should not be 0:", proc.Statm)
|
|
|
|
}
|
|
|
|
if proc.Statm.Resident == 0 {
|
|
|
|
t.Error("Proc Statm Resident should not be 0:", proc.Statm)
|
|
|
|
}
|
|
|
|
if proc.Statm.Shared == 0 {
|
|
|
|
t.Error("Proc Statm Shared should not be 0:", proc.Statm)
|
|
|
|
}
|
|
|
|
if proc.Statm.Text == 0 {
|
|
|
|
t.Error("Proc Statm Text should not be 0:", proc.Statm)
|
|
|
|
}
|
|
|
|
if proc.Statm.Lib != 0 {
|
|
|
|
t.Error("Proc Statm Lib should not be 0:", proc.Statm)
|
|
|
|
}
|
|
|
|
if proc.Statm.Data == 0 {
|
|
|
|
t.Error("Proc Statm Data should not be 0:", proc.Statm)
|
|
|
|
}
|
|
|
|
if proc.Statm.Dt != 0 {
|
|
|
|
t.Error("Proc Statm Dt should not be 0:", proc.Statm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcCleanPath(t *testing.T) {
|
|
|
|
proc.Path = "/fake/path/binary (deleted)"
|
2022-06-24 01:09:45 +02:00
|
|
|
proc.CleanPath()
|
2020-12-19 19:31:09 +01:00
|
|
|
if proc.Path != "/fake/path/binary" {
|
|
|
|
t.Error("Proc cleanPath() not cleaned:", proc.Path)
|
|
|
|
}
|
|
|
|
}
|
2024-10-18 01:08:43 +02:00
|
|
|
|
|
|
|
func BenchmarkProcReadEnv(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
proc.ReadEnv()
|
|
|
|
}
|
|
|
|
}
|