InstallBinaryAside starts the download suspended (trialStart), ends
it, sets the running exe aside and renames the download over it. A
program that checks an executable as its process starts (an antivirus)
can hold the file a moment after the process has ended, and the rename
then fails with a sharing violation; the report says the first install
of every update failed this way. The rename (and the put-back of the
old exe on failure) now goes through moveIn, which tries again over
asideWaits (100 ms to 1.5 s) while Windows says the file is held
(errors 32/33), as moveAside already does for the running exe. Any
other failure is returned at once. The Windows CI step now runs the
trial tests too (TestTrialStart and the new ones).
What was run:
- Windows box (Windows 10.0.26200 x64, Defender real-time on, a
cross-compiled update.test.exe in a temp dir with a sandbox
USERPROFILE/APPDATA/LOCALAPPDATA/TEMP):
- TestInstallBinaryWaitsForHeldDownload (new; the download held open
for 150 ms): old update.go FAIL "rename ...magpie.exe.new
...magpie.exe: The process cannot access the file because it is
being used by another process."; new PASS (0.31 s).
- TestInstallBinaryRightAfterTheTrial (new; real trialStart, then the
install) and TestTrialStart pass on old and new code there.
- The whole update package passes; the CI pattern
^(TestInstallBinary|TestMoveAside|TestTrialStart) passes.
- Rollback tests now use 1 ms waits so a file held for good fails
fast.
- The real update, in a sandbox: v0.1.1243's magpie-windows-amd64.exe
run as `magpie update` twice in separate sandbox homes: both updated
to 0.1.1244, exit 0.
- macOS: gofmt -l clean; go vet darwin, GOOS=linux -tags nogui,
GOOS=windows clean; go test -p 4 ./... (see the last commit of this
push for the result).
Not reproduced: the hold itself. On this box the real trial-then-
install path succeeded every time (10/10 with a 12 MB exe, 10/10 with
a 74 MB magpie.exe, 2/2 real updates), and origin/main's TestTrialStart
passed 3/3. The fix is verified with an injected hold, not a real
antivirus one.
153 lines
4.9 KiB
Go
153 lines
4.9 KiB
Go
package update
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// The other install tests put text in for the new version, which Windows
|
|
// won't start: they are about moving files, so the try is left out.
|
|
func init() { canStart = func(string) error { return nil } }
|
|
|
|
// #894: a download Windows won't start isn't put in; the running exe
|
|
// stays where it is and the download goes.
|
|
func TestInstallBinaryRefusedStaysOut(t *testing.T) {
|
|
was := canStart
|
|
canStart = func(string) error { return &os.PathError{Op: "fork/exec", Err: errIntegrityPolicy} }
|
|
t.Cleanup(func() { canStart = was })
|
|
exe, staged := rollbackFiles(t)
|
|
old, err := InstallBinaryAside(staged, exe)
|
|
var be *BlockedError
|
|
if !errors.As(err, &be) || !be.Policy() || old != "" {
|
|
t.Fatalf("InstallBinaryAside = %q, %v", old, err)
|
|
}
|
|
wantInstallFile(t, exe, "old version")
|
|
for _, p := range []string{staged, exe + ".old"} {
|
|
if _, err := os.Stat(p); !os.IsNotExist(err) {
|
|
t.Errorf("%s: %v", filepath.Base(p), err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// trialStart starts a real program, under the .new name a download has,
|
|
// without running it, and fails on what isn't one.
|
|
func TestTrialStart(t *testing.T) {
|
|
ping := filepath.Join(os.Getenv("SystemRoot"), "System32", "PING.EXE")
|
|
b, err := os.ReadFile(ping)
|
|
if err != nil {
|
|
t.Skip("no PING.EXE:", err)
|
|
}
|
|
dir := t.TempDir()
|
|
good := filepath.Join(dir, "magpie.exe.new")
|
|
os.WriteFile(good, b, 0o755)
|
|
if err := trialStart(good); err != nil {
|
|
t.Fatalf("a program: %v", err)
|
|
}
|
|
bad := filepath.Join(dir, "text.exe.new")
|
|
os.WriteFile(bad, []byte("not a program"), 0o755)
|
|
if err := trialStart(bad); err == nil {
|
|
t.Fatal("text started")
|
|
}
|
|
// ended, not left running: its file can go, once whatever checks a
|
|
// program as it starts (an antivirus) has let go, as moveIn waits (#2272)
|
|
for i := 0; ; i++ {
|
|
err := os.Remove(good)
|
|
if err == nil {
|
|
break
|
|
}
|
|
if i == len(asideWaits) {
|
|
t.Fatalf("still held: %v", err)
|
|
}
|
|
time.Sleep(asideWaits[i])
|
|
}
|
|
}
|
|
|
|
// #2272: a download something still holds a moment after the trial (an
|
|
// antivirus that checks a program as it starts) goes in once it is let
|
|
// go, rather than failing the install.
|
|
func TestInstallBinaryWaitsForHeldDownload(t *testing.T) {
|
|
exe, staged := rollbackFiles(t)
|
|
release := holdForRollbackTest(t, staged)
|
|
if err := os.Rename(staged, staged+".probe"); !errors.Is(err, errSharingViolation) {
|
|
t.Fatalf("fixture: renaming the held download = %v, want sharing violation", err)
|
|
}
|
|
go func() { time.Sleep(150 * time.Millisecond); release() }()
|
|
old, err := InstallBinaryAside(staged, exe)
|
|
if err != nil {
|
|
t.Fatal("install while the download is held a moment:", err)
|
|
}
|
|
wantInstallFile(t, exe, "new version")
|
|
wantInstallFile(t, old, "old version")
|
|
if _, err := os.Stat(staged); !os.IsNotExist(err) {
|
|
t.Errorf("download left: %v", err)
|
|
}
|
|
}
|
|
|
|
// #2272: the install as an update runs it, the real trial start first and
|
|
// the move straight after, on a real program under the .new name a
|
|
// download has, its bytes new to this computer.
|
|
func TestInstallBinaryRightAfterTheTrial(t *testing.T) {
|
|
ping := filepath.Join(os.Getenv("SystemRoot"), "System32", "PING.EXE")
|
|
b, err := os.ReadFile(ping)
|
|
if err != nil {
|
|
t.Skip("no PING.EXE:", err)
|
|
}
|
|
was := canStart
|
|
canStart = trialStart
|
|
t.Cleanup(func() { canStart = was })
|
|
exe, staged := rollbackFiles(t)
|
|
prog := append(b, []byte(t.Name()+time.Now().String())...) // a file no scanner has seen
|
|
if err := os.WriteFile(staged, prog, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := InstallBinaryAside(staged, exe); err != nil {
|
|
t.Fatal("install after the trial:", err)
|
|
}
|
|
if got, _ := os.ReadFile(exe); string(got) != string(prog) {
|
|
t.Fatal("the new version is not in place")
|
|
}
|
|
}
|
|
|
|
// The restart for real: a new version that can't be started, one that
|
|
// quits at once and one that runs on without saying it is up are each
|
|
// ended, their file let go of, and the running exe put back.
|
|
func TestStartCheckedPutsBackForReal(t *testing.T) {
|
|
ping := filepath.Join(os.Getenv("SystemRoot"), "System32", "PING.EXE")
|
|
b, err := os.ReadFile(ping)
|
|
if err != nil {
|
|
t.Skip("no PING.EXE:", err)
|
|
}
|
|
was := startWait
|
|
startWait = 2 * time.Second
|
|
t.Cleanup(func() { startWait = was })
|
|
for _, c := range []struct {
|
|
name string
|
|
prog []byte
|
|
args []string
|
|
}{
|
|
{"not a program", []byte("not a program"), nil},
|
|
{"quits at once", b, []string{"-n", "1", "127.0.0.1"}},
|
|
{"never says it is up", b, []string{"-n", "60", "127.0.0.1"}},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
exe, old := filepath.Join(dir, "magpie.exe"), filepath.Join(dir, "magpie.exe.old")
|
|
os.WriteFile(exe, c.prog, 0o755)
|
|
os.WriteFile(old, []byte("running version"), 0o755)
|
|
err := startChecked(exe, old, c.args, os.Environ())
|
|
var be *BlockedError
|
|
if !errors.As(err, &be) {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
t.Log(err)
|
|
wantInstallFile(t, exe, "running version")
|
|
if _, err := os.Stat(old); !os.IsNotExist(err) {
|
|
t.Errorf("old left: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|