serve_test.go 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. package main
  2. import (
  3. "bufio"
  4. "crypto/tls"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "io/ioutil"
  9. "math"
  10. "os"
  11. "os/exec"
  12. "runtime"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "testing"
  17. "time"
  18. "github.com/flashmob/go-guerrilla"
  19. "github.com/flashmob/go-guerrilla/backends"
  20. "github.com/flashmob/go-guerrilla/log"
  21. test "github.com/flashmob/go-guerrilla/tests"
  22. "github.com/flashmob/go-guerrilla/tests/testcert"
  23. "github.com/spf13/cobra"
  24. )
  25. var configJsonA = `
  26. {
  27. "log_file" : "../../tests/testlog",
  28. "log_level" : "debug",
  29. "pid_file" : "./pidfile.pid",
  30. "allowed_hosts": [
  31. "guerrillamail.com",
  32. "guerrillamailblock.com",
  33. "sharklasers.com",
  34. "guerrillamail.net",
  35. "guerrillamail.org"
  36. ],
  37. "backend_config": {
  38. "save_workers_size" : 1,
  39. "save_process": "HeadersParser|Debugger",
  40. "log_received_mails": true
  41. },
  42. "servers" : [
  43. {
  44. "is_enabled" : true,
  45. "host_name":"mail.test.com",
  46. "max_size": 1000000,
  47. "timeout":180,
  48. "listen_interface":"127.0.0.1:3536",
  49. "max_clients": 200,
  50. "log_file" : "../../tests/testlog",
  51. "tls" : {
  52. "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
  53. "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
  54. "start_tls_on":true,
  55. "tls_always_on":false
  56. }
  57. },
  58. {
  59. "is_enabled" : false,
  60. "host_name":"enable.test.com",
  61. "max_size": 1000000,
  62. "timeout":180,
  63. "listen_interface":"127.0.0.1:2228",
  64. "max_clients": 200,
  65. "log_file" : "../../tests/testlog",
  66. "tls" : {
  67. "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
  68. "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
  69. "start_tls_on":true,
  70. "tls_always_on":false
  71. }
  72. }
  73. ]
  74. }
  75. `
  76. // backend config changed, log_received_mails is false
  77. var configJsonB = `
  78. {
  79. "log_file" : "../../tests/testlog",
  80. "log_level" : "debug",
  81. "pid_file" : "./pidfile2.pid",
  82. "allowed_hosts": [
  83. "guerrillamail.com",
  84. "guerrillamailblock.com",
  85. "sharklasers.com",
  86. "guerrillamail.net",
  87. "guerrillamail.org"
  88. ],
  89. "backend_config": {
  90. "save_workers_size" : 1,
  91. "save_process": "HeadersParser|Debugger",
  92. "log_received_mails": false
  93. },
  94. "servers" : [
  95. {
  96. "is_enabled" : true,
  97. "host_name":"mail.test.com",
  98. "max_size": 1000000,
  99. "timeout":180,
  100. "listen_interface":"127.0.0.1:3536",
  101. "max_clients": 200,
  102. "log_file" : "../../tests/testlog",
  103. "tls" : {
  104. "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
  105. "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
  106. "start_tls_on":true,
  107. "tls_always_on":false
  108. }
  109. }
  110. ]
  111. }
  112. `
  113. // added a server
  114. var configJsonC = `
  115. {
  116. "log_file" : "../../tests/testlog",
  117. "log_level" : "debug",
  118. "pid_file" : "./pidfile.pid",
  119. "allowed_hosts": [
  120. "guerrillamail.com",
  121. "guerrillamailblock.com",
  122. "sharklasers.com",
  123. "guerrillamail.net",
  124. "guerrillamail.org"
  125. ],
  126. "backend_config" :
  127. {
  128. "sql_driver": "mysql",
  129. "sql_dsn": "root:ok@tcp(127.0.0.1:3306)/gmail_mail?readTimeout=10s&writeTimeout=10s",
  130. "mail_table":"new_mail",
  131. "redis_interface" : "127.0.0.1:6379",
  132. "redis_expire_seconds" : 7200,
  133. "save_workers_size" : 3,
  134. "primary_mail_host":"sharklasers.com",
  135. "save_workers_size" : 1,
  136. "save_process": "HeadersParser|Debugger",
  137. "log_received_mails": true
  138. },
  139. "servers" : [
  140. {
  141. "is_enabled" : true,
  142. "host_name":"mail.test.com",
  143. "max_size": 1000000,
  144. "timeout":180,
  145. "listen_interface":"127.0.0.1:25",
  146. "max_clients": 200,
  147. "log_file" : "../../tests/testlog",
  148. "tls" : {
  149. "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
  150. "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
  151. "start_tls_on":true,
  152. "tls_always_on":false
  153. }
  154. },
  155. {
  156. "is_enabled" : true,
  157. "host_name":"mail.test.com",
  158. "max_size":1000000,
  159. "timeout":180,
  160. "listen_interface":"127.0.0.1:465",
  161. "max_clients":200,
  162. "log_file" : "../../tests/testlog",
  163. "tls" : {
  164. "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
  165. "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
  166. "start_tls_on":false,
  167. "tls_always_on":true
  168. }
  169. }
  170. ]
  171. }
  172. `
  173. // adds 127.0.0.1:4655, a secure server
  174. var configJsonD = `
  175. {
  176. "log_file" : "../../tests/testlog",
  177. "log_level" : "debug",
  178. "pid_file" : "./pidfile.pid",
  179. "allowed_hosts": [
  180. "guerrillamail.com",
  181. "guerrillamailblock.com",
  182. "sharklasers.com",
  183. "guerrillamail.net",
  184. "guerrillamail.org"
  185. ],
  186. "backend_config": {
  187. "save_workers_size" : 1,
  188. "save_process": "HeadersParser|Debugger",
  189. "log_received_mails": false
  190. },
  191. "servers" : [
  192. {
  193. "is_enabled" : true,
  194. "host_name":"mail.test.com",
  195. "max_size": 1000000,
  196. "timeout":180,
  197. "listen_interface":"127.0.0.1:2552",
  198. "max_clients": 200,
  199. "log_file" : "../../tests/testlog",
  200. "tls" : {
  201. "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
  202. "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
  203. "start_tls_on":true,
  204. "tls_always_on":false
  205. }
  206. },
  207. {
  208. "is_enabled" : true,
  209. "host_name":"secure.test.com",
  210. "max_size":1000000,
  211. "timeout":180,
  212. "listen_interface":"127.0.0.1:4655",
  213. "max_clients":200,
  214. "log_file" : "../../tests/testlog",
  215. "tls" : {
  216. "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
  217. "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
  218. "start_tls_on":false,
  219. "tls_always_on":true
  220. }
  221. }
  222. ]
  223. }
  224. `
  225. // adds 127.0.0.1:4655, a secure server
  226. var configJsonE = `
  227. {
  228. "log_file" : "../../tests/testlog",
  229. "log_level" : "debug",
  230. "pid_file" : "./pidfile2.pid",
  231. "allowed_hosts": [
  232. "guerrillamail.com",
  233. "guerrillamailblock.com",
  234. "sharklasers.com",
  235. "guerrillamail.net",
  236. "guerrillamail.org"
  237. ],
  238. "backend_config" :
  239. {
  240. "save_process_old": "HeadersParser|Debugger|Hasher|Header|Compressor|Redis|MySql",
  241. "save_process": "GuerrillaRedisDB",
  242. "log_received_mails" : true,
  243. "sql_driver": "mysql",
  244. "sql_dsn": "root:secret@tcp(127.0.0.1:3306)/gmail_mail?readTimeout=10s&writeTimeout=10s",
  245. "mail_table":"new_mail",
  246. "redis_interface" : "127.0.0.1:6379",
  247. "redis_expire_seconds" : 7200,
  248. "save_workers_size" : 3,
  249. "primary_mail_host":"sharklasers.com"
  250. },
  251. "servers" : [
  252. {
  253. "is_enabled" : true,
  254. "host_name":"mail.test.com",
  255. "max_size": 1000000,
  256. "timeout":180,
  257. "listen_interface":"127.0.0.1:2552",
  258. "max_clients": 200,
  259. "log_file" : "../../tests/testlog",
  260. "tls" : {
  261. "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
  262. "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
  263. "start_tls_on":true,
  264. "tls_always_on":false
  265. }
  266. },
  267. {
  268. "is_enabled" : true,
  269. "host_name":"secure.test.com",
  270. "max_size":1000000,
  271. "timeout":180,
  272. "listen_interface":"127.0.0.1:4655",
  273. "max_clients":200,
  274. "log_file" : "../../tests/testlog",
  275. "tls" : {
  276. "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
  277. "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
  278. "start_tls_on":false,
  279. "tls_always_on":true
  280. }
  281. }
  282. ]
  283. }
  284. `
  285. const testPauseDuration = time.Millisecond * 1010
  286. // reload config
  287. func sigHup() {
  288. if data, err := ioutil.ReadFile("pidfile.pid"); err == nil {
  289. mainlog.Infof("pid read is %s", data)
  290. ecmd := exec.Command("kill", "-HUP", string(data))
  291. _, err = ecmd.Output()
  292. if err != nil {
  293. mainlog.Infof("could not SIGHUP", err)
  294. }
  295. } else {
  296. mainlog.WithError(err).Info("sighup - Could not read pidfle")
  297. }
  298. }
  299. // shutdown after calling serve()
  300. func sigKill() {
  301. if data, err := ioutil.ReadFile("pidfile.pid"); err == nil {
  302. mainlog.Infof("pid read is %s", data)
  303. ecmd := exec.Command("kill", string(data))
  304. _, err = ecmd.Output()
  305. if err != nil {
  306. mainlog.Infof("could not sigkill", err)
  307. }
  308. } else {
  309. mainlog.WithError(err).Info("sigKill - Could not read pidfle")
  310. }
  311. }
  312. func Round(x float64) float64 {
  313. t := math.Trunc(x)
  314. if math.Abs(x-t) >= 0.5 {
  315. return t + math.Copysign(1, x)
  316. }
  317. return t
  318. }
  319. // exponentialBackoff sleeps in nanoseconds, according to this formula 2^(i-1) * 25 / 2
  320. func exponentialBackoff(i int) {
  321. time.Sleep(time.Duration(Round(math.Pow(3.0, float64(i))-1.0)*100.0/2.0) * time.Millisecond)
  322. }
  323. var grepNotFound error
  324. // grepTestlog looks for the `match` string in the testlog
  325. // the lineNumber indicates what line to start the search from
  326. // returns line number it was found on
  327. // error otherwise
  328. //
  329. // It will attempt to search the log multiple times, pausing loner for each re-try
  330. //
  331. func grepTestlog(match string, lineNumber int) (found int, err error) {
  332. found = 0
  333. fd, err := os.Open("../../tests/testlog")
  334. if err != nil {
  335. return found, err
  336. }
  337. defer func() {
  338. _ = fd.Close()
  339. }()
  340. buff := bufio.NewReader(fd)
  341. var ln int
  342. var line string
  343. for tries := 0; tries < 6; tries++ {
  344. //fmt.Println("try..", tries)
  345. for {
  346. ln++
  347. line, err = buff.ReadString('\n')
  348. if err != nil {
  349. break
  350. }
  351. if ln > lineNumber {
  352. //fmt.Print(ln, line)
  353. if i := strings.Index(line, match); i != -1 {
  354. return ln, nil
  355. }
  356. }
  357. }
  358. if err != io.EOF {
  359. return found, err
  360. }
  361. err = fd.Close()
  362. if err != nil {
  363. return 0, err
  364. }
  365. fd = nil
  366. // sleep
  367. exponentialBackoff(tries)
  368. _ = mainlog.Reopen()
  369. // re-open
  370. fd, err = os.OpenFile("../../tests/testlog", os.O_RDONLY, 0644)
  371. if err != nil {
  372. return found, err
  373. }
  374. buff.Reset(fd)
  375. ln = 0
  376. }
  377. grepNotFound = errors.New("could not find " + match + " in tests/testlog after line" + strconv.Itoa(lineNumber))
  378. return found, grepNotFound
  379. }
  380. // In all the tests, there will be a minimum of about 2000 available
  381. func TestFileLimit(t *testing.T) {
  382. cfg := &guerrilla.AppConfig{LogFile: log.OutputOff.String()}
  383. sc := guerrilla.ServerConfig{
  384. ListenInterface: "127.0.0.1:2526",
  385. IsEnabled: true,
  386. MaxClients: 1000,
  387. }
  388. cfg.Servers = append(cfg.Servers, sc)
  389. d := guerrilla.Daemon{Config: cfg}
  390. if ok, maxClients, fileLimit := guerrilla.CheckFileLimit(d.Config); !ok {
  391. t.Errorf("Combined max clients for all servers (%d) is greater than open file limit (%d). "+
  392. "Please increase your open file limit. Please check your OS docs for how to increase the limit.", maxClients, fileLimit)
  393. }
  394. }
  395. func getTestLog() (mainlog log.Logger, err error) {
  396. return log.GetLogger("../../tests/testlog", "debug")
  397. }
  398. func truncateIfExists(filename string) error {
  399. if _, err := os.Stat(filename); !os.IsNotExist(err) {
  400. return os.Truncate(filename, 0)
  401. }
  402. return nil
  403. }
  404. func deleteIfExists(filename string) error {
  405. if _, err := os.Stat(filename); !os.IsNotExist(err) {
  406. return os.Remove(filename)
  407. }
  408. return nil
  409. }
  410. func cleanTestArtifacts(t *testing.T) {
  411. if err := truncateIfExists("../../tests/testlog"); err != nil {
  412. t.Error("could not clean tests/testlog:", err)
  413. }
  414. if err := truncateIfExists("../../tests/testlog2"); err != nil {
  415. t.Error("could not clean tests/testlog2:", err)
  416. }
  417. letters := []byte{'A', 'B', 'C', 'D', 'E'}
  418. for _, l := range letters {
  419. if err := deleteIfExists("configJson" + string(l) + ".json"); err != nil {
  420. t.Error("could not delete configJson"+string(l)+".json:", err)
  421. }
  422. }
  423. if err := deleteIfExists("./pidfile.pid"); err != nil {
  424. t.Error("could not delete ./pidfile.pid", err)
  425. }
  426. if err := deleteIfExists("./pidfile2.pid"); err != nil {
  427. t.Error("could not delete ./pidfile2.pid", err)
  428. }
  429. if err := deleteIfExists("../../tests/mail.guerrillamail.com.cert.pem"); err != nil {
  430. t.Error("could not delete ../../tests/mail.guerrillamail.com.cert.pem", err)
  431. }
  432. if err := deleteIfExists("../../tests/mail.guerrillamail.com.key.pem"); err != nil {
  433. t.Error("could not delete ../../tests/mail.guerrillamail.com.key.pem", err)
  434. }
  435. if err := deleteIfExists("../../tests/mail2.guerrillamail.com.cert.pem"); err != nil {
  436. t.Error("could not delete ../../tests/mail2.guerrillamail.com.cert.pem", err)
  437. }
  438. if err := deleteIfExists("../../tests/mail2.guerrillamail.com.key.pem"); err != nil {
  439. t.Error("could not delete ../../tests/mail2.guerrillamail.com.key.pem", err)
  440. }
  441. }
  442. // make sure that we get all the config change events
  443. func TestCmdConfigChangeEvents(t *testing.T) {
  444. defer cleanTestArtifacts(t)
  445. var err error
  446. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  447. if err != nil {
  448. t.Error("failed to generate a test certificate", err)
  449. t.FailNow()
  450. }
  451. oldconf := &guerrilla.AppConfig{}
  452. if err := oldconf.Load([]byte(configJsonA)); err != nil {
  453. t.Error("configJsonA is invalid", err)
  454. }
  455. newconf := &guerrilla.AppConfig{}
  456. if err := newconf.Load([]byte(configJsonB)); err != nil {
  457. t.Error("configJsonB is invalid", err)
  458. }
  459. newerconf := &guerrilla.AppConfig{}
  460. if err := newerconf.Load([]byte(configJsonC)); err != nil {
  461. t.Error("configJsonC is invalid", err)
  462. }
  463. expectedEvents := map[guerrilla.Event]bool{
  464. guerrilla.EventConfigBackendConfig: false,
  465. guerrilla.EventConfigServerNew: false,
  466. }
  467. mainlog, err = getTestLog()
  468. if err != nil {
  469. t.Error("could not get logger,", err)
  470. t.FailNow()
  471. }
  472. bcfg := backends.BackendConfig{"log_received_mails": true}
  473. backend, err := backends.New(bcfg, mainlog)
  474. app, err := guerrilla.New(oldconf, backend, mainlog)
  475. if err != nil {
  476. t.Error("Failed to create new app", err)
  477. }
  478. toUnsubscribe := map[guerrilla.Event]func(c *guerrilla.AppConfig){}
  479. toUnsubscribeS := map[guerrilla.Event]func(c *guerrilla.ServerConfig){}
  480. for event := range expectedEvents {
  481. // Put in anon func since range is overwriting event
  482. func(e guerrilla.Event) {
  483. if strings.Index(e.String(), "server_change") == 0 {
  484. f := func(c *guerrilla.ServerConfig) {
  485. expectedEvents[e] = true
  486. }
  487. _ = app.Subscribe(e, f)
  488. toUnsubscribeS[e] = f
  489. } else {
  490. f := func(c *guerrilla.AppConfig) {
  491. expectedEvents[e] = true
  492. }
  493. _ = app.Subscribe(e, f)
  494. toUnsubscribe[e] = f
  495. }
  496. }(event)
  497. }
  498. // emit events
  499. newconf.EmitChangeEvents(oldconf, app)
  500. newerconf.EmitChangeEvents(newconf, app)
  501. // unsubscribe
  502. for unevent, unfun := range toUnsubscribe {
  503. _ = app.Unsubscribe(unevent, unfun)
  504. }
  505. for unevent, unfun := range toUnsubscribeS {
  506. _ = app.Unsubscribe(unevent, unfun)
  507. }
  508. for event, val := range expectedEvents {
  509. if val == false {
  510. t.Error("Did not fire config change event:", event)
  511. t.FailNow()
  512. }
  513. }
  514. }
  515. // start server, change config, send SIG HUP, confirm that the pidfile changed & backend reloaded
  516. func TestServe(t *testing.T) {
  517. defer cleanTestArtifacts(t)
  518. var err error
  519. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  520. if err != nil {
  521. t.Error("failed to generate a test certificate", err)
  522. t.FailNow()
  523. }
  524. mainlog, err = getTestLog()
  525. if err != nil {
  526. t.Error("could not get logger,", err)
  527. t.FailNow()
  528. }
  529. if err := ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644); err != nil {
  530. t.Error(err)
  531. t.FailNow()
  532. }
  533. cmd := &cobra.Command{}
  534. configPath = "configJsonA.json"
  535. go func() {
  536. serve(cmd, []string{})
  537. }()
  538. if _, err := grepTestlog("istening on TCP 127.0.0.1:3536", 0); err != nil {
  539. t.Error("server not started")
  540. }
  541. data, err := ioutil.ReadFile("pidfile.pid")
  542. if err != nil {
  543. t.Error("error reading pidfile.pid", err)
  544. t.FailNow()
  545. }
  546. _, err = strconv.Atoi(string(data))
  547. if err != nil {
  548. t.Error("could not parse pidfile.pid", err)
  549. t.FailNow()
  550. }
  551. // change the config file
  552. err = ioutil.WriteFile("configJsonA.json", []byte(configJsonB), 0644)
  553. if err != nil {
  554. t.Error(err)
  555. t.FailNow()
  556. }
  557. // test SIGHUP via the kill command
  558. // Would not work on windows as kill is not available.
  559. // TODO: Implement an alternative test for windows.
  560. if runtime.GOOS != "windows" {
  561. sigHup()
  562. // did the pidfile change as expected?
  563. if _, err := grepTestlog("Configuration was reloaded", 0); err != nil {
  564. t.Error("server did not catch sighp")
  565. }
  566. }
  567. // send kill signal and wait for exit
  568. d.Shutdown()
  569. // did backend started as expected?
  570. if _, err := grepTestlog("new backend started", 0); err != nil {
  571. t.Error("Dummy backend not restarted")
  572. }
  573. // wait for shutdown
  574. if _, err := grepTestlog("Backend shutdown completed", 0); err != nil {
  575. t.Error("server didn't stop")
  576. }
  577. }
  578. // Start with configJsonA.json,
  579. // then add a new server to it (127.0.0.1:2526),
  580. // then SIGHUP (to reload config & trigger config update events),
  581. // then connect to it & HELO.
  582. func TestServerAddEvent(t *testing.T) {
  583. var err error
  584. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  585. if err != nil {
  586. t.Error("failed to generate a test certificate", err)
  587. t.FailNow()
  588. }
  589. defer cleanTestArtifacts(t)
  590. mainlog, err = getTestLog()
  591. if err != nil {
  592. t.Error("could not get logger,", err)
  593. t.FailNow()
  594. }
  595. // start the server by emulating the serve command
  596. if err := ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644); err != nil {
  597. t.Error(err)
  598. t.FailNow()
  599. }
  600. cmd := &cobra.Command{}
  601. configPath = "configJsonA.json"
  602. go func() {
  603. serve(cmd, []string{})
  604. }()
  605. // allow the server to start
  606. if _, err := grepTestlog("Listening on TCP 127.0.0.1:3536", 0); err != nil {
  607. t.Error("server didn't start")
  608. }
  609. // now change the config by adding a server
  610. conf := &guerrilla.AppConfig{} // blank one
  611. err = conf.Load([]byte(configJsonA)) // load configJsonA
  612. if err != nil {
  613. t.Error(err)
  614. }
  615. newServer := conf.Servers[0] // copy the first server config
  616. newServer.ListenInterface = "127.0.0.1:2526" // change it
  617. newConf := conf // copy the cmdConfg
  618. newConf.Servers = append(newConf.Servers, newServer) // add the new server
  619. if jsonbytes, err := json.Marshal(newConf); err == nil {
  620. if err := ioutil.WriteFile("configJsonA.json", jsonbytes, 0644); err != nil {
  621. t.Error(err)
  622. }
  623. }
  624. // send a sighup signal to the server
  625. sigHup()
  626. if _, err := grepTestlog("[127.0.0.1:2526] Waiting for a new client", 0); err != nil {
  627. t.Error("new server didn't start")
  628. }
  629. if conn, buffin, err := test.Connect(newServer, 20); err != nil {
  630. t.Error("Could not connect to new server", newServer.ListenInterface, err)
  631. } else {
  632. if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
  633. expect := "250 mail.test.com Hello"
  634. if strings.Index(result, expect) != 0 {
  635. t.Error("Expected", expect, "but got", result)
  636. }
  637. } else {
  638. t.Error(err)
  639. }
  640. }
  641. // shutdown the server
  642. d.Shutdown()
  643. // did backend started as expected?
  644. if _, err := grepTestlog("New server added [127.0.0.1:2526]", 0); err != nil {
  645. t.Error("Did not add server [127.0.0.1:2526] after sighup")
  646. }
  647. if _, err := grepTestlog("Backend shutdown completed", 0); err != nil {
  648. t.Error("Server failed to stop")
  649. }
  650. }
  651. // Start with configJsonA.json,
  652. // then change the config to enable 127.0.0.1:2228,
  653. // then write the new config,
  654. // then SIGHUP (to reload config & trigger config update events),
  655. // then connect to 127.0.0.1:2228 & HELO.
  656. func TestServerStartEvent(t *testing.T) {
  657. var err error
  658. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  659. if err != nil {
  660. t.Error("failed to generate a test certificate", err)
  661. t.FailNow()
  662. }
  663. defer cleanTestArtifacts(t)
  664. mainlog, err = getTestLog()
  665. if err != nil {
  666. t.Error("could not get logger,", err)
  667. t.FailNow()
  668. }
  669. if err := ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644); err != nil {
  670. t.Error(err)
  671. t.FailNow()
  672. }
  673. cmd := &cobra.Command{}
  674. configPath = "configJsonA.json"
  675. go func() {
  676. serve(cmd, []string{})
  677. }()
  678. if _, err := grepTestlog("Listening on TCP 127.0.0.1:3536", 0); err != nil {
  679. t.Error("server didn't start")
  680. }
  681. // now change the config by adding a server
  682. conf := &guerrilla.AppConfig{} // blank one
  683. if err = conf.Load([]byte(configJsonA)); err != nil { // load configJsonA
  684. t.Error(err)
  685. }
  686. newConf := conf // copy the cmdConfg
  687. newConf.Servers[1].IsEnabled = true
  688. if jsonbytes, err := json.Marshal(newConf); err == nil {
  689. //fmt.Println(string(jsonbytes))
  690. if err = ioutil.WriteFile("configJsonA.json", jsonbytes, 0644); err != nil {
  691. t.Error(err)
  692. }
  693. } else {
  694. t.Error(err)
  695. }
  696. // send a sighup signal to the server
  697. sigHup()
  698. // see if the new server started?
  699. if _, err := grepTestlog("Listening on TCP 127.0.0.1:2228", 0); err != nil {
  700. t.Error("second server didn't start")
  701. }
  702. // can we talk to it?
  703. if conn, buffin, err := test.Connect(newConf.Servers[1], 20); err != nil {
  704. t.Error("Could not connect to new server", newConf.Servers[1].ListenInterface)
  705. } else {
  706. if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
  707. expect := "250 enable.test.com Hello"
  708. if strings.Index(result, expect) != 0 {
  709. t.Error("Expected", expect, "but got", result)
  710. }
  711. } else {
  712. t.Error(err)
  713. }
  714. }
  715. // shutdown and wait for exit
  716. d.Shutdown()
  717. if _, err := grepTestlog("Backend shutdown completed", 0); err != nil {
  718. t.Error("server didn't stop")
  719. }
  720. }
  721. // Start with configJsonA.json,
  722. // then change the config to enable 127.0.0.1:2228,
  723. // then write the new config,
  724. // then SIGHUP (to reload config & trigger config update events),
  725. // then connect to 127.0.0.1:2228 & HELO.
  726. // then change the config to dsiable 127.0.0.1:2228,
  727. // then SIGHUP (to reload config & trigger config update events),
  728. // then connect to 127.0.0.1:2228 - it should not connect
  729. func TestServerStopEvent(t *testing.T) {
  730. var err error
  731. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  732. if err != nil {
  733. t.Error("failed to generate a test certificate", err)
  734. t.FailNow()
  735. }
  736. defer cleanTestArtifacts(t)
  737. mainlog, err = getTestLog()
  738. if err != nil {
  739. t.Error("could not get logger,", err)
  740. t.FailNow()
  741. }
  742. if err := ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644); err != nil {
  743. t.Error(err)
  744. t.FailNow()
  745. }
  746. cmd := &cobra.Command{}
  747. configPath = "configJsonA.json"
  748. go func() {
  749. serve(cmd, []string{})
  750. }()
  751. // allow the server to start
  752. if _, err := grepTestlog("Listening on TCP 127.0.0.1:3536", 0); err != nil {
  753. t.Error("server didn't start")
  754. }
  755. // now change the config by enabling a server
  756. conf := &guerrilla.AppConfig{} // blank one
  757. if err = conf.Load([]byte(configJsonA)); err != nil { // load configJsonA
  758. t.Error(err)
  759. }
  760. newConf := conf // copy the cmdConfg
  761. newConf.Servers[1].IsEnabled = true // enable 2nd server
  762. if jsonbytes, err := json.Marshal(newConf); err == nil {
  763. //fmt.Println(string(jsonbytes))
  764. if err = ioutil.WriteFile("configJsonA.json", jsonbytes, 0644); err != nil {
  765. t.Error(err)
  766. }
  767. } else {
  768. t.Error(err)
  769. }
  770. // send a sighup signal to the server
  771. sigHup()
  772. // detect config change
  773. if _, err := grepTestlog("Listening on TCP 127.0.0.1:2228", 0); err != nil {
  774. t.Error("new server didn't start")
  775. }
  776. if conn, buffin, err := test.Connect(newConf.Servers[1], 20); err != nil {
  777. t.Error("Could not connect to new server", newConf.Servers[1].ListenInterface)
  778. } else {
  779. if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
  780. expect := "250 enable.test.com Hello"
  781. if strings.Index(result, expect) != 0 {
  782. t.Error("Expected", expect, "but got", result)
  783. }
  784. } else {
  785. t.Error(err)
  786. }
  787. if err = conn.Close(); err != nil {
  788. t.Error(err)
  789. }
  790. }
  791. // now disable the server
  792. newerConf := newConf // copy the cmdConfg
  793. newerConf.Servers[1].IsEnabled = false
  794. if jsonbytes, err := json.Marshal(newerConf); err == nil {
  795. //fmt.Println(string(jsonbytes))
  796. if err = ioutil.WriteFile("configJsonA.json", jsonbytes, 0644); err != nil {
  797. t.Error(err)
  798. }
  799. } else {
  800. t.Error(err)
  801. }
  802. // send a sighup signal to the server
  803. sigHup()
  804. // detect config change
  805. if _, err := grepTestlog("Server [127.0.0.1:2228] has stopped accepting new clients", 27); err != nil {
  806. t.Error("127.0.0.1:2228 did not stop")
  807. }
  808. // it should not connect to the server
  809. if _, _, err := test.Connect(newConf.Servers[1], 20); err == nil {
  810. t.Error("127.0.0.1:2228 was disabled, but still accepting connections", newConf.Servers[1].ListenInterface)
  811. }
  812. // shutdown wait for exit
  813. d.Shutdown()
  814. // wait for shutdown
  815. if _, err := grepTestlog("Backend shutdown completed", 0); err != nil {
  816. t.Error("server didn't stop")
  817. }
  818. }
  819. // just a utility for debugging when using the debugger, skipped by default
  820. func TestDebug(t *testing.T) {
  821. t.SkipNow()
  822. conf := guerrilla.ServerConfig{ListenInterface: "127.0.0.1:2526"}
  823. if conn, buffin, err := test.Connect(conf, 20); err != nil {
  824. t.Error("Could not connect to new server", conf.ListenInterface, err)
  825. } else {
  826. if result, err := test.Command(conn, buffin, "HELO"); err == nil {
  827. expect := "250 mai1.guerrillamail.com Hello"
  828. if strings.Index(result, expect) != 0 {
  829. t.Error("Expected", expect, "but got", result)
  830. } else {
  831. if result, err = test.Command(conn, buffin, "RCPT TO:<[email protected]>"); err == nil {
  832. expect := "250 2.1.5 OK"
  833. if strings.Index(result, expect) != 0 {
  834. t.Error("Expected:", expect, "but got:", result)
  835. }
  836. }
  837. }
  838. }
  839. _ = conn.Close()
  840. }
  841. }
  842. // Start with configJsonD.json,
  843. // then connect to 127.0.0.1:4655 & HELO & try RCPT TO with an invalid host [grr.la]
  844. // then change the config to enable add new host [grr.la] to allowed_hosts
  845. // then write the new config,
  846. // then SIGHUP (to reload config & trigger config update events),
  847. // connect to 127.0.0.1:4655 & HELO & try RCPT TO, grr.la should work
  848. func TestAllowedHostsEvent(t *testing.T) {
  849. var err error
  850. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  851. if err != nil {
  852. t.Error("failed to generate a test certificate", err)
  853. t.FailNow()
  854. }
  855. defer cleanTestArtifacts(t)
  856. mainlog, err = getTestLog()
  857. if err != nil {
  858. t.Error("could not get logger,", err)
  859. t.FailNow()
  860. }
  861. if err := ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
  862. t.Error(err)
  863. t.FailNow()
  864. }
  865. // start the server by emulating the serve command
  866. conf := &guerrilla.AppConfig{} // blank one
  867. if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
  868. t.Error(err)
  869. }
  870. cmd := &cobra.Command{}
  871. configPath = "configJsonD.json"
  872. go func() {
  873. serve(cmd, []string{})
  874. }()
  875. // wait for start
  876. if _, err := grepTestlog("Listening on TCP 127.0.0.1:2552", 0); err != nil {
  877. t.Error("server didn't start")
  878. }
  879. // now connect and try RCPT TO with an invalid host
  880. if conn, buffin, err := test.Connect(conf.Servers[1], 20); err != nil {
  881. t.Error("Could not connect to new server", conf.Servers[1].ListenInterface, err)
  882. } else {
  883. if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
  884. expect := "250 secure.test.com Hello"
  885. if strings.Index(result, expect) != 0 {
  886. t.Error("Expected", expect, "but got", result)
  887. } else {
  888. if result, err = test.Command(conn, buffin, "RCPT TO:<[email protected]>"); err == nil {
  889. expect := "454 4.1.1 Error: Relay access denied: grr.la"
  890. if strings.Index(result, expect) != 0 {
  891. t.Error("Expected:", expect, "but got:", result)
  892. }
  893. }
  894. }
  895. }
  896. _ = conn.Close()
  897. }
  898. // now change the config by adding a host to allowed hosts
  899. newConf := conf
  900. newConf.AllowedHosts = append(newConf.AllowedHosts, "grr.la")
  901. if jsonbytes, err := json.Marshal(newConf); err == nil {
  902. if err = ioutil.WriteFile("configJsonD.json", jsonbytes, 0644); err != nil {
  903. t.Error(err)
  904. }
  905. } else {
  906. t.Error(err)
  907. }
  908. // send a sighup signal to the server to reload config
  909. sigHup()
  910. if _, err := grepTestlog("allowed_hosts config changed", 0); err != nil {
  911. t.Error("allowed_hosts config not changed")
  912. t.FailNow()
  913. }
  914. // now repeat the same conversion, RCPT TO should be accepted
  915. if conn, buffin, err := test.Connect(conf.Servers[1], 20); err != nil {
  916. t.Error("Could not connect to new server", conf.Servers[1].ListenInterface, err)
  917. } else {
  918. if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
  919. expect := "250 secure.test.com Hello"
  920. if strings.Index(result, expect) != 0 {
  921. t.Error("Expected", expect, "but got", result)
  922. } else {
  923. if result, err = test.Command(conn, buffin, "RCPT TO:<[email protected]>"); err == nil {
  924. expect := "250 2.1.5 OK"
  925. if strings.Index(result, expect) != 0 {
  926. t.Error("Expected:", expect, "but got:", result)
  927. }
  928. }
  929. }
  930. }
  931. _ = conn.Close()
  932. }
  933. // shutdown wait for exit
  934. d.Shutdown()
  935. // wait for shutdown
  936. if _, err := grepTestlog("Backend shutdown completed", 0); err != nil {
  937. t.Error("server didn't stop")
  938. }
  939. }
  940. // Test TLS config change event
  941. // start with configJsonD
  942. // should be able to STARTTLS to 127.0.0.1:2525 with no problems
  943. // generate new certs & reload config
  944. // should get a new tls event & able to STARTTLS with no problem
  945. func TestTLSConfigEvent(t *testing.T) {
  946. var err error
  947. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  948. if err != nil {
  949. t.Error("failed to generate a test certificate", err)
  950. t.FailNow()
  951. }
  952. defer cleanTestArtifacts(t)
  953. mainlog, err = getTestLog()
  954. if err != nil {
  955. t.Error("could not get logger,", err)
  956. t.FailNow()
  957. }
  958. if err := ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
  959. t.Error(err)
  960. t.FailNow()
  961. }
  962. conf := &guerrilla.AppConfig{} // blank one
  963. if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
  964. t.Error(err)
  965. t.FailNow()
  966. }
  967. cmd := &cobra.Command{}
  968. configPath = "configJsonD.json"
  969. go func() {
  970. serve(cmd, []string{})
  971. }()
  972. // wait for server to start
  973. if _, err := grepTestlog("Listening on TCP 127.0.0.1:2552", 0); err != nil {
  974. t.Error("server didn't start")
  975. }
  976. // Test STARTTLS handshake
  977. testTlsHandshake := func() {
  978. if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
  979. t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
  980. } else {
  981. if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
  982. expect := "250 mail.test.com Hello"
  983. if strings.Index(result, expect) != 0 {
  984. t.Error("Expected", expect, "but got", result)
  985. } else {
  986. if result, err = test.Command(conn, buffin, "STARTTLS"); err == nil {
  987. expect := "220 2.0.0 Ready to start TLS"
  988. if strings.Index(result, expect) != 0 {
  989. t.Error("Expected:", expect, "but got:", result)
  990. } else {
  991. tlsConn := tls.Client(conn, &tls.Config{
  992. InsecureSkipVerify: true,
  993. ServerName: "127.0.0.1",
  994. })
  995. if err := tlsConn.Handshake(); err != nil {
  996. t.Error("Failed to handshake", conf.Servers[0].ListenInterface)
  997. } else {
  998. conn = tlsConn
  999. mainlog.Info("TLS Handshake succeeded")
  1000. }
  1001. }
  1002. }
  1003. }
  1004. }
  1005. _ = conn.Close()
  1006. }
  1007. }
  1008. testTlsHandshake()
  1009. // TLS Handshake succeeded?
  1010. if _, err := grepTestlog("TLS Handshake succeeded", 0); err != nil {
  1011. t.Error("TLS Handshake did not succeed")
  1012. t.FailNow()
  1013. }
  1014. // now delete old certs, configure new certs, and send a sighup to load them in
  1015. if err := deleteIfExists("../../tests/mail2.guerrillamail.com.cert.pem"); err != nil {
  1016. t.Error("could not delete ../../tests/mail2.guerrillamail.com.cert.pem", err)
  1017. }
  1018. if err := deleteIfExists("../../tests/mail2.guerrillamail.com.key.pem"); err != nil {
  1019. t.Error("could not delete ../../tests/mail2.guerrillamail.com.key.pem", err)
  1020. }
  1021. time.Sleep(testPauseDuration) // need to pause so that the new certs have different timestamps!
  1022. // generate a new cert
  1023. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  1024. if err != nil {
  1025. t.Error("failed to generate a test certificate", err)
  1026. t.FailNow()
  1027. }
  1028. // pause for generated cert to output (don't need, since we've fsynced)
  1029. // time.Sleep(testPauseDuration) // (don't need, since we've fsynced)
  1030. // did cert output?
  1031. if _, err := os.Stat("../../tests/mail2.guerrillamail.com.cert.pem"); err != nil {
  1032. t.Error("Did not create cert ", err)
  1033. }
  1034. sigHup()
  1035. // wait for config to reload
  1036. if _, err := grepTestlog("Server [127.0.0.1:4655] re-opened", 0); err != nil {
  1037. t.Error("server didn't catch sighup")
  1038. }
  1039. // did tls configuration reload as expected?
  1040. if _, err := grepTestlog("new TLS configuration loaded", 0); err != nil {
  1041. t.Error("server didn't catch sighup")
  1042. }
  1043. // test again
  1044. testTlsHandshake()
  1045. // after line 25
  1046. if _, err := grepTestlog("TLS Handshake succeeded", 25); err != nil {
  1047. t.Error("TLS Handshake did not succeed")
  1048. t.FailNow()
  1049. }
  1050. d.Shutdown()
  1051. // wait for shutdown
  1052. if _, err := grepTestlog("Backend shutdown completed", 0); err != nil {
  1053. t.Error("server didn't stop")
  1054. }
  1055. }
  1056. // Testing starting a server with a bad TLS config
  1057. // It should not start, return exit code 1
  1058. func TestBadTLSStart(t *testing.T) {
  1059. var err error
  1060. mainlog, err = getTestLog()
  1061. if err != nil {
  1062. t.Error("could not get logger,", err)
  1063. t.FailNow()
  1064. }
  1065. // Need to run the test in a different process by executing a command
  1066. // because the serve() does os.Exit when starting with a bad TLS config
  1067. if os.Getenv("BE_CRASHER") == "1" {
  1068. // do the test
  1069. // first, remove the good certs, if any
  1070. if err := deleteIfExists("../../tests/mail2.guerrillamail.com.cert.pem"); err != nil {
  1071. t.Error("could not delete ../../tests/mail2.guerrillamail.com.cert.pem", err)
  1072. }
  1073. if err := deleteIfExists("../../tests/mail2.guerrillamail.com.key.pem"); err != nil {
  1074. t.Error("could not delete ../../tests/mail2.guerrillamail.com.key.pem", err)
  1075. }
  1076. // next run the server
  1077. if err = ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
  1078. t.Error(err)
  1079. }
  1080. conf := &guerrilla.AppConfig{} // blank one
  1081. if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
  1082. t.Error(err)
  1083. }
  1084. cmd := &cobra.Command{}
  1085. configPath = "configJsonD.json"
  1086. var serveWG sync.WaitGroup
  1087. serveWG.Add(1)
  1088. go func() {
  1089. serve(cmd, []string{})
  1090. serveWG.Done()
  1091. }()
  1092. // it should exit by now because the TLS config is incorrect
  1093. time.Sleep(testPauseDuration)
  1094. sigKill()
  1095. serveWG.Wait()
  1096. return
  1097. }
  1098. defer cleanTestArtifacts(t)
  1099. cmd := exec.Command(os.Args[0], "-test.run=TestBadTLSStart")
  1100. cmd.Env = append(os.Environ(), "BE_CRASHER=1")
  1101. err = cmd.Run()
  1102. if e, ok := err.(*exec.ExitError); ok && !e.Success() {
  1103. if _, err := grepTestlog("level=fatal", 0); err != nil {
  1104. t.Error("server didn't exit with a fatal error")
  1105. }
  1106. return
  1107. }
  1108. t.Error("Server started with a bad TLS config, was expecting exit status 0")
  1109. }
  1110. // Test config reload with a bad TLS config
  1111. // It should ignore the config reload, keep running with old settings
  1112. func TestBadTLSReload(t *testing.T) {
  1113. var err error
  1114. mainlog, err = getTestLog()
  1115. if err != nil {
  1116. t.Error("could not get logger,", err)
  1117. t.FailNow()
  1118. }
  1119. defer cleanTestArtifacts(t)
  1120. // start with a good cert
  1121. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  1122. if err != nil {
  1123. t.Error("failed to generate a test certificate", err)
  1124. t.FailNow()
  1125. }
  1126. // start the server by emulating the serve command
  1127. if err = ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
  1128. t.Error(err)
  1129. t.FailNow()
  1130. }
  1131. conf := &guerrilla.AppConfig{} // blank one
  1132. if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
  1133. t.Error(err)
  1134. t.FailNow()
  1135. }
  1136. cmd := &cobra.Command{}
  1137. configPath = "configJsonD.json"
  1138. go func() {
  1139. serve(cmd, []string{})
  1140. }()
  1141. // wait for server to start
  1142. if _, err := grepTestlog("Listening on TCP 127.0.0.1:4655", 0); err != nil {
  1143. t.Error("server didn't start")
  1144. }
  1145. if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
  1146. t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
  1147. } else {
  1148. if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
  1149. expect := "250 mail.test.com Hello"
  1150. if strings.Index(result, expect) != 0 {
  1151. t.Error("Expected", expect, "but got", result)
  1152. }
  1153. }
  1154. }
  1155. // write some trash data
  1156. if err = ioutil.WriteFile("./../../tests/mail2.guerrillamail.com.cert.pem",
  1157. []byte("trash data"),
  1158. 0664); err != nil {
  1159. t.Error(err)
  1160. }
  1161. if err = ioutil.WriteFile("./../../tests/mail2.guerrillamail.com.key.pem",
  1162. []byte("trash data"),
  1163. 0664); err != nil {
  1164. t.Error(err)
  1165. }
  1166. newConf := conf // copy the cmdConfg
  1167. if jsonbytes, err := json.Marshal(newConf); err == nil {
  1168. if err = ioutil.WriteFile("configJsonD.json", jsonbytes, 0644); err != nil {
  1169. t.Error(err)
  1170. }
  1171. } else {
  1172. t.Error(err)
  1173. }
  1174. // send a sighup signal to the server to reload config
  1175. sigHup()
  1176. // did the config reload reload event fire? There should be config read error
  1177. if _, err := grepTestlog("could not read config file", 0); err != nil {
  1178. t.Error("was expecting an error reading config")
  1179. }
  1180. // we should still be able to to talk to it
  1181. if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
  1182. t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
  1183. } else {
  1184. if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
  1185. expect := "250 mail.test.com Hello"
  1186. if strings.Index(result, expect) != 0 {
  1187. t.Error("Expected", expect, "but got", result)
  1188. }
  1189. }
  1190. }
  1191. // shutdown & wait for exit
  1192. d.Shutdown()
  1193. // wait for shutdown
  1194. if _, err := grepTestlog("Backend shutdown completed", 0); err != nil {
  1195. t.Error("server didn't stop")
  1196. }
  1197. }
  1198. // Test for when the server config Timeout value changes
  1199. // Start with configJsonD.json
  1200. func TestSetTimeoutEvent(t *testing.T) {
  1201. var err error
  1202. mainlog, err = getTestLog()
  1203. if err != nil {
  1204. t.Error("could not get logger,", err)
  1205. t.FailNow()
  1206. }
  1207. defer cleanTestArtifacts(t)
  1208. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  1209. if err != nil {
  1210. t.Error("failed to generate a test certificate", err)
  1211. t.FailNow()
  1212. }
  1213. // start the server by emulating the serve command
  1214. if err = ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
  1215. t.Error(err)
  1216. }
  1217. conf := &guerrilla.AppConfig{} // blank one
  1218. if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
  1219. t.Error(err)
  1220. }
  1221. cmd := &cobra.Command{}
  1222. configPath = "configJsonD.json"
  1223. go func() {
  1224. serve(cmd, []string{})
  1225. }()
  1226. // wait for start
  1227. if _, err := grepTestlog("Listening on TCP 127.0.0.1:4655", 0); err != nil {
  1228. t.Error("server didn't start")
  1229. }
  1230. // set the timeout to 1 second
  1231. newConf := conf // copy the cmdConfg
  1232. newConf.Servers[0].Timeout = 1
  1233. if jsonbytes, err := json.Marshal(newConf); err == nil {
  1234. if err = ioutil.WriteFile("configJsonD.json", jsonbytes, 0644); err != nil {
  1235. t.Error(err)
  1236. }
  1237. } else {
  1238. t.Error(err)
  1239. }
  1240. // send a sighup signal to the server to reload config
  1241. sigHup()
  1242. // did config update?
  1243. if _, err := grepTestlog("a new config has been saved", 0); err != nil {
  1244. t.Error("config didn't update")
  1245. }
  1246. var waitTimeout sync.WaitGroup
  1247. if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
  1248. t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
  1249. } else {
  1250. waitTimeout.Add(1)
  1251. go func() {
  1252. if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
  1253. expect := "250 mail.test.com Hello"
  1254. if strings.Index(result, expect) != 0 {
  1255. t.Error("Expected", expect, "but got", result)
  1256. } else {
  1257. b := make([]byte, 1024)
  1258. _, _ = conn.Read(b)
  1259. }
  1260. }
  1261. waitTimeout.Done()
  1262. }()
  1263. }
  1264. // wait for timeout
  1265. waitTimeout.Wait()
  1266. d.Shutdown()
  1267. // wait for shutdown
  1268. if _, err := grepTestlog("Backend shutdown completed", 0); err != nil {
  1269. t.Error("server didn't stop")
  1270. }
  1271. // so the connection we have opened should timeout by now
  1272. // did we get timeout as expected?
  1273. if _, err := grepTestlog("i/o timeout", 0); err != nil {
  1274. t.Error("it looks like the timeout config didnt change")
  1275. t.FailNow()
  1276. }
  1277. }
  1278. // Test debug level config change
  1279. // Start in log_level = debug
  1280. // Load config & start server
  1281. func TestDebugLevelChange(t *testing.T) {
  1282. var err error
  1283. mainlog, err = getTestLog()
  1284. if err != nil {
  1285. t.Error("could not get logger,", err)
  1286. t.FailNow()
  1287. }
  1288. defer cleanTestArtifacts(t)
  1289. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  1290. if err != nil {
  1291. t.Error("failed to generate a test certificate", err)
  1292. t.FailNow()
  1293. } // start the server by emulating the serve command
  1294. if err = ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
  1295. t.Error(err)
  1296. }
  1297. conf := &guerrilla.AppConfig{} // blank one
  1298. if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
  1299. t.Error(err)
  1300. }
  1301. conf.LogLevel = "debug"
  1302. cmd := &cobra.Command{}
  1303. configPath = "configJsonD.json"
  1304. go func() {
  1305. serve(cmd, []string{})
  1306. }()
  1307. if _, err := grepTestlog("Listening on TCP 127.0.0.1:2552", 0); err != nil {
  1308. t.Error("server didn't start")
  1309. }
  1310. if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
  1311. t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
  1312. } else {
  1313. if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
  1314. expect := "250 mail.test.com Hello"
  1315. if strings.Index(result, expect) != 0 {
  1316. t.Error("Expected", expect, "but got", result)
  1317. }
  1318. }
  1319. _ = conn.Close()
  1320. }
  1321. // set the log_level to info
  1322. newConf := conf // copy the cmdConfg
  1323. newConf.LogLevel = log.InfoLevel.String()
  1324. if jsonbytes, err := json.Marshal(newConf); err == nil {
  1325. if err = ioutil.WriteFile("configJsonD.json", jsonbytes, 0644); err != nil {
  1326. t.Error(err)
  1327. }
  1328. } else {
  1329. t.Error(err)
  1330. }
  1331. // send a sighup signal to the server to reload config
  1332. sigHup()
  1333. // did the config reload?
  1334. if _, err := grepTestlog("Configuration was reloaded", 0); err != nil {
  1335. t.Error("config did not reload")
  1336. t.FailNow()
  1337. }
  1338. // connect again, this time we should see info
  1339. if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
  1340. t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
  1341. } else {
  1342. if result, err := test.Command(conn, buffin, "NOOP"); err == nil {
  1343. expect := "200 2.0.0 OK"
  1344. if strings.Index(result, expect) != 0 {
  1345. t.Error("Expected", expect, "but got", result)
  1346. }
  1347. }
  1348. _ = conn.Close()
  1349. }
  1350. d.Shutdown()
  1351. // did the log level change to info?
  1352. if _, err := grepTestlog("log level changed to [info]", 0); err != nil {
  1353. t.Error("log level did not change to [info]")
  1354. t.FailNow()
  1355. }
  1356. }
  1357. // When reloading with a bad backend config, it should revert to old backend config
  1358. func TestBadBackendReload(t *testing.T) {
  1359. var err error
  1360. err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
  1361. if err != nil {
  1362. t.Error("failed to generate a test certificate", err)
  1363. t.FailNow()
  1364. }
  1365. defer cleanTestArtifacts(t)
  1366. mainlog, err = getTestLog()
  1367. if err != nil {
  1368. t.Error("could not get logger,", err)
  1369. t.FailNow()
  1370. }
  1371. if err = ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644); err != nil {
  1372. t.Error(err)
  1373. }
  1374. cmd := &cobra.Command{}
  1375. configPath = "configJsonA.json"
  1376. go func() {
  1377. serve(cmd, []string{})
  1378. }()
  1379. if _, err := grepTestlog("Listening on TCP 127.0.0.1:3536", 0); err != nil {
  1380. t.Error("server didn't start")
  1381. }
  1382. // change the config file to the one with a broken backend
  1383. if err = ioutil.WriteFile("configJsonA.json", []byte(configJsonE), 0644); err != nil {
  1384. t.Error(err)
  1385. }
  1386. // test SIGHUP via the kill command
  1387. // Would not work on windows as kill is not available.
  1388. // TODO: Implement an alternative test for windows.
  1389. if runtime.GOOS != "windows" {
  1390. sigHup()
  1391. // did config update?
  1392. if _, err := grepTestlog("Configuration was reloaded", 0); err != nil {
  1393. t.Error("config didn't update")
  1394. }
  1395. // did the pidfile change as expected?
  1396. if _, err := grepTestlog("pid_file (./pidfile2.pid) written", 0); err != nil {
  1397. t.Error("pid_file (./pidfile2.pid) not written")
  1398. }
  1399. if _, err := os.Stat("./pidfile2.pid"); os.IsNotExist(err) {
  1400. t.Error("pidfile not changed after sighup SIGHUP", err)
  1401. }
  1402. }
  1403. // send kill signal and wait for exit
  1404. d.Shutdown()
  1405. // did backend started as expected?
  1406. if _, err := grepTestlog("reverted to old backend config", 0); err != nil {
  1407. t.Error("did not revert to old backend config")
  1408. t.FailNow()
  1409. }
  1410. }