1
0

host_migration_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package database
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. func initMigrateDB(t *testing.T) {
  10. t.Helper()
  11. if err := InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
  12. t.Fatalf("InitDB: %v", err)
  13. }
  14. t.Cleanup(func() { _ = CloseDB() })
  15. }
  16. func seedInboundWithStream(t *testing.T, tag string, port int, stream string) *model.Inbound {
  17. t.Helper()
  18. ib := &model.Inbound{
  19. UserId: 1, Tag: tag, Enable: true, Port: port, Protocol: model.VLESS,
  20. Remark: tag, Settings: `{"clients":[]}`, StreamSettings: stream,
  21. }
  22. if err := GetDB().Create(ib).Error; err != nil {
  23. t.Fatalf("create inbound %s: %v", tag, err)
  24. }
  25. return ib
  26. }
  27. const epMigrationStream = `{"network":"ws","security":"tls","externalProxy":[
  28. {"forceTls":"tls","dest":"a.cdn.com","port":8443,"remark":"A","sni":"a.sni","fingerprint":"chrome","alpn":["h2","h3"],"pinnedPeerCertSha256":["AAAA"],"echConfigList":"ECHV"},
  29. {"forceTls":"none","dest":"b.cdn.com","port":80,"remark":"B"}
  30. ]}`
  31. // #1 — each externalProxy entry becomes one host row with the exact field
  32. // mapping; sort_order is the entry index; inbound_id is correct.
  33. func TestMigrate_ExternalProxyToHosts(t *testing.T) {
  34. initMigrateDB(t)
  35. ib := seedInboundWithStream(t, "m1", 5551, epMigrationStream)
  36. if err := seedHostsFromExternalProxy(); err != nil {
  37. t.Fatalf("migrate: %v", err)
  38. }
  39. var hosts []model.Host
  40. if err := GetDB().Where("inbound_id = ?", ib.Id).Order("sort_order asc").Find(&hosts).Error; err != nil {
  41. t.Fatalf("load hosts: %v", err)
  42. }
  43. if len(hosts) != 2 {
  44. t.Fatalf("hosts = %d, want 2", len(hosts))
  45. }
  46. a := hosts[0]
  47. if a.InboundId != ib.Id || a.SortOrder != 0 || a.Security != "tls" || a.Address != "a.cdn.com" ||
  48. a.Port != 8443 || a.Remark != "A" || a.Sni != "a.sni" || a.Fingerprint != "chrome" || a.EchConfigList != "ECHV" {
  49. t.Fatalf("host A mapping wrong: %+v", a)
  50. }
  51. if len(a.Alpn) != 2 || a.Alpn[0] != "h2" || a.Alpn[1] != "h3" {
  52. t.Fatalf("host A alpn = %v, want [h2 h3]", a.Alpn)
  53. }
  54. if len(a.PinnedPeerCertSha256) != 1 || a.PinnedPeerCertSha256[0] != "AAAA" {
  55. t.Fatalf("host A pins = %v, want [AAAA]", a.PinnedPeerCertSha256)
  56. }
  57. b := hosts[1]
  58. if b.InboundId != ib.Id || b.SortOrder != 1 || b.Security != "none" || b.Address != "b.cdn.com" ||
  59. b.Port != 80 || b.Remark != "B" {
  60. t.Fatalf("host B mapping wrong: %+v", b)
  61. }
  62. }
  63. // #2 — a second run is a no-op (the HistoryOfSeeders gate).
  64. func TestMigrate_Idempotent(t *testing.T) {
  65. initMigrateDB(t)
  66. seedInboundWithStream(t, "m2", 5552, epMigrationStream)
  67. if err := seedHostsFromExternalProxy(); err != nil {
  68. t.Fatalf("first run: %v", err)
  69. }
  70. if err := seedHostsFromExternalProxy(); err != nil {
  71. t.Fatalf("second run: %v", err)
  72. }
  73. var count int64
  74. GetDB().Model(&model.Host{}).Count(&count)
  75. if count != 2 {
  76. t.Fatalf("host count = %d, want 2 (second run must be a no-op)", count)
  77. }
  78. }
  79. // #3 — inbounds without externalProxy create no hosts.
  80. func TestMigrate_NoExternalProxy_NoHosts(t *testing.T) {
  81. initMigrateDB(t)
  82. seedInboundWithStream(t, "m3", 5553, `{"network":"tcp","security":"none"}`)
  83. if err := seedHostsFromExternalProxy(); err != nil {
  84. t.Fatalf("migrate: %v", err)
  85. }
  86. var count int64
  87. GetDB().Model(&model.Host{}).Count(&count)
  88. if count != 0 {
  89. t.Fatalf("host count = %d, want 0", count)
  90. }
  91. }
  92. // #4 — externalProxy stays in StreamSettings (additive, rollback-safe).
  93. func TestMigrate_KeepsExternalProxyIntact(t *testing.T) {
  94. initMigrateDB(t)
  95. ib := seedInboundWithStream(t, "m4", 5554, epMigrationStream)
  96. if err := seedHostsFromExternalProxy(); err != nil {
  97. t.Fatalf("migrate: %v", err)
  98. }
  99. var got model.Inbound
  100. if err := GetDB().First(&got, ib.Id).Error; err != nil {
  101. t.Fatalf("reload inbound: %v", err)
  102. }
  103. if !strings.Contains(got.StreamSettings, "externalProxy") || !strings.Contains(got.StreamSettings, "a.cdn.com") {
  104. t.Fatalf("externalProxy must remain in StreamSettings: %s", got.StreamSettings)
  105. }
  106. }
  107. // #5 — same against a real Postgres DSN (sequence resync); skips without a DSN.
  108. func TestMigrate_Postgres(t *testing.T) {
  109. if strings.TrimSpace(os.Getenv("XUI_DB_DSN")) == "" || os.Getenv("XUI_DB_TYPE") != "postgres" {
  110. t.Skip("set XUI_DB_TYPE=postgres and XUI_DB_DSN to run the postgres migration test")
  111. }
  112. if err := InitDB(""); err != nil {
  113. t.Fatalf("InitDB: %v", err)
  114. }
  115. t.Cleanup(func() { _ = CloseDB() })
  116. // Clean slate so this run owns the migration regardless of prior tests.
  117. GetDB().Exec("TRUNCATE TABLE hosts, inbounds RESTART IDENTITY CASCADE")
  118. GetDB().Where("seeder_name = ?", "HostsFromExternalProxy").Delete(&model.HistoryOfSeeders{})
  119. seedInboundWithStream(t, "mpg", 5555, epMigrationStream)
  120. if err := seedHostsFromExternalProxy(); err != nil {
  121. t.Fatalf("migrate pg: %v", err)
  122. }
  123. var count int64
  124. GetDB().Model(&model.Host{}).Count(&count)
  125. if count != 2 {
  126. t.Fatalf("pg host count = %d, want 2", count)
  127. }
  128. if err := seedHostsFromExternalProxy(); err != nil {
  129. t.Fatalf("migrate pg (2nd): %v", err)
  130. }
  131. GetDB().Model(&model.Host{}).Count(&count)
  132. if count != 2 {
  133. t.Fatalf("pg host count after 2nd run = %d, want 2 (idempotent)", count)
  134. }
  135. }