check_valid_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package entity
  2. import "testing"
  3. func TestCheckValidSmtpFrom(t *testing.T) {
  4. base := func() *AllSetting {
  5. return &AllSetting{WebPort: 2053, SubPort: 2096}
  6. }
  7. for _, v := range []string{"", "[email protected]"} {
  8. s := base()
  9. s.SmtpFrom = v
  10. if err := s.CheckValid(); err != nil {
  11. t.Errorf("CheckValid with smtpFrom=%q: unexpected error %v", v, err)
  12. }
  13. }
  14. for _, v := range []string{
  15. "not-an-address",
  16. "[email protected]\r\nBcc: [email protected]",
  17. "a@b\nSubject: injected",
  18. } {
  19. s := base()
  20. s.SmtpFrom = v
  21. if err := s.CheckValid(); err == nil {
  22. t.Errorf("CheckValid with smtpFrom=%q: want error, got nil", v)
  23. }
  24. }
  25. }
  26. func TestCheckValidWildcardListenPortConflict(t *testing.T) {
  27. // Same port, both bind all interfaces but spelled differently -> conflict.
  28. s := &AllSetting{WebPort: 2053, SubPort: 2053, WebListen: "0.0.0.0", SubListen: ""}
  29. if err := s.CheckValid(); err == nil {
  30. t.Error("CheckValid must reject the same port bound on 0.0.0.0 and \"\" (both wildcard)")
  31. }
  32. // Same port on two distinct specific addresses can coexist and must be allowed.
  33. ok := &AllSetting{WebPort: 2053, SubPort: 2053, WebListen: "127.0.0.1", SubListen: "192.168.1.1"}
  34. if err := ok.CheckValid(); err != nil {
  35. t.Errorf("distinct specific listens on the same port should be allowed: %v", err)
  36. }
  37. }