server_test.go 3.1 KB

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