Debian Codegraph @

pathrs-lite/procfs/procfs_linux_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // SPDX-License-Identifier: MPL-2.0
2
3 //go:build linux
4
5 // Copyright (C) 2024-2025 Aleksa Sarai <[email protected]>
6 // Copyright (C) 2024-2025 SUSE LLC
7 //
8 // This Source Code Form is subject to the terms of the Mozilla Public
9 // License, v. 2.0. If a copy of the MPL was not distributed with this
10 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
11
12 package procfs_test
13
14 import (
15 "fmt"
16 "os"
17 "path/filepath"
18 "runtime"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23 "golang.org/x/sys/unix"
24
25 "github.com/cyphar/filepath-securejoin/pathrs-lite/internal/fd"
26 "github.com/cyphar/filepath-securejoin/pathrs-lite/procfs"
27 )
28
29 // This code is all actually tested in internal/procfs, this is mainly
30 // necessary to make sure our one-line wrappers are correct.
31
32 func TestOpenProcRoot(t *testing.T) {
33 t.Run("OpenProcRoot", func(t *testing.T) {
34 proc, err := procfs.OpenProcRoot()
35 require.NoError(t, err, "OpenProcRoot")
36 assert.NotNil(t, proc, "procfs *Handle")
37 assert.NoError(t, proc.Close(), "close handle")
38 })
39
40 t.Run("OpenUnsafeProcRoot", func(t *testing.T) {
41 proc, err := procfs.OpenUnsafeProcRoot()
42 require.NoError(t, err, "OpenUnsafeProcRoot")
43 assert.NotNil(t, proc, "procfs *Handle")
44 defer proc.Close() //nolint:errcheck // test code
45
46 // Make sure the handle actually is !subset=pid.
47 f, err := proc.OpenRoot(".")
48 require.NoError(t, err, "open root .")
49 err = fd.Faccessat(f, "uptime", unix.F_OK, unix.AT_SYMLINK_NOFOLLOW)
50 assert.NoError(t, err, "/proc/uptime should exist") //nolint:testifylint // this is an isolated operation so we can continue despite an error
51
52 assert.NoError(t, proc.Close(), "close handle")
53 })
54 }
55
56 type procRootFunc func() (*procfs.Handle, error)
57
58 func TestProcRoot(t *testing.T) {
59 for _, test := range []struct {
60 name string
61 procRootFn procRootFunc
62 }{
63 {"OpenProcRoot", procfs.OpenProcRoot},
64 {"OpenUnsafeProcRoot", procfs.OpenUnsafeProcRoot},
65 } {
66 test := test // copy iterator
67 t.Run(test.name, func(t *testing.T) {
68 proc, err := test.procRootFn()
69 require.NoError(t, err)
70 defer proc.Close() //nolint:errcheck // test code
71
72 t.Run("OpenThreadSelf", func(t *testing.T) {
73 // Make sure our tid checks below are correct.
74 runtime.LockOSThread()
75 defer runtime.UnlockOSThread()
76
77 stat, closer, err := proc.OpenThreadSelf("stat")
78 require.NoError(t, err, "open /proc/thread-self/stat")
79 if assert.NotNil(t, closer, "closer should be non-nil for /proc/thread-self") {
80 defer closer()
81 }
82 require.NotNil(t, stat, "open /proc/thread-self/stat")
83 defer stat.Close() //nolint:errcheck // test code
84
85 statData, err := os.ReadFile(fmt.Sprintf("/proc/self/fd/%d", stat.Fd()))
86 runtime.KeepAlive(stat)
87 require.NoError(t, err)
88 assert.Regexp(t, fmt.Sprintf("^%d ", unix.Gettid()), string(statData), "/proc/thread-self/stat should have tid prefix")
89
90 // Confirm that this is /proc/$pid/task/$tid, not /proc/$pid.
91 f, closer, err := proc.OpenThreadSelf("task")
92 require.ErrorIs(t, err, os.ErrNotExist, "/proc/thread-self should not have a 'task' dir")
93 if !assert.Nil(t, closer, "returned closer on error") {
94 defer closer()
95 }
96 if !assert.Nil(t, f, "returned *os.File on error") {
97 _ = f.Close()
98 }
99 })
100
101 t.Run("OpenSelf", func(t *testing.T) {
102 stat, err := proc.OpenSelf("stat")
103 require.NoError(t, err, "open /proc/self/stat")
104 require.NotNil(t, stat, "open /proc/self/stat")
105 defer stat.Close() //nolint:errcheck // test code
106
107 statData, err := os.ReadFile(fmt.Sprintf("/proc/self/fd/%d", stat.Fd()))
108 runtime.KeepAlive(stat)
109 require.NoError(t, err)
110 assert.Regexp(t, fmt.Sprintf("^%d ", os.Getpid()), string(statData), "/proc/self/stat should have pid prefix")
111
112 // Confirm that this is /proc/$pid, not /proc/$pid/task/$tid.
113 f, err := proc.OpenSelf("task")
114 require.NoError(t, err, "/proc/self has a 'task' dir")
115 require.NotNil(t, f, "open /proc/self/task")
116 _ = f.Close()
117 })
118
119 t.Run("OpenPid", func(t *testing.T) {
120 stat, err := proc.OpenPid(1, "stat")
121 require.NoError(t, err, "open /proc/1/stat")
122 require.NotNil(t, stat, "open /proc/1/stat")
123 defer stat.Close() //nolint:errcheck // test code
124
125 statData, err := os.ReadFile(fmt.Sprintf("/proc/self/fd/%d", stat.Fd()))
126 runtime.KeepAlive(stat)
127 require.NoError(t, err)
128 assert.Regexp(t, "^1 ", string(statData), "/proc/1/stat should have pid1 prefix")
129
130 // Confirm that this is /proc/$pid, not /proc/$pid/task/$tid.
131 f, err := proc.OpenPid(1, "task")
132 require.NoError(t, err, "/proc/1 has a 'task' dir")
133 require.NotNil(t, f, "open /proc/1/task")
134 _ = f.Close()
135 })
136
137 t.Run("OpenRoot", func(t *testing.T) {
138 uptime, err := proc.OpenRoot("uptime")
139 require.NoError(t, err, "open /proc/uptime")
140 require.NotNil(t, uptime, "open /proc/uptime")
141 defer uptime.Close() //nolint:errcheck // test code
142 })
143 })
144 }
145 }
146
147 func TestProcSelfFdReadlink(t *testing.T) {
148 root, err := os.Open(".")
149 require.NoError(t, err)
150
151 fullPath, err := procfs.ProcSelfFdReadlink(root)
152 require.NoError(t, err, "ProcSelfFdReadlink")
153
154 cwd, err := os.Getwd()
155 require.NoError(t, err, "getwd")
156 cwd, err = filepath.EvalSymlinks(cwd)
157 require.NoError(t, err, "expand symlinks getwd")
158
159 assert.Equal(t, cwd, fullPath, "ProcSelfFdReadlink('.')")
160 }