guerrilla_test.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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 testlog file which is then used by the tests to look for
  8. // expected behaviour.
  9. //
  10. // (self signed certs are also generated on each run)
  11. // server's responses from a connection are also used to check for expected behaviour
  12. // to run:
  13. // $ go test
  14. package test
  15. import (
  16. "encoding/json"
  17. "github.com/flashmob/go-guerrilla/mail/smtp"
  18. "testing"
  19. "time"
  20. "github.com/flashmob/go-guerrilla"
  21. "github.com/flashmob/go-guerrilla/backends"
  22. "github.com/flashmob/go-guerrilla/log"
  23. "bufio"
  24. "crypto/tls"
  25. "errors"
  26. "fmt"
  27. "io/ioutil"
  28. "net"
  29. "strings"
  30. "os"
  31. "github.com/flashmob/go-guerrilla/tests/testcert"
  32. )
  33. type TestConfig struct {
  34. guerrilla.AppConfig
  35. BackendConfig backends.BackendConfig `json:"backend"`
  36. }
  37. var (
  38. // app config loaded here
  39. config *TestConfig
  40. app guerrilla.Guerrilla
  41. initErr error
  42. logger log.Logger
  43. )
  44. func init() {
  45. config = &TestConfig{}
  46. if err := json.Unmarshal([]byte(configJson), config); err != nil {
  47. initErr = errors.New("Could not Unmarshal config," + err.Error())
  48. } else {
  49. logger, _ = log.GetLogger(config.LogFile, "debug")
  50. initErr = setupCerts(config)
  51. if initErr != nil {
  52. return
  53. }
  54. backend, _ := getBackend(config.BackendConfig, logger)
  55. app, initErr = guerrilla.New(&config.AppConfig, logger, backend)
  56. }
  57. }
  58. // a configuration file with a dummy backend
  59. var configJson = `
  60. {
  61. "log_file" : "./testlog",
  62. "log_level" : "debug",
  63. "pid_file" : "go-guerrilla.pid",
  64. "allowed_hosts": ["spam4.me","grr.la"],
  65. "backend" : {
  66. "processors" : {
  67. "debugger" : {
  68. "log_received_mails" : true
  69. }
  70. }
  71. },
  72. "servers" : [
  73. {
  74. "is_enabled" : true,
  75. "host_name":"mail.guerrillamail.com",
  76. "max_size": 100017,
  77. "timeout":160,
  78. "listen_interface":"127.0.0.1:2526",
  79. "max_clients": 2,
  80. "log_file" : "",
  81. "tls" : {
  82. "private_key_file":"/this/will/be/ignored/guerrillamail.com.key.pem",
  83. "public_key_file":"/this/will/be/ignored//guerrillamail.com.crt",
  84. "start_tls_on":true,
  85. "tls_always_on":false
  86. }
  87. },
  88. {
  89. "is_enabled" : true,
  90. "host_name":"mail.guerrillamail.com",
  91. "max_size":1000001,
  92. "timeout":180,
  93. "listen_interface":"127.0.0.1:4654",
  94. "max_clients":1,
  95. "log_file" : "",
  96. "tls" : {
  97. "private_key_file":"/this/will/be/ignored/guerrillamail.com.key.pem",
  98. "public_key_file":"/this/will/be/ignored/guerrillamail.com.crt",
  99. "start_tls_on":false,
  100. "tls_always_on":true
  101. }
  102. }
  103. ]
  104. }
  105. `
  106. func getBackend(backendConfig backends.BackendConfig, l log.Logger) (backends.Backend, error) {
  107. _ = backendConfig.ConfigureDefaults()
  108. b, err := backends.New(backends.DefaultGateway, backendConfig, l)
  109. if err != nil {
  110. fmt.Println("backend init error", err)
  111. os.Exit(1)
  112. }
  113. return b, err
  114. }
  115. func setupCerts(c *TestConfig) error {
  116. for i := range c.Servers {
  117. err := testcert.GenerateCert(c.Servers[i].Hostname, "", 365*24*time.Hour, false, 2048, "P256", "./")
  118. if err != nil {
  119. return err
  120. }
  121. c.Servers[i].TLS.PrivateKeyFile = c.Servers[i].Hostname + ".key.pem"
  122. c.Servers[i].TLS.PublicKeyFile = c.Servers[i].Hostname + ".cert.pem"
  123. }
  124. return nil
  125. }
  126. func truncateIfExists(filename string) error {
  127. if _, err := os.Stat(filename); !os.IsNotExist(err) {
  128. return os.Truncate(filename, 0)
  129. }
  130. return nil
  131. }
  132. func deleteIfExists(filename string) error {
  133. if _, err := os.Stat(filename); !os.IsNotExist(err) {
  134. return os.Remove(filename)
  135. }
  136. return nil
  137. }
  138. func cleanTestArtifacts(t *testing.T) {
  139. if err := truncateIfExists("./testlog"); err != nil {
  140. t.Error("could not clean tests/testlog:", err)
  141. }
  142. letters := []byte{'A', 'B', 'C', 'D', 'E'}
  143. for _, l := range letters {
  144. if err := deleteIfExists("configJson" + string(l) + ".json"); err != nil {
  145. t.Error("could not delete configJson"+string(l)+".json:", err)
  146. }
  147. }
  148. if err := deleteIfExists("./go-guerrilla.pid"); err != nil {
  149. t.Error("could not delete ./guerrilla", err)
  150. }
  151. if err := deleteIfExists("./go-guerrilla2.pid"); err != nil {
  152. t.Error("could not delete ./go-guerrilla2.pid", err)
  153. }
  154. if err := deleteIfExists("./mail.guerrillamail.com.cert.pem"); err != nil {
  155. t.Error("could not delete ./mail.guerrillamail.com.cert.pem", err)
  156. }
  157. if err := deleteIfExists("./mail.guerrillamail.com.key.pem"); err != nil {
  158. t.Error("could not delete ./mail.guerrillamail.com.key.pem", err)
  159. }
  160. }
  161. func TestMatchConfig(t *testing.T) {
  162. str := `
  163. time="2020-07-20T14:14:17+09:00" level=info msg="pid_file written" file=tests/go-guerrilla.pid pid=15247
  164. time="2020-07-20T14:14:17+09:00" level=debug msg="making servers"
  165. time="2020-07-20T14:14:17+09:00" level=info msg="processing worker started" gateway=default id=3
  166. time="2020-07-20T14:14:17+09:00" level=info msg="processing worker started" gateway=default id=2
  167. time="2020-07-20T14:14:17+09:00" level=info msg="starting server" iface="127.0.0.1:2526" serverID=0
  168. time="2020-07-20T14:14:17+09:00" level=info msg="processing worker started" gateway=default id=1
  169. time="2020-07-20T14:14:17+09:00" level=info msg="processing worker started" gateway=temp id=2
  170. time="2020-07-20T14:14:17+09:00" level=info msg="processing worker started" gateway=default id=4
  171. time="2020-07-20T14:14:17+09:00" level=info msg="processing worker started" gateway=temp id=3
  172. time="2020-07-20T14:14:17+09:00" level=info msg="processing worker started" gateway=temp id=4
  173. time="2020-07-20T14:14:17+09:00" level=info msg="processing worker started" gateway=temp id=1
  174. time="2020-07-20T14:14:17+09:00" level=info msg="listening on TCP" iface="127.0.0.1:2526" serverID=0
  175. time="2020-07-20T14:14:17+09:00" level=debug msg="waiting for a new client" nextSeq=1 serverID=0
  176. `
  177. defer cleanTestArtifacts(t)
  178. if !MatchLog(str, 1, "msg", "making servers") {
  179. t.Error("making servers not matched")
  180. }
  181. if MatchLog(str, 10, "msg", "making servers") {
  182. t.Error("not expecting making servers matched")
  183. }
  184. if !MatchLog(str, 1, "msg", "listening on TCP", "serverID", 0) {
  185. t.Error("2 not pairs matched")
  186. }
  187. }
  188. // Testing start and stop of server
  189. func TestStart(t *testing.T) {
  190. if initErr != nil {
  191. t.Error(initErr)
  192. t.FailNow()
  193. }
  194. defer cleanTestArtifacts(t)
  195. if startErrors := app.Start(); startErrors != nil {
  196. t.Error(startErrors)
  197. t.FailNow()
  198. }
  199. time.Sleep(time.Second)
  200. app.Shutdown()
  201. if read, err := ioutil.ReadFile("./testlog"); err == nil {
  202. logOutput := string(read)
  203. if !MatchLog(logOutput, 1, "msg", "listening on TCP", "iface", "127.0.0.1:2526") {
  204. t.Error("Server did not listen on 127.0.0.1:4654")
  205. }
  206. if !MatchLog(logOutput, 1, "msg", "listening on TCP", "iface", "127.0.0.1:2526") {
  207. t.Error("Server did not listen on 127.0.0.1:2526")
  208. }
  209. if !MatchLog(logOutput, 1, "msg", "waiting for a new client", "iface", "127.0.0.1:4654") {
  210. t.Error("Server did not wait on 127.0.0.1:4654")
  211. }
  212. if !MatchLog(logOutput, 1, "msg", "waiting for a new client", "iface", "127.0.0.1:2526") {
  213. t.Error("Server did not wait on 127.0.0.1:2526")
  214. }
  215. if !MatchLog(logOutput, 1, "msg", "server has stopped accepting new clients", "iface", "127.0.0.1:4654") {
  216. t.Error("Server did not stop on 127.0.0.1:4654")
  217. }
  218. if !MatchLog(logOutput, 1, "msg", "server has stopped accepting new clients", "iface", "127.0.0.1:2526") {
  219. t.Error("Server did not stop on 127.0.0.1:2526")
  220. }
  221. if !MatchLog(logOutput, 1, "msg", "shutdown completed", "iface", "127.0.0.1:4654") {
  222. t.Error("Server did not complete shutdown on 127.0.0.1:4654")
  223. }
  224. if !MatchLog(logOutput, 1, "msg", "shutdown completed", "iface", "127.0.0.1:2526") {
  225. t.Error("Server did not complete shutdown on 127.0.0.1:2526")
  226. }
  227. if !MatchLog(logOutput, 1, "msg", "shutting down pool", "iface", "127.0.0.1:4654") {
  228. t.Error("Server did not shutdown pool on 127.0.0.1:4654")
  229. }
  230. if !MatchLog(logOutput, 1, "msg", "shutting down pool", "iface", "127.0.0.1:2526") {
  231. t.Error("Server did not shutdown pool on 127.0.0.1:2526")
  232. }
  233. if !MatchLog(logOutput, 1, "msg", "backend shutdown completed") {
  234. t.Error("Backend didn't shut down")
  235. }
  236. }
  237. }
  238. // Simple smoke-test to see if the server can listen & issues a greeting on connect
  239. func TestGreeting(t *testing.T) {
  240. if initErr != nil {
  241. t.Error(initErr)
  242. t.FailNow()
  243. }
  244. defer cleanTestArtifacts(t)
  245. if startErrors := app.Start(); startErrors == nil {
  246. // 1. plaintext connection
  247. conn, err := net.Dial("tcp", config.Servers[0].ListenInterface)
  248. if err != nil {
  249. // handle error
  250. t.Error("Cannot dial server", config.Servers[0].ListenInterface)
  251. }
  252. if err := conn.SetReadDeadline(time.Now().Add(time.Millisecond * 500)); err != nil {
  253. t.Error(err)
  254. }
  255. greeting, err := bufio.NewReader(conn).ReadString('\n')
  256. if err != nil {
  257. t.Error(err)
  258. t.FailNow()
  259. } else {
  260. expected := "220 mail.guerrillamail.com SMTP Guerrilla"
  261. if strings.Index(greeting, expected) != 0 {
  262. t.Error("Server[1] did not have the expected greeting prefix", expected)
  263. }
  264. }
  265. _ = conn.Close()
  266. // 2. tls connection
  267. // roots, err := x509.SystemCertPool()
  268. conn, err = tls.Dial("tcp", config.Servers[1].ListenInterface, &tls.Config{
  269. InsecureSkipVerify: true,
  270. ServerName: "127.0.0.1",
  271. })
  272. if err != nil {
  273. // handle error
  274. t.Error(err, "Cannot dial server (TLS)", config.Servers[1].ListenInterface)
  275. t.FailNow()
  276. }
  277. if err := conn.SetReadDeadline(time.Now().Add(time.Millisecond * 500)); err != nil {
  278. t.Error(err)
  279. }
  280. greeting, err = bufio.NewReader(conn).ReadString('\n')
  281. if err != nil {
  282. t.Error(err)
  283. t.FailNow()
  284. } else {
  285. expected := "220 mail.guerrillamail.com SMTP Guerrilla"
  286. if strings.Index(greeting, expected) != 0 {
  287. t.Error("Server[2] (TLS) did not have the expected greeting prefix", expected)
  288. }
  289. }
  290. _ = conn.Close()
  291. } else {
  292. fmt.Println("Nope", startErrors)
  293. if startErrors := app.Start(); startErrors != nil {
  294. t.Error(startErrors)
  295. t.FailNow()
  296. }
  297. }
  298. app.Shutdown()
  299. if read, err := ioutil.ReadFile("./testlog"); err == nil {
  300. logOutput := string(read)
  301. if !MatchLog(logOutput, 1, "msg", "handle client", "peer", "127.0.0.1") {
  302. t.Error("Server did not handle any clients")
  303. }
  304. }
  305. }
  306. // start up a server, connect a client, greet, then shutdown, then client sends a command
  307. // expecting: 421 Server is shutting down. Please try again later. Sayonara!
  308. // server should close connection after that
  309. func TestShutDown(t *testing.T) {
  310. if initErr != nil {
  311. t.Error(initErr)
  312. t.FailNow()
  313. }
  314. defer cleanTestArtifacts(t)
  315. if startErrors := app.Start(); startErrors == nil {
  316. conn, bufin, err := Connect(config.Servers[0], 20)
  317. if err != nil {
  318. // handle error
  319. t.Error(err.Error(), config.Servers[0].ListenInterface)
  320. t.FailNow()
  321. } else {
  322. // client goes into command state
  323. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  324. t.Error("Hello command failed", err.Error())
  325. }
  326. // do a shutdown while the client is connected & in client state
  327. go app.Shutdown()
  328. time.Sleep(time.Millisecond * 150) // let server to Shutdown
  329. // issue a command while shutting down
  330. response, err := Command(conn, bufin, "HELP")
  331. if err != nil {
  332. t.Error("Help command failed", err.Error())
  333. }
  334. expected := "421 4.3.0 Server is shutting down. Please try again later. Sayonara!"
  335. if strings.Index(response, expected) != 0 {
  336. t.Error("Server did not shut down with", expected, ", it said:"+response)
  337. }
  338. time.Sleep(time.Millisecond * 250) // let server to close
  339. }
  340. _ = conn.Close()
  341. } else {
  342. if startErrors := app.Start(); startErrors != nil {
  343. t.Error(startErrors)
  344. app.Shutdown()
  345. t.FailNow()
  346. }
  347. }
  348. // assuming server has shutdown by now
  349. if read, err := ioutil.ReadFile("./testlog"); err == nil {
  350. logOutput := string(read)
  351. // fmt.Println(logOutput)
  352. if !MatchLog(logOutput, 1, "msg", "handle client", "peer", "127.0.0.1") {
  353. t.Error("Server did not handle any clients")
  354. }
  355. }
  356. }
  357. // add more than 100 recipients, it should fail at 101
  358. func TestRFC2821LimitRecipients(t *testing.T) {
  359. if initErr != nil {
  360. t.Error(initErr)
  361. t.FailNow()
  362. }
  363. defer cleanTestArtifacts(t)
  364. if startErrors := app.Start(); startErrors == nil {
  365. conn, bufin, err := Connect(config.Servers[0], 20)
  366. if err != nil {
  367. // handle error
  368. t.Error(err.Error(), config.Servers[0].ListenInterface)
  369. t.FailNow()
  370. } else {
  371. // client goes into command state
  372. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  373. t.Error("Hello command failed", err.Error())
  374. }
  375. for i := 0; i < 101; i++ {
  376. //fmt.Println(fmt.Sprintf("RCPT TO:test%[email protected]", i))
  377. if _, err := Command(conn, bufin, fmt.Sprintf("RCPT TO:<test%[email protected]>", i)); err != nil {
  378. t.Error("RCPT TO", err.Error())
  379. break
  380. }
  381. }
  382. response, err := Command(conn, bufin, "RCPT TO:<[email protected]>")
  383. if err != nil {
  384. t.Error("rcpt command failed", err.Error())
  385. }
  386. expected := "452 4.5.3 Too many recipients"
  387. if strings.Index(response, expected) != 0 {
  388. t.Error("Server did not respond with", expected, ", it said:"+response)
  389. }
  390. }
  391. _ = conn.Close()
  392. app.Shutdown()
  393. } else {
  394. if startErrors := app.Start(); startErrors != nil {
  395. t.Error(startErrors)
  396. app.Shutdown()
  397. t.FailNow()
  398. }
  399. }
  400. }
  401. // RCPT TO & MAIL FROM with 64 chars in local part, it should fail at 65
  402. func TestRFC2832LimitLocalPart(t *testing.T) {
  403. if initErr != nil {
  404. t.Error(initErr)
  405. t.FailNow()
  406. }
  407. defer cleanTestArtifacts(t)
  408. if startErrors := app.Start(); startErrors == nil {
  409. conn, bufin, err := Connect(config.Servers[0], 20)
  410. if err != nil {
  411. // handle error
  412. t.Error(err.Error(), config.Servers[0].ListenInterface)
  413. t.FailNow()
  414. } else {
  415. // client goes into command state
  416. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  417. t.Error("Hello command failed", err.Error())
  418. }
  419. // repeat > 64 characters in local part
  420. response, err := Command(conn, bufin, fmt.Sprintf("RCPT TO:<%[email protected]>", strings.Repeat("a", smtp.LimitLocalPart+1)))
  421. if err != nil {
  422. t.Error("rcpt command failed", err.Error())
  423. }
  424. expected := "550 5.5.4 Local part too long"
  425. if strings.Index(response, expected) != 0 {
  426. t.Error("Server did not respond with", expected, ", it said:"+response)
  427. }
  428. // what about if it's exactly 64?
  429. // repeat > 64 characters in local part
  430. response, err = Command(conn, bufin, fmt.Sprintf("RCPT TO:<%[email protected]>", strings.Repeat("a", smtp.LimitLocalPart-1)))
  431. if err != nil {
  432. t.Error("rcpt command failed", err.Error())
  433. }
  434. expected = "250 2.1.5 OK"
  435. if strings.Index(response, expected) != 0 {
  436. t.Error("Server did not respond with", expected, ", it said:"+response)
  437. }
  438. }
  439. _ = conn.Close()
  440. app.Shutdown()
  441. } else {
  442. if startErrors := app.Start(); startErrors != nil {
  443. t.Error(startErrors)
  444. app.Shutdown()
  445. t.FailNow()
  446. }
  447. }
  448. }
  449. //RFC2821LimitPath fail if path > 256 but different error if below
  450. func TestRFC2821LimitPath(t *testing.T) {
  451. if initErr != nil {
  452. t.Error(initErr)
  453. t.FailNow()
  454. }
  455. if startErrors := app.Start(); startErrors == nil {
  456. conn, bufin, err := Connect(config.Servers[0], 20)
  457. if err != nil {
  458. // handle error
  459. t.Error(err.Error(), config.Servers[0].ListenInterface)
  460. t.FailNow()
  461. } else {
  462. // client goes into command state
  463. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  464. t.Error("Hello command failed", err.Error())
  465. }
  466. // repeat > 256 characters in local part
  467. response, err := Command(conn, bufin, fmt.Sprintf("RCPT TO:<%[email protected]>", strings.Repeat("a", 257-7)))
  468. if err != nil {
  469. t.Error("rcpt command failed", err.Error())
  470. }
  471. expected := "550 5.5.4 Path too long"
  472. if strings.Index(response, expected) != 0 {
  473. t.Error("Server did not respond with", expected, ", it said:"+response)
  474. }
  475. // what about if it's exactly 256?
  476. response, err = Command(conn, bufin,
  477. fmt.Sprintf("RCPT TO:<%s@%s.la>", strings.Repeat("a", 64), strings.Repeat("b", 186)))
  478. if err != nil {
  479. t.Error("rcpt command failed", err.Error())
  480. }
  481. expected = "454 4.1.1 Error: Relay access denied"
  482. if strings.Index(response, expected) != 0 {
  483. t.Error("Server did not respond with", expected, ", it said:"+response)
  484. }
  485. }
  486. _ = conn.Close()
  487. app.Shutdown()
  488. } else {
  489. if startErrors := app.Start(); startErrors != nil {
  490. t.Error(startErrors)
  491. app.Shutdown()
  492. t.FailNow()
  493. }
  494. }
  495. }
  496. // RFC2821LimitDomain 501 Domain cannot exceed 255 characters
  497. func TestRFC2821LimitDomain(t *testing.T) {
  498. if initErr != nil {
  499. t.Error(initErr)
  500. t.FailNow()
  501. }
  502. defer cleanTestArtifacts(t)
  503. if startErrors := app.Start(); startErrors == nil {
  504. conn, bufin, err := Connect(config.Servers[0], 20)
  505. if err != nil {
  506. // handle error
  507. t.Error(err.Error(), config.Servers[0].ListenInterface)
  508. t.FailNow()
  509. } else {
  510. // client goes into command state
  511. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  512. t.Error("Hello command failed", err.Error())
  513. }
  514. // repeat > 64 characters in local part
  515. response, err := Command(conn, bufin, fmt.Sprintf("RCPT TO:<a@%s.l>", strings.Repeat("a", 255-2)))
  516. if err != nil {
  517. t.Error("command failed", err.Error())
  518. }
  519. expected := "550 5.5.4 Path too long"
  520. if strings.Index(response, expected) != 0 {
  521. t.Error("Server did not respond with", expected, ", it said:"+response)
  522. }
  523. // what about if it's exactly 255?
  524. response, err = Command(conn, bufin,
  525. fmt.Sprintf("RCPT TO:<a@%s.la>", strings.Repeat("b", 255-6)))
  526. if err != nil {
  527. t.Error("command failed", err.Error())
  528. }
  529. expected = "454 4.1.1 Error: Relay access denied"
  530. if strings.Index(response, expected) != 0 {
  531. t.Error("Server did not respond with", expected, ", it said:"+response)
  532. }
  533. }
  534. _ = conn.Close()
  535. app.Shutdown()
  536. } else {
  537. if startErrors := app.Start(); startErrors != nil {
  538. t.Error(startErrors)
  539. app.Shutdown()
  540. t.FailNow()
  541. }
  542. }
  543. }
  544. // Test several different inputs to MAIL FROM command
  545. func TestMailFromCmd(t *testing.T) {
  546. if initErr != nil {
  547. t.Error(initErr)
  548. t.FailNow()
  549. }
  550. defer cleanTestArtifacts(t)
  551. if startErrors := app.Start(); startErrors == nil {
  552. conn, bufin, err := Connect(config.Servers[0], 20)
  553. if err != nil {
  554. // handle error
  555. t.Error(err.Error(), config.Servers[0].ListenInterface)
  556. t.FailNow()
  557. } else {
  558. // client goes into command state
  559. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  560. t.Error("Hello command failed", err.Error())
  561. }
  562. // Basic valid address
  563. response, err := Command(conn, bufin, "MAIL FROM:<[email protected]>")
  564. if err != nil {
  565. t.Error("command failed", err.Error())
  566. }
  567. expected := "250 2.1.0 OK"
  568. if strings.Index(response, expected) != 0 {
  569. t.Error("Server did not respond with", expected, ", it said:"+response)
  570. }
  571. // Reset
  572. response, err = Command(conn, bufin, "RSET")
  573. if err != nil {
  574. t.Error("command failed", err.Error())
  575. }
  576. expected = "250 2.1.0 OK"
  577. if strings.Index(response, expected) != 0 {
  578. t.Error("Server did not respond with", expected, ", it said:"+response)
  579. }
  580. // Basic valid address (RfC)
  581. response, err = Command(conn, bufin, "MAIL FROM:<[email protected]>")
  582. if err != nil {
  583. t.Error("command failed", err.Error())
  584. }
  585. expected = "250 2.1.0 OK"
  586. if strings.Index(response, expected) != 0 {
  587. t.Error("Server did not respond with", expected, ", it said:"+response)
  588. }
  589. // Reset
  590. response, err = Command(conn, bufin, "RSET")
  591. if err != nil {
  592. t.Error("command failed", err.Error())
  593. }
  594. expected = "250 2.1.0 OK"
  595. if strings.Index(response, expected) != 0 {
  596. t.Error("Server did not respond with", expected, ", it said:"+response)
  597. }
  598. // Bounce
  599. response, err = Command(conn, bufin, "MAIL FROM:<>")
  600. if err != nil {
  601. t.Error("command failed", err.Error())
  602. }
  603. expected = "250 2.1.0 OK"
  604. if strings.Index(response, expected) != 0 {
  605. t.Error("Server did not respond with", expected, ", it said:"+response)
  606. }
  607. // Reset
  608. response, err = Command(conn, bufin, "RSET")
  609. if err != nil {
  610. t.Error("command failed", err.Error())
  611. }
  612. expected = "250 2.1.0 OK"
  613. if strings.Index(response, expected) != 0 {
  614. t.Error("Server did not respond with", expected, ", it said:"+response)
  615. }
  616. // No mail from content
  617. response, err = Command(conn, bufin, "MAIL FROM:")
  618. if err != nil {
  619. t.Error("command failed", err.Error())
  620. }
  621. expected = "501 5.5.4 Invalid address"
  622. if strings.Index(response, expected) != 0 {
  623. t.Error("Server did not respond with", expected, ", it said:"+response)
  624. }
  625. // Reset
  626. response, err = Command(conn, bufin, "RSET")
  627. if err != nil {
  628. t.Error("command failed", err.Error())
  629. }
  630. expected = "250 2.1.0 OK"
  631. if strings.Index(response, expected) != 0 {
  632. t.Error("Server did not respond with", expected, ", it said:"+response)
  633. }
  634. // Short mail from content
  635. response, err = Command(conn, bufin, "MAIL FROM:<")
  636. if err != nil {
  637. t.Error("command failed", err.Error())
  638. }
  639. expected = "501 5.5.4 Invalid address"
  640. if strings.Index(response, expected) != 0 {
  641. t.Error("Server did not respond with", expected, ", it said:"+response)
  642. }
  643. // Reset
  644. response, err = Command(conn, bufin, "RSET")
  645. if err != nil {
  646. t.Error("command failed", err.Error())
  647. }
  648. expected = "250 2.1.0 OK"
  649. if strings.Index(response, expected) != 0 {
  650. t.Error("Server did not respond with", expected, ", it said:"+response)
  651. }
  652. // Short mail from content 2
  653. response, err = Command(conn, bufin, "MAIL FROM:x")
  654. if err != nil {
  655. t.Error("command failed", err.Error())
  656. }
  657. expected = "501 5.5.4 Invalid address"
  658. if strings.Index(response, expected) != 0 {
  659. t.Error("Server did not respond with", expected, ", it said:"+response)
  660. }
  661. // Reset
  662. response, err = Command(conn, bufin, "RSET")
  663. if err != nil {
  664. t.Error("command failed", err.Error())
  665. }
  666. expected = "250 2.1.0 OK"
  667. if strings.Index(response, expected) != 0 {
  668. t.Error("Server did not respond with", expected, ", it said:"+response)
  669. }
  670. // What?
  671. response, err = Command(conn, bufin, "MAIL FROM:<<>>")
  672. if err != nil {
  673. t.Error("command failed", err.Error())
  674. }
  675. expected = "501 5.5.4 Invalid address"
  676. if strings.Index(response, expected) != 0 {
  677. t.Error("Server did not respond with", expected, ", it said:"+response)
  678. }
  679. // Reset
  680. response, err = Command(conn, bufin, "RSET")
  681. if err != nil {
  682. t.Error("command failed", err.Error())
  683. }
  684. expected = "250 2.1.0 OK"
  685. if strings.Index(response, expected) != 0 {
  686. t.Error("Server did not respond with", expected, ", it said:"+response)
  687. }
  688. // Invalid address?
  689. response, err = Command(conn, bufin, "MAIL FROM:<justatest>")
  690. if err != nil {
  691. t.Error("command failed", err.Error())
  692. }
  693. expected = "501 5.5.4 Invalid address"
  694. if strings.Index(response, expected) != 0 {
  695. t.Error("Server did not respond with", expected, ", it said:"+response)
  696. }
  697. // Reset
  698. response, err = Command(conn, bufin, "RSET")
  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. /*
  707. // todo SMTPUTF8 not implemented for now,
  708. response, err = Command(conn, bufin, "MAIL FROM:<anö[email protected]>")
  709. if err != nil {
  710. t.Error("command failed", err.Error())
  711. }
  712. expected = "250 2.1.0 OK"
  713. if strings.Index(response, expected) != 0 {
  714. t.Error("Server did not respond with", expected, ", it said:"+response)
  715. }
  716. */
  717. // Reset
  718. response, err = Command(conn, bufin, "RSET")
  719. if err != nil {
  720. t.Error("command failed", err.Error())
  721. }
  722. expected = "250 2.1.0 OK"
  723. if strings.Index(response, expected) != 0 {
  724. t.Error("Server did not respond with", expected, ", it said:"+response)
  725. }
  726. // 8BITMIME (RfC 6152)
  727. response, err = Command(conn, bufin, "MAIL FROM:<[email protected]> BODY=8BITMIME")
  728. if err != nil {
  729. t.Error("command failed", err.Error())
  730. }
  731. expected = "250 2.1.0 OK"
  732. if strings.Index(response, expected) != 0 {
  733. t.Error("Server did not respond with", expected, ", it said:"+response)
  734. }
  735. // Reset
  736. response, err = Command(conn, bufin, "RSET")
  737. if err != nil {
  738. t.Error("command failed", err.Error())
  739. }
  740. expected = "250 2.1.0 OK"
  741. if strings.Index(response, expected) != 0 {
  742. t.Error("Server did not respond with", expected, ", it said:"+response)
  743. }
  744. // 8BITMIME (RfC 6152) Bounce
  745. response, err = Command(conn, bufin, "MAIL FROM:<> BODY=8BITMIME")
  746. if err != nil {
  747. t.Error("command failed", err.Error())
  748. }
  749. expected = "250 2.1.0 OK"
  750. if strings.Index(response, expected) != 0 {
  751. t.Error("Server did not respond with", expected, ", it said:"+response)
  752. }
  753. // Reset
  754. response, err = Command(conn, bufin, "RSET")
  755. if err != nil {
  756. t.Error("command failed", err.Error())
  757. }
  758. expected = "250 2.1.0 OK"
  759. if strings.Index(response, expected) != 0 {
  760. t.Error("Server did not respond with", expected, ", it said:"+response)
  761. }
  762. }
  763. _ = conn.Close()
  764. app.Shutdown()
  765. } else {
  766. if startErrors := app.Start(); startErrors != nil {
  767. t.Error(startErrors)
  768. app.Shutdown()
  769. t.FailNow()
  770. }
  771. }
  772. }
  773. // Test several different inputs to MAIL FROM command
  774. func TestHeloEhlo(t *testing.T) {
  775. if initErr != nil {
  776. t.Error(initErr)
  777. t.FailNow()
  778. }
  779. defer cleanTestArtifacts(t)
  780. if startErrors := app.Start(); startErrors == nil {
  781. conn, bufin, err := Connect(config.Servers[0], 20)
  782. hostname := config.Servers[0].Hostname
  783. if err != nil {
  784. // handle error
  785. t.Error(err.Error(), config.Servers[0].ListenInterface)
  786. t.FailNow()
  787. } else {
  788. // Test HELO
  789. response, err := Command(conn, bufin, "HELO localtester")
  790. if err != nil {
  791. t.Error("command failed", err.Error())
  792. }
  793. expected := fmt.Sprintf("250 %s Hello", hostname)
  794. if strings.Index(response, expected) != 0 {
  795. t.Error("Server did not respond with", expected, ", it said:"+response)
  796. }
  797. // Reset
  798. response, err = Command(conn, bufin, "RSET")
  799. if err != nil {
  800. t.Error("command failed", err.Error())
  801. }
  802. expected = "250 2.1.0 OK"
  803. if strings.Index(response, expected) != 0 {
  804. t.Error("Server did not respond with", expected, ", it said:"+response)
  805. }
  806. // Test EHLO
  807. // This is tricky as it is a multiline response
  808. var fullresp string
  809. response, err = Command(conn, bufin, "EHLO localtester")
  810. fullresp = fullresp + response
  811. if err != nil {
  812. t.Error("command failed", err.Error())
  813. }
  814. for err == nil {
  815. response, err = bufin.ReadString('\n')
  816. fullresp = fullresp + response
  817. if strings.HasPrefix(response, "250 ") { // Last response has a whitespace and no "-"
  818. break // bail
  819. }
  820. }
  821. expected = fmt.Sprintf("250-%s Hello\r\n250-SIZE 100017\r\n250-PIPELINING\r\n250-STARTTLS\r\n250-ENHANCEDSTATUSCODES\r\n250-8BITMIME\r\n250 HELP\r\n", hostname)
  822. if fullresp != expected {
  823. t.Error("Server did not respond with [" + expected + "], it said [" + fullresp + "]")
  824. }
  825. // be kind, QUIT. And we are sure that bufin does not contain fragments from the EHLO command.
  826. response, err = Command(conn, bufin, "QUIT")
  827. if err != nil {
  828. t.Error("command failed", err.Error())
  829. }
  830. expected = "221 2.0.0 Bye"
  831. if strings.Index(response, expected) != 0 {
  832. t.Error("Server did not respond with", expected, ", it said:"+response)
  833. }
  834. }
  835. _ = conn.Close()
  836. app.Shutdown()
  837. } else {
  838. if startErrors := app.Start(); startErrors != nil {
  839. t.Error(startErrors)
  840. app.Shutdown()
  841. t.FailNow()
  842. }
  843. }
  844. }
  845. // It should error when MAIL FROM was given twice
  846. func TestNestedMailCmd(t *testing.T) {
  847. if initErr != nil {
  848. t.Error(initErr)
  849. t.FailNow()
  850. }
  851. defer cleanTestArtifacts(t)
  852. if startErrors := app.Start(); startErrors == nil {
  853. conn, bufin, err := Connect(config.Servers[0], 20)
  854. if err != nil {
  855. // handle error
  856. t.Error(err.Error(), config.Servers[0].ListenInterface)
  857. t.FailNow()
  858. } else {
  859. // client goes into command state
  860. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  861. t.Error("Hello command failed", err.Error())
  862. }
  863. // repeat > 64 characters in local part
  864. response, err := Command(conn, bufin, "MAIL FROM:<[email protected]>")
  865. if err != nil {
  866. t.Error("command failed", err.Error())
  867. }
  868. response, err = Command(conn, bufin, "MAIL FROM:<[email protected]>")
  869. if err != nil {
  870. t.Error("command failed", err.Error())
  871. }
  872. expected := "503 5.5.1 Error: nested MAIL command"
  873. if strings.Index(response, expected) != 0 {
  874. t.Error("Server did not respond with", expected, ", it said:"+response)
  875. }
  876. // Plot twist: if you EHLO , it should allow MAIL FROM again
  877. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  878. t.Error("Hello command failed", err.Error())
  879. }
  880. response, err = Command(conn, bufin, "MAIL FROM:<[email protected]>")
  881. if err != nil {
  882. t.Error("command failed", err.Error())
  883. }
  884. expected = "250 2.1.0 OK"
  885. if strings.Index(response, expected) != 0 {
  886. t.Error("Server did not respond with", expected, ", it said:"+response)
  887. }
  888. // Plot twist: if you RSET , it should allow MAIL FROM again
  889. response, err = Command(conn, bufin, "RSET")
  890. if err != nil {
  891. t.Error("command failed", err.Error())
  892. }
  893. expected = "250 2.1.0 OK"
  894. if strings.Index(response, expected) != 0 {
  895. t.Error("Server did not respond with", expected, ", it said:"+response)
  896. }
  897. response, err = Command(conn, bufin, "MAIL FROM:<[email protected]>")
  898. if err != nil {
  899. t.Error("command failed", err.Error())
  900. }
  901. expected = "250 2.1.0 OK"
  902. if strings.Index(response, expected) != 0 {
  903. t.Error("Server did not respond with", expected, ", it said:"+response)
  904. }
  905. }
  906. _ = conn.Close()
  907. app.Shutdown()
  908. } else {
  909. if startErrors := app.Start(); startErrors != nil {
  910. t.Error(startErrors)
  911. app.Shutdown()
  912. t.FailNow()
  913. }
  914. }
  915. }
  916. // It should error on a very long command line, exceeding CommandLineMaxLength 1024
  917. func TestCommandLineMaxLength(t *testing.T) {
  918. if initErr != nil {
  919. t.Error(initErr)
  920. t.FailNow()
  921. }
  922. defer cleanTestArtifacts(t)
  923. if startErrors := app.Start(); startErrors == nil {
  924. conn, bufin, err := Connect(config.Servers[0], 20)
  925. if err != nil {
  926. // handle error
  927. t.Error(err.Error(), config.Servers[0].ListenInterface)
  928. t.FailNow()
  929. } else {
  930. // client goes into command state
  931. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  932. t.Error("Hello command failed", err.Error())
  933. }
  934. // repeat > 1024 characters
  935. response, err := Command(conn, bufin, strings.Repeat("s", guerrilla.CommandLineMaxLength+1))
  936. if err != nil {
  937. t.Error("command failed", err.Error())
  938. }
  939. expected := "554 5.5.1 Line too long"
  940. if strings.Index(response, expected) != 0 {
  941. t.Error("Server did not respond with", expected, ", it said:"+response)
  942. }
  943. }
  944. _ = conn.Close()
  945. app.Shutdown()
  946. } else {
  947. if startErrors := app.Start(); startErrors != nil {
  948. t.Error(startErrors)
  949. app.Shutdown()
  950. t.FailNow()
  951. }
  952. }
  953. }
  954. // It should error on a very long message, exceeding servers config value
  955. func TestDataMaxLength(t *testing.T) {
  956. if initErr != nil {
  957. t.Error(initErr)
  958. t.FailNow()
  959. }
  960. defer cleanTestArtifacts(t)
  961. if startErrors := app.Start(); startErrors == nil {
  962. conn, bufin, err := Connect(config.Servers[0], 20)
  963. if err != nil {
  964. // handle error
  965. t.Error(err.Error(), config.Servers[0].ListenInterface)
  966. t.FailNow()
  967. } else {
  968. // client goes into command state
  969. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  970. t.Error("Hello command failed", err.Error())
  971. }
  972. response, err := Command(conn, bufin, "MAIL FROM:[email protected]")
  973. if err != nil {
  974. t.Error("command failed", err.Error())
  975. }
  976. //fmt.Println(response)
  977. response, err = Command(conn, bufin, "RCPT TO:<[email protected]>")
  978. if err != nil {
  979. t.Error("command failed", err.Error())
  980. }
  981. //fmt.Println(response)
  982. response, err = Command(conn, bufin, "DATA")
  983. if err != nil {
  984. t.Error("command failed", err.Error())
  985. }
  986. response, err = Command(
  987. conn,
  988. bufin,
  989. fmt.Sprintf("Subject:test\r\n\r\nHello %s\r\n.\r\n",
  990. strings.Repeat("n", int(config.Servers[0].MaxSize-20))))
  991. //expected := "500 Line too long"
  992. expected := "451 4.3.0 Error: maximum DATA size exceeded"
  993. if strings.Index(response, expected) != 0 {
  994. t.Error("Server did not respond with", expected, ", it said:"+response)
  995. }
  996. }
  997. _ = conn.Close()
  998. app.Shutdown()
  999. } else {
  1000. if startErrors := app.Start(); startErrors != nil {
  1001. t.Error(startErrors)
  1002. app.Shutdown()
  1003. t.FailNow()
  1004. }
  1005. }
  1006. }
  1007. func TestDataCommand(t *testing.T) {
  1008. if initErr != nil {
  1009. t.Error(initErr)
  1010. t.FailNow()
  1011. }
  1012. defer cleanTestArtifacts(t)
  1013. testHeader :=
  1014. "Subject: =?Shift_JIS?B?W4NYg06DRYNGg0GBRYNHg2qDYoNOg1ggg0GDSoNFg5ODZ12DQYNKg0WDk4Nn?=\r\n" +
  1015. "\t=?Shift_JIS?B?k2+YXoqul7mCzIKokm2C54K5?=\r\n"
  1016. email :=
  1017. "Delivered-To: [email protected]\r\n" +
  1018. "\tReceived: from mail.guerrillamail.com (mail.guerrillamail.com [104.218.55.28:44246])\r\n" +
  1019. "\tby grr.la with SMTP id [email protected];\r\n" +
  1020. "\tWed, 18 Jan 2017 15:43:29 +0000\r\n" +
  1021. "Received: by 192.99.19.220 with HTTP; Wed, 18 Jan 2017 15:43:29 +0000\r\n" +
  1022. "MIME-Version: 1.0\r\n" +
  1023. "Message-ID: <[email protected]>\r\n" +
  1024. "Date: Wed, 18 Jan 2017 15:43:29 +0000\r\n" +
  1025. "To: \"[email protected]\" <[email protected]>\r\n" +
  1026. "From: <[email protected]>\r\n" +
  1027. "Subject: test\r\n" +
  1028. "X-Originating-IP: [60.241.160.150]\r\n" +
  1029. "Content-Type: text/plain; charset=\"utf-8\"\r\n" +
  1030. "Content-Transfer-Encoding: quoted-printable\r\n" +
  1031. "X-Domain-Signer: PHP mailDomainSigner 0.2-20110415 <http://code.google.com/p/php-mail-domain-signer/>\r\n" +
  1032. "DKIM-Signature: v=1; a=rsa-sha256; s=highgrade; d=guerrillamail.com; l=182;\r\n" +
  1033. "\tt=1484754209; c=relaxed/relaxed; h=to:from:subject;\r\n" +
  1034. "\tbh=GHSgjHpBp5QjNn9tzfug681+RcWMOUgpwAuTzppM5wY=;\r\n" +
  1035. "\tb=R7FxWgACnT+pKXqEg15qgzH4ywMFRx5pDlIFCnSt1BfwmLvZPZK7oOLrbiRoGGR2OJnSfyCxeASH\r\n" +
  1036. "\t019LNeLB/B8o+fMRX87m/tBpqIZ2vgXdT9rUCIbSDJnYoCHXakGcF+zGtTE3SEksMbeJQ76aGj6M\r\n" +
  1037. "\tG80p76IT2Xu3iDJLYYWxcAeX+7z4M/bbYNeqxMQcXYZp1wNYlSlHahL6RDUYdcqikDqKoXmzMNVd\r\n" +
  1038. "\tDr0EbH9iiu1DQtfUDzVE5LLus1yn36WU/2KJvEak45gJvm9s9J+Xrcb882CaYkxlAbgQDz1KeQLf\r\n" +
  1039. "\teUyNspyAabkh2yTg7kOvNZSOJtbMSQS6/GMxsg==\r\n" +
  1040. "\r\n" +
  1041. "test=0A.mooo=0A..mooo=0Atest=0A.=0A=0A=0A=0A=0A=0A----=0ASent using Guerril=\r\n" +
  1042. "lamail.com=0ABlock or report abuse: https://www.guerrillamail.com//abuse/?a=\r\n" +
  1043. "=3DVURnES0HUaZbhA8%3D=0A\r\n.\r\n"
  1044. if startErrors := app.Start(); startErrors == nil {
  1045. conn, bufin, err := Connect(config.Servers[0], 20)
  1046. if err != nil {
  1047. // handle error
  1048. t.Error(err.Error(), config.Servers[0].ListenInterface)
  1049. t.FailNow()
  1050. } else {
  1051. // client goes into command state
  1052. if _, err := Command(conn, bufin, "HELO localtester"); err != nil {
  1053. t.Error("Hello command failed", err.Error())
  1054. }
  1055. response, err := Command(conn, bufin, "MAIL FROM:<[email protected]>")
  1056. if err != nil {
  1057. t.Error("command failed", err.Error())
  1058. }
  1059. //fmt.Println(response)
  1060. response, err = Command(conn, bufin, "RCPT TO:<[email protected]>")
  1061. if err != nil {
  1062. t.Error("command failed", err.Error())
  1063. }
  1064. //fmt.Println(response)
  1065. response, err = Command(conn, bufin, "DATA")
  1066. if err != nil {
  1067. t.Error("command failed", err.Error())
  1068. }
  1069. /*
  1070. response, err = Command(
  1071. conn,
  1072. bufin,
  1073. testHeader+"\r\nHello World\r\n.\r\n")
  1074. */
  1075. _ = testHeader
  1076. response, err = Command(
  1077. conn,
  1078. bufin,
  1079. email+"\r\n.\r\n")
  1080. //expected := "500 Line too long"
  1081. expected := "250 2.0.0 OK: queued as "
  1082. if strings.Index(response, expected) != 0 {
  1083. t.Error("Server did not respond with", expected, ", it said:"+response, err)
  1084. }
  1085. }
  1086. _ = conn.Close()
  1087. app.Shutdown()
  1088. } else {
  1089. if startErrors := app.Start(); startErrors != nil {
  1090. t.Error(startErrors)
  1091. app.Shutdown()
  1092. t.FailNow()
  1093. }
  1094. }
  1095. }
  1096. // Fuzzer crashed the server by submitting "DATA\r\n" as the first command
  1097. func TestFuzz86f25b86b09897aed8f6c2aa5b5ee1557358a6de(t *testing.T) {
  1098. if initErr != nil {
  1099. t.Error(initErr)
  1100. t.FailNow()
  1101. }
  1102. defer cleanTestArtifacts(t)
  1103. if startErrors := app.Start(); startErrors == nil {
  1104. conn, bufin, err := Connect(config.Servers[0], 20)
  1105. if err != nil {
  1106. // handle error
  1107. t.Error(err.Error(), config.Servers[0].ListenInterface)
  1108. t.FailNow()
  1109. } else {
  1110. response, err := Command(
  1111. conn,
  1112. bufin,
  1113. "DATA\r\n")
  1114. expected := "503 5.5.1 Error: No recipients"
  1115. if strings.Index(response, expected) != 0 {
  1116. t.Error("Server did not respond with", expected, ", it said:"+response, err)
  1117. }
  1118. }
  1119. _ = conn.Close()
  1120. app.Shutdown()
  1121. } else {
  1122. if startErrors := app.Start(); startErrors != nil {
  1123. t.Error(startErrors)
  1124. app.Shutdown()
  1125. t.FailNow()
  1126. }
  1127. }
  1128. }
  1129. // Appears to hang the fuzz test, but not server.
  1130. func TestFuzz21c56f89989d19c3bbbd81b288b2dae9e6dd2150(t *testing.T) {
  1131. if initErr != nil {
  1132. t.Error(initErr)
  1133. t.FailNow()
  1134. }
  1135. defer cleanTestArtifacts(t)
  1136. str := "X_\r\nMAIL FROM:<u\xfd\xfdrU" +
  1137. "\x10c22695140\xfd727235530" +
  1138. " Walter Sobchak\x1a\tDon" +
  1139. "ny, x_6_, Donnyre " +
  1140. "\t\t outof89 !om>\r\nMAI" +
  1141. "L\t\t \t\tFROM:<C4o\xfd\xfdr@e" +
  1142. "xample.c22695140\xfd727" +
  1143. "235530 Walter Sobcha" +
  1144. "k: Donny, you>re out" +
  1145. " of your element!om>" +
  1146. "\r\nMAIL RCPT TO:t@IRS" +
  1147. "ETRCPTIRSETRCP:<\x00\xfd\xfdr" +
  1148. "@example 7A924_F__4_" +
  1149. "c22695140\xfd-061.0x30C" +
  1150. "8bC87fE4d3 Walter MA" +
  1151. "IL Donny, youiq__n_l" +
  1152. "wR8qs_0RBcw_0hIY_pS_" +
  1153. "___x9_E0___sL598_G82" +
  1154. "_6 out your elemen" +
  1155. "t!>\r\nX _9KB___X_p:<o" +
  1156. "ut\xfd\xfdr@example9gTnr2N" +
  1157. "__Vl_T7U_AqfU_dPfJ_0" +
  1158. "HIqKK0037f6W_KGM_y_Z" +
  1159. "_9_96_w_815Q572py2_9" +
  1160. "F\xfd727235530Walter\tSo" +
  1161. "bchakRSET MAIL from:" +
  1162. " : cows eat\t\t grass" +
  1163. " , _S___46_PbG03_iW'" +
  1164. "__v5L2_2L_J61u_38J55" +
  1165. "_PpwQ_Fs_7L_3p7S_t__" +
  1166. "g9XP48T_9HY_EDl_c_C3" +
  1167. "3_3b708EreT_OR out 9" +
  1168. "9_pUY4 \t\t\t \x05om>\r" +
  1169. "\n FROM<u\xfd\xfdr@example." +
  1170. "<\xfd-05110602 Walter S" +
  1171. "obchak: Donny, \t\t w" +
  1172. "50TI__m_5EsC___n_l_d" +
  1173. "__57GP9G02_32n_FR_xw" +
  1174. "_2_103___rnED5PGIKN7" +
  1175. "BBs3VIuNV_514qDBp_Gs" +
  1176. "_qj4\tre out all cows" +
  1177. " eatof your element\x03" +
  1178. "om>\r\n_2 FROM:<u\x10\xfdr@e" +
  1179. "xample.oQ_VLq909_E_5" +
  1180. "AQ7_4_\xfd1935012674150" +
  1181. "6773818422493001838." +
  1182. "-010\tWalter\tSobchak:" +
  1183. " Donny, youyouteIz2y" +
  1184. "__Z2q5_qoA're Q6MP2_" +
  1185. "CT_z70____0c0nU7_83d" +
  1186. "4jn_eFD7h_9MbPjr_s_L" +
  1187. "9_X23G_7 of _kU_L9Yz" +
  1188. "_K52345QVa902H1__Hj_" +
  1189. "Nl_PP2tW2ODi0_V80F15" +
  1190. "_i65i_V5uSQdiG eleme" +
  1191. "nt!om>\r\n"
  1192. if startErrors := app.Start(); startErrors == nil {
  1193. conn, bufin, err := Connect(config.Servers[0], 20)
  1194. if err != nil {
  1195. // handle error
  1196. t.Error(err.Error(), config.Servers[0].ListenInterface)
  1197. t.FailNow()
  1198. } else {
  1199. response, err := Command(
  1200. conn,
  1201. bufin,
  1202. str)
  1203. expected := "554 5.5.1 Unrecognized command"
  1204. if strings.Index(response, expected) != 0 {
  1205. t.Error("Server did not respond with", expected, ", it said:"+response, err)
  1206. }
  1207. }
  1208. _ = conn.Close()
  1209. app.Shutdown()
  1210. } else {
  1211. if startErrors := app.Start(); startErrors != nil {
  1212. t.Error(startErrors)
  1213. app.Shutdown()
  1214. t.FailNow()
  1215. }
  1216. }
  1217. }