package accounts import ( "context" "errors" "strings" "testing" ) // An account is read by its name (novox/hq ADR 0325 §4): getent reads an all-digit key as a uid, so "1000" would // answer with whoever has uid 1000 — refused, never taken for the account named. func TestAnAccountIsReadByItsNameNeverItsNumber(t *testing.T) { db := map[string]string{ "nox": "nox:x:1003:1003::/home/nox:/bin/sh", "1000": "jochen:x:1000:1000::/home/jochen:/bin/zsh", // what getent answers for a uid } e := Exec{Run: func(_ context.Context, name string, args ...string) (string, error) { if line, ok := db[args[len(args)-1]]; ok { return line + "\n", nil } return "", errors.New("getent exited 2") }} if uid, gid, found, err := e.IDs(context.Background(), "nox"); err != nil || !found || uid != 1003 || gid != 1003 { t.Fatalf("nox: %d %d %v %v", uid, gid, found, err) } if _, _, found, err := e.IDs(context.Background(), "1000"); found || err == nil || !strings.Contains(err.Error(), "jochen") { t.Fatalf("an all-digit name was read as a uid: found=%v err=%v", found, err) } if _, _, found, err := e.IDs(context.Background(), "ghost"); found || err != nil { t.Fatalf("an absent account: found=%v err=%v", found, err) } }