import_host_settings_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. // An imported database carries the source machine's listen addresses,
  8. // certificates and node identity. Keeping this machine's own values is what
  9. // stops the panel from becoming unreachable on its own address after a restore.
  10. func TestImportKeepsHostBoundSettings(t *testing.T) {
  11. setupConflictDB(t)
  12. db := database.GetDB()
  13. mine := map[string]string{
  14. "webPort": "8443",
  15. "webCertFile": "/etc/ssl/this-host.pem",
  16. "webBasePath": "/mine/",
  17. "subURI": "https://this-host.example/sub/",
  18. "panelGuid": "this-host-guid",
  19. "nodeMtlsClientCertPem": "this-host-leaf",
  20. }
  21. for key, value := range mine {
  22. if err := db.Create(&model.Setting{Key: key, Value: value}).Error; err != nil {
  23. t.Fatalf("seed %s: %v", key, err)
  24. }
  25. }
  26. // A setting that belongs to the configuration, not the machine.
  27. if err := db.Create(&model.Setting{Key: "remarkTemplate", Value: "mine"}).Error; err != nil {
  28. t.Fatal(err)
  29. }
  30. kept := captureHostBoundSettings()
  31. if len(kept.values) != len(mine) {
  32. t.Fatalf("captured %d host settings, want %d: %v", len(kept.values), len(mine), kept.values)
  33. }
  34. // Stand in for the import: every row now holds the source machine's value.
  35. for key := range mine {
  36. if err := db.Model(&model.Setting{}).Where("key = ?", key).
  37. Update("value", "from-imported-file").Error; err != nil {
  38. t.Fatalf("overwrite %s: %v", key, err)
  39. }
  40. }
  41. if err := db.Model(&model.Setting{}).Where("key = ?", "remarkTemplate").
  42. Update("value", "from-imported-file").Error; err != nil {
  43. t.Fatal(err)
  44. }
  45. restoreHostBoundSettings(kept)
  46. for key, want := range mine {
  47. var got model.Setting
  48. if err := db.Where("key = ?", key).First(&got).Error; err != nil {
  49. t.Fatalf("read back %s: %v", key, err)
  50. }
  51. if got.Value != want {
  52. t.Fatalf("setting %s = %q after import, want this machine's %q", key, got.Value, want)
  53. }
  54. }
  55. var carried model.Setting
  56. if err := db.Where("key = ?", "remarkTemplate").First(&carried).Error; err != nil {
  57. t.Fatal(err)
  58. }
  59. if carried.Value != "from-imported-file" {
  60. t.Fatalf("remarkTemplate = %q, want the imported value: only host-bound keys may survive", carried.Value)
  61. }
  62. }
  63. // The destination usually has no row at all for the certificate paths and the
  64. // node identity — the built-in default applies. The imported row must go, or
  65. // the panel quietly adopts the source machine's certificate path.
  66. func TestImportDropsHostBoundSettingsThisMachineNeverHad(t *testing.T) {
  67. setupConflictDB(t)
  68. db := database.GetDB()
  69. kept := captureHostBoundSettings()
  70. for _, key := range []string{"webCertFile", "subCertFile", "nodeMtlsClientCertPem"} {
  71. if err := db.Create(&model.Setting{Key: key, Value: "from-imported-file"}).Error; err != nil {
  72. t.Fatalf("seed imported %s: %v", key, err)
  73. }
  74. }
  75. restoreHostBoundSettings(kept)
  76. for _, key := range []string{"webCertFile", "subCertFile", "nodeMtlsClientCertPem"} {
  77. var count int64
  78. if err := db.Model(&model.Setting{}).Where("key = ?", key).Count(&count).Error; err != nil {
  79. t.Fatal(err)
  80. }
  81. if count != 0 {
  82. t.Fatalf("imported %s survived although this machine had no row for it", key)
  83. }
  84. }
  85. }