service_dedup_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package sub
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  10. )
  11. // TestGetSubs_DuplicateSettingsClients_Deduped reproduces #5134: multi-node
  12. // sync/import drift can leave the same client twice inside an inbound's
  13. // legacy settings.clients JSON while the normalized client_inbounds table
  14. // stays clean. The subscription output must still contain one profile per
  15. // (inbound, client).
  16. func TestGetSubs_DuplicateSettingsClients_Deduped(t *testing.T) {
  17. dbDir := t.TempDir()
  18. t.Setenv("XUI_DB_FOLDER", dbDir)
  19. dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
  20. const subId = "sub-dup"
  21. const email = "[email protected]"
  22. const uuid = "f1b9265f-26a8-4b75-9be2-c64a94b15de1"
  23. db := database.GetDB()
  24. settings := fmt.Sprintf(`{"clients": [
  25. {"id": %q, "email": %q, "subId": %q, "enable": true},
  26. {"id": %q, "email": %q, "subId": %q, "enable": true}
  27. ]}`, uuid, email, subId, uuid, email, subId)
  28. ib := &model.Inbound{
  29. UserId: 1,
  30. Tag: "dup-in",
  31. Enable: true,
  32. Port: 42001,
  33. Protocol: model.VLESS,
  34. Settings: settings,
  35. StreamSettings: `{"network": "tcp", "security": "none"}`,
  36. }
  37. if err := db.Create(ib).Error; err != nil {
  38. t.Fatalf("seed inbound: %v", err)
  39. }
  40. client := &model.ClientRecord{Email: email, SubID: subId, UUID: uuid, Enable: true}
  41. if err := db.Create(client).Error; err != nil {
  42. t.Fatalf("seed client: %v", err)
  43. }
  44. if err := db.Create(&model.ClientInbound{ClientId: client.Id, InboundId: ib.Id}).Error; err != nil {
  45. t.Fatalf("seed client_inbound: %v", err)
  46. }
  47. s := NewSubService("")
  48. links, emails, _, _, err := s.GetSubs(subId, "sub.example.com")
  49. if err != nil {
  50. t.Fatalf("GetSubs: %v", err)
  51. }
  52. if len(links) != 1 {
  53. t.Fatalf("links = %d, want 1 (duplicate settings.clients entries must collapse)", len(links))
  54. }
  55. if len(emails) != 1 {
  56. t.Fatalf("emails = %d, want 1, got %v", len(emails), emails)
  57. }
  58. // Identity, not just count: the single surviving link must be for this client.
  59. if !strings.Contains(links[0], uuid) {
  60. t.Fatalf("surviving link must carry the client uuid %q, got %q", uuid, links[0])
  61. }
  62. }
  63. // TestMatchingClients_DedupsCaseInsensitiveEmail pins the dedup KEY, not just the count.
  64. // clients.email is unique but case-sensitively so: two rows differing only by email case
  65. // can coexist (import drift), and dropping strings.ToLower (or keying on another field)
  66. // would emit both. The first row by id must win, matching the old settings-JSON order.
  67. func TestMatchingClients_DedupsCaseInsensitiveEmail(t *testing.T) {
  68. dbDir := t.TempDir()
  69. t.Setenv("XUI_DB_FOLDER", dbDir)
  70. dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
  71. const subId = "s1"
  72. const uuid = "11111111-2222-4333-8444-555555555555"
  73. db := database.GetDB()
  74. ib := &model.Inbound{Protocol: model.VLESS, Enable: true, Port: 42002, Tag: "dedup-ci", Settings: `{"clients":[]}`}
  75. if err := db.Create(ib).Error; err != nil {
  76. t.Fatalf("seed inbound: %v", err)
  77. }
  78. for _, email := range []string{"[email protected]", "[email protected]"} {
  79. c := &model.ClientRecord{Email: email, SubID: subId, UUID: uuid, Enable: true}
  80. if err := db.Create(c).Error; err != nil {
  81. t.Fatalf("seed client %q: %v", email, err)
  82. }
  83. if err := db.Create(&model.ClientInbound{ClientId: c.Id, InboundId: ib.Id}).Error; err != nil {
  84. t.Fatalf("seed client_inbound %q: %v", email, err)
  85. }
  86. }
  87. s := &SubService{}
  88. got := s.matchingClients(ib, subId)
  89. if len(got) != 1 {
  90. t.Fatalf("case-differing duplicate emails must dedup to 1 client, got %d", len(got))
  91. }
  92. if got[0].Email != "[email protected]" {
  93. t.Fatalf("first occurrence must be kept, got %q", got[0].Email)
  94. }
  95. // A wrong subId must still be excluded (guards the SQL subId filter).
  96. if other := s.matchingClients(ib, "nope"); len(other) != 0 {
  97. t.Fatalf("non-matching subId must yield 0 clients, got %d", len(other))
  98. }
  99. }