xray_drop_clients_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  5. )
  6. func setRestartOnClientDisable(t *testing.T, value bool) {
  7. t.Helper()
  8. if err := (&SettingService{}).SetRestartXrayOnClientDisable(value); err != nil {
  9. t.Fatalf("SetRestartXrayOnClientDisable(%v): %v", value, err)
  10. }
  11. }
  12. // RemoveUser drops the credential only, so a dropped client needs the restart the
  13. // setting asks for; an edit re-adds the email and needs none.
  14. func TestRestartToDropClients(t *testing.T) {
  15. cases := []struct {
  16. name string
  17. diff *xray.HotDiff
  18. setSetting *bool
  19. wantRestart bool
  20. }{
  21. {
  22. // The default is what makes this reachable for a plain install.
  23. "disabled client, setting untouched",
  24. &xray.HotDiff{RemovedUsers: []xray.UserOp{{Tag: "in-443", Email: "a@x"}}},
  25. nil, true,
  26. },
  27. {
  28. "disabled client, setting on",
  29. &xray.HotDiff{RemovedUsers: []xray.UserOp{{Tag: "in-443", Email: "a@x"}}},
  30. new(true), true,
  31. },
  32. {
  33. "disabled client, setting off",
  34. &xray.HotDiff{RemovedUsers: []xray.UserOp{{Tag: "in-443", Email: "a@x"}}},
  35. new(false), false,
  36. },
  37. {
  38. "edited client",
  39. &xray.HotDiff{
  40. RemovedUsers: []xray.UserOp{{Tag: "in-443", Email: "a@x"}},
  41. AddedUsers: []xray.UserOp{{Tag: "in-443", Email: "a@x"}},
  42. },
  43. new(true), false,
  44. },
  45. {
  46. "unrelated change",
  47. &xray.HotDiff{AddedInbounds: [][]byte{[]byte(`{}`)}},
  48. new(true), false,
  49. },
  50. {
  51. "nil diff",
  52. nil,
  53. new(true), false,
  54. },
  55. }
  56. for _, tc := range cases {
  57. t.Run(tc.name, func(t *testing.T) {
  58. setupConflictDB(t)
  59. if tc.setSetting != nil {
  60. setRestartOnClientDisable(t, *tc.setSetting)
  61. }
  62. got := (&XrayService{}).restartToDropClients(tc.diff)
  63. if got != tc.wantRestart {
  64. t.Fatalf("restartToDropClients = %v, want %v", got, tc.wantRestart)
  65. }
  66. })
  67. }
  68. }