traffic_overflow_repair_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package database
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  6. )
  7. // TestRepairOverflowedTrafficCounters_HealsSQLiteRealPromotion reproduces
  8. // #5762: a counter pushed past int64 makes SQLite silently store the cell as
  9. // REAL, after which scanning the row back into the Go int64 field fails and
  10. // every reader of client_traffics breaks. The startup repair must convert the
  11. // cell back to a scannable integer clamped to TrafficMax.
  12. func TestRepairOverflowedTrafficCounters_HealsSQLiteRealPromotion(t *testing.T) {
  13. dbDir := t.TempDir()
  14. t.Setenv("XUI_DB_FOLDER", dbDir)
  15. if err := InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  16. t.Fatalf("InitDB failed: %v", err)
  17. }
  18. t.Cleanup(func() { _ = CloseDB() })
  19. rows := []xray.ClientTraffic{
  20. {Email: "overflowed@x", Enable: true, Up: 5, Down: 6},
  21. {Email: "negative@x", Enable: true, Up: 7, Down: 8},
  22. {Email: "healthy@x", Enable: true, Up: 100, Down: 200},
  23. }
  24. for i := range rows {
  25. if err := db.Create(&rows[i]).Error; err != nil {
  26. t.Fatalf("create traffic row %d: %v", i, err)
  27. }
  28. }
  29. if err := db.Exec("UPDATE client_traffics SET down = 1.2247589467272907e+19 WHERE email = 'overflowed@x'").Error; err != nil {
  30. t.Fatalf("corrupt down: %v", err)
  31. }
  32. if err := db.Exec("UPDATE client_traffics SET up = -42 WHERE email = 'negative@x'").Error; err != nil {
  33. t.Fatalf("corrupt up: %v", err)
  34. }
  35. var broken []xray.ClientTraffic
  36. if err := db.Find(&broken).Error; err == nil {
  37. t.Fatal("expected the REAL-promoted row to break scanning before the repair")
  38. }
  39. if err := repairOverflowedTrafficCounters(); err != nil {
  40. t.Fatalf("repairOverflowedTrafficCounters: %v", err)
  41. }
  42. byEmail := map[string]xray.ClientTraffic{}
  43. var repaired []xray.ClientTraffic
  44. if err := db.Find(&repaired).Error; err != nil {
  45. t.Fatalf("scan after repair: %v", err)
  46. }
  47. for _, r := range repaired {
  48. byEmail[r.Email] = r
  49. }
  50. if got := byEmail["overflowed@x"].Down; got != TrafficMax {
  51. t.Errorf("overflowed down: expected clamp to %d, got %d", TrafficMax, got)
  52. }
  53. if got := byEmail["overflowed@x"].Up; got != 5 {
  54. t.Errorf("overflowed up: expected untouched 5, got %d", got)
  55. }
  56. if got := byEmail["negative@x"].Up; got != 0 {
  57. t.Errorf("negative up: expected clamp to 0, got %d", got)
  58. }
  59. if got := byEmail["healthy@x"]; got.Up != 100 || got.Down != 200 {
  60. t.Errorf("healthy row changed: %+v", got)
  61. }
  62. }
  63. // TestClampedAddExpr_CapsAtTrafficMax verifies the write-path clamp: a delta
  64. // applied to a counter near the cap must saturate at TrafficMax instead of
  65. // overflowing int64 (which SQLite would promote to REAL).
  66. func TestClampedAddExpr_CapsAtTrafficMax(t *testing.T) {
  67. dbDir := t.TempDir()
  68. t.Setenv("XUI_DB_FOLDER", dbDir)
  69. if err := InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  70. t.Fatalf("InitDB failed: %v", err)
  71. }
  72. t.Cleanup(func() { _ = CloseDB() })
  73. row := xray.ClientTraffic{Email: "near-cap@x", Enable: true, Up: TrafficMax - 10, Down: 1}
  74. if err := db.Create(&row).Error; err != nil {
  75. t.Fatalf("create traffic row: %v", err)
  76. }
  77. query := "UPDATE client_traffics SET up = " + ClampedAddExpr("up") + ", down = " + ClampedAddExpr("down") + " WHERE email = ?"
  78. if err := db.Exec(query, int64(1_000_000), int64(5), "near-cap@x").Error; err != nil {
  79. t.Fatalf("clamped add: %v", err)
  80. }
  81. var got xray.ClientTraffic
  82. if err := db.Where("email = ?", "near-cap@x").First(&got).Error; err != nil {
  83. t.Fatalf("scan after clamped add: %v", err)
  84. }
  85. if got.Up != TrafficMax {
  86. t.Errorf("up: expected saturation at %d, got %d", TrafficMax, got.Up)
  87. }
  88. if got.Down != 6 {
  89. t.Errorf("down: expected 6, got %d", got.Down)
  90. }
  91. }