1// Copyright 2018 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
14package procfs
15
16import (
17	"testing"
18
19	"github.com/google/go-cmp/cmp"
20)
21
22// Whether or not to run tests with inode fixtures.
23const (
24	checkInode   = true
25	noCheckInode = false
26)
27
28func TestNetUnix(t *testing.T) {
29	fs, err := NewFS(procTestFixtures)
30	if err != nil {
31		t.Fatalf("failed to open procfs: %v", err)
32	}
33
34	got, err := fs.NetUNIX()
35	if err != nil {
36		t.Fatalf("failed to get UNIX socket data: %v", err)
37	}
38
39	testNetUNIX(t, checkInode, got)
40}
41
42func TestNetUnixNoInode(t *testing.T) {
43	fs, err := NewFS(procTestFixtures)
44	if err != nil {
45		t.Fatalf("failed to open procfs: %v", err)
46	}
47
48	got, err := readNetUNIX(fs.proc.Path("net/unix_without_inode"))
49	if err != nil {
50		t.Fatalf("failed to read UNIX socket data: %v", err)
51	}
52
53	testNetUNIX(t, noCheckInode, got)
54}
55
56func testNetUNIX(t *testing.T, testInode bool, got *NetUNIX) {
57	t.Helper()
58
59	// First verify that the input data matches a prepopulated structure.
60
61	want := []*NetUNIXLine{
62		{
63			KernelPtr: "0000000000000000",
64			RefCount:  2,
65			Flags:     1 << 16,
66			Type:      1,
67			State:     1,
68			Inode:     3442596,
69			Path:      "/var/run/postgresql/.s.PGSQL.5432",
70		},
71		{
72			KernelPtr: "0000000000000000",
73			RefCount:  10,
74			Flags:     1 << 16,
75			Type:      5,
76			State:     1,
77			Inode:     10061,
78			Path:      "/run/udev/control",
79		},
80		{
81			KernelPtr: "0000000000000000",
82			RefCount:  7,
83			Flags:     0,
84			Type:      2,
85			State:     1,
86			Inode:     12392,
87			Path:      "/dev/log",
88		},
89		{
90			KernelPtr: "0000000000000000",
91			RefCount:  3,
92			Flags:     0,
93			Type:      1,
94			State:     3,
95			Inode:     4787297,
96			Path:      "/var/run/postgresql/.s.PGSQL.5432",
97		},
98		{
99			KernelPtr: "0000000000000000",
100			RefCount:  3,
101			Flags:     0,
102			Type:      1,
103			State:     3,
104			Inode:     5091797,
105		},
106	}
107
108	// Enable the fixtures to be used for multiple tests by clearing the inode
109	// field when appropriate.
110	if !testInode {
111		for i := 0; i < len(want); i++ {
112			want[i].Inode = 0
113		}
114	}
115
116	if diff := cmp.Diff(want, got.Rows); diff != "" {
117		t.Fatalf("unexpected /proc/net/unix data (-want +got):\n%s", diff)
118	}
119
120	// Now test the field enumerations and ensure they match up correctly
121	// with the constants used to generate readable strings.
122
123	wantFlags := []NetUNIXFlags{
124		netUnixFlagListen,
125		netUnixFlagListen,
126		netUnixFlagDefault,
127		netUnixFlagDefault,
128		netUnixFlagDefault,
129	}
130
131	wantType := []NetUNIXType{
132		netUnixTypeStream,
133		netUnixTypeSeqpacket,
134		netUnixTypeDgram,
135		netUnixTypeStream,
136		netUnixTypeStream,
137	}
138
139	wantState := []NetUNIXState{
140		netUnixStateUnconnected,
141		netUnixStateUnconnected,
142		netUnixStateUnconnected,
143		netUnixStateConnected,
144		netUnixStateConnected,
145	}
146
147	var (
148		gotFlags []NetUNIXFlags
149		gotType  []NetUNIXType
150		gotState []NetUNIXState
151	)
152
153	for _, r := range got.Rows {
154		gotFlags = append(gotFlags, r.Flags)
155		gotType = append(gotType, r.Type)
156		gotState = append(gotState, r.State)
157	}
158
159	if diff := cmp.Diff(wantFlags, gotFlags); diff != "" {
160		t.Fatalf("unexpected /proc/net/unix flags (-want +got):\n%s", diff)
161	}
162
163	if diff := cmp.Diff(wantType, gotType); diff != "" {
164		t.Fatalf("unexpected /proc/net/unix types (-want +got):\n%s", diff)
165	}
166
167	if diff := cmp.Diff(wantState, gotState); diff != "" {
168		t.Fatalf("unexpected /proc/net/unix states (-want +got):\n%s", diff)
169	}
170}
171