1// Copyright 2019 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
15// +build !386,!arm,!mips,!mipsle
16
17package procfs
18
19import (
20	"testing"
21
22	"github.com/google/go-cmp/cmp"
23	"golang.org/x/sys/unix"
24)
25
26func TestProcMaps(t *testing.T) {
27	tsts64 := []*ProcMap{
28		{
29			StartAddr: 0x55680ae1e000,
30			EndAddr:   0x55680ae20000,
31			Perms:     &ProcMapPermissions{true, false, false, false, true},
32			Offset:    0,
33			Dev:       unix.Mkdev(0xfd, 0x01),
34			Inode:     47316994,
35			Pathname:  "/bin/cat",
36		},
37		{
38			StartAddr: 0x55680ae29000,
39			EndAddr:   0x55680ae2a000,
40			Perms:     &ProcMapPermissions{true, true, true, true, false},
41			Offset:    40960,
42			Dev:       unix.Mkdev(0xfd, 0x01),
43			Inode:     47316994,
44			Pathname:  "/bin/cat",
45		},
46		{
47			StartAddr: 0x55680bed6000,
48			EndAddr:   0x55680bef7000,
49			Perms:     &ProcMapPermissions{true, true, false, false, true},
50			Offset:    0,
51			Dev:       unix.Mkdev(0, 0),
52			Inode:     0,
53			Pathname:  "[heap]",
54		},
55		{
56			StartAddr: 0x7fdf964fc000,
57			EndAddr:   0x7fdf973f2000,
58			Perms:     &ProcMapPermissions{true, false, false, false, true},
59			Offset:    0,
60			Dev:       unix.Mkdev(0xfd, 0x01),
61			Inode:     17432624,
62			Pathname:  "/usr/lib/locale/locale-archive",
63		},
64		{
65			StartAddr: 0x7fdf973f2000,
66			EndAddr:   0x7fdf97417000,
67			Perms:     &ProcMapPermissions{true, false, false, false, true},
68			Offset:    0,
69			Dev:       unix.Mkdev(0xfd, 0x01),
70			Inode:     60571062,
71			Pathname:  "/lib/x86_64-linux-gnu/libc-2.29.so",
72		},
73		{
74			StartAddr: 0x7ffe9215c000,
75			EndAddr:   0x7ffe9217f000,
76			Perms:     &ProcMapPermissions{true, true, false, false, true},
77			Offset:    0,
78			Dev:       0,
79			Inode:     0,
80			Pathname:  "[stack]",
81		},
82		{
83			StartAddr: 0x7ffe921da000,
84			EndAddr:   0x7ffe921dd000,
85			Perms:     &ProcMapPermissions{true, false, false, false, true},
86			Offset:    0,
87			Dev:       0,
88			Inode:     0,
89			Pathname:  "[vvar]",
90		},
91		{
92			StartAddr: 0x7ffe921dd000,
93			EndAddr:   0x7ffe921de000,
94			Perms:     &ProcMapPermissions{true, false, true, false, true},
95			Offset:    0,
96			Dev:       0,
97			Inode:     0,
98			Pathname:  "[vdso]",
99		},
100		{
101			StartAddr: 0xffffffffff600000,
102			EndAddr:   0xffffffffff601000,
103			Perms:     &ProcMapPermissions{false, false, true, false, true},
104			Offset:    0,
105			Dev:       0,
106			Inode:     0,
107			Pathname:  "[vsyscall]",
108		},
109	}
110
111	// 64-bit test pid and fixtures
112	tpid := 26232
113	tsts := tsts64
114
115	p, err := getProcFixtures(t).Proc(tpid)
116	if err != nil {
117		t.Fatal(err)
118	}
119
120	maps, err := p.ProcMaps()
121	if err != nil {
122		t.Fatal(err)
123	}
124
125	if want, have := len(maps), len(tsts); want > have {
126		t.Errorf("want at least %d parsed proc/map entries, have %d", want, have)
127	}
128
129	for idx, tst := range tsts {
130		want, got := tst, maps[idx]
131		if diff := cmp.Diff(want, got); diff != "" {
132			t.Fatalf("unexpected proc/map entry (-want +got):\n%s", diff)
133		}
134	}
135
136}
137