package plugins import ( "context" "crypto/ed25519" "encoding/base64" "encoding/json" "net/http" "net/http/httptest" "sync/atomic" "testing" "time" ) // catalogServer mimics the cloud endpoints used by the panel. type catalogServer struct { hits atomic.Int32 manifest []byte sha string platform ed25519.PrivateKey } func (c *catalogServer) handler() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/api/v1/plugins", func(w http.ResponseWriter, r *http.Request) { c.hits.Add(1) if r.URL.Query().Get("arch") == "" || r.URL.Query().Get("panel") == "" { http.Error(w, "missing query", http.StatusBadRequest) return } _ = json.NewEncoder(w).Encode(map[string]any{ "code": 0, "msg": "ok", "data": map[string]any{ "api": 1, "updated_at": time.Now().Unix(), "plugins": []any{map[string]any{ "slug": "redis-manager", "name": "Redis 管理器", "summary": "管理 Redis", "category": "database", "license": "MIT", "author": "dev", "permissions": []any{"service:redis"}, "latest": map[string]any{ "version": "1.2.0", "arch": "amd64", "size": 3, "sha256": c.sha, "channel": "stable", }, "versions": []any{map[string]any{ "version": "1.2.0", "arch": "amd64", "size": 3, "sha256": c.sha, "channel": "stable", }}, }}, }, }) }) mux.HandleFunc("/api/v1/plugins/redis-manager/1.2.0/manifest", func(w http.ResponseWriter, r *http.Request) { sig := sigPrefix + base64.StdEncoding.EncodeToString(ed25519.Sign(c.platform, c.manifest)) _ = json.NewEncoder(w).Encode(map[string]any{ "code": 0, "msg": "ok", "data": map[string]any{ "slug": "redis-manager", "version": "1.2.0", "arch": "amd64", "manifest": string(c.manifest), "platform_sig": sig, "platform_pubkey": pub(c.platform), "artifact": map[string]any{ "url": "http://" + r.Host + "/download/plugin/1", "sha256": c.sha, "size": 3, "arch": "amd64", }, }, }) }) return mux } func TestCatalogParsesCloudContractAndCaches(t *testing.T) { svc, _, platform, dev := newTestService(t) manifest := buildManifest(t, dev, nil) cs := &catalogServer{manifest: manifest, sha: manifestSHA(t, manifest), platform: platform} ts := httptest.NewServer(cs.handler()) defer ts.Close() svc.cfg.PluginRepoURL = ts.URL cat, err := svc.Catalog(context.Background(), true) if err != nil { t.Fatal(err) } if len(cat.Plugins) != 1 { t.Fatalf("plugins = %d", len(cat.Plugins)) } entry := cat.Plugins[0] if entry.Slug != "redis-manager" || entry.Latest == nil || entry.Latest.Version != "1.2.0" { t.Fatalf("unexpected entry: %+v", entry) } if _, err := svc.Catalog(context.Background(), false); err != nil { t.Fatal(err) } if cs.hits.Load() != 1 { t.Fatalf("catalog fetched %d times, want 1 (cache)", cs.hits.Load()) } doc, err := svc.ManifestDoc(context.Background(), "redis-manager", "1.2.0", "amd64") if err != nil { t.Fatal(err) } m, err := svc.VerifyDocument(context.Background(), doc) if err != nil { t.Fatalf("manifest rejected: %v", err) } if m.Slug != "redis-manager" { t.Fatalf("slug = %s", m.Slug) } } func TestCatalogFallsBackToDiskCache(t *testing.T) { svc, _, platform, dev := newTestService(t) manifest := buildManifest(t, dev, nil) cs := &catalogServer{manifest: manifest, sha: manifestSHA(t, manifest), platform: platform} ts := httptest.NewServer(cs.handler()) svc.cfg.PluginRepoURL = ts.URL if _, err := svc.Catalog(context.Background(), true); err != nil { t.Fatal(err) } ts.Close() // Drop only the in-memory copy: the disk cache must keep the catalog usable. svc.mu.Lock() svc.catalog = nil svc.catalogAt = time.Time{} svc.mu.Unlock() cat, err := svc.Catalog(context.Background(), false) if err != nil { t.Fatalf("disk cache not used: %v", err) } if len(cat.Plugins) != 1 { t.Fatalf("cached plugins = %d", len(cat.Plugins)) } } func TestCatalogRejectsRepositoryErrors(t *testing.T) { svc, _, _, _ := newTestService(t) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(`{"code":500,"msg":"boom"}`)) })) defer ts.Close() svc.cfg.PluginRepoURL = ts.URL if _, err := svc.Catalog(context.Background(), true); err == nil { t.Fatal("server error accepted") } } func manifestSHA(t *testing.T, raw []byte) string { t.Helper() var m struct { Artifact struct { SHA256 string `json:"sha256"` } `json:"artifact"` } if err := json.Unmarshal(raw, &m); err != nil { t.Fatal(err) } return m.Artifact.SHA256 }