package mac import ( "context" "encoding/xml" "errors" "io" "os" "path/filepath" "slices" "strings" "testing" ) // The installer runs only as a Standard user with equipo in its name: // never root, never an administrator, never a shared "equipo" or a // person's own account (own-Mac mode is not in scope). func TestRefuseAccount(t *testing.T) { for _, c := range []struct { a Account want string }{ {Account{Name: "equipo-paul", UID: 502, Groups: []string{"staff", "everyone", "_developer"}}, ""}, {Account{Name: "root", UID: 0, Groups: []string{"wheel"}}, "runner.mac.root"}, {Account{Name: "equipo-paul", UID: 0}, "runner.mac.root"}, {Account{Name: "equipo-paul", UID: 502, Groups: []string{"staff", "admin"}}, "runner.mac.admin"}, {Account{Name: "paul", UID: 501, Groups: []string{"staff"}}, "runner.mac.user"}, {Account{Name: "paulequip", UID: 503, Groups: []string{"staff"}}, "runner.mac.user"}, // macOS's Add User dialog makes "paulequipo" from "Paul Equipo": // any name with equipo in it, in any case, is a dedicated user. {Account{Name: "paulequipo", UID: 503, Groups: []string{"staff"}}, ""}, {Account{Name: "paul-equipo", UID: 503, Groups: []string{"staff"}}, ""}, {Account{Name: "PaulEquipo", UID: 503, Groups: []string{"staff"}}, ""}, {Account{Name: "paulequipo", UID: 503, Groups: []string{"staff", "admin"}}, "runner.mac.admin"}, {Account{Name: "paulequipo", UID: 0}, "runner.mac.root"}, } { if got := RefuseAccount(c.a); got != c.want { t.Fatalf("%+v: %q, want %q", c.a, got, c.want) } } } func TestReadAccount(t *testing.T) { out := map[string]string{"-un": "equipo-paul\n", "-u": "502\n", "-Gn": "staff everyone localaccounts _appserverusr admin _developer\n"} a, err := ReadAccount(context.Background(), func(_ context.Context, name string, args ...string) (string, error) { if name != "/usr/bin/id" { return "", errors.New(name) } return out[args[0]], nil }) if err != nil || a.Name != "equipo-paul" || a.UID != 502 || RefuseAccount(a) != "runner.mac.admin" { t.Fatalf("%+v %v", a, err) } } // Another person's home counts as closed only when it can be neither // listed, passed through, nor read: a home this user can traverse (but // not list) is open, as is one with a readable file. Symlinked homes are // listed with where they lead; a /Users that cannot be read is an error. func TestHomes(t *testing.T) { if os.Geteuid() == 0 { t.Skip("root reads every home") } users := t.TempDir() own := filepath.Join(users, "equipo-paul") elsewhere := t.TempDir() for _, d := range []string{"equipo-paul", "paul", "Shared", "tito", "ana"} { if err := os.MkdirAll(filepath.Join(users, d), 0700); err != nil { t.Fatal(err) } } if err := os.WriteFile(filepath.Join(users, ".localized"), nil, 0600); err != nil { t.Fatal(err) } if err := os.Symlink(elsewhere, filepath.Join(users, "linked")); err != nil { t.Fatal(err) } if err := os.Symlink(own, filepath.Join(users, "me")); err != nil { t.Fatal(err) } others, err := OtherHomes(users, own) want := []string{elsewhere, filepath.Join(users, "Shared"), filepath.Join(users, "ana"), filepath.Join(users, "linked"), filepath.Join(users, "paul"), filepath.Join(users, "tito")} slices.Sort(want) if err != nil || !slices.Equal(others, want) { t.Fatalf("%v %v", others, err) } // paul: closed. tito: traversable only (0711). ana: closed but... a // readable file inside needs traversal, so give ana 0711 and a file. for d, mode := range map[string]os.FileMode{"paul": 0, "tito": 0o111, "ana": 0o111} { if d == "ana" { if err := os.WriteFile(filepath.Join(users, d, ".zshrc"), []byte("x"), 0644); err != nil { t.Fatal(err) } } if err := os.Chmod(filepath.Join(users, d), mode); err != nil { t.Fatal(err) } defer os.Chmod(filepath.Join(users, d), 0700) } if err := os.Chmod(elsewhere, 0); err != nil { t.Fatal(err) } defer os.Chmod(elsewhere, 0700) got := ExposedHomes(others, Exposed) if !slices.Equal(got, []string{filepath.Join(users, "ana"), filepath.Join(users, "tito")}) { t.Fatalf("exposed %v", got) } if Exposed(filepath.Join(users, "paul")) || Exposed(elsewhere) { t.Fatal("a closed home taken as open") } if _, err := OtherHomes(filepath.Join(users, "missing"), own); err == nil { t.Fatal("an unreadable /Users taken as no homes") } if !slices.Contains(DenyList(Paths{Home: own, HomesUnknown: true}), "/Users") { t.Fatal("unknown homes not denied wholesale") } } // The launchd agent: a well-formed plist, the runner with its env file, // kept alive only after a failure (an update exits 75 and restarts; a // superseded runner exits 0 and stays down), a Standard process, paths // escaped. func TestLaunchAgent(t *testing.T) { raw := LaunchAgent("/Users/equipo-paul/.local/lib/equipo-runner/equipo-runner", "/Users/equipo-paul/.config/equipo-runner/runner.env", "/Users/equipo-paul/Library/Logs/equipo-runner & ") plist := parsePlist(t, raw) if plist["Label"] != AgentLabel || plist["RunAtLoad"] != "true" || plist["ProcessType"] != "Standard" || plist["KeepAlive.SuccessfulExit"] != "false" { t.Fatalf("%v", plist) } if plist["ProgramArguments"] != "/Users/equipo-paul/.local/lib/equipo-runner/equipo-runner|run|--env-file|/Users/equipo-paul/.config/equipo-runner/runner.env" { t.Fatalf("arguments %q", plist["ProgramArguments"]) } if plist["StandardErrorPath"] != "/Users/equipo-paul/Library/Logs/equipo-runner & /runner.log" || plist["Umask"] != "63" { t.Fatalf("%v", plist) } if ExitUpdated == 0 { t.Fatal("an update must exit non-zero to be restarted") } } // parsePlist flattens a plist dict: key to value, arrays joined by |, // nested dicts as parent.child. func parsePlist(t *testing.T, raw []byte) map[string]string { t.Helper() d := xml.NewDecoder(strings.NewReader(string(raw))) d.Strict = true out := map[string]string{} var stack []string key, inArray := "", "" var arr []string for { tok, err := d.Token() if err == io.EOF { break } if err != nil { t.Fatalf("not well formed: %v", err) } switch e := tok.(type) { case xml.StartElement: switch e.Name.Local { case "key": var k string if err := d.DecodeElement(&k, &e); err != nil { t.Fatal(err) } key = strings.Join(append(append([]string{}, stack...), k), ".") case "string", "integer": var v string if err := d.DecodeElement(&v, &e); err != nil { t.Fatal(err) } if inArray != "" { arr = append(arr, v) } else { out[key] = v } case "true", "false": out[key] = e.Name.Local case "array": inArray, arr = key, nil case "dict": if key != "" { stack = append(stack, key[strings.LastIndex(key, ".")+1:]) } } case xml.EndElement: switch e.Name.Local { case "array": out[inArray] = strings.Join(arr, "|") inArray = "" case "dict": if len(stack) > 0 { stack = stack[:len(stack)-1] } } } } return out } // srt is pinned: the lockfile the installer ships names 0.0.81 with its // integrity hash, and every package in it has one. func TestSRTPinned(t *testing.T) { if !SRTPinned() { t.Fatal("srt not pinned") } _, lock := SRTPackage() if strings.Count(string(lock), `"integrity": "sha512-`) < 5 { t.Fatal("a package without an integrity hash") } }