CI / go-test (push) Canceled after 0s
CI / postgres-durable-first (push) Canceled after 0s
CI / codegen (push) Canceled after 0s
CI / govulncheck (push) Canceled after 0s
CI / race (push) Canceled after 0s
CI / fuzz-smoke (push) Canceled after 0s
CI / golangci (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CodeQL Advanced / Analyze (go) (push) Canceled after 0s
CodeQL Advanced / Analyze (actions) (push) Canceled after 0s
CodeQL Advanced / Analyze (javascript-typescript) (push) Canceled after 0s
Docs CI / build (push) Canceled after 0s
Docs Deploy (GitHub Pages) / build (push) Canceled after 0s
Release 3X-UI / build (386) (push) Canceled after 0s
Release 3X-UI / build (amd64) (push) Canceled after 0s
Release 3X-UI / build (arm64) (push) Canceled after 0s
Release 3X-UI / build (armv5) (push) Canceled after 0s
Release 3X-UI / build (armv6) (push) Canceled after 0s
Release 3X-UI / build (armv7) (push) Canceled after 0s
Release 3X-UI / build (s390x) (push) Canceled after 0s
Release 3X-UI / Build for Windows (amd64) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-latest) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-latest) (push) Canceled after 0s
Docs Deploy (GitHub Pages) / deploy (push) Canceled after 0s
Release 3X-UI / Publish rolling dev release (push) Canceled after 0s
79 lines
2.8 KiB
Go
79 lines
2.8 KiB
Go
package database
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
// TestDedupeInboundSettingsClients_CollapsesDuplicateEmails covers the #5770
|
|
// repair: settings.clients arrays written by older builds can carry the same
|
|
// email several times; startup must collapse them to the first occurrence and
|
|
// leave clean inbounds byte-for-byte untouched.
|
|
func TestDedupeInboundSettingsClients_CollapsesDuplicateEmails(t *testing.T) {
|
|
dbDir := t.TempDir()
|
|
t.Setenv("XUI_DB_FOLDER", dbDir)
|
|
if err := InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
|
|
t.Fatalf("InitDB failed: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = CloseDB() })
|
|
|
|
dupSettings := `{"clients": [` +
|
|
`{"id": "u1", "email": "dup@x", "subId": "s1", "enable": true},` +
|
|
`{"id": "u2", "email": "keep@x", "subId": "s2", "enable": true},` +
|
|
`{"id": "u1", "email": "dup@x", "subId": "s1", "enable": true},` +
|
|
`{"id": "u1", "email": "dup@x", "subId": "s1", "enable": true}]}`
|
|
dirty := model.Inbound{UserId: 1, Port: 21001, Protocol: model.VLESS, Tag: "dedupe-dirty", Settings: dupSettings}
|
|
if err := db.Create(&dirty).Error; err != nil {
|
|
t.Fatalf("create dirty inbound: %v", err)
|
|
}
|
|
|
|
cleanSettings := `{"clients": [{"id": "u3", "email": "solo@x", "subId": "s3", "enable": true}]}`
|
|
clean := model.Inbound{UserId: 1, Port: 21002, Protocol: model.VLESS, Tag: "dedupe-clean", Settings: cleanSettings}
|
|
if err := db.Create(&clean).Error; err != nil {
|
|
t.Fatalf("create clean inbound: %v", err)
|
|
}
|
|
|
|
if err := dedupeInboundSettingsClients(); err != nil {
|
|
t.Fatalf("dedupeInboundSettingsClients: %v", err)
|
|
}
|
|
|
|
var gotDirty model.Inbound
|
|
if err := db.First(&gotDirty, dirty.Id).Error; err != nil {
|
|
t.Fatalf("reload dirty inbound: %v", err)
|
|
}
|
|
var parsed struct {
|
|
Clients []map[string]any `json:"clients"`
|
|
}
|
|
if err := json.Unmarshal([]byte(gotDirty.Settings), &parsed); err != nil {
|
|
t.Fatalf("parse repaired settings: %v", err)
|
|
}
|
|
if len(parsed.Clients) != 2 {
|
|
t.Fatalf("expected 2 clients after dedupe, got %d: %s", len(parsed.Clients), gotDirty.Settings)
|
|
}
|
|
if parsed.Clients[0]["email"] != "dup@x" || parsed.Clients[1]["email"] != "keep@x" {
|
|
t.Fatalf("expected first occurrences [dup@x keep@x], got %v", parsed.Clients)
|
|
}
|
|
|
|
var gotClean model.Inbound
|
|
if err := db.First(&gotClean, clean.Id).Error; err != nil {
|
|
t.Fatalf("reload clean inbound: %v", err)
|
|
}
|
|
if gotClean.Settings != cleanSettings {
|
|
t.Fatalf("clean inbound settings were rewritten:\nbefore: %s\nafter: %s", cleanSettings, gotClean.Settings)
|
|
}
|
|
|
|
if err := dedupeInboundSettingsClients(); err != nil {
|
|
t.Fatalf("second dedupe run: %v", err)
|
|
}
|
|
var again model.Inbound
|
|
if err := db.First(&again, dirty.Id).Error; err != nil {
|
|
t.Fatalf("reload after second run: %v", err)
|
|
}
|
|
if again.Settings != gotDirty.Settings {
|
|
t.Fatal("dedupe is not idempotent: settings changed on the second run")
|
|
}
|
|
}
|