server_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package guerrilla
  2. import (
  3. "testing"
  4. "bufio"
  5. "net/textproto"
  6. "strings"
  7. "sync"
  8. "github.com/flashmob/go-guerrilla/backends"
  9. "github.com/flashmob/go-guerrilla/log"
  10. "github.com/flashmob/go-guerrilla/mocks"
  11. )
  12. // getMockServerConfig gets a mock ServerConfig struct used for creating a new server
  13. func getMockServerConfig() *ServerConfig {
  14. sc := &ServerConfig{
  15. IsEnabled: true, // not tested here
  16. Hostname: "saggydimes.test.com",
  17. MaxSize: 1024, // smtp message max size
  18. PrivateKeyFile: "./tests/mail.guerrillamail.com.key.pem",
  19. PublicKeyFile: "./tests/mail.guerrillamail.com.cert.pem",
  20. Timeout: 5,
  21. ListenInterface: "127.0.0.1:2529",
  22. StartTLSOn: true,
  23. TLSAlwaysOn: false,
  24. MaxClients: 30, // not tested here
  25. LogFile: "./tests/testlog",
  26. }
  27. return sc
  28. }
  29. // getMockServerConn gets a new server using sc. Server will be using a mocked TCP connection
  30. // using the dummy backend
  31. // RCP TO command only allows test.com host
  32. func getMockServerConn(sc *ServerConfig, t *testing.T) (*mocks.Conn, *server) {
  33. var logOpenError error
  34. var mainlog log.Logger
  35. mainlog, logOpenError = log.GetLogger(sc.LogFile)
  36. if logOpenError != nil {
  37. mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
  38. }
  39. backend, err := backends.New("dummy", backends.BackendConfig{"log_received_mails": true}, mainlog)
  40. if err != nil {
  41. t.Error("new dummy backend failed because:", err)
  42. }
  43. server, err := newServer(sc, backend, mainlog)
  44. if err != nil {
  45. //t.Error("new server failed because:", err)
  46. } else {
  47. server.setAllowedHosts([]string{"test.com"})
  48. }
  49. conn := mocks.NewConn()
  50. return conn, server
  51. }
  52. func TestHandleClient(t *testing.T) {
  53. var mainlog log.Logger
  54. var logOpenError error
  55. sc := getMockServerConfig()
  56. mainlog, logOpenError = log.GetLogger(sc.LogFile)
  57. if logOpenError != nil {
  58. mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
  59. }
  60. conn, server := getMockServerConn(sc, t)
  61. // call the serve.handleClient() func in a goroutine.
  62. client := NewClient(conn.Server, 1, mainlog)
  63. var wg sync.WaitGroup
  64. wg.Add(1)
  65. go func() {
  66. server.handleClient(client)
  67. wg.Done()
  68. }()
  69. // Wait for the greeting from the server
  70. r := textproto.NewReader(bufio.NewReader(conn.Client))
  71. line, _ := r.ReadLine()
  72. // fmt.Println(line)
  73. w := textproto.NewWriter(bufio.NewWriter(conn.Client))
  74. w.PrintfLine("HELO test.test.com")
  75. line, _ = r.ReadLine()
  76. //fmt.Println(line)
  77. w.PrintfLine("QUIT")
  78. line, _ = r.ReadLine()
  79. //fmt.Println("line is:", line)
  80. expected := "221 2.0.0 Bye"
  81. if strings.Index(line, expected) != 0 {
  82. t.Error("expected", expected, "but got:", line)
  83. }
  84. wg.Wait() // wait for handleClient to exit
  85. }
  86. // TODO
  87. // - test github issue #44 and #42
  88. // - test other commands
  89. // also, could test
  90. // - test allowsHost() and allowsHost()
  91. // - test isInTransaction() (make sure it returns true after MAIL command, but false after HELO/EHLO/RSET/end of DATA
  92. // - test to make sure client envelope
  93. // - perhaps anything else that can be tested in server_test.go