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	tsts32 := []*ProcMap{
28		{
29			StartAddr: 0x08048000,
30			EndAddr:   0x08089000,
31			Perms:     &ProcMapPermissions{true, false, true, false, true},
32			Offset:    0,
33			Dev:       unix.Mkdev(0x03, 0x01),
34			Inode:     104219,
35			Pathname:  "/bin/tcsh",
36		},
37		{
38			StartAddr: 0x08089000,
39			EndAddr:   0x0808c000,
40			Perms:     &ProcMapPermissions{true, true, false, false, true},
41			Offset:    266240,
42			Dev:       unix.Mkdev(0x03, 0x01),
43			Inode:     104219,
44			Pathname:  "/bin/tcsh",
45		},
46		{
47			StartAddr: 0x0808c000,
48			EndAddr:   0x08146000,
49			Perms:     &ProcMapPermissions{true, true, true, false, true},
50			Offset:    0,
51			Dev:       unix.Mkdev(0x00, 0x00),
52			Inode:     0,
53			Pathname:  "",
54		},
55		{
56			StartAddr: 0x40000000,
57			EndAddr:   0x40015000,
58			Perms:     &ProcMapPermissions{true, false, true, false, true},
59			Offset:    0,
60			Dev:       unix.Mkdev(0x03, 0x01),
61			Inode:     61874,
62			Pathname:  "/lib/ld-2.3.2.so",
63		},
64	}
65
66	// 32-bit test pid and fixtures
67	tpid := 26234
68	tsts := tsts32
69
70	p, err := getProcFixtures(t).Proc(tpid)
71	if err != nil {
72		t.Fatal(err)
73	}
74
75	maps, err := p.ProcMaps()
76	if err != nil {
77		t.Fatal(err)
78	}
79
80	if want, have := len(maps), len(tsts); want > have {
81		t.Errorf("want at least %d parsed proc/map entries, have %d", want, have)
82	}
83
84	for idx, tst := range tsts {
85		want, got := tst, maps[idx]
86		if diff := cmp.Diff(want, got); diff != "" {
87			t.Fatalf("unexpected proc/map entry (-want +got):\n%s", diff)
88		}
89	}
90
91}
92