Merge branch 'develop' into patch-1

This commit is contained in:
Neil 2024-12-18 20:53:28 +01:00 committed by GitHub
commit 7f3bf4935d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 42 additions and 18 deletions

View file

@ -14,6 +14,12 @@ import (
func chuser(input string) error {
givenUser, givenGroup, _ := strings.Cut(input, ":")
if givenUser == "" {
return fmt.Errorf("user is empty")
}
if strings.Contains(input, ":") && givenGroup == "" {
return fmt.Errorf("group is empty")
}
var (
err error

View file

@ -4,33 +4,33 @@
package main
import (
"testing"
"os/user"
"testing"
)
// Usernames must not contain a number sign.
func TestEmptyString (t *testing.T) {
func TestEmptyString(t *testing.T) {
if chuser("") == nil {
t.Fatal("the empty string is not a valid user")
}
}
// Either omit delimiter and group, or omit both.
func TestEmptyGroup (t *testing.T) {
func TestEmptyGroup(t *testing.T) {
if chuser("0:") == nil {
t.Fatal("the empty group is not allowed")
}
}
// Either user only or user and group.
func TestGroupOnly (t *testing.T) {
func TestGroupOnly(t *testing.T) {
if chuser(":0") == nil {
t.Fatal("group only is not allowed")
}
}
// Usenames must not contain the number sign.
func TestInvalidUsername (t *testing.T) {
func TestInvalidUsername(t *testing.T) {
const username = "#user"
if chuser(username) == nil {
t.Fatalf("'%s' is not a valid username", username)
@ -38,14 +38,14 @@ func TestInvalidUsername (t *testing.T) {
}
// User IDs must be non-negative.
func TestInvalidUserid (t *testing.T) {
func TestInvalidUserid(t *testing.T) {
if chuser("-1") == nil {
t.Fatal("User ID cannot be negative")
}
}
// Change to the current user by ID.
func TestCurrentUserid (t *testing.T) {
func TestCurrentUserid(t *testing.T) {
usr, err := user.Current()
if err != nil {
t.Fatal(err)
@ -61,7 +61,7 @@ func TestCurrentUserid (t *testing.T) {
}
// Change to a common user by name.
func TestCommonUsername (t *testing.T) {
func TestCommonUsername(t *testing.T) {
usr, err := user.Current()
if err != nil {
t.Fatal(err)