mirror of
https://github.com/opencontainers/runc.git
synced 2026-06-24 08:48:44 +00:00
Before commit7dc24868, when process.env was nil, prepareEnv returned a flag telling HOME is not set, and it was added. Commit7dc24868moved the functionality of adding HOME into prepareEnv but did not properly handle nil case. As a result, runc exec -p with process.json having no env set resulted in an exec with no HOME set. Fix this, and add unit and integration tests. Fixes:7dc24868("libct: switch to numeric UID/GID/groups") Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
69 lines
1.2 KiB
Go
69 lines
1.2 KiB
Go
package libcontainer
|
|
|
|
import (
|
|
"os/user"
|
|
"slices"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
func TestPrepareEnv(t *testing.T) {
|
|
u, err := user.Current()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
home := "HOME=" + u.HomeDir
|
|
uid, err := strconv.Atoi(u.Uid)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
tests := []struct {
|
|
env, wantEnv []string
|
|
}{
|
|
{
|
|
env: nil,
|
|
wantEnv: []string{home},
|
|
},
|
|
{
|
|
env: []string{},
|
|
wantEnv: []string{home},
|
|
},
|
|
{
|
|
env: []string{"HOME=/whoo", "FOO=bar"},
|
|
wantEnv: []string{"HOME=/whoo", "FOO=bar"},
|
|
},
|
|
{
|
|
env: []string{"A=a", "A=b", "A=c"},
|
|
wantEnv: []string{"A=c", home},
|
|
},
|
|
{
|
|
env: []string{"TERM=vt100", "HOME=/home/one", "HOME=/home/two", "TERM=xterm", "HOME=/home/three", "FOO=bar"},
|
|
wantEnv: []string{"TERM=xterm", "HOME=/home/three", "FOO=bar"},
|
|
},
|
|
{
|
|
env: []string{"HOME=", "HOME=/foo"},
|
|
wantEnv: []string{"HOME=/foo"},
|
|
},
|
|
{
|
|
env: []string{"HOME="},
|
|
wantEnv: []string{home},
|
|
},
|
|
{
|
|
env: []string{"HOME=/foo", "HOME="},
|
|
wantEnv: []string{home},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
env, err := prepareEnv(tc.env, uid)
|
|
if err != nil {
|
|
t.Error(err)
|
|
continue
|
|
}
|
|
if !slices.Equal(env, tc.wantEnv) {
|
|
t.Errorf("want %v, got %v", tc.wantEnv, env)
|
|
}
|
|
}
|
|
}
|