Files
3x-ui/internal/web/service/server_import_sniff_test.go
T
MHSanaei 77dffe9a85 feat(server): sniff sqlite panel restore uploads and keep the fallback on failure
The SQLite panel's Restore now detects the upload by content like the
PostgreSQL panel does: migration dumps are rebuilt with RestoreSQLite,
pg_dump archives get a clear error instead of 'Invalid db file format',
and every upload passes the panel-schema pre-flight before Xray stops.
The .backup fallback survives a failed Xray start and is named in the
error, the DB pool is reopened on every error path after CloseDB, and a
failed InitDB closes the imported file before restoring the fallback so
the rename cannot hit a Windows sharing violation.
2026-07-12 20:14:22 +02:00

46 lines
1.5 KiB
Go

package service
import (
"path/filepath"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
)
func TestSniffImportKind(t *testing.T) {
cases := []struct {
name string
header []byte
want int
}{
{"pg custom archive", []byte("PGDMP\x01\x10\x04"), importKindPgDump},
{"raw sqlite database", []byte("SQLite format 3\x00rest of header"), importKindSQLiteDB},
{"sqlite cli dump without pragma", []byte("BEGIN TRANSACTION;\nCREATE TABLE t(i);"), importKindSQLiteDump},
{"bom and whitespace before pragma", []byte("\xef\xbb\xbf\r\n PRAGMA foreign_keys=OFF;"), importKindSQLiteDump},
{"plain-format postgres dump", []byte("--\n-- PostgreSQL database dump\n--"), importKindUnknown},
{"empty file", nil, importKindUnknown},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := sniffImportKind(tc.header); got != tc.want {
t.Errorf("sniffImportKind(%q) = %d, want %d", tc.header, got, tc.want)
}
})
}
t.Run("panel migration dump", func(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "x-ui.db")
if err := database.InitDB(dbPath); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
dump, err := database.DumpSQLiteToBytes(dbPath)
if err != nil {
t.Fatalf("DumpSQLiteToBytes: %v", err)
}
if got := sniffImportKind(dump[:64]); got != importKindSQLiteDump {
t.Errorf("sniffImportKind(real migration dump) = %d, want %d", got, importKindSQLiteDump)
}
})
}