2024-11-18 00:37:07 +03:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/user"
|
2024-12-13 16:54:14 +00:00
|
|
|
"testing"
|
2024-11-18 00:37:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Usernames must not contain a number sign.
|
2024-12-13 16:54:14 +00:00
|
|
|
func TestEmptyString(t *testing.T) {
|
2024-11-18 00:37:07 +03:00
|
|
|
if chuser("") == nil {
|
|
|
|
t.Fatal("the empty string is not a valid user")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either omit delimiter and group, or omit both.
|
2024-12-13 16:54:14 +00:00
|
|
|
func TestEmptyGroup(t *testing.T) {
|
2024-11-18 00:37:07 +03:00
|
|
|
if chuser("0:") == nil {
|
|
|
|
t.Fatal("the empty group is not allowed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either user only or user and group.
|
2024-12-13 16:54:14 +00:00
|
|
|
func TestGroupOnly(t *testing.T) {
|
2024-11-18 00:37:07 +03:00
|
|
|
if chuser(":0") == nil {
|
|
|
|
t.Fatal("group only is not allowed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Usenames must not contain the number sign.
|
2024-12-13 16:54:14 +00:00
|
|
|
func TestInvalidUsername(t *testing.T) {
|
2024-11-18 00:37:07 +03:00
|
|
|
const username = "#user"
|
|
|
|
if chuser(username) == nil {
|
|
|
|
t.Fatalf("'%s' is not a valid username", username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// User IDs must be non-negative.
|
2024-12-13 16:54:14 +00:00
|
|
|
func TestInvalidUserid(t *testing.T) {
|
2024-11-18 00:37:07 +03:00
|
|
|
if chuser("-1") == nil {
|
|
|
|
t.Fatal("User ID cannot be negative")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change to the current user by ID.
|
2024-12-13 16:54:14 +00:00
|
|
|
func TestCurrentUserid(t *testing.T) {
|
2024-11-18 00:37:07 +03:00
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if usr.Uid != "0" {
|
|
|
|
t.Skip("setgroups(2): Only the superuser may set new groups.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = chuser(usr.Uid); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change to a common user by name.
|
2024-12-13 16:54:14 +00:00
|
|
|
func TestCommonUsername(t *testing.T) {
|
2024-11-18 00:37:07 +03:00
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if usr.Uid != "0" {
|
|
|
|
t.Skip("setgroups(2): Only the superuser may set new groups.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := chuser("nobody"); err != nil {
|
|
|
|
if _, ok := err.(user.UnknownUserError); ok {
|
|
|
|
t.Skip(err)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|