Files
Reims-Go/internal/fetch/fetch_test.go
T
Nwflower f0b69c252c feat(update,plugin,ui): add in-app self-update, plugin locale packs, and About page
- Add shared resumable + SHA256SUMS-verified downloader (internal/fetch) and Windows executable swap/relaunch flow (internal/selfupdate, internal/app/appupdate_flow.go, --wait-pid in main.go/cli).
- Add dedicated About page and sidebar update capsule with background release checks, download progress, and restart confirmation when VMs are running.
- Implement plugin-contributed UI and backend/CLI locale packs (contributes.locales, LocalizedText extra tags, validation, runtime overlay, and PopupSelect language picker).
- Move kernel-irqchip and Windows processor scheduling controls into VM Compute > Performance settings, hide Plugins settings gear for VM-only schemas, and align locked system plugin switches.
2026-11-29 10:32:11 -07:00

98 lines
3.0 KiB
Go

package fetch
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func sum(b []byte) string {
h := sha256.Sum256(b)
return hex.EncodeToString(h[:])
}
// rangeServer serves body at /file (honouring Range) and a SHA256SUMS.txt.
func rangeServer(t *testing.T, name string, body []byte, sums string) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/" + SumsAsset:
fmt.Fprint(w, sums)
case "/file":
http.ServeContent(w, r, name, timeZero, strings.NewReader(string(body)))
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
return srv
}
func TestExpectedSum(t *testing.T) {
body := []byte("new exe")
srv := rangeServer(t, "app.exe", body, sum(body)+" *app.exe\n"+strings.Repeat("0", 64)+" other.zip\n")
got, err := ExpectedSum(context.Background(), srv.Client(), srv.URL+"/"+SumsAsset, "app.exe")
if err != nil || got != sum(body) {
t.Fatalf("got %q, %v", got, err)
}
if _, err := ExpectedSum(context.Background(), srv.Client(), srv.URL+"/"+SumsAsset, "missing.exe"); err == nil {
t.Fatal("a name without an entry must fail")
}
if _, err := ExpectedSum(context.Background(), srv.Client(), "", "app.exe"); err == nil {
t.Fatal("no sums URL must fail closed")
}
}
func TestResumeContinuesPartialAndVerifies(t *testing.T) {
body := []byte(strings.Repeat("reims-go update payload ", 4096))
srv := rangeServer(t, "app.exe", body, "")
dir := t.TempDir()
final := filepath.Join(dir, "app.exe")
if err := os.WriteFile(final+".part", body[:1000], 0o644); err != nil {
t.Fatal(err)
}
var last int64
path, err := Resume(context.Background(), srv.Client(), srv.URL+"/file", final, int64(len(body)), sum(body), func(done, _ int64) { last = done })
if err != nil {
t.Fatal(err)
}
got, _ := os.ReadFile(path)
if string(got) != string(body) || last != int64(len(body)) {
t.Fatalf("resumed file wrong: %d bytes, last progress %d", len(got), last)
}
if _, err := os.Stat(final + ".part"); !os.IsNotExist(err) {
t.Fatal(".part must be gone after a verified download")
}
// A verified final file is returned without hitting the network.
srv.Close()
if _, err := Resume(context.Background(), http.DefaultClient, srv.URL+"/file", final, int64(len(body)), sum(body), nil); err != nil {
t.Fatalf("already-verified file: %v", err)
}
}
func TestResumeDiscardsMismatch(t *testing.T) {
body := []byte("tampered")
srv := rangeServer(t, "app.exe", body, "")
final := filepath.Join(t.TempDir(), "app.exe")
_, err := Resume(context.Background(), srv.Client(), srv.URL+"/file", final, 0, sum([]byte("expected")), nil)
if err == nil || !strings.Contains(err.Error(), "SHA256 mismatch") {
t.Fatalf("want a mismatch error, got %v", err)
}
for _, p := range []string{final, final + ".part"} {
if _, err := os.Stat(p); !os.IsNotExist(err) {
t.Errorf("%s must not be kept after a mismatch", filepath.Base(p))
}
}
}
var timeZero = time.Time{}