Files
magpie/internal/gui/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

106 lines
3.5 KiB
Go

package gui
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"sync/atomic"
"syscall"
"testing"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/update"
)
// #894: on Windows with Smart App Control on, the new unsigned exe was
// put in and refused at the restart, and magpie never started again. A
// restart into a version that doesn't start leaves this one running and
// says why; the clock's checks don't download that version again, and
// a check asked for tries it once more.
func TestUpdateThatWontStart(t *testing.T) {
body := []byte("new magpie")
sum := sha256.Sum256(body)
var downloads atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/asset" {
downloads.Add(1)
w.Write(body)
return
}
json.NewEncoder(w).Encode(update.Release{Version: "0.1.11", Assets: map[string]update.Asset{
update.BinaryAsset(): {URL: "http://" + r.Host + "/asset", SHA256: hex.EncodeToString(sum[:])},
}})
}))
defer srv.Close()
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
oldV := Version
Version = "0.1.10"
defer func() { Version = oldV }()
t.Cleanup(update.ClearBlocked)
// the running binary, in a folder of its own
exe := filepath.Join(t.TempDir(), "magpie")
os.WriteFile(exe, []byte("running magpie"), 0o755)
self, _ := os.Stat(exe)
staged := exe + ".new"
os.WriteFile(staged, body, 0o755)
o := updates
u := &updater{exe: exe, self: self, staged: staged, state: "ready", latest: &update.Release{Version: "0.1.11"}}
updates = u
defer func() { updates = o }()
var relaunched atomic.Int32
wasRelaunch := relaunchBinary
relaunchBinary = func(exe, old string, window bool, view string) error {
relaunched.Add(1)
// what update.RelaunchBinary does when Windows refuses the new exe:
// the running one put back, and says so
os.WriteFile(exe, []byte("running magpie"), 0o755)
return &update.BlockedError{Err: &os.PathError{Op: "fork/exec", Path: exe, Err: syscall.Errno(4551)}}
}
defer func() { relaunchBinary = wasRelaunch }()
if restartToUpdate(false, true, "settings") {
t.Fatal("restartToUpdate said to quit, with nothing left that starts")
}
if relaunched.Load() != 1 {
t.Fatalf("relaunched %d times", relaunched.Load())
}
j := u.json()
if j.State != "blocked" || j.Blocked == nil || j.Blocked.Version != "0.1.11" || !j.Blocked.Policy {
t.Fatalf("after the refused restart: %+v", j)
}
if _, err := os.Stat(filepath.Join(settings.Dir(), updatedInAppFile)); !os.IsNotExist(err) {
t.Errorf("noted as updated in the app: %v", err)
}
// magpie starts again later (the note is on disk): the clock's check
// leaves the version alone
u2 := &updater{exe: exe}
u2.self, _ = os.Stat(exe)
u2.blocked = update.ReadBlocked(Version)
if u2.blocked == nil {
t.Fatal("the refused version isn't noted")
}
u2.checkByClock()
if j := u2.json(); j.State != "blocked" || j.Blocked == nil || downloads.Load() != 0 {
t.Fatalf("clock's check: %+v, %d downloads", j, downloads.Load())
}
// Check, clicked: it is downloaded to be tried again
if self, err := update.Executable(); err == nil {
defer os.Remove(self + ".new") // where StageBinary puts it
}
u2.check()
if j := u2.json(); j.State != "ready" || downloads.Load() != 1 {
t.Fatalf("clicked check: %+v, %d downloads", j, downloads.Load())
}
// once this version is the one running, the note goes
if update.ReadBlocked("0.1.11") != nil {
t.Error("the note outlived the version")
}
}