inbound_client_ips_merge_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package service
  2. import (
  3. "encoding/json"
  4. "path/filepath"
  5. "testing"
  6. "time"
  7. "github.com/mhsanaei/3x-ui/v3/database"
  8. "github.com/mhsanaei/3x-ui/v3/database/model"
  9. )
  10. // setupClientIpTestDB spins up a throwaway SQLite database (migrations + seeders)
  11. // for a single test, mirroring the harness used by the other service tests.
  12. func setupClientIpTestDB(t *testing.T) {
  13. t.Helper()
  14. dbDir := t.TempDir()
  15. t.Setenv("XUI_DB_FOLDER", dbDir)
  16. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  17. t.Fatalf("InitDB: %v", err)
  18. }
  19. t.Cleanup(func() { _ = database.CloseDB() })
  20. }
  21. func marshalIps(t *testing.T, entries ...clientIpEntry) string {
  22. t.Helper()
  23. b, err := json.Marshal(entries)
  24. if err != nil {
  25. t.Fatalf("marshal ips: %v", err)
  26. }
  27. return string(b)
  28. }
  29. // readClientIps returns the stored IP entries for an email as a map[ip]timestamp,
  30. // plus whether the row exists at all.
  31. func readClientIps(t *testing.T, email string) (map[string]int64, bool) {
  32. t.Helper()
  33. var row model.InboundClientIps
  34. err := database.GetDB().Where("client_email = ?", email).First(&row).Error
  35. if database.IsNotFound(err) {
  36. return nil, false
  37. }
  38. if err != nil {
  39. t.Fatalf("read client ips for %s: %v", email, err)
  40. }
  41. var entries []clientIpEntry
  42. if row.Ips != "" {
  43. if err := json.Unmarshal([]byte(row.Ips), &entries); err != nil {
  44. t.Fatalf("unmarshal stored ips for %s: %v", email, err)
  45. }
  46. }
  47. out := make(map[string]int64, len(entries))
  48. for _, e := range entries {
  49. out[e.IP] = e.Timestamp
  50. }
  51. return out, true
  52. }
  53. func TestMergeInboundClientIps_CreatesNodeOnlyRowIgnoringRemoteId(t *testing.T) {
  54. setupClientIpTestDB(t)
  55. db := database.GetDB()
  56. now := time.Now().Unix()
  57. // Local client occupies id 1.
  58. local := &model.InboundClientIps{ClientEmail: "local@x", Ips: marshalIps(t, clientIpEntry{IP: "1.1.1.1", Timestamp: now})}
  59. if err := db.Create(local).Error; err != nil {
  60. t.Fatalf("seed local row: %v", err)
  61. }
  62. // Incoming node-only client carries the remote node's id 1, which must not
  63. // collide with the local row.
  64. incoming := []model.InboundClientIps{{
  65. Id: 1,
  66. ClientEmail: "node@x",
  67. Ips: marshalIps(t, clientIpEntry{IP: "2.2.2.2", Timestamp: now}),
  68. }}
  69. if err := (&InboundService{}).MergeInboundClientIps(incoming); err != nil {
  70. t.Fatalf("merge: %v", err)
  71. }
  72. // Local row is untouched.
  73. if ips, ok := readClientIps(t, "local@x"); !ok || ips["1.1.1.1"] != now {
  74. t.Fatalf("local@x changed unexpectedly: %v (exists=%v)", ips, ok)
  75. }
  76. // Node row exists with its own ip and a freshly assigned id (not the remote 1).
  77. var nodeRow model.InboundClientIps
  78. if err := db.Where("client_email = ?", "node@x").First(&nodeRow).Error; err != nil {
  79. t.Fatalf("node@x not created: %v", err)
  80. }
  81. if nodeRow.Id == local.Id {
  82. t.Fatalf("node@x reused local id %d instead of a fresh one", nodeRow.Id)
  83. }
  84. if ips, _ := readClientIps(t, "node@x"); ips["2.2.2.2"] != now {
  85. t.Fatalf("node@x missing expected ip: %v", ips)
  86. }
  87. }
  88. func TestMergeInboundClientIps_DedupKeepsMaxTimestamp(t *testing.T) {
  89. setupClientIpTestDB(t)
  90. db := database.GetDB()
  91. now := time.Now().Unix()
  92. if err := db.Create(&model.InboundClientIps{
  93. ClientEmail: "a@x",
  94. Ips: marshalIps(t, clientIpEntry{IP: "1.1.1.1", Timestamp: now - 100}),
  95. }).Error; err != nil {
  96. t.Fatalf("seed: %v", err)
  97. }
  98. incoming := []model.InboundClientIps{{
  99. ClientEmail: "a@x",
  100. Ips: marshalIps(t,
  101. clientIpEntry{IP: "1.1.1.1", Timestamp: now - 50}, // newer than stored -> wins
  102. clientIpEntry{IP: "2.2.2.2", Timestamp: now - 10},
  103. ),
  104. }}
  105. if err := (&InboundService{}).MergeInboundClientIps(incoming); err != nil {
  106. t.Fatalf("merge: %v", err)
  107. }
  108. ips, _ := readClientIps(t, "a@x")
  109. if len(ips) != 2 {
  110. t.Fatalf("want 2 ips, got %v", ips)
  111. }
  112. if ips["1.1.1.1"] != now-50 {
  113. t.Fatalf("1.1.1.1 should keep max timestamp %d, got %d", now-50, ips["1.1.1.1"])
  114. }
  115. if ips["2.2.2.2"] != now-10 {
  116. t.Fatalf("2.2.2.2 missing/incorrect: %d", ips["2.2.2.2"])
  117. }
  118. }
  119. func TestMergeInboundClientIps_DropsStaleIps(t *testing.T) {
  120. setupClientIpTestDB(t)
  121. db := database.GetDB()
  122. now := time.Now().Unix()
  123. if err := db.Create(&model.InboundClientIps{
  124. ClientEmail: "a@x",
  125. Ips: marshalIps(t,
  126. clientIpEntry{IP: "old", Timestamp: now - 3600}, // > 30m -> stale
  127. clientIpEntry{IP: "fresh", Timestamp: now - 60},
  128. ),
  129. }).Error; err != nil {
  130. t.Fatalf("seed: %v", err)
  131. }
  132. incoming := []model.InboundClientIps{{
  133. ClientEmail: "a@x",
  134. Ips: marshalIps(t,
  135. clientIpEntry{IP: "incStale", Timestamp: now - 4000}, // > 30m -> stale
  136. clientIpEntry{IP: "incFresh", Timestamp: now - 10},
  137. ),
  138. }}
  139. if err := (&InboundService{}).MergeInboundClientIps(incoming); err != nil {
  140. t.Fatalf("merge: %v", err)
  141. }
  142. ips, _ := readClientIps(t, "a@x")
  143. if len(ips) != 2 {
  144. t.Fatalf("want only fresh ips, got %v", ips)
  145. }
  146. if _, ok := ips["old"]; ok {
  147. t.Fatalf("stale local ip not dropped: %v", ips)
  148. }
  149. if _, ok := ips["incStale"]; ok {
  150. t.Fatalf("stale incoming ip not dropped: %v", ips)
  151. }
  152. if ips["fresh"] != now-60 || ips["incFresh"] != now-10 {
  153. t.Fatalf("fresh ips wrong: %v", ips)
  154. }
  155. }
  156. func TestMergeInboundClientIps_SkipsAllStaleCreate(t *testing.T) {
  157. setupClientIpTestDB(t)
  158. now := time.Now().Unix()
  159. incoming := []model.InboundClientIps{{
  160. ClientEmail: "b@x",
  161. Ips: marshalIps(t, clientIpEntry{IP: "1.1.1.1", Timestamp: now - 9999}),
  162. }}
  163. if err := (&InboundService{}).MergeInboundClientIps(incoming); err != nil {
  164. t.Fatalf("merge: %v", err)
  165. }
  166. if _, ok := readClientIps(t, "b@x"); ok {
  167. t.Fatalf("all-stale node-only client should not create a row")
  168. }
  169. }
  170. func TestMergeInboundClientIps_SkipsBlankRows(t *testing.T) {
  171. setupClientIpTestDB(t)
  172. now := time.Now().Unix()
  173. incoming := []model.InboundClientIps{
  174. {ClientEmail: "", Ips: marshalIps(t, clientIpEntry{IP: "1.1.1.1", Timestamp: now})},
  175. {ClientEmail: "c@x", Ips: ""},
  176. }
  177. if err := (&InboundService{}).MergeInboundClientIps(incoming); err != nil {
  178. t.Fatalf("merge: %v", err)
  179. }
  180. var count int64
  181. if err := database.GetDB().Model(&model.InboundClientIps{}).Count(&count).Error; err != nil {
  182. t.Fatalf("count: %v", err)
  183. }
  184. if count != 0 {
  185. t.Fatalf("blank rows should be skipped, but %d row(s) created", count)
  186. }
  187. }