server_test.go 2.5 KB

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