package creds import ( "errors" "os" "path/filepath" "slices" "strings" "testing" ) // The fixtures, each named for its date and machine: // - ls-led-users-2026-11-28-tito-studio.txt is recorded: `ls -led // /Users/*` on tito-studio as paulca, 2026-11-28 17:55Z (task 1, R2). // - ls-led-constructed-2026-11-29-docs-t1.txt is constructed on docs-t1 // in the recording's format, for the ACL cases no recording shows. // uids are the Studio's (R2: paulequipo is 506). var uids = map[string]int{"paulequipo": 506, "paulca": 502, "vicky": 507, "root": 0, "billhorsman": 501, "ci": 503, "drbparsons": 504, "murraysum": 505} // facts reads a fixture into the guard's reader, each owner's uid as the // Studio has it. func facts(t *testing.T, names ...string) map[string]Fact { t.Helper() out := map[string]Fact{} for _, name := range names { raw, err := os.ReadFile(filepath.Join("testdata", name)) if err != nil { t.Fatal(err) } list, err := ParseLS(string(raw)) if err != nil { t.Fatalf("%s: %v", name, err) } for _, f := range list { f.UID = uids[f.Owner] out[f.Path] = f } } return out } func reader(m map[string]Fact) func(string) (Fact, error) { return func(p string) (Fact, error) { f, ok := m[p] if !ok { return Fact{Path: p, Missing: true, UID: -1}, nil } return f, nil } } // private is a 0700 directory of paulequipo's with no ACL. func private(p string) Fact { return Fact{Path: p, Kind: 'd', Perm: 0700, Owner: "paulequipo", UID: 506} } const home = "/Users/paulequipo" // studio is the Studio's runner layout, every folder private: the state // dir, the config dir and the accounts root, and the folders between. func studio() map[string]Fact { m := map[string]Fact{} for _, p := range []string{home, home + "/.local", home + "/.local/share", home + "/.local/share/equipo-runner", home + "/.local/share/equipo-runner/state", home + "/.config", home + "/.config/equipo-runner", home + "/.local/share/equipo", home + "/.local/share/equipo/accounts"} { m[p] = private(p) } return m } var studioIn = Input{User: "paulequipo", UID: 506, Home: home, Roots: []string{home + "/.local/share/equipo-runner/state", home + "/.config/equipo-runner", home + "/.local/share/equipo/accounts"}} // codes is each problem as path=code. func codes(r Result) []string { var out []string for _, p := range r.Problems { out = append(out, p.Path+"="+p.Code) } return out } // TestHomeGuard: the guard passes only a private layout, and names what // to change for each way another user could reach it. func TestHomeGuard(t *testing.T) { t.Run("0700 passes", func(t *testing.T) { r := Check(studioIn, reader(studio())) if !r.OK() || r.Reason() != "" || r.Fix() != "" { t.Fatalf("a private layout failed: %v", codes(r)) } }) t.Run("an allow entry for the user itself passes", func(t *testing.T) { m := studio() f := facts(t, "ls-led-constructed-2026-11-29-docs-t1.txt")[home+"/.self"] f.Path = home m[home] = f if r := Check(studioIn, reader(m)); !r.OK() { t.Fatalf("the user's own entry and a deny failed it: %v", codes(r)) } }) // The Studio's own home today (recorded, R2): drwxr-x---+ group // staff, and every person is in staff. It must fail, naming chmod 700 ~. t.Run("the Studio's paulequipo today", func(t *testing.T) { m := studio() m[home] = facts(t, "ls-led-users-2026-11-28-tito-studio.txt")[home] r := Check(studioIn, reader(m)) if r.OK() || r.Reason() != CodeHomeMode || r.Fix() != "chmod 700 ~" || len(r.Problems) != 1 { t.Fatalf("paulequipo's recorded home: %v %q", codes(r), r.Fix()) } }) t.Run("every recorded home fails for its own user", func(t *testing.T) { for path, f := range facts(t, "ls-led-users-2026-11-28-tito-studio.txt") { if f.Owner == "root" { continue } r := Check(Input{User: f.Owner, UID: f.UID, Home: path}, reader(map[string]Fact{path: f})) if r.Reason() != CodeHomeMode { t.Fatalf("%s: %v", path, codes(r)) } } }) // Each case changes one folder of the private layout to the fixture's // folder of that name; the guard must fail with code, naming fix. cases := []struct { name, at, fixture, code, fix string }{ {"0750 on a root", home + "/.config/equipo-runner", ".g750", CodeMode, "chmod 700 ~/.config/equipo-runner"}, {"0750 on the home", home, ".g750", CodeHomeMode, "chmod 700 ~"}, {"group read alone on the home", home, ".g740", CodeHomeMode, "chmod 700 ~"}, {"group read alone on a root", home + "/.local/share/equipo/accounts", ".g740", CodeMode, "chmod 700 ~/.local/share/equipo/accounts"}, {"other read alone on a root", home + "/.local/share/equipo-runner/state", ".o704", CodeMode, "chmod 700 ~/.local/share/equipo-runner/state"}, {"an ACL grant to everyone", home, ".everyone", CodeACL, "chmod -a# 0 ~"}, {"an ACL grant to one user", home + "/.local/share/equipo/accounts", ".vicky", CodeACL, "chmod -a# 0 ~/.local/share/equipo/accounts"}, {"a write grant", home + "/.local/share/equipo-runner/state", ".write", CodeACL, "chmod -a# 0 ~/.local/share/equipo-runner/state"}, {"a writesecurity grant", home + "/.config/equipo-runner", ".writesecurity", CodeACL, "chmod -a# 0 ~/.config/equipo-runner"}, {"a chown grant to a group", home + "/.config/equipo-runner", ".chown", CodeACL, "chmod -a# 0 ~/.config/equipo-runner"}, {"an inherited grant", home + "/.local/share/equipo/accounts", ".inherited", CodeACL, "chmod -a# 1 ~/.local/share/equipo/accounts"}, {"an ACL grant on an ancestor", home + "/.local/share", ".vicky", CodeACL, "chmod -a# 0 ~/.local/share"}, {"a symlinked ancestor", home + "/.local", ".link", CodeLink, "replace the link ~/.local with a folder"}, {"a symlinked root", home + "/.config/equipo-runner", ".link", CodeLink, "replace the link ~/.config/equipo-runner with a folder"}, {"an ancestor owned by another user", home + "/.local/share", ".vickys", CodeOwner, "sudo chown paulequipo ~/.local/share (an administrator)"}, {"a root owned by another user", home + "/.local/share/equipo/accounts", ".vickys", CodeOwner, "sudo chown paulequipo ~/.local/share/equipo/accounts (an administrator)"}, } constructed := facts(t, "ls-led-constructed-2026-11-29-docs-t1.txt") for _, c := range cases { t.Run(c.name, func(t *testing.T) { m := studio() f := constructed[home+"/"+c.fixture] f.Path = c.at m[c.at] = f r := Check(studioIn, reader(m)) if r.OK() || !slices.Contains(codes(r), c.at+"="+c.code) || !strings.Contains(r.Fix(), c.fix) { t.Fatalf("%s: %v %q", c.name, codes(r), r.Fix()) } }) } t.Run("an ancestor readable by others but not writable passes", func(t *testing.T) { m := studio() f := m[home+"/.config"] f.Perm = 0755 m[home+"/.config"] = f if r := Check(studioIn, reader(m)); !r.OK() { t.Fatalf("a 0755 ancestor failed: %v", codes(r)) } }) t.Run("an ancestor writable by its group fails", func(t *testing.T) { m := studio() f := m[home+"/.config"] f.Perm = 0770 m[home+"/.config"] = f r := Check(studioIn, reader(m)) if !slices.Contains(codes(r), home+"/.config="+CodeMode) || !strings.Contains(r.Fix(), "chmod go-w ~/.config") { t.Fatalf("a group-writable ancestor: %v %q", codes(r), r.Fix()) } }) // Fix round 1, #5: a protected root is held to the root rule however // it is reached, first as another root's ancestor or not. t.Run("nested roots, both orders", func(t *testing.T) { m := studio() cfg, state := home+"/.config/equipo-runner", home+"/.config/equipo-runner/state" m[state] = private(state) f := m[cfg] f.Perm = 0o755 m[cfg] = f for _, roots := range [][]string{{state, cfg}, {cfg, state}} { r := Check(Input{User: "paulequipo", UID: 506, Home: home, Roots: roots}, reader(m)) if !slices.Contains(codes(r), cfg+"="+CodeMode) || !strings.Contains(r.Fix(), "chmod 700 ~/.config/equipo-runner") { t.Fatalf("roots %v: %v %q", roots, codes(r), r.Fix()) } } }) t.Run("a root outside the home", func(t *testing.T) { in := studioIn in.Roots = append(slices.Clone(in.Roots), "/Users/Shared/equipo") m := studio() m["/Users/Shared/equipo"] = private("/Users/Shared/equipo") r := Check(in, reader(m)) if !slices.Contains(codes(r), "/Users/Shared/equipo="+CodeOutside) { t.Fatalf("a root outside the home: %v", codes(r)) } }) t.Run("a root that escapes through ..", func(t *testing.T) { in := studioIn in.Roots = []string{home + "/../vicky/equipo"} if r := Check(in, reader(studio())); r.Reason() != CodeOutside { t.Fatalf("a root through ..: %v", codes(r)) } }) t.Run("a missing root", func(t *testing.T) { m := studio() delete(m, home+"/.local/share/equipo/accounts") r := Check(studioIn, reader(m)) if r.Reason() != CodeMissing || !strings.Contains(r.Fix(), "mkdir -m 700 ~/.local/share/equipo/accounts") { t.Fatalf("a missing root: %v %q", codes(r), r.Fix()) } }) t.Run("an unreadable fact decides nothing", func(t *testing.T) { r := Check(studioIn, func(p string) (Fact, error) { if p == home+"/.config" { return Fact{}, ErrUnreadable } return reader(studio())(p) }) if r.OK() || !slices.Contains(codes(r), home+"/.config="+CodeUnknown) { t.Fatalf("an unreadable ancestor: %v", codes(r)) } }) t.Run("a uid that is not the user's", func(t *testing.T) { m := studio() f := m[home] f.UID = 507 m[home] = f if r := Check(studioIn, reader(m)); r.Reason() != CodeOwner { t.Fatalf("the home's uid is another's: %v", codes(r)) } }) } // TestParseLSRefusesWhatItCannotRead: a line it does not understand, or // a "+" with no entries, or entries out of order, is ErrUnreadable. func TestParseLSRefusesWhatItCannotRead(t *testing.T) { for _, out := range []string{ "drwx------+ 19 paulequipo staff 608 28 Nov 15:22 /Users/paulequipo\n", "drwx------+ 19 paulequipo staff 608 28 Nov 15:22 /Users/paulequipo\n 0: user:vicky maybe read\n", "drwx------+ 19 paulequipo staff 608 28 Nov 15:22 /Users/paulequipo\n 1: user:vicky allow read\n", "total 0\n", " 0: user:vicky allow read\n", } { if _, err := ParseLS(out); !errors.Is(err, ErrUnreadable) { t.Fatalf("read %q: %v", out, err) } } f, err := ParseLS("drwxr-x---+ 19 paulequipo staff 608 28 Nov 15:22 /Users/paul equipo\n 0: group:everyone deny delete\n") if err != nil || len(f) != 1 || f[0].Path != "/Users/paul equipo" || f[0].Perm != 0750 || len(f[0].ACL) != 1 || f[0].ACL[0].Allow { t.Fatalf("a path with a space: %+v %v", f, err) } } // This boot has an identity and a time, the same on every read. func TestBootIsStable(t *testing.T) { a, err := Boot() if err != nil || a.ID == "" || a.Time.IsZero() { t.Fatalf("%+v %v", a, err) } if b, err := Boot(); err != nil || b != a { t.Fatalf("%+v then %+v (%v)", a, b, err) } }