* feat: add experimental Apple Silicon support with Nivfold Integrate the standalone Nivfold protocol 1 runner; tested against Nivfold f436fb6. The Windows Reims vGPU runtime remains independent. * fix: open QQ invites and VM SSH on macOS * fix: move VM, View and Help to the macOS menu bar * feat(macos): add resumable Apple restore image downloads Expose the compatible restore image in the VM wizard and Images page, validate downloads before caching, and fix macOS memory reporting and plugin SVG rendering. * fix(macos): localize the native application menu * fix(macos): address native desktop review findings
289 lines
11 KiB
Go
289 lines
11 KiB
Go
package fetch
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidatedTransferUsesBoundPartialAndPublishesAfterValidation(t *testing.T) {
|
|
const payload = "fake IPSW body used only in a local test"
|
|
for _, tc := range []struct{ name, reply string }{{"resume", "matched"}, {"restart when file changes", "changed"}, {"restart on incorrect content range", "wrong-range"}} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var requests []string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
rangeHeader := r.Header.Get("Range")
|
|
requests = append(requests, rangeHeader)
|
|
w.Header().Set("ETag", `"stable"`)
|
|
if rangeHeader != "" {
|
|
if rangeHeader != "bytes=5-" || r.Header.Get("If-Range") != `"stable"` {
|
|
t.Errorf("unsafe resume request: %s, %s", rangeHeader, r.Header.Get("If-Range"))
|
|
}
|
|
switch tc.reply {
|
|
case "changed":
|
|
w.Header().Set("ETag", `"changed"`)
|
|
case "wrong-range", "matched":
|
|
first := 5
|
|
if tc.reply == "wrong-range" {
|
|
first = 4
|
|
}
|
|
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", first, len(payload)-1, len(payload)))
|
|
w.WriteHeader(http.StatusPartialContent)
|
|
fmt.Fprint(w, payload[first:])
|
|
return
|
|
}
|
|
}
|
|
fmt.Fprint(w, payload)
|
|
}))
|
|
defer server.Close()
|
|
dir := t.TempDir()
|
|
final, part := filepath.Join(dir, "restore.ipsw"), filepath.Join(dir, "restore.part.ipsw")
|
|
for p, body := range map[string]string{part: payload[:5], part + ".etag": `"stable"`} {
|
|
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
validate := func(_ context.Context, name string) error {
|
|
if name != part {
|
|
t.Errorf("validation path = %s", name)
|
|
}
|
|
if _, err := os.Stat(final); !os.IsNotExist(err) {
|
|
t.Error("download published before validation")
|
|
}
|
|
body, err := os.ReadFile(name)
|
|
if string(body) != payload {
|
|
t.Errorf("unexpected body: %q", body)
|
|
}
|
|
return err
|
|
}
|
|
if err := ResumeValidated(t.Context(), server.Client(), server.URL, final, part, 1024, nil, validate); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(requests) != 1 && tc.reply != "wrong-range" || len(requests) != 2 && tc.reply == "wrong-range" {
|
|
t.Fatalf("requests: %v", requests)
|
|
}
|
|
if body, err := os.ReadFile(final); err != nil || string(body) != payload {
|
|
t.Fatalf("final = %q, %v", body, err)
|
|
}
|
|
if _, err := os.Stat(part + ".etag"); !os.IsNotExist(err) {
|
|
t.Fatalf("validator remains: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidatedTransferReusesOnlyBoundComplete416(t *testing.T) {
|
|
const payload = "bound fixture"
|
|
const replacement = "changed bytes" // Same size, distinct bytes.
|
|
exact := fmt.Sprintf("bytes */%d", len(payload))
|
|
for _, tc := range []struct {
|
|
name, savedTag string
|
|
tags, ranges []string
|
|
wantRange, wantFresh bool
|
|
}{
|
|
{"complete", `"stable"`, []string{`"stable"`}, []string{exact}, true, false},
|
|
{"changed", `"stable"`, []string{`"changed"`}, []string{exact}, true, true},
|
|
{"weak response ETag", `"stable"`, []string{`W/"stable"`}, []string{exact}, true, true},
|
|
{"ambiguous response ETag", `"stable"`, []string{`"stable"`, `"changed"`}, []string{exact}, true, true},
|
|
{"wrong size", `"stable"`, []string{`"stable"`}, []string{fmt.Sprintf("bytes */%d", len(payload)-1)}, true, true},
|
|
{"missing size", `"stable"`, []string{`"stable"`}, nil, true, true},
|
|
{"malformed size", `"stable"`, []string{`"stable"`}, []string{fmt.Sprintf("bytes */+%d", len(payload))}, true, true},
|
|
{"ambiguous size", `"stable"`, []string{`"stable"`}, []string{exact, "bytes */1"}, true, true},
|
|
{"missing saved ETag", "", []string{`"stable"`}, []string{exact}, false, true},
|
|
{"weak saved ETag", `W/"stable"`, []string{`"stable"`}, []string{exact}, false, true},
|
|
{"malformed saved ETag", `"not valid"`, []string{`"stable"`}, []string{exact}, false, true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var requests []string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
rangeHeader := r.Header.Get("Range")
|
|
requests = append(requests, rangeHeader)
|
|
if rangeHeader == "" {
|
|
w.Header().Set("ETag", `"changed"`)
|
|
fmt.Fprint(w, replacement)
|
|
return
|
|
}
|
|
if r.Header.Get("If-Range") != tc.savedTag || r.Header.Get("Accept-Encoding") != "identity" {
|
|
t.Errorf("unexpected resume headers: %v", r.Header)
|
|
}
|
|
for _, tag := range tc.tags {
|
|
w.Header().Add("ETag", tag)
|
|
}
|
|
for _, value := range tc.ranges {
|
|
w.Header().Add("Content-Range", value)
|
|
}
|
|
w.Header().Set("Content-Encoding", "gzip") // The 416 error body is ignored.
|
|
w.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
|
|
fmt.Fprint(w, "not image data")
|
|
}))
|
|
defer server.Close()
|
|
dir := t.TempDir()
|
|
final, part := filepath.Join(dir, "restore.ipsw"), filepath.Join(dir, "restore.part.ipsw")
|
|
if err := os.WriteFile(part, []byte(payload), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tc.savedTag != "" {
|
|
if err := os.WriteFile(part+".etag", []byte(tc.savedTag), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var wantRequests []string
|
|
wantBody := payload
|
|
if tc.wantRange {
|
|
wantRequests = append(wantRequests, fmt.Sprintf("bytes=%d-", len(payload)))
|
|
}
|
|
if tc.wantFresh {
|
|
wantRequests = append(wantRequests, "")
|
|
wantBody = replacement
|
|
}
|
|
var done, total int64
|
|
err := ResumeValidated(t.Context(), server.Client(), server.URL, final, part, 1024, func(d, n int64) {
|
|
done, total = d, n
|
|
}, func(_ context.Context, file string) error {
|
|
if file != part {
|
|
t.Errorf("validation path = %s", file)
|
|
}
|
|
if _, err := os.Stat(final); !os.IsNotExist(err) {
|
|
t.Errorf("download published before validation: %v", err)
|
|
}
|
|
if body, err := os.ReadFile(file); err != nil || string(body) != wantBody {
|
|
t.Errorf("staged body = %q, %v; want %q", body, err, wantBody)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !slices.Equal(requests, wantRequests) {
|
|
t.Fatalf("requests = %q, want %q", requests, wantRequests)
|
|
}
|
|
if want := int64(len(wantBody)); done != want || total != want {
|
|
t.Fatalf("progress = %d/%d, want %d/%d", done, total, want, want)
|
|
}
|
|
if body, err := os.ReadFile(final); err != nil || string(body) != wantBody {
|
|
t.Fatalf("final = %q, %v; want %q", body, err, wantBody)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidatedTransferConfirmsUntaggedComplete416WithHead(t *testing.T) {
|
|
const staged = "staged bytes"
|
|
const fresh = "fresh bytes"
|
|
const tag = `"stable"`
|
|
for _, tc := range []struct {
|
|
name, headTag, encoding string
|
|
headSize, headStatus int
|
|
redirect, wantFresh bool
|
|
}{
|
|
{"match", tag, "", len(staged), http.StatusOK, false, false},
|
|
{"changed", `"changed"`, "", len(staged), http.StatusOK, false, true},
|
|
{"weak", `W/"stable"`, "", len(staged), http.StatusOK, false, true},
|
|
{"missing", "", "", len(staged), http.StatusOK, false, true},
|
|
{"wrong size", tag, "", len(staged) + 1, http.StatusOK, false, true},
|
|
{"encoded", tag, "gzip", len(staged), http.StatusOK, false, true},
|
|
{"precondition failed", tag, "", len(staged), http.StatusPreconditionFailed, false, true},
|
|
{"different target", tag, "", len(staged), http.StatusOK, true, true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var requests []string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
requests = append(requests, r.Method+" "+r.URL.Path+" "+r.Header.Get("Range"))
|
|
if r.Method == http.MethodHead {
|
|
if r.Header.Get("If-Match") != tag || r.Header.Get("Range") != "" || r.Header.Get("If-Range") != "" || r.Header.Get("Accept-Encoding") != "identity" {
|
|
t.Errorf("unexpected confirmation headers: %v", r.Header)
|
|
}
|
|
if tc.redirect && r.URL.Path == "/" {
|
|
http.Redirect(w, r, "/other", http.StatusTemporaryRedirect)
|
|
return
|
|
}
|
|
if tc.headTag != "" {
|
|
w.Header().Set("ETag", tc.headTag)
|
|
}
|
|
if tc.encoding != "" {
|
|
w.Header().Set("Content-Encoding", tc.encoding)
|
|
}
|
|
w.Header().Set("Content-Length", fmt.Sprint(tc.headSize))
|
|
w.WriteHeader(tc.headStatus)
|
|
return
|
|
}
|
|
if r.Header.Get("Range") != "" {
|
|
w.Header().Set("Content-Range", fmt.Sprintf("bytes */%d", len(staged)))
|
|
w.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
|
|
return
|
|
}
|
|
fmt.Fprint(w, fresh)
|
|
}))
|
|
defer server.Close()
|
|
dir := t.TempDir()
|
|
final, part := filepath.Join(dir, "restore.ipsw"), filepath.Join(dir, "restore.part.ipsw")
|
|
for path, bytes := range map[string]string{part: staged, part + ".etag": tag} {
|
|
if err := os.WriteFile(path, []byte(bytes), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
wantRequests := []string{fmt.Sprintf("GET / bytes=%d-", len(staged)), "HEAD / "}
|
|
if tc.redirect {
|
|
wantRequests = append(wantRequests, "HEAD /other ")
|
|
}
|
|
wantBody := staged
|
|
if tc.wantFresh {
|
|
wantRequests = append(wantRequests, "GET / ")
|
|
wantBody = fresh
|
|
}
|
|
if err := ResumeValidated(t.Context(), server.Client(), server.URL, final, part, 1024, nil, func(_ context.Context, file string) error {
|
|
body, err := os.ReadFile(file)
|
|
if err == nil && string(body) != wantBody {
|
|
return fmt.Errorf("staged body = %q, want %q", body, wantBody)
|
|
}
|
|
return err
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !slices.Equal(requests, wantRequests) {
|
|
t.Fatalf("requests = %q, want %q", requests, wantRequests)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidatedTransferKeepsPartialOnPauseButRejectsInvalidImage(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("ETag", `"first"`)
|
|
fmt.Fprint(w, "fake body")
|
|
}))
|
|
defer server.Close()
|
|
dir := t.TempDir()
|
|
final, part := filepath.Join(dir, "restore.ipsw"), filepath.Join(dir, "restore.part.ipsw")
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
err := ResumeValidated(ctx, server.Client(), server.URL, final, part, 1024, nil, func(ctx context.Context, _ string) error {
|
|
cancel()
|
|
return ctx.Err()
|
|
})
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("pause: %v", err)
|
|
}
|
|
if _, err := os.Stat(part); err != nil {
|
|
t.Fatalf("pause did not keep partial: %v", err)
|
|
}
|
|
badImage := errors.New("not a restore image")
|
|
err = ResumeValidated(t.Context(), server.Client(), server.URL, final, part, 1024, nil, func(context.Context, string) error {
|
|
return &InvalidDownloadError{Err: badImage}
|
|
})
|
|
if !errors.Is(err, badImage) {
|
|
t.Fatalf("validate: %v", err)
|
|
}
|
|
for _, file := range []string{part, part + ".etag", final} {
|
|
if _, err := os.Stat(file); !os.IsNotExist(err) {
|
|
t.Fatalf("invalid image left %s: %v", file, err)
|
|
}
|
|
}
|
|
}
|