Files
magpie/internal/update/admin_test.go
T
yetone eed140c4d9 update: a Mac update no longer leaves a second magpie in Spotlight (#3965)
Stage unpacked the new app to /Applications/.magpie-update/app/magpie.app
and Install moved the old one to .magpie-update/old.app. LaunchServices
registers a folder ending in .app in /Applications within seconds, and
Install only deleted the folder, so Spotlight kept listing the gone path
as another magpie.

- Stage ditto's the zip (one top folder, magpie.app, checked against the
  real v0.1.2119 zip: no FinderInfo bundle bit, no __MACOSX) into
  .magpie-update/unzip and renames magpie.app to .magpie-update/payload
  at once, then has lsregister -u forget the unzip path in case it was
  seen in that moment. codesign verifies a bundle named payload the same
  (checked on the real zip: verify ok, a tampered copy fails).
- Install and InstallAsAdmin move the old app to <stage>/old, and remove
  filepath.Dir(staged), which is still .magpie-update (or the cache's
  update folder on the admin path); renames stay on one volume.
- ForgetOldStaging runs once from the GUI's updater start, in a
  goroutine: lsregister -u on .magpie-update/app/magpie.app and
  .magpie-update/old.app beside the app and in ~/Library/Caches/magpie/
  update. lsregister is /System/Library/Frameworks/CoreServices.framework/
  Frameworks/LaunchServices.framework/Support/lsregister (present on
  macOS 27.2); failures are ignored, a 10s cap, marker in magpie's cache.
  /Volumes/magpie (an old DMG) is left alone.

Run at this head (base ffa07395e):
- mote (macOS 27.2): gofmt -l clean; go build, GOOS=windows go build,
  GOOS=linux go build -tags nogui; go vet, GOOS=windows go vet and
  GOOS=linux go vet -tags nogui on internal/update, internal/gui,
  internal/cli; go test internal/update internal/gui internal/proc
  internal/appdir: build and vet ok;
  update, proc, appdir ok; gui FAIL only in TestMainWindowCorners/
  macOS_26_SDK (the titlebar toolbar on macOS 27), already filed as
  #3949 and not in code this touches
- win-run (xiaomi-gpu, Windows 11): go test ./internal/update ok.
- wsl-run (magpie-desktop): go test ./internal/update ok.
- Real LaunchServices on mote, a fake bundle id (com.example...) in
  /Applications/lstest-3965, not magpie.app: the old flow by hand
  registered .magpie-update/app/magpie.app within 30s and kept it after
  the folder was deleted. A throwaway test running this Stage + Install
  with the real lsregister: after 45s staged and 15s installed,
  lsregister -dump shows only lstest-3965/magpie.app; with stagedName
  set back to magpie.app the same run shows .magpie-update/magpie.app
  registered (red). ForgetOldStaging with the real lsregister removed
  both registered .magpie-update paths after their folders were deleted.
  Everything registered was unregistered and the folder removed.

Broken by hand, each red: asideName "old.app" (TestInstallAsAdminAside-
IsNoApp), stagedName "magpie.app" (TestInstallStagesNoApp, TestStage-
UnpacksNoApp), RemoveAll two folders up as before (TestInstallStagesNoApp:
the Applications folder went with it), no unregister of the unzip path
(TestStageUnpacksNoApp), no marker (TestForgetOldStaging).

Not tried: a real magpie update of the installed /Applications/magpie.app
with Spotlight's UI (the fake bundle stood in), the administrator-password
path for real, the full go test ./... suite.
2026-10-30 09:23:35 +08:00

127 lines
3.9 KiB
Go

