client_add_dedupe_test.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package service
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/google/uuid"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. func mustUnmarshal(t *testing.T, raw string, v any) {
  10. t.Helper()
  11. if err := json.Unmarshal([]byte(raw), v); err != nil {
  12. t.Fatalf("unmarshal %q: %v", raw, err)
  13. }
  14. }
  15. func settingsClientEmails(t *testing.T, inboundId int) []string {
  16. t.Helper()
  17. var ib model.Inbound
  18. if err := database.GetDB().First(&ib, inboundId).Error; err != nil {
  19. t.Fatalf("load inbound %d: %v", inboundId, err)
  20. }
  21. clients, err := (&InboundService{}).GetClients(&ib)
  22. if err != nil {
  23. t.Fatalf("GetClients: %v", err)
  24. }
  25. emails := make([]string, 0, len(clients))
  26. for _, c := range clients {
  27. emails = append(emails, c.Email)
  28. }
  29. return emails
  30. }
  31. // Re-adding a client that is already on the inbound must be an idempotent
  32. // no-op, not a second settings entry: checkEmailsExistForClients exempts a
  33. // matching subId (so one identity can span inbounds), which let retried or
  34. // raced adds duplicate the same email inside one settings array (#5770).
  35. func TestAddInboundClient_SkipsClientsAlreadyOnInbound(t *testing.T) {
  36. setupBulkDB(t)
  37. nodeID, _ := setupNodeRuntime(t)
  38. alice := model.Client{ID: uuid.NewString(), Email: "alice@dup", SubID: "alice-sub-1234567", Enable: true}
  39. ib := nodeInbound(t, nodeID, 33001, []model.Client{alice})
  40. svc := &ClientService{}
  41. inboundSvc := &InboundService{}
  42. if _, err := svc.AddInboundClient(inboundSvc, &model.Inbound{Id: ib.Id, Protocol: model.VLESS, Settings: clientsSettings(t, []model.Client{alice})}); err != nil {
  43. t.Fatalf("re-add of existing client should be a no-op, got error: %v", err)
  44. }
  45. if emails := settingsClientEmails(t, ib.Id); len(emails) != 1 || emails[0] != "alice@dup" {
  46. t.Fatalf("settings after duplicate re-add: expected exactly [alice@dup], got %v", emails)
  47. }
  48. bob := model.Client{ID: uuid.NewString(), Email: "bob@dup", SubID: "bob-sub-123456789", Enable: true}
  49. if _, err := svc.AddInboundClient(inboundSvc, &model.Inbound{Id: ib.Id, Protocol: model.VLESS, Settings: clientsSettings(t, []model.Client{alice, bob})}); err != nil {
  50. t.Fatalf("mixed add (one duplicate, one new): %v", err)
  51. }
  52. if emails := settingsClientEmails(t, ib.Id); len(emails) != 2 || emails[0] != "alice@dup" || emails[1] != "bob@dup" {
  53. t.Fatalf("settings after mixed add: expected [alice@dup bob@dup], got %v", emails)
  54. }
  55. }
  56. func TestDedupeSettingsClients(t *testing.T) {
  57. dup := `{"clients": [` +
  58. `{"id": "u1", "email": "a@x", "subId": "s1"},` +
  59. `{"id": "u2", "email": "b@x", "subId": "s2"},` +
  60. `{"id": "u1", "email": "a@x", "subId": "s1"},` +
  61. `{"id": "u1", "email": "A@X", "subId": "s1"}]}`
  62. out, changed := dedupeSettingsClients(dup)
  63. if !changed {
  64. t.Fatal("expected duplicates to be removed")
  65. }
  66. var parsed struct {
  67. Clients []model.Client `json:"clients"`
  68. }
  69. mustUnmarshal(t, out, &parsed)
  70. if len(parsed.Clients) != 2 || parsed.Clients[0].Email != "a@x" || parsed.Clients[1].Email != "b@x" {
  71. t.Fatalf("expected first occurrences [a@x b@x], got %+v", parsed.Clients)
  72. }
  73. clean := `{"clients": [{"id": "u1", "email": "a@x"}, {"id": "u2", "email": "b@x"}]}`
  74. if _, changed := dedupeSettingsClients(clean); changed {
  75. t.Fatal("clean settings must not be rewritten")
  76. }
  77. if _, changed := dedupeSettingsClients(""); changed {
  78. t.Fatal("empty settings must not be rewritten")
  79. }
  80. if _, changed := dedupeSettingsClients("{not json"); changed {
  81. t.Fatal("invalid settings must not be rewritten")
  82. }
  83. }