setting_factory_defaults_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package service
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/util/reflect_util"
  6. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  7. )
  8. func allSettingJSONTags(t *testing.T) map[string]bool {
  9. t.Helper()
  10. tags := make(map[string]bool)
  11. for _, field := range reflect_util.GetFields(reflect.TypeFor[entity.AllSetting]()) {
  12. if tag := field.Tag.Get("json"); tag != "" {
  13. tags[tag] = true
  14. }
  15. }
  16. return tags
  17. }
  18. func TestGetFactoryDefaultsExposesBrowserSafeKeys(t *testing.T) {
  19. defaults := (&SettingService{}).GetFactoryDefaults()
  20. tests := []struct {
  21. key string
  22. want string
  23. }{
  24. {key: "webPort", want: "2053"},
  25. {key: "subPort", want: "2096"},
  26. }
  27. for _, tc := range tests {
  28. t.Run(tc.key, func(t *testing.T) {
  29. got, ok := defaults[tc.key]
  30. if !ok {
  31. t.Fatalf("expected key %q in factory defaults", tc.key)
  32. }
  33. if got != tc.want {
  34. t.Errorf("factory default for %q = %q, want %q", tc.key, got, tc.want)
  35. }
  36. })
  37. }
  38. }
  39. func TestGetFactoryDefaultsOmitsSensitiveMaterial(t *testing.T) {
  40. defaults := (&SettingService{}).GetFactoryDefaults()
  41. for _, key := range []string{
  42. "secret",
  43. "panelGuid",
  44. "nodeMtlsCaCertPem",
  45. "nodeMtlsCaKeyPem",
  46. "nodeMtlsClientCertPem",
  47. "nodeMtlsClientKeyPem",
  48. "nodeMtlsClientCertSha256",
  49. "xrayTemplateConfig",
  50. "tgBotToken",
  51. "twoFactorToken",
  52. "ldapPassword",
  53. "smtpPassword",
  54. } {
  55. t.Run(key, func(t *testing.T) {
  56. if _, ok := defaults[key]; ok {
  57. t.Errorf("factory defaults must not expose %q", key)
  58. }
  59. })
  60. }
  61. }
  62. func TestGetFactoryDefaultsInvariant(t *testing.T) {
  63. defaults := (&SettingService{}).GetFactoryDefaults()
  64. tags := allSettingJSONTags(t)
  65. for key := range defaults {
  66. t.Run(key, func(t *testing.T) {
  67. if !tags[key] {
  68. t.Errorf("key %q is not an entity.AllSetting json tag", key)
  69. }
  70. if factoryDefaultSecretKeys[key] {
  71. t.Errorf("key %q is in the credential deny-list and must not be returned", key)
  72. }
  73. })
  74. }
  75. }