guerrilla_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. // integration / smokeless
  2. // =======================
  3. // Tests are in a different package so we can test as a consumer of the guerrilla package
  4. // The following are integration / smokeless, that test the overall server.
  5. // (Please put unit tests to go in a different file)
  6. // How it works:
  7. // Server's log output is redirected to the logBuffer which is then used by the tests to look for expected behaviour
  8. // the package sets up the logBuffer & redirection by the use of package init()
  9. // (self signed certs are also generated on each run)
  10. // server's responses from a connection are also used to check for expected behaviour
  11. // to run:
  12. // $ go test
  13. package test
  14. import (
  15. "encoding/json"
  16. "testing"
  17. log "github.com/Sirupsen/logrus"
  18. "time"
  19. "github.com/flashmob/go-guerrilla"
  20. "github.com/flashmob/go-guerrilla/backends"
  21. "bufio"
  22. "bytes"
  23. "crypto/tls"
  24. "errors"
  25. "fmt"
  26. "io/ioutil"
  27. "net"
  28. "strings"
  29. "github.com/flashmob/go-guerrilla/tests/testcert"
  30. )
  31. type TestConfig struct {
  32. guerrilla.AppConfig
  33. BackendName string `json:"backend_name"`
  34. BackendConfig map[string]interface{} `json:"backend_config"`
  35. }
  36. var (
  37. // hold the output of logs
  38. logBuffer bytes.Buffer
  39. // logs redirected to this writer
  40. logOut *bufio.Writer
  41. // read the logs
  42. logIn *bufio.Reader
  43. // app config loaded here
  44. config *TestConfig
  45. app guerrilla.Guerrilla
  46. initErr error
  47. )
  48. func init() {
  49. logOut = bufio.NewWriter(&logBuffer)
  50. logIn = bufio.NewReader(&logBuffer)
  51. log.SetLevel(log.DebugLevel)
  52. log.SetOutput(logOut)
  53. config = &TestConfig{}
  54. if err := json.Unmarshal([]byte(configJson), config); err != nil {
  55. initErr = errors.New("Could not unmartial config," + err.Error())
  56. } else {
  57. setupCerts(config)
  58. backend, _ := getBackend("dummy", config.BackendConfig)
  59. app, _ = guerrilla.New(&config.AppConfig, backend)
  60. }
  61. }
  62. // a configuration file with a dummy backend
  63. var configJson = `
  64. {
  65. "pid_file" : "/var/run/go-guerrilla.pid",
  66. "allowed_hosts": ["spam4.me","grr.la"],
  67. "backend_name" : "dummy",
  68. "backend_config" :
  69. {
  70. "log_received_mails" : true
  71. },
  72. "servers" : [
  73. {
  74. "is_enabled" : true,
  75. "host_name":"mail.guerrillamail.com",
  76. "max_size": 100017,
  77. "private_key_file":"/vagrant/projects/htdocs/guerrilla/config/ssl/guerrillamail.com.key",
  78. "public_key_file":"/vagrant/projects/htdocs/guerrilla/config/ssl/guerrillamail.com.crt",
  79. "timeout":160,
  80. "listen_interface":"127.0.0.1:2526",
  81. "start_tls_on":true,
  82. "tls_always_on":false,
  83. "max_clients": 2,
  84. "log_file":"/dev/stdout"
  85. },
  86. {
  87. "is_enabled" : true,
  88. "host_name":"mail.guerrillamail.com",
  89. "max_size":1000001,
  90. "private_key_file":"/vagrant/projects/htdocs/guerrilla/config/ssl/guerrillamail.com.key",
  91. "public_key_file":"/vagrant/projects/htdocs/guerrilla/config/ssl/guerrillamail.com.crt",
  92. "timeout":180,
  93. "listen_interface":"127.0.0.1:4654",
  94. "start_tls_on":false,
  95. "tls_always_on":true,
  96. "max_clients":1,
  97. "log_file":"/dev/stdout"
  98. }
  99. ]
  100. }
  101. `
  102. func getBackend(backendName string, backendConfig map[string]interface{}) (backends.Backend, error) {
  103. return backends.New(backendName, backendConfig)
  104. }
  105. func setupCerts(c *TestConfig) {
  106. for i := range c.Servers {
  107. testcert.GenerateCert(c.Servers[i].Hostname, "", 365*24*time.Hour, false, 2048, "P256", "./")
  108. c.Servers[i].PrivateKeyFile = c.Servers[i].Hostname + ".key.pem"
  109. c.Servers[i].PublicKeyFile = c.Servers[i].Hostname + ".cert.pem"
  110. }
  111. }
  112. // Testing start and stop of server
  113. func TestStart(t *testing.T) {
  114. if initErr != nil {
  115. t.Error(initErr)
  116. t.FailNow()
  117. }
  118. if startErrors := app.Start(); startErrors != nil {
  119. t.Error(startErrors)
  120. t.FailNow()
  121. }
  122. time.Sleep(time.Second)
  123. app.Shutdown()
  124. logOut.Flush()
  125. if read, err := ioutil.ReadAll(logIn); err == nil {
  126. logOutput := string(read)
  127. if i := strings.Index(logOutput, "Listening on TCP 127.0.0.1:4654"); i < 0 {
  128. t.Error("Server did not listen on 127.0.0.1:4654")
  129. }
  130. if i := strings.Index(logOutput, "Listening on TCP 127.0.0.1:2526"); i < 0 {
  131. t.Error("Server did not listen on 127.0.0.1:2526")
  132. }
  133. if i := strings.Index(logOutput, "[127.0.0.1:4654] Waiting for a new client"); i < 0 {
  134. t.Error("Server did not wait on 127.0.0.1:4654")
  135. }
  136. if i := strings.Index(logOutput, "[127.0.0.1:2526] Waiting for a new client"); i < 0 {
  137. t.Error("Server did not wait on 127.0.0.1:2526")
  138. }
  139. if i := strings.Index(logOutput, "Server [127.0.0.1:4654] has stopped accepting new clients"); i < 0 {
  140. t.Error("Server did not stop on 127.0.0.1:4654")
  141. }
  142. if i := strings.Index(logOutput, "Server [127.0.0.1:2526] has stopped accepting new clients"); i < 0 {
  143. t.Error("Server did not stop on 127.0.0.1:2526")
  144. }
  145. if i := strings.Index(logOutput, "shutdown completed for [127.0.0.1:4654]"); i < 0 {
  146. t.Error("Server did not complete shutdown on 127.0.0.1:4654")
  147. }
  148. if i := strings.Index(logOutput, "shutdown completed for [127.0.0.1:2526]"); i < 0 {
  149. t.Error("Server did not complete shutdown on 127.0.0.1:2526")
  150. }
  151. if i := strings.Index(logOutput, "shutting down pool [127.0.0.1:4654]"); i < 0 {
  152. t.Error("Server did not shutdown pool on 127.0.0.1:4654")
  153. }
  154. if i := strings.Index(logOutput, "shutting down pool [127.0.0.1:2526]"); i < 0 {
  155. t.Error("Server did not shutdown pool on 127.0.0.1:2526")
  156. }
  157. if i := strings.Index(logOutput, "Backend shutdown completed"); i < 0 {
  158. t.Error("Backend didn't shut down")
  159. }
  160. }
  161. // don't forget to reset
  162. logBuffer.Reset()
  163. logIn.Reset(&logBuffer)
  164. }
  165. // Simple smoke-test to see if the server can listen & issues a greeting on connect
  166. func TestGreeting(t *testing.T) {
  167. //log.SetOutput(os.Stdout)
  168. if initErr != nil {
  169. t.Error(initErr)
  170. t.FailNow()
  171. }
  172. if startErrors := app.Start(); startErrors == nil {
  173. // 1. plaintext connection
  174. conn, err := net.Dial("tcp", config.Servers[0].ListenInterface)
  175. if err != nil {
  176. // handle error
  177. t.Error("Cannot dial server", config.Servers[0].ListenInterface)
  178. }
  179. conn.SetReadDeadline(time.Now().Add(time.Duration(time.Millisecond * 500)))
  180. greeting, err := bufio.NewReader(conn).ReadString('\n')
  181. //fmt.Println(greeting)
  182. if err != nil {
  183. t.Error(err)
  184. t.FailNow()
  185. } else {
  186. expected := "220 mail.guerrillamail.com SMTP Guerrilla"
  187. if strings.Index(greeting, expected) != 0 {
  188. t.Error("Server[1] did not have the expected greeting prefix", expected)
  189. }
  190. }
  191. conn.Close()
  192. // 2. tls connection
  193. // roots, err := x509.SystemCertPool()
  194. conn, err = tls.Dial("tcp", config.Servers[1].ListenInterface, &tls.Config{
  195. InsecureSkipVerify: true,
  196. ServerName: "127.0.0.1",
  197. })
  198. if err != nil {
  199. // handle error
  200. t.Error(err, "Cannot dial server (TLS)", config.Servers[1].ListenInterface)
  201. t.FailNow()
  202. }
  203. conn.SetReadDeadline(time.Now().Add(time.Duration(time.Millisecond * 500)))
  204. greeting, err = bufio.NewReader(conn).ReadString('\n')
  205. //fmt.Println(greeting)
  206. if err != nil {
  207. t.Error(err)
  208. t.FailNow()
  209. } else {
  210. expected := "220 mail.guerrillamail.com SMTP Guerrilla"
  211. if strings.Index(greeting, expected) != 0 {
  212. t.Error("Server[2] (TLS) did not have the expected greeting prefix", expected)
  213. }
  214. }
  215. conn.Close()
  216. } else {
  217. if startErrors := app.Start(); startErrors != nil {
  218. t.Error(startErrors)
  219. t.FailNow()
  220. }
  221. }
  222. app.Shutdown()
  223. logOut.Flush()
  224. if read, err := ioutil.ReadAll(logIn); err == nil {
  225. logOutput := string(read)
  226. //fmt.Println(logOutput)
  227. if i := strings.Index(logOutput, "Handle client [127.0.0.1:"); i < 0 {
  228. t.Error("Server did not handle any clients")
  229. }
  230. }
  231. // don't forget to reset
  232. logBuffer.Reset()
  233. logIn.Reset(&logBuffer)
  234. }
  235. // start up a server, connect a client, greet, then shutdown, then client sends a command
  236. // expecting: 421 Server is shutting down. Please try again later. Sayonara!
  237. // server should close connection after that
  238. func TestShutDown(t *testing.T) {
  239. if initErr != nil {
  240. t.Error(initErr)
  241. t.FailNow()
  242. }
  243. if startErrors := app.Start(); startErrors == nil {
  244. conn, bufin, err := Connect(config.Servers[0], 20)
  245. if err != nil {
  246. // handle error
  247. t.Error(err.Error(), config.Servers[0].ListenInterface)
  248. t.FailNow()
  249. } else {
  250. // client goes into command state
  251. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  252. t.Error("Hello command failed", err.Error())
  253. }
  254. // do a shutdown while the client is connected & in client state
  255. go app.Shutdown()
  256. time.Sleep(time.Millisecond * 150) // let server to Shutdown
  257. // issue a command while shutting down
  258. response, err := Command(conn, bufin, "HELP")
  259. if err != nil {
  260. t.Error("Help command failed", err.Error())
  261. }
  262. expected := "421 4.3.0 Server is shutting down. Please try again later. Sayonara!"
  263. if strings.Index(response, expected) != 0 {
  264. t.Error("Server did not shut down with", expected, ", it said:"+response)
  265. }
  266. time.Sleep(time.Millisecond * 250) // let server to close
  267. }
  268. conn.Close()
  269. } else {
  270. if startErrors := app.Start(); startErrors != nil {
  271. t.Error(startErrors)
  272. app.Shutdown()
  273. t.FailNow()
  274. }
  275. }
  276. // assuming server has shutdown by now
  277. logOut.Flush()
  278. if read, err := ioutil.ReadAll(logIn); err == nil {
  279. logOutput := string(read)
  280. // fmt.Println(logOutput)
  281. if i := strings.Index(logOutput, "Handle client [127.0.0.1:"); i < 0 {
  282. t.Error("Server did not handle any clients")
  283. }
  284. }
  285. // don't forget to reset
  286. logBuffer.Reset()
  287. logIn.Reset(&logBuffer)
  288. }
  289. // add more than 100 recipients, it should fail at 101
  290. func TestRFC2821LimitRecipients(t *testing.T) {
  291. if initErr != nil {
  292. t.Error(initErr)
  293. t.FailNow()
  294. }
  295. if startErrors := app.Start(); startErrors == nil {
  296. conn, bufin, err := Connect(config.Servers[0], 20)
  297. if err != nil {
  298. // handle error
  299. t.Error(err.Error(), config.Servers[0].ListenInterface)
  300. t.FailNow()
  301. } else {
  302. // client goes into command state
  303. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  304. t.Error("Hello command failed", err.Error())
  305. }
  306. for i := 0; i < 101; i++ {
  307. if _, err := Command(conn, bufin, fmt.Sprintf("RCPT TO:test%[email protected]", i)); err != nil {
  308. t.Error("RCPT TO", err.Error())
  309. break
  310. }
  311. }
  312. response, err := Command(conn, bufin, "RCPT TO:[email protected]")
  313. if err != nil {
  314. t.Error("rcpt command failed", err.Error())
  315. }
  316. expected := "452 4.5.3 Too many recipients"
  317. if strings.Index(response, expected) != 0 {
  318. t.Error("Server did not respond with", expected, ", it said:"+response)
  319. }
  320. }
  321. conn.Close()
  322. app.Shutdown()
  323. } else {
  324. if startErrors := app.Start(); startErrors != nil {
  325. t.Error(startErrors)
  326. app.Shutdown()
  327. t.FailNow()
  328. }
  329. }
  330. logOut.Flush()
  331. // don't forget to reset
  332. logBuffer.Reset()
  333. logIn.Reset(&logBuffer)
  334. }
  335. // RCPT TO & MAIL FROM with 64 chars in local part, it should fail at 65
  336. func TestRFC2832LimitLocalPart(t *testing.T) {
  337. if initErr != nil {
  338. t.Error(initErr)
  339. t.FailNow()
  340. }
  341. if startErrors := app.Start(); startErrors == nil {
  342. conn, bufin, err := Connect(config.Servers[0], 20)
  343. if err != nil {
  344. // handle error
  345. t.Error(err.Error(), config.Servers[0].ListenInterface)
  346. t.FailNow()
  347. } else {
  348. // client goes into command state
  349. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  350. t.Error("Hello command failed", err.Error())
  351. }
  352. // repeat > 64 characters in local part
  353. response, err := Command(conn, bufin, fmt.Sprintf("RCPT TO:%[email protected]", strings.Repeat("a", 65)))
  354. if err != nil {
  355. t.Error("rcpt command failed", err.Error())
  356. }
  357. expected := "550 5.5.4 Local part too long"
  358. if strings.Index(response, expected) != 0 {
  359. t.Error("Server did not respond with", expected, ", it said:"+response)
  360. }
  361. // what about if it's exactly 64?
  362. // repeat > 64 characters in local part
  363. response, err = Command(conn, bufin, fmt.Sprintf("RCPT TO:%[email protected]", strings.Repeat("a", 64)))
  364. if err != nil {
  365. t.Error("rcpt command failed", err.Error())
  366. }
  367. expected = "250 2.1.5 OK"
  368. if strings.Index(response, expected) != 0 {
  369. t.Error("Server did not respond with", expected, ", it said:"+response)
  370. }
  371. }
  372. conn.Close()
  373. app.Shutdown()
  374. } else {
  375. if startErrors := app.Start(); startErrors != nil {
  376. t.Error(startErrors)
  377. app.Shutdown()
  378. t.FailNow()
  379. }
  380. }
  381. logOut.Flush()
  382. // don't forget to reset
  383. logBuffer.Reset()
  384. logIn.Reset(&logBuffer)
  385. }
  386. //RFC2821LimitPath fail if path > 256 but different error if below
  387. func TestRFC2821LimitPath(t *testing.T) {
  388. if initErr != nil {
  389. t.Error(initErr)
  390. t.FailNow()
  391. }
  392. if startErrors := app.Start(); startErrors == nil {
  393. conn, bufin, err := Connect(config.Servers[0], 20)
  394. if err != nil {
  395. // handle error
  396. t.Error(err.Error(), config.Servers[0].ListenInterface)
  397. t.FailNow()
  398. } else {
  399. // client goes into command state
  400. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  401. t.Error("Hello command failed", err.Error())
  402. }
  403. // repeat > 256 characters in local part
  404. response, err := Command(conn, bufin, fmt.Sprintf("RCPT TO:%[email protected]", strings.Repeat("a", 257-7)))
  405. if err != nil {
  406. t.Error("rcpt command failed", err.Error())
  407. }
  408. expected := "550 5.5.4 Path too long"
  409. if strings.Index(response, expected) != 0 {
  410. t.Error("Server did not respond with", expected, ", it said:"+response)
  411. }
  412. // what about if it's exactly 256?
  413. response, err = Command(conn, bufin,
  414. fmt.Sprintf("RCPT TO:%s@%s.la", strings.Repeat("a", 64), strings.Repeat("b", 257-5-64)))
  415. if err != nil {
  416. t.Error("rcpt command failed", err.Error())
  417. }
  418. expected = "454 4.1.1 Error: Relay access denied"
  419. if strings.Index(response, expected) != 0 {
  420. t.Error("Server did not respond with", expected, ", it said:"+response)
  421. }
  422. }
  423. conn.Close()
  424. app.Shutdown()
  425. } else {
  426. if startErrors := app.Start(); startErrors != nil {
  427. t.Error(startErrors)
  428. app.Shutdown()
  429. t.FailNow()
  430. }
  431. }
  432. logOut.Flush()
  433. // don't forget to reset
  434. logBuffer.Reset()
  435. logIn.Reset(&logBuffer)
  436. }
  437. // RFC2821LimitDomain 501 Domain cannot exceed 255 characters
  438. func TestRFC2821LimitDomain(t *testing.T) {
  439. if initErr != nil {
  440. t.Error(initErr)
  441. t.FailNow()
  442. }
  443. if startErrors := app.Start(); startErrors == nil {
  444. conn, bufin, err := Connect(config.Servers[0], 20)
  445. if err != nil {
  446. // handle error
  447. t.Error(err.Error(), config.Servers[0].ListenInterface)
  448. t.FailNow()
  449. } else {
  450. // client goes into command state
  451. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  452. t.Error("Hello command failed", err.Error())
  453. }
  454. // repeat > 64 characters in local part
  455. response, err := Command(conn, bufin, fmt.Sprintf("RCPT TO:a@%s.l", strings.Repeat("a", 255-2)))
  456. if err != nil {
  457. t.Error("command failed", err.Error())
  458. }
  459. expected := "550 5.5.4 Path too long"
  460. if strings.Index(response, expected) != 0 {
  461. t.Error("Server did not respond with", expected, ", it said:"+response)
  462. }
  463. // what about if it's exactly 255?
  464. response, err = Command(conn, bufin,
  465. fmt.Sprintf("RCPT TO:a@%s.la", strings.Repeat("b", 255-4)))
  466. if err != nil {
  467. t.Error("command failed", err.Error())
  468. }
  469. expected = "454 4.1.1 Error: Relay access denied"
  470. if strings.Index(response, expected) != 0 {
  471. t.Error("Server did not respond with", expected, ", it said:"+response)
  472. }
  473. }
  474. conn.Close()
  475. app.Shutdown()
  476. } else {
  477. if startErrors := app.Start(); startErrors != nil {
  478. t.Error(startErrors)
  479. app.Shutdown()
  480. t.FailNow()
  481. }
  482. }
  483. logOut.Flush()
  484. // don't forget to reset
  485. logBuffer.Reset()
  486. logIn.Reset(&logBuffer)
  487. }
  488. // Test several different inputs to MAIL FROM command
  489. func TestMailFromCmd(t *testing.T) {
  490. if initErr != nil {
  491. t.Error(initErr)
  492. t.FailNow()
  493. }
  494. if startErrors := app.Start(); startErrors == nil {
  495. conn, bufin, err := Connect(config.Servers[0], 20)
  496. if err != nil {
  497. // handle error
  498. t.Error(err.Error(), config.Servers[0].ListenInterface)
  499. t.FailNow()
  500. } else {
  501. // client goes into command state
  502. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  503. t.Error("Hello command failed", err.Error())
  504. }
  505. // Basic valid address
  506. response, err := Command(conn, bufin, "MAIL FROM:[email protected]")
  507. if err != nil {
  508. t.Error("command failed", err.Error())
  509. }
  510. expected := "250 2.1.0 OK"
  511. if strings.Index(response, expected) != 0 {
  512. t.Error("Server did not respond with", expected, ", it said:"+response)
  513. }
  514. // Reset
  515. response, err = Command(conn, bufin, "RSET")
  516. if err != nil {
  517. t.Error("command failed", err.Error())
  518. }
  519. expected = "250 2.1.0 OK"
  520. if strings.Index(response, expected) != 0 {
  521. t.Error("Server did not respond with", expected, ", it said:"+response)
  522. }
  523. // Basic valid address (RfC)
  524. response, err = Command(conn, bufin, "MAIL FROM:<[email protected]>")
  525. if err != nil {
  526. t.Error("command failed", err.Error())
  527. }
  528. expected = "250 2.1.0 OK"
  529. if strings.Index(response, expected) != 0 {
  530. t.Error("Server did not respond with", expected, ", it said:"+response)
  531. }
  532. // Reset
  533. response, err = Command(conn, bufin, "RSET")
  534. if err != nil {
  535. t.Error("command failed", err.Error())
  536. }
  537. expected = "250 2.1.0 OK"
  538. if strings.Index(response, expected) != 0 {
  539. t.Error("Server did not respond with", expected, ", it said:"+response)
  540. }
  541. // Bounce
  542. response, err = Command(conn, bufin, "MAIL FROM:<>")
  543. if err != nil {
  544. t.Error("command failed", err.Error())
  545. }
  546. expected = "250 2.1.0 OK"
  547. if strings.Index(response, expected) != 0 {
  548. t.Error("Server did not respond with", expected, ", it said:"+response)
  549. }
  550. // Reset
  551. response, err = Command(conn, bufin, "RSET")
  552. if err != nil {
  553. t.Error("command failed", err.Error())
  554. }
  555. expected = "250 2.1.0 OK"
  556. if strings.Index(response, expected) != 0 {
  557. t.Error("Server did not respond with", expected, ", it said:"+response)
  558. }
  559. // What?
  560. response, err = Command(conn, bufin, "MAIL FROM:<<>>")
  561. if err != nil {
  562. t.Error("command failed", err.Error())
  563. }
  564. expected = "501 5.5.4 Invalid address"
  565. if strings.Index(response, expected) != 0 {
  566. t.Error("Server did not respond with", expected, ", it said:"+response)
  567. }
  568. // Reset
  569. response, err = Command(conn, bufin, "RSET")
  570. if err != nil {
  571. t.Error("command failed", err.Error())
  572. }
  573. expected = "250 2.1.0 OK"
  574. if strings.Index(response, expected) != 0 {
  575. t.Error("Server did not respond with", expected, ", it said:"+response)
  576. }
  577. // Invalid address?
  578. response, err = Command(conn, bufin, "MAIL FROM:<justatest>")
  579. if err != nil {
  580. t.Error("command failed", err.Error())
  581. }
  582. expected = "501 5.5.4 Invalid address"
  583. if strings.Index(response, expected) != 0 {
  584. t.Error("Server did not respond with", expected, ", it said:"+response)
  585. }
  586. // Reset
  587. response, err = Command(conn, bufin, "RSET")
  588. if err != nil {
  589. t.Error("command failed", err.Error())
  590. }
  591. expected = "250 2.1.0 OK"
  592. if strings.Index(response, expected) != 0 {
  593. t.Error("Server did not respond with", expected, ", it said:"+response)
  594. }
  595. // SMTPUTF8 not implemented for now, currently still accepted
  596. response, err = Command(conn, bufin, "MAIL FROM:<anö[email protected]>")
  597. if err != nil {
  598. t.Error("command failed", err.Error())
  599. }
  600. expected = "250 2.1.0 OK"
  601. if strings.Index(response, expected) != 0 {
  602. t.Error("Server did not respond with", expected, ", it said:"+response)
  603. }
  604. // Reset
  605. response, err = Command(conn, bufin, "RSET")
  606. if err != nil {
  607. t.Error("command failed", err.Error())
  608. }
  609. expected = "250 2.1.0 OK"
  610. if strings.Index(response, expected) != 0 {
  611. t.Error("Server did not respond with", expected, ", it said:"+response)
  612. }
  613. // 8BITMIME (RfC 6152)
  614. response, err = Command(conn, bufin, "MAIL FROM:<[email protected]> BODY=8BITMIME")
  615. if err != nil {
  616. t.Error("command failed", err.Error())
  617. }
  618. expected = "250 2.1.0 OK"
  619. if strings.Index(response, expected) != 0 {
  620. t.Error("Server did not respond with", expected, ", it said:"+response)
  621. }
  622. // Reset
  623. response, err = Command(conn, bufin, "RSET")
  624. if err != nil {
  625. t.Error("command failed", err.Error())
  626. }
  627. expected = "250 2.1.0 OK"
  628. if strings.Index(response, expected) != 0 {
  629. t.Error("Server did not respond with", expected, ", it said:"+response)
  630. }
  631. // 8BITMIME (RfC 6152) Bounce
  632. response, err = Command(conn, bufin, "MAIL FROM:<> BODY=8BITMIME")
  633. if err != nil {
  634. t.Error("command failed", err.Error())
  635. }
  636. expected = "250 2.1.0 OK"
  637. if strings.Index(response, expected) != 0 {
  638. t.Error("Server did not respond with", expected, ", it said:"+response)
  639. }
  640. // Reset
  641. response, err = Command(conn, bufin, "RSET")
  642. if err != nil {
  643. t.Error("command failed", err.Error())
  644. }
  645. expected = "250 2.1.0 OK"
  646. if strings.Index(response, expected) != 0 {
  647. t.Error("Server did not respond with", expected, ", it said:"+response)
  648. }
  649. }
  650. conn.Close()
  651. app.Shutdown()
  652. } else {
  653. if startErrors := app.Start(); startErrors != nil {
  654. t.Error(startErrors)
  655. app.Shutdown()
  656. t.FailNow()
  657. }
  658. }
  659. logOut.Flush()
  660. // don't forget to reset
  661. logBuffer.Reset()
  662. logIn.Reset(&logBuffer)
  663. }
  664. // It should error when MAIL FROM was given twice
  665. func TestNestedMailCmd(t *testing.T) {
  666. if initErr != nil {
  667. t.Error(initErr)
  668. t.FailNow()
  669. }
  670. if startErrors := app.Start(); startErrors == nil {
  671. conn, bufin, err := Connect(config.Servers[0], 20)
  672. if err != nil {
  673. // handle error
  674. t.Error(err.Error(), config.Servers[0].ListenInterface)
  675. t.FailNow()
  676. } else {
  677. // client goes into command state
  678. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  679. t.Error("Hello command failed", err.Error())
  680. }
  681. // repeat > 64 characters in local part
  682. response, err := Command(conn, bufin, "MAIL FROM:[email protected]")
  683. if err != nil {
  684. t.Error("command failed", err.Error())
  685. }
  686. response, err = Command(conn, bufin, "MAIL FROM:[email protected]")
  687. if err != nil {
  688. t.Error("command failed", err.Error())
  689. }
  690. expected := "503 5.5.1 Error: nested MAIL command"
  691. if strings.Index(response, expected) != 0 {
  692. t.Error("Server did not respond with", expected, ", it said:"+response)
  693. }
  694. // Plot twist: if you EHLO , it should allow MAIL FROM again
  695. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  696. t.Error("Hello command failed", err.Error())
  697. }
  698. response, err = Command(conn, bufin, "MAIL FROM:[email protected]")
  699. if err != nil {
  700. t.Error("command failed", err.Error())
  701. }
  702. expected = "250 2.1.0 OK"
  703. if strings.Index(response, expected) != 0 {
  704. t.Error("Server did not respond with", expected, ", it said:"+response)
  705. }
  706. // Plot twist: if you RSET , it should allow MAIL FROM again
  707. response, err = Command(conn, bufin, "RSET")
  708. if err != nil {
  709. t.Error("command failed", err.Error())
  710. }
  711. expected = "250 2.1.0 OK"
  712. if strings.Index(response, expected) != 0 {
  713. t.Error("Server did not respond with", expected, ", it said:"+response)
  714. }
  715. response, err = Command(conn, bufin, "MAIL FROM:[email protected]")
  716. if err != nil {
  717. t.Error("command failed", err.Error())
  718. }
  719. expected = "250 2.1.0 OK"
  720. if strings.Index(response, expected) != 0 {
  721. t.Error("Server did not respond with", expected, ", it said:"+response)
  722. }
  723. }
  724. conn.Close()
  725. app.Shutdown()
  726. } else {
  727. if startErrors := app.Start(); startErrors != nil {
  728. t.Error(startErrors)
  729. app.Shutdown()
  730. t.FailNow()
  731. }
  732. }
  733. logOut.Flush()
  734. // don't forget to reset
  735. logBuffer.Reset()
  736. logIn.Reset(&logBuffer)
  737. }
  738. // It should error on a very long command line, exceeding CommandLineMaxLength 1024
  739. func TestCommandLineMaxLength(t *testing.T) {
  740. if initErr != nil {
  741. t.Error(initErr)
  742. t.FailNow()
  743. }
  744. if startErrors := app.Start(); startErrors == nil {
  745. conn, bufin, err := Connect(config.Servers[0], 20)
  746. if err != nil {
  747. // handle error
  748. t.Error(err.Error(), config.Servers[0].ListenInterface)
  749. t.FailNow()
  750. } else {
  751. // client goes into command state
  752. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  753. t.Error("Hello command failed", err.Error())
  754. }
  755. // repeat > 1024 characters
  756. response, err := Command(conn, bufin, strings.Repeat("s", guerrilla.CommandLineMaxLength+1))
  757. if err != nil {
  758. t.Error("command failed", err.Error())
  759. }
  760. expected := "554 5.5.1 Line too long"
  761. if strings.Index(response, expected) != 0 {
  762. t.Error("Server did not respond with", expected, ", it said:"+response)
  763. }
  764. }
  765. conn.Close()
  766. app.Shutdown()
  767. } else {
  768. if startErrors := app.Start(); startErrors != nil {
  769. t.Error(startErrors)
  770. app.Shutdown()
  771. t.FailNow()
  772. }
  773. }
  774. logOut.Flush()
  775. // don't forget to reset
  776. logBuffer.Reset()
  777. logIn.Reset(&logBuffer)
  778. }
  779. // It should error on a very long message, exceeding servers config value
  780. func TestDataMaxLength(t *testing.T) {
  781. if initErr != nil {
  782. t.Error(initErr)
  783. t.FailNow()
  784. }
  785. if startErrors := app.Start(); startErrors == nil {
  786. conn, bufin, err := Connect(config.Servers[0], 20)
  787. if err != nil {
  788. // handle error
  789. t.Error(err.Error(), config.Servers[0].ListenInterface)
  790. t.FailNow()
  791. } else {
  792. // client goes into command state
  793. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  794. t.Error("Hello command failed", err.Error())
  795. }
  796. response, err := Command(conn, bufin, "MAIL FROM:[email protected]")
  797. if err != nil {
  798. t.Error("command failed", err.Error())
  799. }
  800. //fmt.Println(response)
  801. response, err = Command(conn, bufin, "RCPT TO:[email protected]")
  802. if err != nil {
  803. t.Error("command failed", err.Error())
  804. }
  805. //fmt.Println(response)
  806. response, err = Command(conn, bufin, "DATA")
  807. if err != nil {
  808. t.Error("command failed", err.Error())
  809. }
  810. response, err = Command(
  811. conn,
  812. bufin,
  813. fmt.Sprintf("Subject:test\r\n\r\nHello %s\r\n.\r\n",
  814. strings.Repeat("n", int(config.Servers[0].MaxSize-20))))
  815. //expected := "500 Line too long"
  816. expected := "451 4.3.0 Error: Maximum DATA size exceeded"
  817. if strings.Index(response, expected) != 0 {
  818. t.Error("Server did not respond with", expected, ", it said:"+response, err)
  819. }
  820. }
  821. conn.Close()
  822. app.Shutdown()
  823. } else {
  824. if startErrors := app.Start(); startErrors != nil {
  825. t.Error(startErrors)
  826. app.Shutdown()
  827. t.FailNow()
  828. }
  829. }
  830. logOut.Flush()
  831. // don't forget to reset
  832. logBuffer.Reset()
  833. logIn.Reset(&logBuffer)
  834. }
  835. func TestDataCommand(t *testing.T) {
  836. if initErr != nil {
  837. t.Error(initErr)
  838. t.FailNow()
  839. }
  840. testHeader :=
  841. "Subject: =?Shift_JIS?B?W4NYg06DRYNGg0GBRYNHg2qDYoNOg1ggg0GDSoNFg5ODZ12DQYNKg0WDk4Nn?=\r\n" +
  842. "\t=?Shift_JIS?B?k2+YXoqul7mCzIKokm2C54K5?=\r\n"
  843. email :=
  844. "Delivered-To: [email protected]\r\n" +
  845. "\tReceived: from mail.guerrillamail.com (mail.guerrillamail.com [104.218.55.28:44246])\r\n" +
  846. "\tby grr.la with SMTP id [email protected];\r\n" +
  847. "\tWed, 18 Jan 2017 15:43:29 +0000\r\n" +
  848. "Received: by 192.99.19.220 with HTTP; Wed, 18 Jan 2017 15:43:29 +0000\r\n" +
  849. "MIME-Version: 1.0\r\n" +
  850. "Message-ID: <[email protected]>\r\n" +
  851. "Date: Wed, 18 Jan 2017 15:43:29 +0000\r\n" +
  852. "To: \"[email protected]\" <[email protected]>\r\n" +
  853. "From: <[email protected]>\r\n" +
  854. "Subject: test\r\n" +
  855. "X-Originating-IP: [60.241.160.150]\r\n" +
  856. "Content-Type: text/plain; charset=\"utf-8\"\r\n" +
  857. "Content-Transfer-Encoding: quoted-printable\r\n" +
  858. "X-Domain-Signer: PHP mailDomainSigner 0.2-20110415 <http://code.google.com/p/php-mail-domain-signer/>\r\n" +
  859. "DKIM-Signature: v=1; a=rsa-sha256; s=highgrade; d=guerrillamail.com; l=182;\r\n" +
  860. "\tt=1484754209; c=relaxed/relaxed; h=to:from:subject;\r\n" +
  861. "\tbh=GHSgjHpBp5QjNn9tzfug681+RcWMOUgpwAuTzppM5wY=;\r\n" +
  862. "\tb=R7FxWgACnT+pKXqEg15qgzH4ywMFRx5pDlIFCnSt1BfwmLvZPZK7oOLrbiRoGGR2OJnSfyCxeASH\r\n" +
  863. "\t019LNeLB/B8o+fMRX87m/tBpqIZ2vgXdT9rUCIbSDJnYoCHXakGcF+zGtTE3SEksMbeJQ76aGj6M\r\n" +
  864. "\tG80p76IT2Xu3iDJLYYWxcAeX+7z4M/bbYNeqxMQcXYZp1wNYlSlHahL6RDUYdcqikDqKoXmzMNVd\r\n" +
  865. "\tDr0EbH9iiu1DQtfUDzVE5LLus1yn36WU/2KJvEak45gJvm9s9J+Xrcb882CaYkxlAbgQDz1KeQLf\r\n" +
  866. "\teUyNspyAabkh2yTg7kOvNZSOJtbMSQS6/GMxsg==\r\n" +
  867. "\r\n" +
  868. "test=0A.mooo=0A..mooo=0Atest=0A.=0A=0A=0A=0A=0A=0A----=0ASent using Guerril=\r\n" +
  869. "lamail.com=0ABlock or report abuse: https://www.guerrillamail.com//abuse/?a=\r\n" +
  870. "=3DVURnES0HUaZbhA8%3D=0A\r\n.\r\n"
  871. if startErrors := app.Start(); startErrors == nil {
  872. conn, bufin, err := Connect(config.Servers[0], 20)
  873. if err != nil {
  874. // handle error
  875. t.Error(err.Error(), config.Servers[0].ListenInterface)
  876. t.FailNow()
  877. } else {
  878. // client goes into command state
  879. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  880. t.Error("Hello command failed", err.Error())
  881. }
  882. response, err := Command(conn, bufin, "MAIL FROM:[email protected]")
  883. if err != nil {
  884. t.Error("command failed", err.Error())
  885. }
  886. //fmt.Println(response)
  887. response, err = Command(conn, bufin, "RCPT TO:[email protected]")
  888. if err != nil {
  889. t.Error("command failed", err.Error())
  890. }
  891. //fmt.Println(response)
  892. response, err = Command(conn, bufin, "DATA")
  893. if err != nil {
  894. t.Error("command failed", err.Error())
  895. }
  896. /*
  897. response, err = Command(
  898. conn,
  899. bufin,
  900. testHeader+"\r\nHello World\r\n.\r\n")
  901. */
  902. _ = testHeader
  903. response, err = Command(
  904. conn,
  905. bufin,
  906. email+"\r\n.\r\n")
  907. //expected := "500 Line too long"
  908. expected := "250 2.0.0 OK : queued as s0m3l337Ha5hva1u3LOL"
  909. if strings.Index(response, expected) != 0 {
  910. t.Error("Server did not respond with", expected, ", it said:"+response, err)
  911. }
  912. }
  913. conn.Close()
  914. app.Shutdown()
  915. } else {
  916. if startErrors := app.Start(); startErrors != nil {
  917. t.Error(startErrors)
  918. app.Shutdown()
  919. t.FailNow()
  920. }
  921. }
  922. logOut.Flush()
  923. // don't forget to reset
  924. logBuffer.Reset()
  925. logIn.Reset(&logBuffer)
  926. }