mutation_audit_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package xray
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "slices"
  7. "testing"
  8. "time"
  9. "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  10. )
  11. // ---------------------------------------------------------------------------
  12. // hot_diff.go mutation audits
  13. // ---------------------------------------------------------------------------
  14. // TestDiffOutbounds_EmptyOutboundsNoPanic pins hot_diff.go:154 — the
  15. // `len(oldOut) > 0` guard that protects the oldOut[0]/newOut[0] index. With no
  16. // outbounds on either side the first-outbound identity check must be SKIPPED
  17. // (an empty hot diff), never executed; a mutated guard (`>= 0`) would index a
  18. // nil slice and panic.
  19. func TestDiffOutbounds_EmptyOutboundsNoPanic(t *testing.T) {
  20. oldCfg := makeHotConfig()
  21. oldCfg.OutboundConfigs = nil
  22. newCfg := makeHotConfig()
  23. newCfg.OutboundConfigs = nil
  24. diff, ok := ComputeHotDiff(oldCfg, newCfg)
  25. if !ok {
  26. t.Fatal("identical empty-outbound configs must be hot-appliable")
  27. }
  28. if len(diff.RemovedOutboundTags) != 0 || len(diff.AddedOutbounds) != 0 {
  29. t.Fatalf("no outbounds on either side must yield no outbound ops, got %+v", diff)
  30. }
  31. }
  32. // TestDiffOutbounds_SingleFirstOutboundChangeNeedsRestart pins the other side
  33. // of the hot_diff.go:154 boundary. With exactly ONE outbound, changing its
  34. // content touches the default (first) handler, which has no replace API — it
  35. // must force a restart. A mutated guard (`> 1`) would skip the first-outbound
  36. // check at this length and wrongly classify the change as hot-appliable.
  37. func TestDiffOutbounds_SingleFirstOutboundChangeNeedsRestart(t *testing.T) {
  38. oldCfg := makeHotConfig()
  39. oldCfg.OutboundConfigs = json_util.RawMessage(`[{"protocol":"freedom","tag":"direct"}]`)
  40. newCfg := makeHotConfig()
  41. newCfg.OutboundConfigs = json_util.RawMessage(`[{"protocol":"freedom","settings":{"domainStrategy":"UseIP"},"tag":"direct"}]`)
  42. if _, ok := ComputeHotDiff(oldCfg, newCfg); ok {
  43. t.Fatal("changing the only (default) outbound must force a restart")
  44. }
  45. }
  46. // TestRoutingWithoutReloadable_EmptyInput pins hot_diff.go:219 — the
  47. // `len(raw) > 0` guard that skips JSON decoding of empty input. Empty input
  48. // must canonicalize to the empty object `{}` with ok=true (no rules/balancers
  49. // to strip). A mutated guard (`>= 0`) would feed an empty reader to the JSON
  50. // decoder, get io.EOF, and wrongly return ok=false.
  51. func TestRoutingWithoutReloadable_EmptyInput(t *testing.T) {
  52. out, ok := routingWithoutReloadable([]byte{})
  53. if !ok {
  54. t.Fatal("empty routing input must canonicalize successfully")
  55. }
  56. if string(out) != "{}" {
  57. t.Fatalf("empty routing input must canonicalize to {}, got %q", out)
  58. }
  59. // nil input behaves the same as empty.
  60. out, ok = routingWithoutReloadable(nil)
  61. if !ok || string(out) != "{}" {
  62. t.Fatalf("nil routing input must canonicalize to {}, ok=%v out=%q", ok, out)
  63. }
  64. }
  65. // TestRoutingWithoutReloadable_StripsRulesAndBalancers complements the guard
  66. // test: with real content the reloadable keys (rules, balancers) are removed
  67. // and only the restart-only remainder is returned. This pins that a routing
  68. // change limited to rules/balancers leaves an identical remainder.
  69. func TestRoutingWithoutReloadable_StripsRulesAndBalancers(t *testing.T) {
  70. a, ok := routingWithoutReloadable([]byte(`{"domainStrategy":"AsIs","rules":[{"x":1}],"balancers":[{"y":2}]}`))
  71. if !ok {
  72. t.Fatal("valid routing input must parse")
  73. }
  74. b, ok := routingWithoutReloadable([]byte(`{"domainStrategy":"AsIs","rules":[],"balancers":[]}`))
  75. if !ok {
  76. t.Fatal("valid routing input must parse")
  77. }
  78. if string(a) != string(b) {
  79. t.Fatalf("rules/balancers must be stripped: %q != %q", a, b)
  80. }
  81. if string(a) != `{"domainStrategy":"AsIs"}` {
  82. t.Fatalf("remainder must keep only restart-only keys, got %q", a)
  83. }
  84. }
  85. // TestApiTagFromConfig pins hot_diff.go:357 — the three-part guard
  86. // `len(api) > 0 && Unmarshal == nil && parsed.Tag != ""`. Each conjunct must
  87. // hold for a custom tag to be honored; otherwise the default "api" is used.
  88. func TestApiTagFromConfig(t *testing.T) {
  89. cases := []struct {
  90. name string
  91. api string
  92. want string
  93. }{
  94. {"empty input falls back to api", "", "api"},
  95. {"explicit tag honored", `{"tag":"my-api"}`, "my-api"},
  96. {"empty tag falls back to api", `{"tag":""}`, "api"},
  97. {"missing tag falls back to api", `{"services":["StatsService"]}`, "api"},
  98. {"unparsable falls back to api", `{not-json`, "api"},
  99. }
  100. for _, tc := range cases {
  101. t.Run(tc.name, func(t *testing.T) {
  102. got := apiTagFromConfig(json_util.RawMessage(tc.api))
  103. if got != tc.want {
  104. t.Fatalf("apiTagFromConfig(%q) = %q, want %q", tc.api, got, tc.want)
  105. }
  106. })
  107. }
  108. }
  109. // TestApiTagDrivesInboundRestartGuard ties hot_diff.go:357 to its consumer:
  110. // the api tag resolved from the api section is the tag whose inbound change
  111. // forces a restart. With a custom api.tag, changing that inbound must NOT be
  112. // hot-appliable (it carries the gRPC server the panel talks through).
  113. func TestApiTagDrivesInboundRestartGuard(t *testing.T) {
  114. oldCfg := makeHotConfig()
  115. oldCfg.API = json_util.RawMessage(`{"services":["HandlerService"],"tag":"custom-api"}`)
  116. oldCfg.InboundConfigs[0].Tag = "custom-api"
  117. newCfg := makeHotConfig()
  118. newCfg.API = json_util.RawMessage(`{"services":["HandlerService"],"tag":"custom-api"}`)
  119. newCfg.InboundConfigs[0].Tag = "custom-api"
  120. newCfg.InboundConfigs[0].Port = 62790 // change the custom-api inbound
  121. if _, ok := ComputeHotDiff(oldCfg, newCfg); ok {
  122. t.Fatal("changing the inbound named by a custom api.tag must force a restart")
  123. }
  124. }
  125. // ---------------------------------------------------------------------------
  126. // process.go mutation audits (pure-logic, cross-platform)
  127. // ---------------------------------------------------------------------------
  128. // TestIsRunning_ExitedProcessWithClosedDone pins process.go:240 — the
  129. // `if p.done != nil` guard that decides whether to consult the done channel.
  130. // When the process has exited (done closed) but ProcessState has not yet been
  131. // observed, IsRunning must report false via the closed-channel select. A
  132. // mutated guard (`== nil`) would skip the select and wrongly report true.
  133. func TestIsRunning_ExitedProcessWithClosedDone(t *testing.T) {
  134. p := newProcess(nil)
  135. p.cmd = &exec.Cmd{Process: &os.Process{}}
  136. done := make(chan struct{})
  137. close(done)
  138. p.done = done
  139. if p.IsRunning() {
  140. t.Fatal("a process whose done channel is closed must report not running")
  141. }
  142. }
  143. // TestIsRunning_LiveProcessWithOpenDone is the complementary case: an open
  144. // done channel and no ProcessState means the process is alive, so IsRunning
  145. // must report true (the select's default branch is taken).
  146. func TestIsRunning_LiveProcessWithOpenDone(t *testing.T) {
  147. p := newProcess(nil)
  148. p.cmd = &exec.Cmd{Process: &os.Process{}}
  149. p.done = make(chan struct{}) // open
  150. if !p.IsRunning() {
  151. t.Fatal("a process with an open done channel and live cmd must report running")
  152. }
  153. }
  154. // TestGetResult pins process.go:260 — the
  155. // `if len(lastLine) == 0 && exitErr != nil` choice between the captured log
  156. // line and the exit error string.
  157. func TestGetResult(t *testing.T) {
  158. cases := []struct {
  159. name string
  160. lastLine string
  161. exitErr error
  162. want string
  163. }{
  164. {"no line, has error -> error string", "", errProcessTest("boom"), "boom"},
  165. {"has line -> line wins over error", "last log", errProcessTest("boom"), "last log"},
  166. {"no line, no error -> empty", "", nil, ""},
  167. {"has line, no error -> line", "last log", nil, "last log"},
  168. }
  169. for _, tc := range cases {
  170. t.Run(tc.name, func(t *testing.T) {
  171. p := newProcess(nil)
  172. p.logWriter.lastLine = tc.lastLine
  173. p.exitErr = tc.exitErr
  174. if got := p.GetResult(); got != tc.want {
  175. t.Fatalf("GetResult() = %q, want %q", got, tc.want)
  176. }
  177. })
  178. }
  179. }
  180. type errProcessTest string
  181. func (e errProcessTest) Error() string { return string(e) }
  182. // TestRefreshLocalOnline_GraceBoundaryEmails pins the exact `<` boundary at
  183. // process.go:407: an email idle for EXACTLY graceMs must be aged out (the
  184. // window is half-open, age < grace). A mutated comparison (`<=`) would keep it.
  185. func TestRefreshLocalOnline_GraceBoundaryEmails(t *testing.T) {
  186. p := newOnlineTestProcess()
  187. const grace = int64(20000)
  188. p.RefreshLocalOnline([]string{"edge"}, nil, 0, grace)
  189. // now-ts == grace exactly: age is not strictly < grace, so it must drop.
  190. p.RefreshLocalOnline(nil, nil, grace, grace)
  191. for _, e := range p.GetLocalOnlineClients() {
  192. if e == "edge" {
  193. t.Fatalf("email idle exactly graceMs must age out (half-open window), got online %v", p.GetLocalOnlineClients())
  194. }
  195. }
  196. // One millisecond inside the window must still be online.
  197. p2 := newOnlineTestProcess()
  198. p2.RefreshLocalOnline([]string{"edge"}, nil, 0, grace)
  199. p2.RefreshLocalOnline(nil, nil, grace-1, grace)
  200. if !containsString(p2.GetLocalOnlineClients(), "edge") {
  201. t.Fatalf("email idle graceMs-1 must still be online, got %v", p2.GetLocalOnlineClients())
  202. }
  203. }
  204. // TestRefreshLocalOnline_GraceBoundaryInbounds pins the same `<` boundary at
  205. // process.go:423 for inbound tags.
  206. func TestRefreshLocalOnline_GraceBoundaryInbounds(t *testing.T) {
  207. p := newOnlineTestProcess()
  208. const grace = int64(20000)
  209. p.RefreshLocalOnline(nil, []string{"in-edge"}, 0, grace)
  210. p.RefreshLocalOnline(nil, nil, grace, grace)
  211. for _, tag := range p.GetLocalActiveInbounds() {
  212. if tag == "in-edge" {
  213. t.Fatalf("inbound idle exactly graceMs must age out, got active %v", p.GetLocalActiveInbounds())
  214. }
  215. }
  216. p2 := newOnlineTestProcess()
  217. p2.RefreshLocalOnline(nil, []string{"in-edge"}, 0, grace)
  218. p2.RefreshLocalOnline(nil, nil, grace-1, grace)
  219. if !containsString(p2.GetLocalActiveInbounds(), "in-edge") {
  220. t.Fatalf("inbound idle graceMs-1 must still be active, got %v", p2.GetLocalActiveInbounds())
  221. }
  222. }
  223. func containsString(s []string, v string) bool {
  224. return slices.Contains(s, v)
  225. }
  226. // ---------------------------------------------------------------------------
  227. // process.go mutation audits (require a real child process; re-invoke the
  228. // test binary so they run cross-platform, no signals needed)
  229. // ---------------------------------------------------------------------------
  230. // TestWaitForCommand_CrashExitRecordsError pins process.go:554 — the
  231. // `if err == nil || intentionalStop` guard. A process that exits with a
  232. // NON-zero code on its own (not an intentional Stop) is a crash and its error
  233. // MUST be recorded. A mutated guard that negates the err check (`err != nil`)
  234. // would early-return and drop the error.
  235. func TestWaitForCommand_CrashExitRecordsError(t *testing.T) {
  236. t.Setenv("XUI_LOG_FOLDER", t.TempDir())
  237. cmd := exec.Command(os.Args[0], "-test.run=TestMutationAuditHelper", "--", "crash-exit")
  238. cmd.Env = append(os.Environ(), "XRAY_MUT_HELPER=1")
  239. p := newProcess(nil)
  240. if err := p.startCommand(cmd); err != nil {
  241. t.Fatalf("startCommand: %v", err)
  242. }
  243. // We never call Stop -> intentionalStop stays false; the child exits 2.
  244. if err := p.waitForExit(5 * time.Second); err != nil {
  245. t.Fatalf("child did not exit: %v", err)
  246. }
  247. if p.GetErr() == nil {
  248. t.Fatal("a non-intentional non-zero exit must record an error")
  249. }
  250. }
  251. // TestStop_RemovesTempConfigFile pins process.go:579 — the
  252. // `if p.configPath != ""` guard that removes the per-run temp config file on
  253. // Stop (so test runs never disturb the main config.json). A mutated guard
  254. // (`== ""`) would skip the removal and leak the temp file.
  255. func TestStop_RemovesTempConfigFile(t *testing.T) {
  256. t.Setenv("XUI_LOG_FOLDER", t.TempDir())
  257. tmpCfg := filepath.Join(t.TempDir(), "test-config.json")
  258. if err := os.WriteFile(tmpCfg, []byte("{}"), 0o644); err != nil {
  259. t.Fatalf("write temp config: %v", err)
  260. }
  261. cmd := exec.Command(os.Args[0], "-test.run=TestMutationAuditHelper", "--", "block")
  262. cmd.Env = append(os.Environ(), "XRAY_MUT_HELPER=1")
  263. p := newProcess(nil)
  264. p.configPath = tmpCfg
  265. if err := p.startCommand(cmd); err != nil {
  266. t.Fatalf("startCommand: %v", err)
  267. }
  268. t.Cleanup(func() {
  269. if p.IsRunning() {
  270. p.intentionalStop.Store(true)
  271. _ = p.cmd.Process.Kill()
  272. _ = p.waitForExit(2 * time.Second)
  273. }
  274. })
  275. if !p.IsRunning() {
  276. t.Fatal("helper process must be running before Stop")
  277. }
  278. if err := p.Stop(); err != nil {
  279. t.Fatalf("Stop: %v", err)
  280. }
  281. if _, err := os.Stat(tmpCfg); !os.IsNotExist(err) {
  282. t.Fatalf("temp config file must be removed on Stop, stat err=%v", err)
  283. }
  284. }
  285. // TestMutationAuditHelper is the re-invoked child for the process tests above.
  286. // It is inert unless XRAY_MUT_HELPER=1 is set.
  287. func TestMutationAuditHelper(t *testing.T) {
  288. if os.Getenv("XRAY_MUT_HELPER") != "1" {
  289. return
  290. }
  291. mode := ""
  292. for i, arg := range os.Args {
  293. if arg == "--" && i+1 < len(os.Args) {
  294. mode = os.Args[i+1]
  295. break
  296. }
  297. }
  298. switch mode {
  299. case "crash-exit":
  300. os.Exit(2)
  301. case "block":
  302. select {}
  303. }
  304. }