Files
magpie/internal/update/blocked_test.go
T
yetone 9ca64d104f update, gui: a new version Windows won't start is never left in place — tried before it goes in, and a restart into one that doesn't say it is up puts the running one back; that version isn't downloaded again by the clock, and Settings › About › Version says why (#894, hisiling) — on Windows 11 with Smart App Control on, the update put the new unsigned exe in place of the running one and restarted into it; CodeIntegrity (3033/3077) refused it, magpie never started again and the working version sat in .old. Now, on Windows, the downloaded exe is first started suspended with no window and ended at once (Windows checks a program when its process is created), and one refused stays out: the running exe isn't moved. At the restart the new process is given MAGPIE_STARTED, a file it writes first thing (AwaitPredecessor); the old one waits up to 20s for it and, if the start fails (ERROR_SYSTEM_INTEGRITY_POLICY_VIOLATION 4551, 1260 or any other), the new process quits before saying so, or it says nothing in time (then it is ended), puts the running exe back over the new one (the new one renamed .blocked if it can't go) and keeps running instead of quitting; nothing notes it as updated in the app. The version is noted in the config folder (update-blocked.json, this machine's only, never synced; gone once a version at least as new runs); the clock's checks leave it alone and show why, a Check clicked downloads and tries it once more. Settings › About › Version: for an application control policy, "0.1.x didn't start on this computer: Windows' Smart App Control (or another app control policy) blocked the new version, which isn't signed. magpie stays on 0.1.y. Download it and run it yourself, or turn Smart App Control off and check again", else the reason, with Download and Check; the error in full on hover. en/zh/ja/de. A user who double-clicks the refused exe is refused by Windows before magpie runs; only signing helps there. TestRelaunchRefusedPutsBack (4551 at the start), TestRelaunchSilentPutsBack (no word in time, killed; quit at once), TestRelaunchReportedStays, TestReportStarted, TestBlockedNote and TestUpdateThatWontStart (the restart doesn't quit, the note, the clock not downloading it again, a Check that does) are new and fail without the change; on Windows TestInstallBinaryRefusedStaysOut, TestTrialStart and TestStartCheckedPutsBackForReal (real processes: not a program, quits at once, runs on silent) pass on the Windows 11 test box with the whole internal/update package (Smart App Control itself isn't on there, so the refusal is simulated). update-blocked.test.cjs (Chromium and WebKit, en and zh: the reason and both versions, full error on hover, Download opens the release, Check downloads, the other reason, no select, no scroll, no left border) fails without the change; update-check, update-mirror, update-auto, update-wait and update-download-percent pass in WebKit; click-scroll's request-list case fails now and then the same on main. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 19:43:31 +08:00

150 lines
4.5 KiB
Go

