151 lines
4.4 KiB
Go
151 lines
4.4 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/cli"
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/version"
|
|
)
|
|
|
|
func TestDownloadAppUpdateVerifiesAgainstAssetDigest(t *testing.T) {
|
|
body := []byte("reims-go v9.9.9 build")
|
|
h := sha256.Sum256(body)
|
|
good := hex.EncodeToString(h[:])
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/app.exe":
|
|
_, _ = w.Write(body)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
info := version.UpdateInfo{
|
|
Latest: "v9.9.9", HasUpdate: true,
|
|
AssetName: "reims-go-v9.9.9-windows-amd64.exe", AssetSize: int64(len(body)),
|
|
DownloadURL: srv.URL + "/app.exe", AssetSHA256: good,
|
|
}
|
|
|
|
dir := t.TempDir()
|
|
path, sum, err := downloadAppUpdate(context.Background(), info, dir, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sum != good || filepath.Dir(path) != dir {
|
|
t.Fatalf("path %s sum %s", path, sum)
|
|
}
|
|
|
|
info.AssetSHA256 = strings.Repeat("0", 64)
|
|
if _, _, err := downloadAppUpdate(context.Background(), info, t.TempDir(), nil); err == nil || !strings.Contains(err.Error(), "SHA256 mismatch") {
|
|
t.Fatalf("a hash mismatch must fail, got %v", err)
|
|
}
|
|
|
|
info.AssetSHA256 = ""
|
|
if _, _, err := downloadAppUpdate(context.Background(), info, t.TempDir(), nil); err == nil {
|
|
t.Fatal("missing asset digest must fail")
|
|
}
|
|
}
|
|
|
|
func TestInstallAppUpdateGuards(t *testing.T) {
|
|
cfg := config.Default()
|
|
cfg.Locale = i18n.EnUS
|
|
a := &App{cfg: cfg}
|
|
|
|
appUpdateReady.Lock()
|
|
appUpdateReady.version, appUpdateReady.path, appUpdateReady.sha256 = "", "", ""
|
|
appUpdateReady.Unlock()
|
|
if err := a.InstallAppUpdate(); err == nil || !strings.Contains(err.Error(), "Download the update first") {
|
|
t.Fatalf("nothing downloaded: got %v", err)
|
|
}
|
|
|
|
// A file that no longer matches its verified hash is refused before any swap.
|
|
f := filepath.Join(t.TempDir(), "reims-go-v9.9.9-windows-amd64.exe")
|
|
if err := os.WriteFile(f, []byte("swapped after download"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
appUpdateReady.Lock()
|
|
appUpdateReady.version, appUpdateReady.path, appUpdateReady.sha256 = "v9.9.9", f, strings.Repeat("a", 64)
|
|
appUpdateReady.Unlock()
|
|
if err := a.InstallAppUpdate(); err == nil || !strings.Contains(err.Error(), "changed on disk") {
|
|
t.Fatalf("changed file: got %v", err)
|
|
}
|
|
appUpdateReady.Lock()
|
|
cleared := appUpdateReady.path == ""
|
|
appUpdateReady.Unlock()
|
|
if !cleared {
|
|
t.Fatal("a changed download must be forgotten")
|
|
}
|
|
}
|
|
|
|
func TestFinishAppUpdatePreservesOldOnAlphaAndDev(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
channel string
|
|
wantKept bool
|
|
}{
|
|
{version.ChannelStable, false},
|
|
{version.ChannelAlpha, true},
|
|
{version.ChannelDev, true},
|
|
} {
|
|
t.Run(tc.channel, func(t *testing.T) {
|
|
binDir := t.TempDir()
|
|
dataRoot := t.TempDir()
|
|
exe := filepath.Join(binDir, "reims-go.exe")
|
|
oldExe := exe + ".old"
|
|
newStaged := exe + ".new"
|
|
dlDir := appUpdateDir(dataRoot)
|
|
dlFile := filepath.Join(dlDir, "reims-go-v0.2.9-alpha.1-windows-amd64.exe")
|
|
|
|
for _, p := range []string{exe, oldExe, newStaged} {
|
|
if err := os.WriteFile(p, []byte("x"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := os.MkdirAll(dlDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(dlFile, []byte("installer"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
cfg := config.Default()
|
|
cfg.DataRoot = dataRoot
|
|
cfg.AppUpdateChannel = tc.channel
|
|
a := &App{cfg: cfg, launch: cli.WindowArgs{WaitPID: 12345}}
|
|
a.finishAppUpdateWithExe(exe)
|
|
|
|
// Aborted staging .new is always cleaned up.
|
|
if _, err := os.Stat(newStaged); !os.IsNotExist(err) {
|
|
t.Fatalf("channel %s: expected .new staging file removed", tc.channel)
|
|
}
|
|
|
|
_, oldErr := os.Stat(oldExe)
|
|
_, dlErr := os.Stat(dlFile)
|
|
if tc.wantKept {
|
|
if oldErr != nil {
|
|
t.Fatalf("channel %s: expected .old backup preserved, err=%v", tc.channel, oldErr)
|
|
}
|
|
if dlErr != nil {
|
|
t.Fatalf("channel %s: expected downloaded installer preserved, err=%v", tc.channel, dlErr)
|
|
}
|
|
} else {
|
|
if !os.IsNotExist(oldErr) {
|
|
t.Fatalf("channel %s: expected .old backup removed", tc.channel)
|
|
}
|
|
if !os.IsNotExist(dlErr) {
|
|
t.Fatalf("channel %s: expected downloaded installer removed", tc.channel)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|