mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-29 04:55:31 +00:00
util/osuser: add unit test for parseGroupIds
Updates #11682 Signed-off-by: Percy Wegmann <percy@tailscale.com>
This commit is contained in:
parent
e985c6e58f
commit
b7e5122226
@ -46,5 +46,9 @@ func getGroupIdsWithId(usernameOrUID string) ([]string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("running 'id' command: %w", err)
|
return nil, fmt.Errorf("running 'id' command: %w", err)
|
||||||
}
|
}
|
||||||
return strings.Split(string(out), "\x00"), nil
|
return parseGroupIds(out), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseGroupIds(cmdOutput []byte) []string {
|
||||||
|
return strings.Split(strings.Trim(string(cmdOutput), "\n\x00"), "\x00")
|
||||||
}
|
}
|
||||||
|
26
util/osuser/group_ids_test.go
Normal file
26
util/osuser/group_ids_test.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
package osuser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseGroupIds(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in string
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{"5000\x005001\n", []string{"5000", "5001"}},
|
||||||
|
{"5000\n", []string{"5000"}},
|
||||||
|
{"\n", []string{""}},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
actual := parseGroupIds([]byte(test.in))
|
||||||
|
if !slices.Equal(actual, test.expected) {
|
||||||
|
t.Errorf("parseGroupIds(%q) = %q, wanted %s", test.in, actual, test.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user