仰望星辰工作室

miaopanel

miaopanel/ internal/update/update_test.go 5.2 KB · 190 行 原始文件
1package update
2
3import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10
11 "github.com/miaopanel/miaopanel/internal/config"
12)
13
14func TestCompare(t *testing.T) {
15 cases := []struct {
16 a, b string
17 want int
18 }{
19 {"1.0.2", "1.0.2", 0},
20 {"1.0.10", "1.0.9", 1},
21 {"v1.1.0", "1.0.9", 1},
22 {"1.0.1", "1.0.2", -1},
23 {"2", "1.9.9", 1},
24 {"1.0.3-beta", "1.0.2", 1},
25 }
26 for _, c := range cases {
27 if got := Compare(c.a, c.b); got != c.want {
28 t.Errorf("Compare(%q,%q) = %d, want %d", c.a, c.b, got, c.want)
29 }
30 }
31}
32
33func releaseServer(t *testing.T, data map[string]any) *httptest.Server {
34 t.Helper()
35 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
36 if r.URL.Path != "/api/v1/releases/latest" {
37 t.Errorf("unexpected path %s", r.URL.Path)
38 }
39 if got := r.URL.Query().Get("current"); got != "1.0.2" {
40 t.Errorf("current = %q", got)
41 }
42 if got := r.URL.Query().Get("os"); got == "" {
43 t.Error("os missing")
44 }
45 w.Header().Set("Content-Type", "application/json")
46 _ = json.NewEncoder(w).Encode(map[string]any{"code": 0, "msg": "ok", "data": data})
47 }))
48}
49
50func serviceFor(t *testing.T, srv *httptest.Server) *Service {
51 t.Helper()
52 cfg := config.Default()
53 cfg.DataDir = t.TempDir()
54 cfg.UpdateURL = srv.URL
55 cfg.UpdateCheck = true
56 return New(cfg, "1.0.2")
57}
58
59func TestCheckReportsNewerRelease(t *testing.T) {
60 srv := releaseServer(t, map[string]any{
61 "available": true, "version": "1.0.3", "os": "linux", "arch": "amd64",
62 "url": "https://example.invalid/download/9", "sha256": strings.Repeat("a", 64),
63 "size": 1234, "notes": "修复更新弹窗", "published_at": 1700000000,
64 })
65 defer srv.Close()
66
67 info, err := serviceFor(t, srv).Check(context.Background())
68 if err != nil {
69 t.Fatal(err)
70 }
71 if !info.Available || info.Version != "1.0.3" || info.Current != "1.0.2" {
72 t.Fatalf("unexpected info %+v", info)
73 }
74 if info.Notes != "修复更新弹窗" || info.Size != 1234 {
75 t.Fatalf("metadata lost: %+v", info)
76 }
77}
78
79func TestCheckRejectsDowngrade(t *testing.T) {
80 srv := releaseServer(t, map[string]any{
81 "available": true, "version": "1.0.1",
82 "url": "https://example.invalid/download/1", "sha256": strings.Repeat("b", 64),
83 })
84 defer srv.Close()
85
86 info, err := serviceFor(t, srv).Check(context.Background())
87 if err != nil {
88 t.Fatal(err)
89 }
90 if info.Available {
91 t.Fatalf("downgrade accepted: %+v", info)
92 }
93 if info.Message == "" {
94 t.Fatal("expected an explanatory message")
95 }
96}
97
98// A release without a hash cannot be verified, so it must never be offered.
99func TestCheckRequiresSHA256(t *testing.T) {
100 srv := releaseServer(t, map[string]any{
101 "available": true, "version": "1.0.3", "url": "https://example.invalid/download/9",
102 })
103 defer srv.Close()
104
105 info, err := serviceFor(t, srv).Check(context.Background())
106 if err != nil {
107 t.Fatal(err)
108 }
109 if info.Available {
110 t.Fatalf("unverifiable release offered: %+v", info)
111 }
112}
113
114func TestCheckPropagatesServerError(t *testing.T) {
115 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
116 _ = json.NewEncoder(w).Encode(map[string]any{"code": 500, "msg": "读取版本失败"})
117 }))
118 defer srv.Close()
119
120 if _, err := serviceFor(t, srv).Check(context.Background()); err == nil {
121 t.Fatal("expected an error")
122 }
123}
124
125// Some deployments only serve the control plane over http. A https base must
126// fall back to http on the same host, and the download URL must follow suit.
127func TestCheckFallsBackToHTTP(t *testing.T) {
128 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
129 _ = json.NewEncoder(w).Encode(map[string]any{
130 "code": 0, "msg": "ok",
131 "data": map[string]any{
132 "available": true, "version": "1.0.3",
133 "url": "https://" + r.Host + "/download/9",
134 "sha256": strings.Repeat("c", 64),
135 },
136 })
137 }))
138 defer srv.Close()
139
140 cfg := config.Default()
141 cfg.DataDir = t.TempDir()
142 cfg.UpdateURL = "https://" + strings.TrimPrefix(srv.URL, "http://")
143 svc := New(cfg, "1.0.2")
144
145 info, err := svc.Check(context.Background())
146 if err != nil {
147 t.Fatalf("https fallback failed: %v", err)
148 }
149 if !info.Available {
150 t.Fatalf("release not offered after fallback: %+v", info)
151 }
152 if !strings.HasPrefix(info.URL, "http://") {
153 t.Fatalf("download URL scheme not aligned: %q", info.URL)
154 }
155}
156
157func TestAlignSchemeKeepsForeignHosts(t *testing.T) {
158 got := alignScheme("https://cdn.example.com/x", "http://mmmb.example.com")
159 if got != "https://cdn.example.com/x" {
160 t.Fatalf("foreign host rewritten: %q", got)
161 }
162 got = alignScheme("https://mmmb.example.com/x", "http://mmmb.example.com")
163 if got != "http://mmmb.example.com/x" {
164 t.Fatalf("same host not aligned: %q", got)
165 }
166}
167
168func TestDisabledService(t *testing.T) {
169 cfg := config.Default()
170 cfg.UpdateCheck = false
171 svc := New(cfg, "1.0.2")
172 if svc.Enabled() {
173 t.Fatal("update check disabled in config but service is enabled")
174 }
175 info := svc.DisabledInfo()
176 if info.Enabled || info.Current != "1.0.2" {
177 t.Fatalf("unexpected disabled info %+v", info)
178 }
179 if _, err := svc.Apply(context.Background()); err == nil {
180 t.Fatal("Apply must refuse when the feature is disabled")
181 }
182}
183
184func TestDevBuildNeverAdvertisesUpdate(t *testing.T) {
185 cfg := config.Default()
186 svc := New(cfg, "dev")
187 if svc.Enabled() {
188 t.Fatal("dev build must not advertise updates")
189 }
190}