check_valid_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package entity
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestCheckValidSmtpFrom(t *testing.T) {
  7. base := func() *AllSetting {
  8. return &AllSetting{WebPort: 2053, SubPort: 2096}
  9. }
  10. for _, v := range []string{"", "[email protected]"} {
  11. s := base()
  12. s.SmtpFrom = v
  13. if err := s.CheckValid(); err != nil {
  14. t.Errorf("CheckValid with smtpFrom=%q: unexpected error %v", v, err)
  15. }
  16. }
  17. for _, v := range []string{
  18. "not-an-address",
  19. "[email protected]\r\nBcc: [email protected]",
  20. "a@b\nSubject: injected",
  21. } {
  22. s := base()
  23. s.SmtpFrom = v
  24. if err := s.CheckValid(); err == nil {
  25. t.Errorf("CheckValid with smtpFrom=%q: want error, got nil", v)
  26. }
  27. }
  28. }
  29. func TestCheckValidWildcardListenPortConflict(t *testing.T) {
  30. s := &AllSetting{WebPort: 2053, SubPort: 2053, WebListen: "0.0.0.0", SubListen: ""}
  31. if err := s.CheckValid(); err == nil {
  32. t.Error("CheckValid must reject the same port bound on 0.0.0.0 and \"\" (both wildcard)")
  33. }
  34. ok := &AllSetting{WebPort: 2053, SubPort: 2053, WebListen: "127.0.0.1", SubListen: "192.168.1.1"}
  35. if err := ok.CheckValid(); err != nil {
  36. t.Errorf("distinct specific listens on the same port should be allowed: %v", err)
  37. }
  38. }
  39. // The allowlist and the trusted-proxy list share one validator, so this also
  40. // pins that each list still reports its own message (#5378).
  41. func TestCheckValidIPOrCIDRLists(t *testing.T) {
  42. base := func() *AllSetting {
  43. return &AllSetting{WebPort: 2053, SubPort: 2096}
  44. }
  45. for _, v := range []string{"", "203.0.113.10", "198.51.100.0/24", " 203.0.113.10 , 2001:db8::/32 ", "203.0.113.10,,"} {
  46. s := base()
  47. s.IpLimitAllowlist = v
  48. if err := s.CheckValid(); err != nil {
  49. t.Errorf("ipLimitAllowlist=%q: unexpected error %v", v, err)
  50. }
  51. }
  52. for _, v := range []string{"nonsense", "203.0.113.10/33", "203.0.113.10, oops"} {
  53. s := base()
  54. s.IpLimitAllowlist = v
  55. err := s.CheckValid()
  56. if err == nil {
  57. t.Errorf("ipLimitAllowlist=%q: want error, got nil", v)
  58. continue
  59. }
  60. if !strings.Contains(err.Error(), "IP limit allowlist entry is not valid:") {
  61. t.Errorf("ipLimitAllowlist=%q: error %q does not name the setting", v, err)
  62. }
  63. }
  64. s := base()
  65. s.TrustedProxyCIDRs = "127.0.0.1/32, bogus"
  66. err := s.CheckValid()
  67. if err == nil || !strings.Contains(err.Error(), "trusted proxy CIDR is not valid: bogus") {
  68. t.Errorf("trustedProxyCIDRs error = %v, want it to name the trusted-proxy list and the bad entry", err)
  69. }
  70. }
  71. // A schedule the scheduler cannot parse used to be saved anyway, and the report
  72. // or sync job then silently never ran.
  73. func TestCheckValidSchedules(t *testing.T) {
  74. set := map[string]func(*AllSetting, string){
  75. "tgRunTime": func(s *AllSetting, v string) { s.TgRunTime = v },
  76. "discordRunTime": func(s *AllSetting, v string) { s.DiscordRunTime = v },
  77. "ldapSyncCron": func(s *AllSetting, v string) { s.LdapSyncCron = v },
  78. }
  79. for name, apply := range set {
  80. for _, v := range []string{"", "@daily", "30 8 * * *", "0 30 8 * * *", "@every 1m"} {
  81. s := &AllSetting{WebPort: 2053, SubPort: 2096}
  82. apply(s, v)
  83. if err := s.CheckValid(); err != nil {
  84. t.Errorf("%s=%q: unexpected error %v", name, v, err)
  85. }
  86. }
  87. s := &AllSetting{WebPort: 2053, SubPort: 2096}
  88. apply(s, "every morning")
  89. err := s.CheckValid()
  90. if err == nil || !strings.Contains(err.Error(), name) {
  91. t.Errorf("%s=%q: want an error naming %s, got %v", name, "every morning", name, err)
  92. }
  93. }
  94. }