mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 06:05:06 +03:00
Omit privileged setgroups(2) call to test set[ug]id(2) to same user
Every user may change its user/group ID to the current one. With an ugly hack, skip the superuser-only part of chuser() to exercise this rest of the code path in regular tests.
This commit is contained in:
parent
988cd0e207
commit
12acd710be
2 changed files with 8 additions and 10 deletions
|
@ -12,7 +12,7 @@ import (
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
func chuser(input string) error {
|
func chuser(input string, skipSetgroupsForTests ...any) error {
|
||||||
givenUser, givenGroup, _ := strings.Cut(input, ":")
|
givenUser, givenGroup, _ := strings.Cut(input, ":")
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -43,8 +43,10 @@ func chuser(input string) error {
|
||||||
gid, _ = strconv.Atoi(usr.Gid)
|
gid, _ = strconv.Atoi(usr.Gid)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := unix.Setgroups([]int{gid}); err != nil {
|
if len(skipSetgroupsForTests) == 0 {
|
||||||
return fmt.Errorf("setgroups: %d: %v", gid, err)
|
if err := unix.Setgroups([]int{gid}); err != nil {
|
||||||
|
return fmt.Errorf("setgroups: %d: %v", gid, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := unix.Setgid(gid); err != nil {
|
if err := unix.Setgid(gid); err != nil {
|
||||||
return fmt.Errorf("setgid: %d: %v", gid, err)
|
return fmt.Errorf("setgid: %d: %v", gid, err)
|
||||||
|
|
|
@ -44,18 +44,14 @@ func TestInvalidUserid (t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change to the current user by ID.
|
// Change to the current user by username and group ID.
|
||||||
func TestCurrentUserid (t *testing.T) {
|
func TestCurrentUser (t *testing.T) {
|
||||||
usr, err := user.Current()
|
usr, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if usr.Uid != "0" {
|
if err = chuser(usr.Username + ":" + usr.Gid, true /* skipSetGroupsForTests */); err != nil {
|
||||||
t.Skip("setgroups(2): Only the superuser may set new groups.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = chuser(usr.Uid); err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue