package installer import ( "context" "crypto/sha256" "encoding/hex" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "sync/atomic" "testing" ) func TestEnsureCachedAssetDownloadsChunkedAndSkipsWhenCached(t *testing.T) { payload := []byte(strings.Repeat("opencore-binary-data-", 512)) sum := sha256.Sum256(payload) wantHash := hex.EncodeToString(sum[:]) var hits atomic.Int32 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hits.Add(1) // Mimic GitHub codeload: HTTP 200 without Content-Length (chunked). flusher, _ := w.(http.Flusher) w.WriteHeader(http.StatusOK) mid := len(payload) / 2 _, _ = w.Write(payload[:mid]) if flusher != nil { flusher.Flush() } _, _ = w.Write(payload[mid:]) })) defer srv.Close() cacheRoot := t.TempDir() asset := PinnedAsset{ Name: "OcBinaryData-test.zip", URL: srv.URL + "/archive.zip", SHA256: wantHash, Step: "downloading OpenCore's picker icons and boot chime", } var updates []Update if err := EnsureCachedAsset(context.Background(), cacheRoot, asset, srv.Client(), func(u Update) { updates = append(updates, u) }); err != nil { t.Fatalf("EnsureCachedAsset failed: %v", err) } if hits.Load() != 1 { t.Fatalf("expected 1 HTTP hit, got %d", hits.Load()) } dest := filepath.Join(cacheRoot, "reims-util", asset.Name) gotBytes, err := os.ReadFile(dest) if err != nil || string(gotBytes) != string(payload) { t.Fatalf("cached file mismatch: err=%v len=%d", err, len(gotBytes)) } if len(updates) < 2 { t.Fatalf("expected step + progress updates, got %d", len(updates)) } // Second call must hit cache without another HTTP request. if err := EnsureCachedAsset(context.Background(), cacheRoot, asset, srv.Client(), nil); err != nil { t.Fatalf("second EnsureCachedAsset failed: %v", err) } if hits.Load() != 1 { t.Fatalf("expected cache hit with 0 extra requests, total hits=%d", hits.Load()) } } func TestEnsureCachedAssetRejectsWrongHash(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("corrupted")) })) defer srv.Close() cacheRoot := t.TempDir() asset := PinnedAsset{ Name: "OcBinaryData-bad.zip", URL: srv.URL, SHA256: strings.Repeat("0", 64), Step: "downloading", } if err := EnsureCachedAsset(context.Background(), cacheRoot, asset, srv.Client(), nil); err == nil { t.Fatalf("expected hash mismatch error") } dest := filepath.Join(cacheRoot, "reims-util", asset.Name) if _, err := os.Stat(dest); !os.IsNotExist(err) { t.Fatalf("corrupted file should not exist at %s", dest) } if _, err := os.Stat(dest + ".part"); !os.IsNotExist(err) { t.Fatalf("part file should be cleaned up on mismatch") } } func TestShouldPrefetchOcBinaryData(t *testing.T) { if !ShouldPrefetchOcBinaryData("7867384abcdef", "reims-util 0.1.0") { t.Fatalf("expected true for pinned commit 7867384") } if !ShouldPrefetchOcBinaryData("", "reims-util 0.1.0") { t.Fatalf("expected true for schema-1 0.1.0") } if ShouldPrefetchOcBinaryData("deadbeef1234", "reims-util 0.2.0") { t.Fatalf("expected false for newer util commit") } }