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": {
|
|
|
|
Pass: "x",
|
|
|
|
Uid: "0",
|
|
|
|
Gid: "0",
|
|
|
|
Gecos: "root",
|
|
|
|
Home: "/root",
|
|
|
|
Shell: "/bin/bash",
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|