package update
import (
"errors"
"os"
"path/filepath"
"strings"
"sync/atomic"
"syscall"
"testing"
"time"
)
// relaunchFiles is an update just put in: exe the new version, old the
// running one moved aside.
func relaunchFiles(t *testing.T) (exe, old string) {
t.Helper()
dir := t.TempDir()
exe, old = filepath.Join(dir, "magpie.exe"), filepath.Join(dir, "magpie.exe.old")
os.WriteFile(exe, []byte("new version"), 0o755)
os.WriteFile(old, []byte("running version"), 0o755)
return exe, old
}
func stubLaunch(t *testing.T, f func(exe string, args, env []string) (<-chan struct{}, func(), error)) {
t.Helper()
was := launch
launch = f
t.Cleanup(func() { launch = was })
}
func wantPutBack(t *testing.T, exe, old string) {
t.Helper()
if b, err := os.ReadFile(exe); err != nil || string(b) != "running version" {
t.Errorf("%s = %q (%v), want the running version back", filepath.Base(exe), b, err)
}
if _, err := os.Stat(old); !os.IsNotExist(err) {
t.Errorf("%s still there: %v", filepath.Base(old), err)
}
}
// #894: Smart App Control refuses the new, unsigned exe when it is
// started (ERROR_SYSTEM_INTEGRITY_POLICY_VIOLATION): the running version
// goes back in its place, and the error says it was Windows' policy.
func TestRelaunchRefusedPutsBack(t *testing.T) {
exe, old := relaunchFiles(t)
stubLaunch(t, func(string, []string, []string) (<-chan struct{}, func(), error) {
return nil, nil, &os.PathError{Op: "fork/exec", Path: exe, Err: errIntegrityPolicy}
})
err := RelaunchBinary(exe, old, true, "settings")
var be *BlockedError
if !errors.As(err, &be) || !be.Policy() {
t.Fatalf("err = %v, want a policy *BlockedError", err)
}
if !strings.Contains(err.Error(), "Smart App Control") {
t.Errorf("err doesn't say why: %v", err)
}
wantPutBack(t, exe, old)
}
// A new version that starts and says nothing in time is ended and the
// running one put back; one that quits at once likewise.
func TestRelaunchSilentPutsBack(t *testing.T) {
was := startWait
startWait = 200 * time.Millisecond
t.Cleanup(func() { startWait = was })
for _, quits := range []bool{false, true} {
exe, old := relaunchFiles(t)
var killed atomic.Bool
stubLaunch(t, func(string, []string, []string) (<-chan struct{}, func(), error) {
exited := make(chan struct{})
if quits {
close(exited)
return exited, func() {}, nil
}
return exited, func() { killed.Store(true); close(exited) }, nil
})
err := RelaunchBinary(exe, old, false, "")
var be *BlockedError
if !errors.As(err, &be) || be.Policy() {
t.Fatalf("quits=%v: err = %v, want a *BlockedError not of policy", quits, err)
}
if !quits && !killed.Load() {
t.Error("the silent new version was left running")
}
wantPutBack(t, exe, old)
}
}
// A new version that says it is up stays; what was moved aside is left for
// it to remove.
func TestRelaunchReportedStays(t *testing.T) {
exe, old := relaunchFiles(t)
var args []string
stubLaunch(t, func(_ string, a, env []string) (<-chan struct{}, func(), error) {
args = a
for _, kv := range env {
if p, ok := strings.CutPrefix(kv, startedEnv+"="); ok {
go func() { time.Sleep(50 * time.Millisecond); os.WriteFile(p, []byte("1"), 0o644) }()
}
}
return make(chan struct{}), func() { t.Error("killed") }, nil
})
if err := RelaunchBinary(exe, old, false, ""); err != nil {
t.Fatal(err)
}
if len(args) != 1 || args[0] != "tray" {
t.Errorf("args = %q", args)
}
if b, _ := os.ReadFile(exe); string(b) != "new version" {
t.Errorf("exe = %q", b)
}
if _, err := os.Stat(old); err != nil {
t.Errorf("old: %v", err)
}
}
// What the started magpie writes is what the one starting it waits for.
func TestReportStarted(t *testing.T) {
p := filepath.Join(t.TempDir(), "started")
t.Setenv(startedEnv, p)
reportStarted()
if _, err := os.Stat(p); err != nil {
t.Fatal(err)
}
if os.Getenv(startedEnv) != "" {
t.Error("left in the environment, for what it starts")
}
}
// The note of a refused version holds while it is newer than the one
// running, and goes once that has caught up.
func TestBlockedNote(t *testing.T) {
t.Cleanup(ClearBlocked)
NoteBlocked("0.1.20", &BlockedError{Err: errIntegrityPolicy})
b := ReadBlocked("0.1.19")
if b == nil || b.Version != "0.1.20" || !b.Policy || b.Error == "" {
t.Fatalf("ReadBlocked = %+v", b)
}
if b := ReadBlocked("0.1.20"); b != nil {
t.Fatalf("caught up: %+v", b)
}
if b := ReadBlocked("0.1.19"); b != nil {
t.Fatalf("not forgotten: %+v", b)
}
if policyRefused(syscall.Errno(2)) || !policyRefused(&os.PathError{Err: syscall.Errno(1260)}) {
t.Error("policyRefused")
}
}