server_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(
  40. "dummy",
  41. backends.BackendConfig{"log_received_mails": true, "save_workers_size": 1},
  42. mainlog)
  43. if err != nil {
  44. t.Error("new dummy backend failed because:", err)
  45. }
  46. server, err := newServer(sc, backend, mainlog)
  47. if err != nil {
  48. //t.Error("new server failed because:", err)
  49. } else {
  50. server.setAllowedHosts([]string{"test.com"})
  51. }
  52. conn := mocks.NewConn()
  53. return conn, server
  54. }
  55. func TestHandleClient(t *testing.T) {
  56. var mainlog log.Logger
  57. var logOpenError error
  58. sc := getMockServerConfig()
  59. mainlog, logOpenError = log.GetLogger(sc.LogFile)
  60. if logOpenError != nil {
  61. mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
  62. }
  63. conn, server := getMockServerConn(sc, t)
  64. // call the serve.handleClient() func in a goroutine.
  65. client := NewClient(conn.Server, 1, mainlog)
  66. var wg sync.WaitGroup
  67. wg.Add(1)
  68. go func() {
  69. server.handleClient(client)
  70. wg.Done()
  71. }()
  72. // Wait for the greeting from the server
  73. r := textproto.NewReader(bufio.NewReader(conn.Client))
  74. line, _ := r.ReadLine()
  75. // fmt.Println(line)
  76. w := textproto.NewWriter(bufio.NewWriter(conn.Client))
  77. w.PrintfLine("HELO test.test.com")
  78. line, _ = r.ReadLine()
  79. //fmt.Println(line)
  80. w.PrintfLine("QUIT")
  81. line, _ = r.ReadLine()
  82. //fmt.Println("line is:", line)
  83. expected := "221 2.0.0 Bye"
  84. if strings.Index(line, expected) != 0 {
  85. t.Error("expected", expected, "but got:", line)
  86. }
  87. wg.Wait() // wait for handleClient to exit
  88. }
  89. // TODO
  90. // - test github issue #44 and #42
  91. // - test other commands
  92. // also, could test
  93. // - test allowsHost() and allowsHost()
  94. // - test isInTransaction() (make sure it returns true after MAIL command, but false after HELO/EHLO/RSET/end of DATA
  95. // - test to make sure client envelope
  96. // - perhaps anything else that can be tested in server_test.go