An rtk.exe that no package manager installed (rtk's own Windows release unzipped into %USERPROFILE%\.local\bin, say) had no upgrader, so the Library page showed RTK's update with no usable Upgrade. LoKi (lokic7123-star) hit it on v0.1.1285 and again on 0.1.1320; Edward4226 reproduced it and wrote this patch (his bc2056804, author kept). rtkUpgrader now gives such an rtk.exe the rtk-windows-release action (shown as "rtk's own Windows release"): on amd64, when winget's folders, cargo's folder and winget's own records/winget list didn't claim it. UpgradeRTK then takes RTKLatest's version, downloads checksums.txt and rtk-x86_64-pc-windows-msvc.zip from rtk-ai/rtk's release through source.DoOfficial, checks the zip's SHA-256 against checksums.txt, takes rtk.exe out of it, writes it beside the old one and swaps it in with steady.Rename, putting the old one back if the swap fails. Changed from Edward's patch in review: - The action no longer names a version: it read rtkLatest under the lock RTKLatest holds while it asks GitHub (up to ~16s), so ReadRTK, a page draw, would wait on it. UpgradeRTK already falls back to the last known version through RTKLatest. - A winget that couldn't say whether it owns the rtk (timed out, failed, couldn't run) is no answer, not "not winget's": askWingetOwns now returns (yes, known); only winget missing, exit 0 without rtk-ai.rtk, or 0x8A150014 (no package) count as known-no. The winget arm above is unchanged (yes only when winget listed it). Without this, a winget timeout handed a possibly winget-owned rtk to the standalone path. - Only a zip member named rtk.exe is taken (no "any .exe" fallback, which could pick rtk-proxy's), and an oversized one is refused rather than cut short. - An aside (rtk.exe.old-<pid>) an earlier upgrade couldn't remove, because an agent was running that rtk, is removed on the next upgrade. - The arch is a var (rtkArch) so the upgrader test runs on arm64 hosts instead of skipping; the redundant winget/cargo folder checks are gone (the switch's earlier cases already return for those). - Two of the patch's tests failed as written on macOS (ReplacesBinary counted VERSION as a leftover; UpgradeRunsTheInstall compared the script rtkPath finds there with rtk.exe's text); both fixed. - New tests: TestRTKWindowsStandaloneAsksWinget (no winget / unlisted / listed / failed / hung), TestRTKWindowsReleaseZipNeedsRTKExe, the arm64 case, and a stale aside in TestRTKWindowsReleaseSwapsTheFile. Run at the cherry-pick on origin/mainef6f3749d, with these changes: - gofmt -l internal/library: nothing. go vet ./... (darwin), GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok. - go test -count=1 ./internal/library/ in a sandbox HOME: ok (207s). The full ./... suite was not run here; it runs in a combined suite. The last edit after that run was comments only (re-vetted). - Red on the old code, each broken by hand and restored: - standalone arm returning nil (as origin/main has it): TestRTKWindowsStandaloneUpgrader "standalone: upgrader []; want rtk-windows-release" and TestRTKWindowsStandaloneAsksWinget "no winget: upgrader []". - winget's unknown taken as not-owned (Edward's semantics): TestRTKWindowsStandaloneAsksWinget "winget failed: upgrader [rtk-windows-release ...]; want none". - UpgradeRTK running the action as an installer: TestRTKWindowsReleaseReplacesBinary and ...UpgradeRunsTheInstall 'exec: "rtk-windows-release": executable file not found'. Real use on the Windows box (Windows amd64, sandbox USERPROFILE, LOCALAPPDATA and APPDATA under C:\Users\yeton\magpie-2006-sb, PATH only the sandbox .local\bin and System32; a throwaway driver calling ReadRTK and UpgradeRTK, not committed): - rtk.exe 1.0.0 from rtk's own release in .local\bin: ReadRTK showed latest 1.0.1, Upgrade "rtk's own Windows release"; UpgradeRTK fetched the real v1.0.1 release, `rtk --version` then said 1.0.1, the file's SHA-256 4f7ad08a...ed1e matches rtk.exe in the v1.0.1 zip, and the folder held rtk.exe alone. - the same with the old rtk.exe running (rtk proxy ping -n 25): replaced to 1.0.1, rtk.exe.old-<pid> left while it ran; the next upgrade removed it. - The library tests built for windows/amd64 ran on the box: the ones that don't need shell fakes pass (Upgrader, ZipNeedsRTKExe, ChecksumMismatch, Shown, UpgradeRunsTheInstall, SwapsTheFile). Not tried: - rtk's releases ship both rtk- and rtk-oss- editions; this always installs the default rtk- edition, as the sh installer path does, so an oss user upgraded this way gets the default edition. - Windows on arm64: rtk has no arm64 Windows build; an arm64 magpie still offers no upgrade there. An amd64 magpie under emulation would take the amd64 path; not tried. - A rtk.exe in a folder only an administrator can write (Program Files) fails with the write error; not tried. - The GUI itself was not driven; the Upgrade text is ReadRTK's. Co-reported-by: LoKi (lokic7123-star) Full suite at this commit on87d703c5c: set -o pipefail; go test -count=1 -timeout 45m ./... under the suite lock, every package ok (SUITE_EXIT 0).
289 lines
9.6 KiB
Go
289 lines
9.6 KiB
Go
package library
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// fakeRTKRelease serves a fake rtk Windows release at the URL rtkDownloadBase
|
|
// is set to: the zip holds exe as rtk.exe, and checksums.txt gives its
|
|
// SHA-256 — a wrong one when bad, so the check can be made to fail (#2006).
|
|
func fakeRTKRelease(t *testing.T, exe string, bad bool) string {
|
|
t.Helper()
|
|
var zb bytes.Buffer
|
|
zw := zip.NewWriter(&zb)
|
|
w, err := zw.Create("rtk.exe")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := w.Write([]byte(exe)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sum := sha256.Sum256(zb.Bytes())
|
|
line := hex.EncodeToString(sum[:])
|
|
if bad {
|
|
line = strings.Repeat("0", 64)
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.HasSuffix(r.URL.Path, "/checksums.txt") {
|
|
fmt.Fprintf(w, "%s rtk-x86_64-pc-windows-msvc.zip\n", line)
|
|
return
|
|
}
|
|
w.Write(zb.Bytes())
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv.URL
|
|
}
|
|
|
|
// #2006: a standalone rtk.exe on Windows — rtk's own release put in
|
|
// %USERPROFILE%\.local\bin, not winget's and not cargo's — had no upgrade
|
|
// command, so the Upgrade button was disabled. It now gets one; an rtk
|
|
// winget's records say winget installed is still not taken over.
|
|
func TestRTKWindowsStandaloneUpgrader(t *testing.T) {
|
|
h := sandbox(t)
|
|
arch := rtkArch
|
|
rtkArch = "amd64"
|
|
t.Cleanup(func() { rtkArch = arch })
|
|
bin := filepath.Join(h, ".local", "bin", "rtk.exe")
|
|
write(t, bin, "rtk")
|
|
win, rec := rtkOnWindows, wingetRecords
|
|
rtkOnWindows = true
|
|
wingetRecords = func() ([]string, bool) { return []string{filepath.Join(h, "winget")}, true }
|
|
forgetWingetOwned()
|
|
t.Cleanup(func() {
|
|
rtkOnWindows, wingetRecords = win, rec
|
|
forgetWingetOwned()
|
|
})
|
|
|
|
if c := rtkUpgrader(bin); len(c) != 2 || c[0] != rtkWindowsRelease || c[1] != bin {
|
|
t.Fatalf("standalone: upgrader %q; want %s … %s", c, rtkWindowsRelease, bin)
|
|
}
|
|
|
|
// winget's records say it is winget's: magpie doesn't take it over
|
|
forgetWingetOwned()
|
|
wingetRecords = func() ([]string, bool) { return []string{filepath.Dir(bin)}, true }
|
|
if c := rtkUpgrader(bin); c != nil {
|
|
t.Fatalf("winget's own: upgrader %q; want none", c)
|
|
}
|
|
|
|
// magpie on arm64: rtk's Windows release has no build for it
|
|
forgetWingetOwned()
|
|
wingetRecords = func() ([]string, bool) { return []string{filepath.Join(h, "winget")}, true }
|
|
rtkArch = "arm64"
|
|
if c := rtkUpgrader(bin); c != nil {
|
|
t.Fatalf("arm64: upgrader %q; want none", c)
|
|
}
|
|
}
|
|
|
|
// #2006: with no record of winget's to read, the standalone action needs
|
|
// winget to say it has no rtk-ai.rtk, or no winget at all. A winget that
|
|
// couldn't say (it timed out) is no answer: the rtk may be winget's, and
|
|
// magpie doesn't take it over.
|
|
func TestRTKWindowsStandaloneAsksWinget(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("the fakes are shell scripts")
|
|
}
|
|
h := sandbox(t)
|
|
bin := filepath.Join(h, ".local", "bin", "rtk.exe")
|
|
write(t, bin, "rtk")
|
|
arch := rtkArch
|
|
rtkArch = "amd64"
|
|
t.Cleanup(func() { rtkArch = arch })
|
|
none := func() ([]string, bool) { return nil, false }
|
|
t.Setenv("PATH", filepath.Join(h, "empty"))
|
|
|
|
// no winget on the PATH: nothing of winget's to take over
|
|
win, rec := rtkOnWindows, wingetRecords
|
|
rtkOnWindows, wingetRecords = true, none
|
|
forgetWingetOwned()
|
|
t.Cleanup(func() {
|
|
rtkOnWindows, wingetRecords = win, rec
|
|
forgetWingetOwned()
|
|
})
|
|
if c := rtkUpgrader(bin); len(c) != 2 || c[0] != rtkWindowsRelease {
|
|
t.Fatalf("no winget: upgrader %q; want %s", c, rtkWindowsRelease)
|
|
}
|
|
|
|
// winget answers, and lists no rtk-ai.rtk
|
|
wingetOn(t, h, wingetUnlisted, 0, none)
|
|
if c := rtkUpgrader(bin); len(c) != 2 || c[0] != rtkWindowsRelease {
|
|
t.Fatalf("unlisted: upgrader %q; want %s", c, rtkWindowsRelease)
|
|
}
|
|
|
|
// winget lists it: winget upgrades it
|
|
wingetOn(t, h, wingetListed, 0, none)
|
|
if c := rtkUpgrader(bin); len(c) == 0 || c[0] != "winget" {
|
|
t.Fatalf("listed: upgrader %q; want winget upgrade", c)
|
|
}
|
|
|
|
// winget fails some other way (its sources unreachable): no answer
|
|
wingetOn(t, h, "", 1, none)
|
|
if c := rtkUpgrader(bin); c != nil {
|
|
t.Fatalf("winget failed: upgrader %q; want none", c)
|
|
}
|
|
|
|
// winget runs out of time: no answer either
|
|
testenv.Program(t, filepath.Join(h, "tools", "winget"), "#!/bin/sh\nexec sleep 5\n")
|
|
old := wingetListTimeout
|
|
wingetListTimeout = 200 * time.Millisecond
|
|
t.Cleanup(func() { wingetListTimeout = old })
|
|
forgetWingetOwned()
|
|
if c := rtkUpgrader(bin); c != nil {
|
|
t.Fatalf("hung winget: upgrader %q; want none", c)
|
|
}
|
|
}
|
|
|
|
// #2006: only a member named rtk.exe is taken out of the release zip;
|
|
// another exe in it (rtk-proxy's) is not rtk.
|
|
func TestRTKWindowsReleaseZipNeedsRTKExe(t *testing.T) {
|
|
var zb bytes.Buffer
|
|
zw := zip.NewWriter(&zb)
|
|
for _, n := range []string{"LICENSE", "rtk-proxy.exe"} {
|
|
w, err := zw.Create(n)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w.Write([]byte(n))
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, err := rtkExeFromZip(zb.Bytes()); err == nil {
|
|
t.Fatalf("took %q from a zip with no rtk.exe", b)
|
|
}
|
|
}
|
|
|
|
// #2006: UpgradeRTK replaces a standalone Windows rtk with rtk.exe out of
|
|
// rtk's own Windows release, and what it then reads is the new version.
|
|
func TestRTKWindowsReleaseReplacesBinary(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("the fake rtk is a shell script")
|
|
}
|
|
h := sandbox(t)
|
|
latestServer(t, "v1.0.1")
|
|
dir := filepath.Join(h, ".local", "bin")
|
|
bin := filepath.Join(dir, "rtk")
|
|
write(t, filepath.Join(dir, "VERSION"), "0.9.0")
|
|
testenv.Program(t, bin, strings.ReplaceAll(versionedRTK, "VERSION", filepath.Join(dir, "VERSION")))
|
|
t.Setenv("PATH", dir)
|
|
// the action rtkUpgrader gives the standalone rtk.exe: this runnable rtk
|
|
// stands in for one, since a fake here is a shell script (rtkUpgrader's
|
|
// own branch is judged above)
|
|
up := upgraderOf
|
|
upgraderOf = func(string) []string { return []string{rtkWindowsRelease, bin} }
|
|
t.Cleanup(func() { upgraderOf = up })
|
|
|
|
const newRTK = "#!/bin/sh\ncase \"$1\" in\n--version) echo 'rtk 1.0.1'; exit 0 ;;\ngain) echo '{\"summary\":{\"total_commands\":0}}' ;;\nesac\nexit 2\n"
|
|
old := rtkDownloadBase
|
|
rtkDownloadBase = fakeRTKRelease(t, newRTK, false)
|
|
t.Cleanup(func() { rtkDownloadBase = old })
|
|
|
|
v, err := UpgradeRTK()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v.Version != "1.0.1" {
|
|
t.Fatalf("version %q; want 1.0.1", v.Version)
|
|
}
|
|
if got := read(t, bin); got != newRTK {
|
|
t.Fatalf("binary not replaced:\n got %q\nwant %q", got, newRTK)
|
|
}
|
|
// the rtk and the fake's VERSION file, nothing written beside them
|
|
if ents, _ := os.ReadDir(dir); len(ents) != 2 {
|
|
t.Fatalf("left files behind in %s: %v", dir, ents)
|
|
}
|
|
}
|
|
|
|
// #2006: a download that doesn't match rtk's own checksums is refused, and
|
|
// the rtk that was there is left untouched — no half-written file either.
|
|
func TestRTKWindowsReleaseChecksumMismatch(t *testing.T) {
|
|
h := sandbox(t)
|
|
dir := filepath.Join(h, ".local", "bin")
|
|
bin := filepath.Join(dir, "rtk.exe")
|
|
write(t, bin, "the rtk that was here")
|
|
old := rtkDownloadBase
|
|
rtkDownloadBase = fakeRTKRelease(t, "the new rtk", true)
|
|
t.Cleanup(func() { rtkDownloadBase = old })
|
|
|
|
err := installRTKWindowsRelease("1.0.1", bin)
|
|
if err == nil || !strings.Contains(err.Error(), "checksum") {
|
|
t.Fatalf("mismatch: %v; want a checksum error", err)
|
|
}
|
|
if got := read(t, bin); got != "the rtk that was here" {
|
|
t.Fatalf("binary changed to %q", got)
|
|
}
|
|
if ents, _ := os.ReadDir(dir); len(ents) != 1 {
|
|
t.Fatalf("left files behind in %s: %v", dir, ents)
|
|
}
|
|
}
|
|
|
|
// #2006: the Upgrade button shows this string, so it must read as what the
|
|
// upgrade does — not the internal action name and the user's binary path.
|
|
func TestRTKWindowsReleaseShown(t *testing.T) {
|
|
bin := `C:\x\rtk.exe`
|
|
got := shown([]string{rtkWindowsRelease, bin})
|
|
if got != "rtk's own Windows release" {
|
|
t.Fatalf("shown %q; want %q", got, "rtk's own Windows release")
|
|
}
|
|
if strings.Contains(got, rtkWindowsRelease) || strings.Contains(got, bin) {
|
|
t.Fatalf("shown %q leaks the action or the path", got)
|
|
}
|
|
}
|
|
|
|
// #2006: UpgradeRTK hands this channel to magpie's own download-and-replace
|
|
// instead of running the action as a command (it is not one). This is the
|
|
// wiring the two tests above leave untested on Windows: the successful one
|
|
// skips there. A download that doesn't match rtk's checksums is enough —
|
|
// its error is the install's, not the installer's.
|
|
func TestRTKWindowsReleaseUpgradeRunsTheInstall(t *testing.T) {
|
|
h := sandbox(t)
|
|
latestServer(t, "v1.0.1")
|
|
// rtk is found on the PATH first (rtkPath), which is how a hand-placed
|
|
// .local\bin\rtk.exe on it is found on the machine of the report
|
|
const script = "#!/bin/sh\nexit 2\n"
|
|
testenv.Program(t, filepath.Join(h, "bin", "rtk"), script)
|
|
write(t, filepath.Join(h, "bin", "rtk.exe"), "rtk")
|
|
bin := rtkPath()
|
|
if bin == "" {
|
|
t.Fatal("no rtk on the sandbox's PATH")
|
|
}
|
|
up := upgraderOf
|
|
upgraderOf = func(string) []string { return []string{rtkWindowsRelease, bin} }
|
|
old := rtkDownloadBase
|
|
rtkDownloadBase = fakeRTKRelease(t, "the new rtk", true)
|
|
ver := rtkVersion
|
|
rtkVersion = func() string { return "0.9.0" }
|
|
t.Cleanup(func() {
|
|
upgraderOf, rtkDownloadBase, rtkVersion = up, old, ver
|
|
})
|
|
|
|
_, err := UpgradeRTK()
|
|
if err == nil || !strings.Contains(err.Error(), "checksum") {
|
|
t.Fatalf("upgrade through the release channel: %v; want the checksum error", err)
|
|
}
|
|
// rtkPath finds the runnable rtk here, rtk.exe on Windows
|
|
want := script
|
|
if filepath.Base(bin) == "rtk.exe" {
|
|
want = "rtk"
|
|
}
|
|
if got := read(t, bin); got != want {
|
|
t.Fatalf("the rtk changed to %q", got)
|
|
}
|
|
}
|