package update
import (
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
// app makes a stand-in .app holding the text v.
func app(t *testing.T, path, v string) {
t.Helper()
if err := os.MkdirAll(filepath.Join(path, "Contents"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(path, "Contents", "v"), []byte(v), 0o644); err != nil {
t.Fatal(err)
}
}
func version(t *testing.T, path string) string {
t.Helper()
b, err := os.ReadFile(filepath.Join(path, "Contents", "v"))
if err != nil {
t.Fatal(err)
}
return string(b)
}
// A folder magpie may not write to: Install fails with a permission error,
// leaves the app as it was, and keeps what it staged.
func TestInstallNeedsAdmin(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("a read-only folder and the administrator's password are a Mac's and Linux's")
}
if os.Getuid() == 0 {
t.Skip("root writes anywhere")
}
root := t.TempDir()
apps := filepath.Join(root, "Applications")
bundle := filepath.Join(apps, "magpie.app")
app(t, bundle, "old")
staged := filepath.Join(root, "cache", "update", stagedName)
app(t, staged, "new")
os.Chmod(apps, 0o555)
defer os.Chmod(apps, 0o755)
if stageDir(apps) == filepath.Join(apps, ".magpie-update") {
t.Error("stageDir picked a folder it can't write to")
}
err := Install(staged, bundle)
if !NeedsAdmin(err) {
t.Fatalf("Install = %v, want a permission error", err)
}
if version(t, bundle) != "old" || version(t, staged) != "new" {
t.Error("a failed Install changed something")
}
}
// The script InstallAsAdmin runs as root swaps the apps, awkward names and
// all; run here as the user in a folder they own.
func TestSwapScript(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the swap runs as root under /bin/sh, on a Mac or Linux")
}
dir := filepath.Join(t.TempDir(), `it's "a" \ $(dir) `)
bundle, staged, old := filepath.Join(dir, "magpie.app"), filepath.Join(dir, "s", stagedName), filepath.Join(dir, "s", asideName)
app(t, bundle, "old")
app(t, staged, "new")
if b, err := exec.Command("/bin/sh", "-c", swapScript(staged, bundle, old)).CombinedOutput(); err != nil {
t.Fatalf("%v: %s", err, b)
}
if version(t, bundle) != "new" {
t.Error("the new app isn't in place")
}
if _, err := os.Stat(old); !os.IsNotExist(err) {
t.Error("the old app was left behind")
}
// a failed second move puts the app back
os.RemoveAll(staged)
if exec.Command("/bin/sh", "-c", swapScript(staged, bundle, old)).Run() == nil {
t.Error("the swap succeeded without a staged app")
}
if version(t, bundle) != "new" {
t.Error("the app wasn't put back")
}
}
// On a Mac the script goes to osascript as an AppleScript string, and a
// dismissed prompt is ErrCanceled.
func TestAsAdmin(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("osascript")
}
defer func(r func(string, ...string) ([]byte, error)) { run = r }(run)
var got []string
run = func(name string, args ...string) ([]byte, error) {
got = append([]string{name}, args...)
return []byte("execution error: User canceled. (-128)"), errors.New("exit status 1")
}
script := swapScript(`/a "b"/s.app`, `/x\y.app`, "/o.app")
if err := asAdmin(script); !errors.Is(err, ErrCanceled) {
t.Errorf("asAdmin = %v, want ErrCanceled", err)
}
if len(got) != 3 || got[0] != "osascript" || !strings.Contains(got[2], `"`+appleString(script)+`"`) || !strings.HasSuffix(got[2], "with administrator privileges") {
t.Errorf("ran %q", got)
}
// AppleScript unescapes it back to the script
out, err := exec.Command("osascript", "-e", `"`+appleString(script)+`"`).Output()
if err != nil || strings.TrimSuffix(string(out), "\n") != script {
t.Errorf("AppleScript read %q (%v), want %q", out, err, script)
}
}
func TestStuck(t *testing.T) {
if Stuck("/private/var/folders/xy/T/AppTranslocation/1234/d/magpie.app") != "translocated" {
t.Error("a translocated app isn't stuck")
}
if Stuck(t.TempDir()) != "" {
t.Error("a writable folder is stuck")
}
}