panel_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package panel
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "sync"
  7. "sync/atomic"
  8. "testing"
  9. "time"
  10. "github.com/mhsanaei/3x-ui/v3/internal/config"
  11. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  12. )
  13. func TestIsNewerVersion(t *testing.T) {
  14. cases := []struct {
  15. latest string
  16. current string
  17. want bool
  18. }{
  19. {"v2.9.4", "2.9.3", true},
  20. {"v2.10.0", "2.9.9", true},
  21. {"v2.9.3", "2.9.3", false},
  22. {"v2.9.2", "2.9.3", false},
  23. {"v3.0.0", "2.9.3", true},
  24. }
  25. for _, tc := range cases {
  26. if got := isNewerVersion(tc.latest, tc.current); got != tc.want {
  27. t.Fatalf("isNewerVersion(%q, %q) = %v, want %v", tc.latest, tc.current, got, tc.want)
  28. }
  29. }
  30. }
  31. func TestCompareVersionStringsRejectsUnexpectedFormats(t *testing.T) {
  32. if _, ok := compareVersionStrings("latest", "2.9.3"); ok {
  33. t.Fatal("expected non-semver latest tag to be rejected")
  34. }
  35. if _, ok := compareVersionStrings("v2.9", "2.9.3"); ok {
  36. t.Fatal("expected short version to be rejected")
  37. }
  38. }
  39. func TestShellQuote(t *testing.T) {
  40. if got := shellQuote("/usr/bin/curl"); got != "'/usr/bin/curl'" {
  41. t.Fatalf("unexpected quote result: %s", got)
  42. }
  43. if got := shellQuote("/tmp/a'b"); got != "'/tmp/a'\\''b'" {
  44. t.Fatalf("unexpected quote result with single quote: %s", got)
  45. }
  46. }
  47. func TestExtractReleaseCommit(t *testing.T) {
  48. full := "1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b"
  49. cases := []struct {
  50. name string
  51. release service.Release
  52. want string
  53. }{
  54. {
  55. name: "from body marker",
  56. release: service.Release{Body: "Rolling build\n\ncommit=" + full + "\nbuilt=2026-06-24T00:00:00Z"},
  57. want: full,
  58. },
  59. {
  60. name: "body marker is case-insensitive and wins over target",
  61. release: service.Release{Body: "COMMIT=" + full, TargetCommitish: "deadbeef"},
  62. want: full,
  63. },
  64. {
  65. name: "fallback to target commit sha",
  66. release: service.Release{Body: "no marker here", TargetCommitish: full},
  67. want: full,
  68. },
  69. {
  70. name: "branch target is not a commit",
  71. release: service.Release{Body: "no marker", TargetCommitish: "main"},
  72. want: "",
  73. },
  74. }
  75. for _, tc := range cases {
  76. if got := extractReleaseCommit(&tc.release); got != tc.want {
  77. t.Fatalf("%s: extractReleaseCommit = %q, want %q", tc.name, got, tc.want)
  78. }
  79. }
  80. }
  81. func TestCommitsEqual(t *testing.T) {
  82. full := "1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b"
  83. cases := []struct {
  84. a, b string
  85. want bool
  86. }{
  87. {"1a2b3c4d", full, true}, // injected 8-char prefix matches full release sha
  88. {full, "1a2b3c4d", true}, // order independent
  89. {"1A2B3C4D", full, true}, // case insensitive
  90. {"deadbeef", full, false}, // different commit
  91. {"", full, false}, // empty current never matches
  92. {"1a2b3c4d", "", false}, // empty latest never matches
  93. }
  94. for _, tc := range cases {
  95. if got := commitsEqual(tc.a, tc.b); got != tc.want {
  96. t.Fatalf("commitsEqual(%q, %q) = %v, want %v", tc.a, tc.b, got, tc.want)
  97. }
  98. }
  99. }
  100. func TestShortCommit(t *testing.T) {
  101. if got := shortCommit("1a2b3c4d5e6f7a8b"); got != "1a2b3c4d" {
  102. t.Fatalf("shortCommit truncation = %q, want %q", got, "1a2b3c4d")
  103. }
  104. if got := shortCommit("abc"); got != "abc" {
  105. t.Fatalf("shortCommit short input = %q, want %q", got, "abc")
  106. }
  107. }
  108. func resetUpdateSlot(t *testing.T) {
  109. t.Helper()
  110. t.Cleanup(func() {
  111. updateMu.Lock()
  112. updateRunning = false
  113. updateRunID = 0
  114. updatePID = 0
  115. updateMu.Unlock()
  116. })
  117. }
  118. // writeStatusFile hand-writes the status file in the exact wire format
  119. // update.sh itself produces (a bare printf, not Go's json.Marshal), since
  120. // that's the real cross-language contract this package reads in production.
  121. func writeStatusFile(t *testing.T, path string, runID int64, state string) {
  122. t.Helper()
  123. body := fmt.Sprintf(`{"runId":"%d","state":"%s","exitCode":0,"finishedAt":%d}`, runID, state, time.Now().Unix())
  124. if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
  125. t.Fatal(err)
  126. }
  127. }
  128. func TestAcquireUpdateSlot(t *testing.T) {
  129. resetUpdateSlot(t)
  130. if !acquireUpdateSlot(1) {
  131. t.Fatal("first acquire: got false, want true")
  132. }
  133. if acquireUpdateSlot(2) {
  134. t.Fatal("second acquire while first is held: got true, want false")
  135. }
  136. releaseUpdateSlot()
  137. if !acquireUpdateSlot(3) {
  138. t.Fatal("acquire after release: got false, want true")
  139. }
  140. releaseUpdateSlot()
  141. }
  142. func TestAcquireUpdateSlotExpiresAfterStaleWindow(t *testing.T) {
  143. resetUpdateSlot(t)
  144. if !acquireUpdateSlot(1) {
  145. t.Fatal("first acquire: got false, want true")
  146. }
  147. updateMu.Lock()
  148. updateStarted = time.Now().Add(-(updateStaleAfter + time.Second))
  149. updateMu.Unlock()
  150. if !acquireUpdateSlot(2) {
  151. t.Fatal("acquire after stale window elapsed: got false, want true")
  152. }
  153. releaseUpdateSlot()
  154. }
  155. // TestAcquireUpdateSlotWaitsForAliveProcessPastStaleWindow is the regression
  156. // test for the concurrency bug an upstream review found: past
  157. // updateStaleAfter, the old logic freed the slot purely on elapsed time, even
  158. // if the process it launched was still genuinely running (not crashed) --
  159. // update.sh's own package-manager step plus several downloads can plausibly
  160. // run long on a slow host with nothing actually wrong. Now a confirmed-alive
  161. // PID keeps the slot held past the stale window.
  162. func TestAcquireUpdateSlotWaitsForAliveProcessPastStaleWindow(t *testing.T) {
  163. if runtime.GOOS != "linux" {
  164. t.Skip("processAlive is a no-op stub on non-Linux; this test only exercises real liveness checking on Linux")
  165. }
  166. resetUpdateSlot(t)
  167. if !acquireUpdateSlot(1) {
  168. t.Fatal("first acquire: got false, want true")
  169. }
  170. recordUpdatePID(os.Getpid()) // the test process itself: guaranteed alive
  171. updateMu.Lock()
  172. updateStarted = time.Now().Add(-(updateStaleAfter + time.Second))
  173. updateMu.Unlock()
  174. if acquireUpdateSlot(2) {
  175. t.Fatal("acquire past the stale window while the recorded PID is still alive: got true, want false")
  176. }
  177. releaseUpdateSlot()
  178. }
  179. // TestAcquireUpdateSlotHardCeilingOverridesLiveness confirms the absolute
  180. // backstop: even a confirmed-alive process can't hold the slot forever, so a
  181. // genuinely wedged run can't lock out retries permanently.
  182. func TestAcquireUpdateSlotHardCeilingOverridesLiveness(t *testing.T) {
  183. if runtime.GOOS != "linux" {
  184. t.Skip("processAlive is a no-op stub on non-Linux; this test only exercises real liveness checking on Linux")
  185. }
  186. resetUpdateSlot(t)
  187. if !acquireUpdateSlot(1) {
  188. t.Fatal("first acquire: got false, want true")
  189. }
  190. recordUpdatePID(os.Getpid())
  191. updateMu.Lock()
  192. updateStarted = time.Now().Add(-(updateHardCeiling + time.Second))
  193. updateMu.Unlock()
  194. if !acquireUpdateSlot(2) {
  195. t.Fatal("acquire past the hard ceiling despite a live PID: got false, want true")
  196. }
  197. releaseUpdateSlot()
  198. }
  199. // TestAcquireUpdateSlotReleasesOnTerminalStatus is the regression test for the
  200. // bug adversarial review found: a fast failure used to still lock out retries
  201. // for the full updateStaleAfter window, because acquireUpdateSlot only looked
  202. // at the in-memory started-at timestamp, never at the status file's own
  203. // terminal state.
  204. func TestAcquireUpdateSlotReleasesOnTerminalStatus(t *testing.T) {
  205. t.Setenv("XUI_DB_FOLDER", t.TempDir())
  206. resetUpdateSlot(t)
  207. path := config.GetUpdateStatusFilePath()
  208. if !acquireUpdateSlot(111) {
  209. t.Fatal("first acquire: got false, want true")
  210. }
  211. writeStatusFile(t, path, 111, updateStateFailed)
  212. if !acquireUpdateSlot(222) {
  213. t.Fatal("acquire after the in-flight run reported failed: got false, want true (should not wait out updateStaleAfter)")
  214. }
  215. releaseUpdateSlot()
  216. }
  217. // TestAcquireUpdateSlotIgnoresStaleUnrelatedStatus confirms the terminal-state
  218. // check is scoped to the run it actually launched: a status file left behind
  219. // by some earlier, unrelated run (different runID) must not be mistaken for
  220. // this run finishing.
  221. func TestAcquireUpdateSlotIgnoresStaleUnrelatedStatus(t *testing.T) {
  222. t.Setenv("XUI_DB_FOLDER", t.TempDir())
  223. resetUpdateSlot(t)
  224. path := config.GetUpdateStatusFilePath()
  225. writeStatusFile(t, path, 999, updateStateSuccess)
  226. if !acquireUpdateSlot(111) {
  227. t.Fatal("first acquire: got false, want true")
  228. }
  229. if acquireUpdateSlot(222) {
  230. t.Fatal("acquire while status file only reflects an unrelated older runID: got true, want false")
  231. }
  232. releaseUpdateSlot()
  233. }
  234. // TestAcquireUpdateSlotConcurrency proves the check-then-set is actually
  235. // atomic under real concurrent access, not just correct when called
  236. // sequentially. A prior version of this test suite only ever called
  237. // acquireUpdateSlot from a single goroutine, so it gave no signal if the
  238. // mutex's core promise (only one concurrent launch wins) were broken.
  239. func TestAcquireUpdateSlotConcurrency(t *testing.T) {
  240. resetUpdateSlot(t)
  241. const attempts = 200
  242. var wins atomic.Int32
  243. var wg sync.WaitGroup
  244. wg.Add(attempts)
  245. for i := range attempts {
  246. go func(runID int64) {
  247. defer wg.Done()
  248. if acquireUpdateSlot(runID) {
  249. wins.Add(1)
  250. }
  251. }(int64(i))
  252. }
  253. wg.Wait()
  254. if got := wins.Load(); got != 1 {
  255. t.Fatalf("concurrent acquireUpdateSlot: %d of %d attempts won, want exactly 1", got, attempts)
  256. }
  257. releaseUpdateSlot()
  258. }
  259. func TestGetUpdateStatus(t *testing.T) {
  260. t.Setenv("XUI_DB_FOLDER", t.TempDir())
  261. path := config.GetUpdateStatusFilePath()
  262. svc := &PanelService{}
  263. if got := svc.GetUpdateStatus(); got.State != updateStatePending {
  264. t.Fatalf("missing status file: State = %q, want %q", got.State, updateStatePending)
  265. }
  266. writeStatusFile(t, path, 1735689600123456789, updateStateSuccess)
  267. got := svc.GetUpdateStatus()
  268. if got.RunID != "1735689600123456789" {
  269. t.Fatalf("RunID = %q, want %q (must round-trip as a decimal string, not a JSON number, or it loses precision past 2^53 in JS)", got.RunID, "1735689600123456789")
  270. }
  271. if got.State != updateStateSuccess {
  272. t.Fatalf("State = %q, want %q", got.State, updateStateSuccess)
  273. }
  274. if err := os.WriteFile(path, []byte("not json"), 0o644); err != nil {
  275. t.Fatal(err)
  276. }
  277. if got := svc.GetUpdateStatus(); got.State != updateStatePending {
  278. t.Fatalf("corrupt status file: State = %q, want %q", got.State, updateStatePending)
  279. }
  280. writeStatusFile(t, path, 1, "some-unrecognized-state")
  281. if got := svc.GetUpdateStatus(); got.State != updateStatePending {
  282. t.Fatalf("unrecognized state normalizes to pending: State = %q, want %q", got.State, updateStatePending)
  283. }
  284. }