1package proc
2
3import (
4	"fmt"
5	"path/filepath"
6	"reflect"
7	"strconv"
8	"testing"
9
10	"android/soong/finder/fs"
11)
12
13func TestNewProcStatus(t *testing.T) {
14	fs := fs.NewMockFs(nil)
15
16	pid := 4032827
17	procDir := filepath.Join("/proc", strconv.Itoa(pid))
18	if err := fs.MkDirs(procDir); err != nil {
19		t.Fatalf("failed to create proc pid dir %s: %v", procDir, err)
20	}
21	statusFilename := filepath.Join(procDir, "status")
22
23	if err := fs.WriteFile(statusFilename, statusData, 0644); err != nil {
24		t.Fatalf("failed to write proc file %s: %v", statusFilename, err)
25	}
26
27	status, err := NewProcStatus(pid, fs)
28	if err != nil {
29		t.Fatalf("got %v, want nil for error", err)
30	}
31
32	fmt.Printf("%d %d\b", status.VmPeak, expectedStatus.VmPeak)
33	if !reflect.DeepEqual(status, expectedStatus) {
34		t.Errorf("got %v, expecting %v for ProcStatus", status, expectedStatus)
35	}
36}
37
38var statusData = []byte(`Name:   fake_process
39Umask:  0022
40State:  S (sleeping)
41Tgid:   4032827
42Ngid:   0
43Pid:    4032827
44PPid:   1
45TracerPid:      0
46Uid:    0       0       0       0
47Gid:    0       0       0       0
48FDSize: 512
49Groups:
50NStgid: 4032827
51NSpid:  4032827
52NSpgid: 4032827
53NSsid:  4032827
54VmPeak:   733232 kB
55VmSize:   733232 kB
56VmLck:       132 kB
57VmPin:       130 kB
58VmHWM:     69156 kB
59VmRSS:     69156 kB
60RssAnon:           50896 kB
61RssFile:           18260 kB
62RssShmem:            122 kB
63VmData:   112388 kB
64VmStk:       132 kB
65VmExe:      9304 kB
66VmLib:         8 kB
67VmPTE:       228 kB
68VmSwap:        10 kB
69HugetlbPages:          22 kB
70CoreDumping:    0
71THP_enabled:    1
72Threads:        46
73SigQ:   2/767780
74SigPnd: 0000000000000000
75ShdPnd: 0000000000000000
76SigBlk: fffffffe3bfa3a00
77SigIgn: 0000000000000000
78SigCgt: fffffffe7fc1feff
79CapInh: 0000000000000000
80CapPrm: 0000003fffffffff
81CapEff: 0000003fffffffff
82CapBnd: 0000003fffffffff
83CapAmb: 0000000000000000
84NoNewPrivs:     0
85Seccomp:        0
86Speculation_Store_Bypass:       thread vulnerable
87Cpus_allowed:   ff,ffffffff,ffffffff
88Cpus_allowed_list:      0-71
89Mems_allowed:   00000000,00000003
90Mems_allowed_list:      0-1
91voluntary_ctxt_switches:        1635
92nonvoluntary_ctxt_switches:     32
93`)
94
95var expectedStatus = &ProcStatus{
96	pid:          4032827,
97	VmPeak:       750829568,
98	VmSize:       750829568,
99	VmLck:        135168,
100	VmPin:        133120,
101	VmHWM:        70815744,
102	VmRss:        70815744,
103	RssAnon:      52117504,
104	RssShmem:     124928,
105	VmData:       115085312,
106	VmStk:        135168,
107	VmExe:        9527296,
108	VmLib:        8192,
109	VmPTE:        233472,
110	VmSwap:       10240,
111	HugetlbPages: 22528,
112}
113