2020-06-08 14:52:16 +08:00
|
|
|
package staros
|
|
|
|
|
|
|
|
|
|
import (
|
2026-06-09 18:10:19 +08:00
|
|
|
"errors"
|
|
|
|
|
"os/user"
|
|
|
|
|
"strconv"
|
2020-06-08 14:52:16 +08:00
|
|
|
"testing"
|
2026-06-09 18:10:19 +08:00
|
|
|
"time"
|
2020-06-08 14:52:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Test_Disk(t *testing.T) {
|
2026-06-09 18:10:19 +08:00
|
|
|
disk, err := DiskUsageE(".")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if disk.All == 0 {
|
|
|
|
|
t.Fatal("expected non-zero total disk size")
|
|
|
|
|
}
|
|
|
|
|
if disk.Used+disk.Free != disk.All {
|
|
|
|
|
t.Fatalf("expected used + free == all, got used=%d free=%d all=%d", disk.Used, disk.Free, disk.All)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCpuUsageDoesNotPanic(t *testing.T) {
|
|
|
|
|
_ = CpuUsage(time.Millisecond)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestWhoamiGID(t *testing.T) {
|
|
|
|
|
_, gid, _, _, _, err := Whoami()
|
|
|
|
|
if errors.Is(err, ERR_UNSUPPORTED) {
|
|
|
|
|
t.Skip(err)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
current, err := user.Current()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
expected, err := strconv.Atoi(current.Gid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if gid != expected {
|
|
|
|
|
t.Fatalf("expected gid %d, got %d", expected, gid)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIdentityLookupFunctions(t *testing.T) {
|
|
|
|
|
current, err := user.Current()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Skipf("user.Current unavailable: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wantUID, uidErr := strconv.ParseUint(current.Uid, 10, 32)
|
|
|
|
|
wantGID, gidErr := strconv.ParseUint(current.Gid, 10, 32)
|
|
|
|
|
|
|
|
|
|
uid, gid, home, err := GetUidGid(current.Username)
|
|
|
|
|
if uidErr == nil && gidErr == nil {
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("GetUidGid failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if uid != uint32(wantUID) || gid != uint32(wantGID) || home != current.HomeDir {
|
|
|
|
|
t.Fatalf("GetUidGid mismatch: uid=%d gid=%d home=%q", uid, gid, home)
|
|
|
|
|
}
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
t.Fatalf("GetUidGid should reject non-numeric ids: uid=%q gid=%q", current.Uid, current.Gid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uid, err = GetUid(current.Username)
|
|
|
|
|
if uidErr == nil {
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("GetUid failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if uid != uint32(wantUID) {
|
|
|
|
|
t.Fatalf("GetUid mismatch: got=%d want=%d", uid, wantUID)
|
|
|
|
|
}
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
t.Fatalf("GetUid should reject non-numeric uid %q", current.Uid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gid, err = GetGid(current.Username)
|
|
|
|
|
if gidErr == nil {
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("GetGid failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if gid != uint32(wantGID) {
|
|
|
|
|
t.Fatalf("GetGid mismatch: got=%d want=%d", gid, wantGID)
|
|
|
|
|
}
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
t.Fatalf("GetGid should reject non-numeric gid %q", current.Gid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group, err := user.LookupGroupId(current.Gid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Skipf("user.LookupGroupId unavailable: %v", err)
|
|
|
|
|
}
|
|
|
|
|
groupID, groupErr := strconv.ParseUint(group.Gid, 10, 32)
|
|
|
|
|
gotGroupID, err := GetGidByName(group.Name)
|
|
|
|
|
if groupErr == nil {
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("GetGidByName failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if gotGroupID != uint32(groupID) {
|
|
|
|
|
t.Fatalf("GetGidByName mismatch: got=%d want=%d", gotGroupID, groupID)
|
|
|
|
|
}
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
t.Fatalf("GetGidByName should reject non-numeric gid %q", group.Gid)
|
|
|
|
|
}
|
2020-06-08 14:52:16 +08:00
|
|
|
}
|