setting_factory_defaults_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. "xrayTemplateConfig",
  49. "tgBotToken",
  50. "twoFactorToken",
  51. "ldapPassword",
  52. "smtpPassword",
  53. } {
  54. t.Run(key, func(t *testing.T) {
  55. if _, ok := defaults[key]; ok {
  56. t.Errorf("factory defaults must not expose %q", key)
  57. }
  58. })
  59. }
  60. }
  61. func TestGetFactoryDefaultsInvariant(t *testing.T) {
  62. defaults := (&SettingService{}).GetFactoryDefaults()
  63. tags := allSettingJSONTags(t)
  64. for key := range defaults {
  65. t.Run(key, func(t *testing.T) {
  66. if !tags[key] {
  67. t.Errorf("key %q is not an entity.AllSetting json tag", key)
  68. }
  69. if factoryDefaultSecretKeys[key] {
  70. t.Errorf("key %q is in the credential deny-list and must not be returned", key)
  71. }
  72. })
  73. }
  74. }