setting_factory_defaults_test.go 1.9 KB

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