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 (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
filename := "/etc/passwd"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
passwdContents string
|
|
|
|
createFile bool
|
|
|
|
wantEntries Entries
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"parse works": {
|
|
|
|
passwdContents: "root:x:0:0:root:/root:/bin/bash\n",
|
|
|
|
createFile: true,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantEntries: Entries{
|
2022-03-22 11:03:15 -04:00
|
|
|
"root": {
|
2022-07-25 08:43:02 -04:00
|
|
|
Password: "x",
|
|
|
|
UID: "0",
|
|
|
|
GID: "0",
|
|
|
|
GECOS: "root",
|
|
|
|
Directory: "/root",
|
|
|
|
Shell: "/bin/bash",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
"multiple lines": {
|
|
|
|
passwdContents: "root:x:0:0:root:/root:/bin/bash\nfoo:y:1:2:bar:baz:sh",
|
|
|
|
createFile: true,
|
|
|
|
wantEntries: Entries{
|
|
|
|
"root": {
|
|
|
|
Password: "x",
|
|
|
|
UID: "0",
|
|
|
|
GID: "0",
|
|
|
|
GECOS: "root",
|
|
|
|
Directory: "/root",
|
|
|
|
Shell: "/bin/bash",
|
|
|
|
},
|
|
|
|
"foo": {
|
|
|
|
Password: "y",
|
|
|
|
UID: "1",
|
|
|
|
GID: "2",
|
|
|
|
GECOS: "bar",
|
|
|
|
Directory: "baz",
|
|
|
|
Shell: "sh",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"passwd is corrupt": {
|
|
|
|
passwdContents: "too:few:fields\n",
|
|
|
|
createFile: true,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"file does not exist": {
|
|
|
|
createFile: false,
|
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()
|
|
|
|
if tc.createFile {
|
|
|
|
assert.NoError(afero.WriteFile(fs, filename, []byte(tc.passwdContents), 0o644))
|
|
|
|
}
|
|
|
|
passwd := Passwd{}
|
|
|
|
entries, err := passwd.Parse(fs)
|
|
|
|
|
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.wantEntries, entries)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|