miaopanel
1package plugins
2
3import (
4 "context"
5 "crypto/ed25519"
6 "encoding/base64"
7 "encoding/json"
8 "net/http"
9 "net/http/httptest"
10 "sync/atomic"
11 "testing"
12 "time"
13)
14
15// catalogServer mimics the cloud endpoints used by the panel.
16type catalogServer struct {
17 hits atomic.Int32
18 manifest []byte
19 sha string
20 platform ed25519.PrivateKey
21}
22
23func (c *catalogServer) handler() http.Handler {
24 mux := http.NewServeMux()
25 mux.HandleFunc("/api/v1/plugins", func(w http.ResponseWriter, r *http.Request) {
26 c.hits.Add(1)
27 if r.URL.Query().Get("arch") == "" || r.URL.Query().Get("panel") == "" {
28 http.Error(w, "missing query", http.StatusBadRequest)
29 return
30 }
31 _ = json.NewEncoder(w).Encode(map[string]any{
32 "code": 0, "msg": "ok",
33 "data": map[string]any{
34 "api": 1, "updated_at": time.Now().Unix(),
35 "plugins": []any{map[string]any{
36 "slug": "redis-manager", "name": "Redis 管理器", "summary": "管理 Redis",
37 "category": "database", "license": "MIT", "author": "dev",
38 "permissions": []any{"service:redis"},
39 "latest": map[string]any{
40 "version": "1.2.0", "arch": "amd64", "size": 3,
41 "sha256": c.sha, "channel": "stable",
42 },
43 "versions": []any{map[string]any{
44 "version": "1.2.0", "arch": "amd64", "size": 3,
45 "sha256": c.sha, "channel": "stable",
46 }},
47 }},
48 },
49 })
50 })
51 mux.HandleFunc("/api/v1/plugins/redis-manager/1.2.0/manifest", func(w http.ResponseWriter, r *http.Request) {
52 sig := sigPrefix + base64.StdEncoding.EncodeToString(ed25519.Sign(c.platform, c.manifest))
53 _ = json.NewEncoder(w).Encode(map[string]any{
54 "code": 0, "msg": "ok",
55 "data": map[string]any{
56 "slug": "redis-manager", "version": "1.2.0", "arch": "amd64",
57 "manifest": string(c.manifest), "platform_sig": sig,
58 "platform_pubkey": pub(c.platform),
59 "artifact": map[string]any{
60 "url": "http://" + r.Host + "/download/plugin/1",
61 "sha256": c.sha, "size": 3, "arch": "amd64",
62 },
63 },
64 })
65 })
66 return mux
67}
68
69func TestCatalogParsesCloudContractAndCaches(t *testing.T) {
70 svc, _, platform, dev := newTestService(t)
71 manifest := buildManifest(t, dev, nil)
72 cs := &catalogServer{manifest: manifest, sha: manifestSHA(t, manifest), platform: platform}
73 ts := httptest.NewServer(cs.handler())
74 defer ts.Close()
75 svc.cfg.PluginRepoURL = ts.URL
76
77 cat, err := svc.Catalog(context.Background(), true)
78 if err != nil {
79 t.Fatal(err)
80 }
81 if len(cat.Plugins) != 1 {
82 t.Fatalf("plugins = %d", len(cat.Plugins))
83 }
84 entry := cat.Plugins[0]
85 if entry.Slug != "redis-manager" || entry.Latest == nil || entry.Latest.Version != "1.2.0" {
86 t.Fatalf("unexpected entry: %+v", entry)
87 }
88 if _, err := svc.Catalog(context.Background(), false); err != nil {
89 t.Fatal(err)
90 }
91 if cs.hits.Load() != 1 {
92 t.Fatalf("catalog fetched %d times, want 1 (cache)", cs.hits.Load())
93 }
94
95 doc, err := svc.ManifestDoc(context.Background(), "redis-manager", "1.2.0", "amd64")
96 if err != nil {
97 t.Fatal(err)
98 }
99 m, err := svc.VerifyDocument(context.Background(), doc)
100 if err != nil {
101 t.Fatalf("manifest rejected: %v", err)
102 }
103 if m.Slug != "redis-manager" {
104 t.Fatalf("slug = %s", m.Slug)
105 }
106}
107
108func TestCatalogFallsBackToDiskCache(t *testing.T) {
109 svc, _, platform, dev := newTestService(t)
110 manifest := buildManifest(t, dev, nil)
111 cs := &catalogServer{manifest: manifest, sha: manifestSHA(t, manifest), platform: platform}
112 ts := httptest.NewServer(cs.handler())
113 svc.cfg.PluginRepoURL = ts.URL
114 if _, err := svc.Catalog(context.Background(), true); err != nil {
115 t.Fatal(err)
116 }
117 ts.Close()
118
119 // Drop only the in-memory copy: the disk cache must keep the catalog usable.
120 svc.mu.Lock()
121 svc.catalog = nil
122 svc.catalogAt = time.Time{}
123 svc.mu.Unlock()
124
125 cat, err := svc.Catalog(context.Background(), false)
126 if err != nil {
127 t.Fatalf("disk cache not used: %v", err)
128 }
129 if len(cat.Plugins) != 1 {
130 t.Fatalf("cached plugins = %d", len(cat.Plugins))
131 }
132}
133
134func TestCatalogRejectsRepositoryErrors(t *testing.T) {
135 svc, _, _, _ := newTestService(t)
136 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
137 w.WriteHeader(http.StatusInternalServerError)
138 _, _ = w.Write([]byte(`{"code":500,"msg":"boom"}`))
139 }))
140 defer ts.Close()
141 svc.cfg.PluginRepoURL = ts.URL
142 if _, err := svc.Catalog(context.Background(), true); err == nil {
143 t.Fatal("server error accepted")
144 }
145}
146
147func manifestSHA(t *testing.T, raw []byte) string {
148 t.Helper()
149 var m struct {
150 Artifact struct {
151 SHA256 string `json:"sha256"`
152 } `json:"artifact"`
153 }
154 if err := json.Unmarshal(raw, &m); err != nil {
155 t.Fatal(err)
156 }
157 return m.Artifact.SHA256
158}