* refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积 * fix: preserve download recovery and bound PBZX decoding Keep VM diagnostics available during plugin initialization failures and verify settings through the unified driver lifecycle.
208 lines
6.0 KiB
Go
208 lines
6.0 KiB
Go
package app
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"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 TestDownloadAppUpdateExtractsZipAndVerifiesDigest(t *testing.T) {
|
|
exePayload := []byte("MZ-reims-go-zip-update-binary")
|
|
exeHash := sha256.Sum256(exePayload)
|
|
wantExeSum := hex.EncodeToString(exeHash[:])
|
|
|
|
var buf bytes.Buffer
|
|
zw := zip.NewWriter(&buf)
|
|
w, err := zw.Create("reims-go-v9.9.9-windows-amd64.exe")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := w.Write(exePayload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
zipBytes := buf.Bytes()
|
|
zipHash := sha256.Sum256(zipBytes)
|
|
wantZipSum := hex.EncodeToString(zipHash[:])
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/app.zip" {
|
|
_, _ = w.Write(zipBytes)
|
|
return
|
|
}
|
|
http.NotFound(w, r)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
dir := t.TempDir()
|
|
info := version.UpdateInfo{
|
|
Latest: "v9.9.9",
|
|
HasUpdate: true,
|
|
AssetName: "reims-go-v9.9.9-windows-amd64.zip",
|
|
AssetSize: int64(len(zipBytes)),
|
|
DownloadURL: srv.URL + "/app.zip",
|
|
AssetSHA256: wantZipSum,
|
|
}
|
|
gotPath, gotSum, err := downloadAppUpdate(context.Background(), info, dir, nil)
|
|
if err != nil {
|
|
t.Fatalf("downloadAppUpdate(zip): %v", err)
|
|
}
|
|
if filepath.Base(gotPath) != "reims-go-v9.9.9-windows-amd64.exe" {
|
|
t.Fatalf("extracted path = %s, want reims-go-v9.9.9-windows-amd64.exe", gotPath)
|
|
}
|
|
if gotSum != wantExeSum {
|
|
t.Fatalf("extracted exe sum = %s, want %s", gotSum, wantExeSum)
|
|
}
|
|
gotBody, err := os.ReadFile(gotPath)
|
|
if err != nil || !bytes.Equal(gotBody, exePayload) {
|
|
t.Fatalf("extracted exe content mismatch: %q, err=%v", gotBody, err)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|