2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-16 11:32:00 -04:00
|
|
|
package user
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestGetLinuxUser(t *testing.T) {
|
|
|
|
username := "user"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
passwdContents string
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
|
|
|
wantUser LinuxUser
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"get works": {
|
2022-06-13 10:23:19 -04:00
|
|
|
passwdContents: "user:x:1000:1000:user:/var/home/user:/bin/bash\n",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
|
|
|
wantUser: LinuxUser{
|
2022-03-22 11:03:15 -04:00
|
|
|
Username: "user",
|
2022-06-13 10:23:19 -04:00
|
|
|
Home: "/var/home/user",
|
|
|
|
UID: 1000,
|
|
|
|
GID: 1000,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"user does not exist": {
|
|
|
|
passwdContents: "",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"parse fails": {
|
|
|
|
passwdContents: "invalid contents\n",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"invalid uid": {
|
2022-06-13 10:23:19 -04:00
|
|
|
passwdContents: "user:x:invalid:1000:user:/var/home/user:/bin/bash\n",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"invalid gid": {
|
2022-06-13 10:23:19 -04:00
|
|
|
passwdContents: "user:x:1000:invalid:user:/var/home/user:/bin/bash\n",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
assert.NoError(afero.WriteFile(fs, "/etc/passwd", []byte(tc.passwdContents), 0o755))
|
2022-05-16 11:32:00 -04:00
|
|
|
manager := NewLinuxUserManagerFake(fs)
|
2022-06-13 10:23:19 -04:00
|
|
|
user, err := manager.GetLinuxUser(username)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantUser, user)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnsureLinuxUserExists(t *testing.T) {
|
|
|
|
username := "user"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-05-16 11:32:00 -04:00
|
|
|
userCreator *StubUserCreator
|
|
|
|
wantErr bool
|
|
|
|
wantUser LinuxUser
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"create works": {
|
2022-05-16 11:32:00 -04:00
|
|
|
userCreator: &StubUserCreator{},
|
|
|
|
wantErr: false,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantUser: LinuxUser{
|
2022-03-22 11:03:15 -04:00
|
|
|
Username: "user",
|
2022-06-13 10:23:19 -04:00
|
|
|
Home: "/var/home/user",
|
|
|
|
UID: 1000,
|
|
|
|
GID: 1000,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"create fails": {
|
2022-05-16 11:32:00 -04:00
|
|
|
userCreator: &StubUserCreator{
|
2022-03-22 11:03:15 -04:00
|
|
|
createUserErr: errors.New("create fails"),
|
|
|
|
},
|
2022-05-16 11:32:00 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fs := afero.NewMemMapFs()
|
2022-05-16 11:32:00 -04:00
|
|
|
manager := NewLinuxUserManagerFake(fs)
|
|
|
|
tc.userCreator.fs = fs
|
|
|
|
manager.Creator = tc.userCreator
|
2022-03-22 11:03:15 -04:00
|
|
|
user, err := manager.EnsureLinuxUserExists(context.Background(), username)
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantUser, user)
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.ElementsMatch([]string{username}, tc.userCreator.usernames)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 11:32:00 -04:00
|
|
|
func TestStringInSlice(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
testSlice := []string{"abc", "efg", "xyz"}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-05-16 11:32:00 -04:00
|
|
|
assert.True(stringInSlice("efg", testSlice))
|
|
|
|
assert.False(stringInSlice("hij", testSlice))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|