2024-05-16 11:39:30 +00:00
|
|
|
package execuser
|
|
|
|
|
|
|
|
|
|
import (
|
2026-02-16 17:15:10 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2024-05-16 11:39:30 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-16 17:15:10 +00:00
|
|
|
func TestReadEnvFromProcFile(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
environPath := filepath.Join(dir, "environ")
|
|
|
|
|
|
2024-05-16 11:39:30 +00:00
|
|
|
testCases := []struct {
|
2026-02-16 17:15:10 +00:00
|
|
|
name string
|
|
|
|
|
environ string
|
|
|
|
|
envVar string
|
|
|
|
|
expected string
|
2024-05-16 11:39:30 +00:00
|
|
|
}{
|
|
|
|
|
{
|
2026-02-16 17:15:10 +00:00
|
|
|
name: "DISPLAY found",
|
|
|
|
|
environ: "HOME=/home/foo\x00DISPLAY=:0\x00LANG=en_US.UTF-8",
|
|
|
|
|
envVar: "DISPLAY",
|
|
|
|
|
expected: ":0",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "DISPLAY :1",
|
|
|
|
|
environ: "HOME=/home/foo\x00DISPLAY=:1\x00LANG=en_US.UTF-8",
|
|
|
|
|
envVar: "DISPLAY",
|
|
|
|
|
expected: ":1",
|
2024-05-16 11:39:30 +00:00
|
|
|
},
|
|
|
|
|
{
|
2026-02-16 17:15:10 +00:00
|
|
|
name: "DISPLAY not present",
|
|
|
|
|
environ: "HOME=/home/foo\x00LANG=en_US.UTF-8",
|
|
|
|
|
envVar: "DISPLAY",
|
|
|
|
|
expected: "",
|
2024-05-16 11:39:30 +00:00
|
|
|
},
|
|
|
|
|
{
|
2026-02-16 17:15:10 +00:00
|
|
|
name: "empty environ",
|
|
|
|
|
environ: "",
|
|
|
|
|
envVar: "DISPLAY",
|
|
|
|
|
expected: "",
|
2024-05-16 11:39:30 +00:00
|
|
|
},
|
|
|
|
|
{
|
2026-02-16 17:15:10 +00:00
|
|
|
name: "does not match prefix substring",
|
|
|
|
|
environ: "DISPLAY_NUM=5\x00DISPLAY=:2",
|
|
|
|
|
envVar: "DISPLAY",
|
|
|
|
|
expected: ":2",
|
2024-05-16 11:39:30 +00:00
|
|
|
},
|
|
|
|
|
{
|
2026-02-16 17:15:10 +00:00
|
|
|
name: "other env var",
|
|
|
|
|
environ: "HOME=/home/foo\x00DISPLAY=:0\x00WAYLAND_DISPLAY=wayland-0",
|
|
|
|
|
envVar: "WAYLAND_DISPLAY",
|
|
|
|
|
expected: "wayland-0",
|
2024-05-16 11:39:30 +00:00
|
|
|
},
|
|
|
|
|
}
|
2026-02-16 17:15:10 +00:00
|
|
|
|
2024-05-16 11:39:30 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2026-02-16 17:15:10 +00:00
|
|
|
require.NoError(t, os.WriteFile(environPath, []byte(tc.environ), 0o644))
|
|
|
|
|
result, err := readEnvFromProcFile(environPath, tc.envVar)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, tc.expected, result)
|
2024-05-16 11:39:30 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-16 17:15:10 +00:00
|
|
|
|
|
|
|
|
func TestReadEnvFromProcFileMissing(t *testing.T) {
|
|
|
|
|
_, err := readEnvFromProcFile("/nonexistent/path/environ", "DISPLAY")
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
}
|