1
0

check_valid_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. }