123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656 |
- package main
- import (
- "bufio"
- "crypto/tls"
- "encoding/json"
- "errors"
- "io"
- "io/ioutil"
- "math"
- "os"
- "os/exec"
- "runtime"
- "strconv"
- "strings"
- "sync"
- "testing"
- "time"
- "github.com/flashmob/go-guerrilla"
- "github.com/flashmob/go-guerrilla/backends"
- "github.com/flashmob/go-guerrilla/log"
- test "github.com/flashmob/go-guerrilla/tests"
- "github.com/flashmob/go-guerrilla/tests/testcert"
- "github.com/spf13/cobra"
- )
- var configJsonA = `
- {
- "log_file" : "../../tests/testlog",
- "log_level" : "debug",
- "pid_file" : "pidfile.pid",
- "allowed_hosts": [
- "guerrillamail.com",
- "guerrillamailblock.com",
- "sharklasers.com",
- "guerrillamail.net",
- "guerrillamail.org"
- ],
- "backend" : {
- "processors" : {
- "debugger" : {
- "log_received_mails" : true
- }
- },
- "gateways" : {
- "default" : {
- "save_workers_size" : 1,
- "save_process": "HeadersParser|Debugger"
- }
- }
- },
- "servers" : [
- {
- "is_enabled" : true,
- "host_name":"mail.test.com",
- "max_size": 1000000,
- "timeout":180,
- "listen_interface":"127.0.0.1:3536",
- "max_clients": 200,
- "log_file" : "../../tests/testlog",
- "tls" : {
- "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
- "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
- "start_tls_on":true,
- "tls_always_on":false
- }
- },
- {
- "is_enabled" : false,
- "host_name":"enable.test.com",
- "max_size": 1000000,
- "timeout":180,
- "listen_interface":"127.0.0.1:2228",
- "max_clients": 200,
- "log_file" : "../../tests/testlog",
- "tls" : {
- "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
- "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
- "start_tls_on":true,
- "tls_always_on":false
- }
- }
- ]
- }
- `
- // backend config changed, log_received_mails is false
- var configJsonB = `
- {
- "log_file" : "../../tests/testlog",
- "log_level" : "debug",
- "pid_file" : "./pidfile2.pid",
- "allowed_hosts": [
- "guerrillamail.com",
- "guerrillamailblock.com",
- "sharklasers.com",
- "guerrillamail.net",
- "guerrillamail.org"
- ],
- "backend" : {
- "processors" : {
- "debugger" : {
- "log_received_mails" : false
- }
- },
- "gateways" : {
- "default" : {
- "save_workers_size" : 1,
- "save_process": "HeadersParser|Debugger"
- }
- }
- },
- "servers" : [
- {
- "is_enabled" : true,
- "host_name":"mail.test.com",
- "max_size": 1000000,
- "timeout":180,
- "listen_interface":"127.0.0.1:3536",
- "max_clients": 200,
- "log_file" : "../../tests/testlog",
- "tls" : {
- "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
- "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
- "start_tls_on":true,
- "tls_always_on":false
- }
- }
- ]
- }
- `
- // added a server
- var configJsonC = `
- {
- "log_file" : "../../tests/testlog",
- "log_level" : "debug",
- "pid_file" : "./pidfile.pid",
- "allowed_hosts": [
- "guerrillamail.com",
- "guerrillamailblock.com",
- "sharklasers.com",
- "guerrillamail.net",
- "guerrillamail.org"
- ],
- "backend" : {
- "processors" : {
- "debugger" : {
- "log_received_mails" : true
- },
- "sql" : {
- "sql_dsn": "root:ok@tcp(127.0.0.1:3306)/gmail_mail?readTimeout=10s&writeTimeout=10s",
- "mail_table":"new_mail",
- "primary_mail_host":"sharklasers.com"
- }
- },
- "gateways" : {
- "default" : {
- "save_workers_size" : 3,
- "save_process": "HeadersParser|Debugger"
- }
- }
- },
- "servers" : [
- {
- "is_enabled" : true,
- "host_name":"mail.test.com",
- "max_size": 1000000,
- "timeout":180,
- "listen_interface":"127.0.0.1:25",
- "max_clients": 200,
- "log_file" : "../../tests/testlog",
- "tls" : {
- "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
- "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
- "start_tls_on":true,
- "tls_always_on":false
- }
- },
- {
- "is_enabled" : true,
- "host_name":"mail.test.com",
- "max_size":1000000,
- "timeout":180,
- "listen_interface":"127.0.0.1:465",
- "max_clients":200,
- "log_file" : "../../tests/testlog",
- "tls" : {
- "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
- "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
- "start_tls_on":false,
- "tls_always_on":true
- }
- }
- ]
- }
- `
- // adds 127.0.0.1:4655, a secure server
- var configJsonD = `
- {
- "log_file" : "../../tests/testlog",
- "log_level" : "debug",
- "pid_file" : "./pidfile.pid",
- "allowed_hosts": [
- "guerrillamail.com",
- "guerrillamailblock.com",
- "sharklasers.com",
- "guerrillamail.net",
- "guerrillamail.org"
- ],
- "backend" : {
- "processors" : {
- "debugger" : {
- "log_received_mails" : false
- }
- },
- "gateways" : {
- "default" : {
- "save_workers_size" : 1,
- "save_process": "HeadersParser|Debugger"
- }
- }
- },
- "servers" : [
- {
- "is_enabled" : true,
- "host_name":"mail.test.com",
- "max_size": 1000000,
- "timeout":180,
- "listen_interface":"127.0.0.1:2552",
- "max_clients": 200,
- "log_file" : "../../tests/testlog",
- "tls" : {
- "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
- "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
- "start_tls_on":true,
- "tls_always_on":false
- }
- },
- {
- "is_enabled" : true,
- "host_name":"secure.test.com",
- "max_size":1000000,
- "timeout":180,
- "listen_interface":"127.0.0.1:4655",
- "max_clients":200,
- "log_file" : "../../tests/testlog",
- "tls" : {
- "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
- "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
- "start_tls_on":false,
- "tls_always_on":true
- }
- }
- ]
- }
- `
- // adds 127.0.0.1:4655, a secure server
- var configJsonE = `
- {
- "log_file" : "../../tests/testlog",
- "log_level" : "debug",
- "pid_file" : "./pidfile2.pid",
- "allowed_hosts": [
- "guerrillamail.com",
- "guerrillamailblock.com",
- "sharklasers.com",
- "guerrillamail.net",
- "guerrillamail.org"
- ],
- "backend" : {
- "processors" : {
- "debugger" : {
- "log_received_mails" : true
- },
- "sql" : {
- "sql_dsn": "root:secret@tcp(127.0.0.1:3306)/gmail_mail?readTimeout=10s&writeTimeout=10s",
- "mail_table":"new_mail",
- "sql_driver": "mysql",
- "primary_mail_host":"sharklasers.com"
- },
- "GuerrillaRedisDB" : {
- "sql_dsn": "root:secret@tcp(127.0.0.1:3306)/gmail_mail?readTimeout=10s&writeTimeout=10s",
- "mail_table":"new_mail",
- "sql_driver": "mysql",
- "redis_interface" : "127.0.0.1:6379",
- "redis_expire_seconds" : 7200,
- "primary_mail_host":"sharklasers.com"
- }
- },
- "gateways" : {
- "default" : {
- "save_workers_size" : 3,
- "save_process": "GuerrillaRedisDB"
- }
- }
- },
- "servers" : [
- {
- "is_enabled" : true,
- "host_name":"mail.test.com",
- "max_size": 1000000,
- "timeout":180,
- "listen_interface":"127.0.0.1:2552",
- "max_clients": 200,
- "log_file" : "../../tests/testlog",
- "tls" : {
- "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
- "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
- "start_tls_on":true,
- "tls_always_on":false
- }
- },
- {
- "is_enabled" : true,
- "host_name":"secure.test.com",
- "max_size":1000000,
- "timeout":180,
- "listen_interface":"127.0.0.1:4655",
- "max_clients":200,
- "log_file" : "../../tests/testlog",
- "tls" : {
- "private_key_file":"../../tests/mail2.guerrillamail.com.key.pem",
- "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
- "start_tls_on":false,
- "tls_always_on":true
- }
- }
- ]
- }
- `
- const testPauseDuration = time.Millisecond * 1010
- // reload config
- func sigHup(pidfile string) {
- if data, err := ioutil.ReadFile(pidfile); err == nil {
- if pid, err := strconv.Atoi(string(data)); err != nil {
- mainlog.WithError(err).Error("could not read pid")
- } else {
- mainlog.Fields("pid", pid).Info("pid read")
- ecmd := exec.Command("kill", "-HUP", string(data))
- _, err = ecmd.Output()
- if err != nil {
- mainlog.WithError(err).Error("could not SIGHUP")
- }
- }
- } else {
- mainlog.Fields("error", err, "file", pidfile).Info("sighup - Could not read pidfle")
- }
- }
- // shutdown after calling serve()
- func sigKill(pidfile string) {
- if data, err := ioutil.ReadFile(pidfile); err == nil {
- if pid, err := strconv.Atoi(string(data)); err != nil {
- mainlog.WithError(err).Error("could not read pid")
- } else {
- mainlog.Fields("pid", pid).Info("pid read")
- ecmd := exec.Command("kill", string(data))
- _, err = ecmd.Output()
- if err != nil {
- mainlog.WithError(err).Info("could not sigkill")
- }
- }
- } else {
- mainlog.WithError(err).Info("sigKill - Could not read pidfle")
- }
- }
- func Round(x float64) float64 {
- t := math.Trunc(x)
- if math.Abs(x-t) >= 0.5 {
- return t + math.Copysign(1, x)
- }
- return t
- }
- // exponentialBackoff sleeps in nanoseconds, according to this formula 2^(i-1) * 25 / 2
- func exponentialBackoff(i int) {
- time.Sleep(time.Duration(Round(math.Pow(3.0, float64(i))-1.0)*100.0/2.0) * time.Millisecond)
- }
- func matchTestlog(startLine int, args ...interface{}) (bool, error) {
- fd, err := os.Open("../../tests/testlog")
- if err != nil {
- return false, err
- }
- defer func() {
- _ = fd.Close()
- }()
- for tries := 0; tries < 6; tries++ {
- if b, err := ioutil.ReadAll(fd); err != nil {
- return false, err
- } else {
- if test.MatchLog(string(b), startLine, args...) {
- return true, nil
- }
- }
- // close and reopen
- err = fd.Close()
- if err != nil {
- return false, err
- }
- fd = nil
- // sleep
- exponentialBackoff(tries)
- _ = mainlog.Reopen()
- // re-open
- fd, err = os.OpenFile("../../tests/testlog", os.O_RDONLY, 0644)
- if err != nil {
- return false, err
- }
- }
- return false, nil
- }
- var grepNotFound error
- // grepTestlog looks for the `match` string in the testlog
- // the lineNumber indicates what line to start the search from
- // returns line number it was found on
- // error otherwise
- //
- // It will attempt to search the log multiple times, pausing loner for each re-try
- //
- func grepTestlog(match string, lineNumber int) (found int, err error) {
- found = 0
- fd, err := os.Open("../../tests/testlog")
- if err != nil {
- return found, err
- }
- defer func() {
- _ = fd.Close()
- }()
- buff := bufio.NewReader(fd)
- var ln int
- var line string
- for tries := 0; tries < 6; tries++ {
- for {
- ln++
- line, err = buff.ReadString('\n')
- if err != nil {
- break
- }
- if ln > lineNumber {
- if i := strings.Index(line, match); i != -1 {
- return ln, nil
- }
- }
- }
- if err != io.EOF {
- return found, err
- }
- err = fd.Close()
- if err != nil {
- return 0, err
- }
- fd = nil
- // sleep
- exponentialBackoff(tries)
- _ = mainlog.Reopen()
- // re-open
- fd, err = os.OpenFile("../../tests/testlog", os.O_RDONLY, 0644)
- if err != nil {
- return found, err
- }
- buff.Reset(fd)
- ln = 0
- }
- grepNotFound = errors.New("could not find " + match + " in tests/testlog after line" + strconv.Itoa(lineNumber))
- return found, grepNotFound
- }
- // In all the tests, there will be a minimum of about 2000 available
- func TestFileLimit(t *testing.T) {
- cfg := &guerrilla.AppConfig{LogFile: log.OutputOff.String()}
- sc := guerrilla.ServerConfig{
- ListenInterface: "127.0.0.1:2526",
- IsEnabled: true,
- MaxClients: 1000,
- }
- cfg.Servers = append(cfg.Servers, sc)
- d := guerrilla.Daemon{Config: cfg}
- if ok, maxClients, fileLimit := guerrilla.CheckFileLimit(d.Config); !ok {
- t.Errorf("Combined max clients for all servers (%d) is greater than open file limit (%d). "+
- "Please increase your open file limit. Please check your OS docs for how to increase the limit.", maxClients, fileLimit)
- }
- }
- func getTestLog() (mainlog log.Logger, err error) {
- return log.GetLogger("../../tests/testlog", "debug")
- }
- func truncateIfExists(filename string) error {
- if _, err := os.Stat(filename); !os.IsNotExist(err) {
- return os.Truncate(filename, 0)
- }
- return nil
- }
- func deleteIfExists(filename string) error {
- if _, err := os.Stat(filename); !os.IsNotExist(err) {
- return os.Remove(filename)
- }
- return nil
- }
- func cleanTestArtifacts(t *testing.T) {
- if err := truncateIfExists("../../tests/testlog"); err != nil {
- t.Error("could not clean tests/testlog:", err)
- }
- if err := truncateIfExists("../../tests/testlog2"); err != nil {
- t.Error("could not clean tests/testlog2:", err)
- }
- letters := []byte{'A', 'B', 'C', 'D', 'E'}
- for _, l := range letters {
- if err := deleteIfExists("configJson" + string(l) + ".json"); err != nil {
- t.Error("could not delete configJson"+string(l)+".json:", err)
- }
- }
- if err := deleteIfExists("./pidfile.pid"); err != nil {
- t.Error("could not delete ./pidfile.pid", err)
- }
- if err := deleteIfExists("./pidfile2.pid"); err != nil {
- t.Error("could not delete ./pidfile2.pid", err)
- }
- if err := deleteIfExists("../../tests/mail.guerrillamail.com.cert.pem"); err != nil {
- t.Error("could not delete ../../tests/mail.guerrillamail.com.cert.pem", err)
- }
- if err := deleteIfExists("../../tests/mail.guerrillamail.com.key.pem"); err != nil {
- t.Error("could not delete ../../tests/mail.guerrillamail.com.key.pem", err)
- }
- if err := deleteIfExists("../../tests/mail2.guerrillamail.com.cert.pem"); err != nil {
- t.Error("could not delete ../../tests/mail2.guerrillamail.com.cert.pem", err)
- }
- if err := deleteIfExists("../../tests/mail2.guerrillamail.com.key.pem"); err != nil {
- t.Error("could not delete ../../tests/mail2.guerrillamail.com.key.pem", err)
- }
- }
- // make sure that we get all the config change events
- func TestCmdConfigChangeEvents(t *testing.T) {
- defer cleanTestArtifacts(t)
- var err error
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- oldconf := &guerrilla.AppConfig{}
- if err := oldconf.Load([]byte(configJsonA)); err != nil {
- t.Error("configJsonA is invalid", err)
- }
- newconf := &guerrilla.AppConfig{}
- if err := newconf.Load([]byte(configJsonB)); err != nil {
- t.Error("configJsonB is invalid", err)
- }
- newerconf := &guerrilla.AppConfig{}
- if err := newerconf.Load([]byte(configJsonC)); err != nil {
- t.Error("configJsonC is invalid", err)
- }
- expectedEvents := map[guerrilla.Event]bool{
- guerrilla.EventConfigBackendConfigChanged: false, // backend_change:backend
- guerrilla.EventConfigServerNew: false, // server_change:new_server
- }
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- oldconf.BackendConfig = backends.BackendConfig{
- backends.ConfigProcessors: {"debugger": {"log_received_mails": true}},
- }
- oldconf.BackendConfig.ConfigureDefaults()
- backend, err := backends.New(backends.DefaultGateway, oldconf.BackendConfig, mainlog)
- if err != nil {
- t.Error("failed to create backend", err)
- return
- }
- app, err := guerrilla.New(oldconf, mainlog, backend)
- if err != nil {
- t.Error("Failed to create new app", err)
- }
- toUnsubscribe := map[guerrilla.Event]interface{}{}
- for event := range expectedEvents {
- // Put in anon func since range is overwriting event
- func(e guerrilla.Event) {
- if strings.Index(e.String(), "backend_change") == 0 {
- f := func(c *guerrilla.AppConfig, gateway string) {
- expectedEvents[e] = true
- }
- _ = app.Subscribe(e, f)
- toUnsubscribe[e] = f
- } else if strings.Index(e.String(), "server_change") == 0 {
- f := func(c *guerrilla.ServerConfig) {
- expectedEvents[e] = true
- }
- _ = app.Subscribe(e, f)
- toUnsubscribe[e] = f
- } else {
- f := func(c *guerrilla.AppConfig) {
- expectedEvents[e] = true
- }
- _ = app.Subscribe(e, f)
- toUnsubscribe[e] = f
- }
- }(event)
- }
- // emit events
- newconf.EmitChangeEvents(oldconf, app)
- newerconf.EmitChangeEvents(newconf, app)
- // unsubscribe
- for unevent, unfun := range toUnsubscribe {
- _ = app.Unsubscribe(unevent, unfun)
- }
- for event, val := range expectedEvents {
- if val == false {
- t.Error("Did not fire config change event:", event)
- t.FailNow()
- }
- }
- }
- // start server, change config, send SIG HUP, confirm that the pidfile changed & backend reloaded
- func TestServe(t *testing.T) {
- defer cleanTestArtifacts(t)
- var err error
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- if err := ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644); err != nil {
- t.Error(err)
- t.FailNow()
- }
- cmd := &cobra.Command{}
- configPath = "configJsonA.json"
- go func() {
- serve(cmd, []string{})
- }()
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:3536\"", 0); err != nil {
- t.Error("server not started")
- }
- // wait for the pidfle to be written out
- if _, err := grepTestlog("pid_file", 0); err != nil {
- t.Error("pid_file not written")
- }
- data, err := ioutil.ReadFile("pidfile.pid")
- if err != nil {
- t.Error("error reading pidfile.pid", err)
- t.FailNow()
- }
- _, err = strconv.Atoi(string(data))
- if err != nil {
- t.Error("could not parse pidfile.pid", err)
- t.FailNow()
- }
- // change the config file
- err = ioutil.WriteFile("configJsonA.json", []byte(configJsonB), 0644)
- if err != nil {
- t.Error(err)
- t.FailNow()
- }
- // test SIGHUP via the kill command
- // Would not work on windows as kill is not available.
- // TODO: Implement an alternative test for windows.
- if runtime.GOOS != "windows" {
- sigHup("pidfile.pid")
- // did the pidfile change as expected?
- if _, err := grepTestlog("configuration was reloaded", 0); err != nil {
- t.Error("server did not catch sighup")
- }
- }
- if _, err := grepTestlog("gateway with new config started", 0); err != nil {
- t.Error("Dummy backend not restarted")
- }
- // send kill signal and wait for exit
- d.Shutdown()
- // did backend started as expected?
- // wait for shutdown
- if _, err := grepTestlog("backend shutdown completed", 0); err != nil {
- t.Error("server didn't stop")
- }
- }
- // Start with configJsonA.json,
- // then add a new server to it (127.0.0.1:2526),
- // then SIGHUP (to reload config & trigger config update events),
- // then connect to it & HELO.
- func TestServerAddEvent(t *testing.T) {
- var err error
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- defer cleanTestArtifacts(t)
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- // start the server by emulating the serve command
- if err := ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644); err != nil {
- t.Error(err)
- t.FailNow()
- }
- cmd := &cobra.Command{}
- configPath = "configJsonA.json"
- go func() {
- serve(cmd, []string{})
- }()
- // allow the server to start
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:3536\"", 0); err != nil {
- t.Error("server didn't start")
- }
- // now change the config by adding a server
- conf := &guerrilla.AppConfig{} // blank one
- err = conf.Load([]byte(configJsonA)) // load configJsonA
- if err != nil {
- t.Error(err)
- }
- newServer := conf.Servers[0] // copy the first server config
- newServer.ListenInterface = "127.0.0.1:2526" // change it
- newConf := conf // copy the cmdConfg
- newConf.Servers = append(newConf.Servers, newServer) // add the new server
- if jsonbytes, err := json.Marshal(newConf); err == nil {
- if err := ioutil.WriteFile("configJsonA.json", jsonbytes, 0644); err != nil {
- t.Error(err)
- }
- }
- // send a sighup signal to the server
- sigHup("./pidfile.pid")
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:2526\"", 0); err != nil {
- t.Error("new server didn't start")
- }
- if conn, buffin, err := test.Connect(newServer, 20); err != nil {
- t.Error("Could not connect to new server", newServer.ListenInterface, err)
- return
- } else {
- if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
- expect := "250 mail.test.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- }
- } else {
- t.Error(err)
- }
- }
- // shutdown the server
- d.Shutdown()
- // sever added as as expected?
- if matched, err := matchTestlog(
- 1, "msg", "new server added",
- "iface", "127.0.0.1:2526",
- "event", "server_change:new_server",
- ); !matched {
- t.Error("Did not add server [127.0.0.1:2526] after sighup", err)
- }
- if matched, err := matchTestlog(
- 1, "msg", "backend shutdown completed",
- ); !matched {
- t.Error("Server failed to stop", err)
- }
- }
- // Start with configJsonA.json,
- // then change the config to enable 127.0.0.1:2228,
- // then write the new config,
- // then SIGHUP (to reload config & trigger config update events),
- // then connect to 127.0.0.1:2228 & HELO.
- func TestServerStartEvent(t *testing.T) {
- var err error
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- defer cleanTestArtifacts(t)
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- if err := ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644); err != nil {
- t.Error(err)
- t.FailNow()
- }
- cmd := &cobra.Command{}
- configPath = "configJsonA.json"
- go func() {
- serve(cmd, []string{})
- }()
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:3536\"", 0); err != nil {
- t.Error("server didn't start")
- }
- // now change the config by adding a server
- conf := &guerrilla.AppConfig{} // blank one
- if err = conf.Load([]byte(configJsonA)); err != nil { // load configJsonA
- t.Error(err)
- }
- newConf := conf // copy the cmdConfg
- newConf.Servers[1].IsEnabled = true
- if jsonbytes, err := json.Marshal(newConf); err == nil {
- //fmt.Println(string(jsonbytes))
- if err = ioutil.WriteFile("configJsonA.json", jsonbytes, 0644); err != nil {
- t.Error(err)
- }
- } else {
- t.Error(err)
- }
- // send a sighup signal to the server
- sigHup("pidfile.pid")
- // see if the new server started?
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:2228\"", 0); err != nil {
- t.Error("second server didn't start")
- }
- // can we talk to it?
- if conn, buffin, err := test.Connect(newConf.Servers[1], 20); err != nil {
- t.Error("Could not connect to new server", newConf.Servers[1].ListenInterface)
- } else {
- if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
- expect := "250 enable.test.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- }
- } else {
- t.Error(err)
- }
- }
- // shutdown and wait for exit
- d.Shutdown()
- if _, err := grepTestlog("backend shutdown completed", 0); err != nil {
- t.Error("server didn't stop")
- }
- }
- // Start with configJsonA.json,
- // then change the config to enable 127.0.0.1:2228,
- // then write the new config,
- // then SIGHUP (to reload config & trigger config update events),
- // then connect to 127.0.0.1:2228 & HELO.
- // then change the config to dsiable 127.0.0.1:2228,
- // then SIGHUP (to reload config & trigger config update events),
- // then connect to 127.0.0.1:2228 - it should not connect
- func TestServerStopEvent(t *testing.T) {
- var err error
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- defer cleanTestArtifacts(t)
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- if err := ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644); err != nil {
- t.Error(err)
- t.FailNow()
- }
- cmd := &cobra.Command{}
- configPath = "configJsonA.json"
- go func() {
- serve(cmd, []string{})
- }()
- // allow the server to start
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:3536\"", 0); err != nil {
- t.Error("server didn't start")
- }
- // now change the config by enabling a server
- conf := &guerrilla.AppConfig{} // blank one
- if err = conf.Load([]byte(configJsonA)); err != nil { // load configJsonA
- t.Error(err)
- }
- newConf := conf // copy the cmdConfg
- newConf.Servers[1].IsEnabled = true // enable 2nd server
- if jsonbytes, err := json.Marshal(newConf); err == nil {
- //fmt.Println(string(jsonbytes))
- if err = ioutil.WriteFile("configJsonA.json", jsonbytes, 0644); err != nil {
- t.Error(err)
- }
- } else {
- t.Error(err)
- }
- // send a sighup signal to the server
- sigHup("pidfile.pid")
- // detect config change
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:2228\"", 0); err != nil {
- t.Error("new server didn't start")
- }
- if conn, buffin, err := test.Connect(newConf.Servers[1], 20); err != nil {
- t.Error("Could not connect to new server", newConf.Servers[1].ListenInterface)
- } else {
- if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
- expect := "250 enable.test.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- }
- } else {
- t.Error(err)
- }
- if err = conn.Close(); err != nil {
- t.Error(err)
- }
- }
- // now disable the server
- newerConf := newConf // copy the cmdConfg
- newerConf.Servers[1].IsEnabled = false
- if jsonbytes, err := json.Marshal(newerConf); err == nil {
- //fmt.Println(string(jsonbytes))
- if err = ioutil.WriteFile("configJsonA.json", jsonbytes, 0644); err != nil {
- t.Error(err)
- }
- } else {
- t.Error(err)
- }
- // send a sighup signal to the server
- sigHup("pidfile.pid")
- // detect config change
- if _, err := grepTestlog("msg=\"server has stopped accepting new clients\" iface=\"127.0.0.1:2228\"", 27); err != nil {
- t.Error("127.0.0.1:2228 did not stop")
- }
- // it should not connect to the server
- if _, _, err := test.Connect(newConf.Servers[1], 20); err == nil {
- t.Error("127.0.0.1:2228 was disabled, but still accepting connections", newConf.Servers[1].ListenInterface)
- }
- // shutdown wait for exit
- d.Shutdown()
- // wait for shutdown
- if _, err := grepTestlog("backend shutdown completed", 0); err != nil {
- t.Error("server didn't stop")
- }
- }
- // just a utility for debugging when using the debugger, skipped by default
- func TestDebug(t *testing.T) {
- t.SkipNow()
- conf := guerrilla.ServerConfig{ListenInterface: "127.0.0.1:2526"}
- if conn, buffin, err := test.Connect(conf, 20); err != nil {
- t.Error("Could not connect to new server", conf.ListenInterface, err)
- } else {
- if result, err := test.Command(conn, buffin, "HELO"); err == nil {
- expect := "250 mai1.guerrillamail.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- } else {
- if result, err = test.Command(conn, buffin, "RCPT TO:<[email protected]>"); err == nil {
- expect := "250 2.1.5 OK"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected:", expect, "but got:", result)
- }
- }
- }
- }
- _ = conn.Close()
- }
- }
- // Start with configJsonD.json,
- // then connect to 127.0.0.1:4655 & HELO & try RCPT TO with an invalid host [grr.la]
- // then change the config to enable add new host [grr.la] to allowed_hosts
- // then write the new config,
- // then SIGHUP (to reload config & trigger config update events),
- // connect to 127.0.0.1:4655 & HELO & try RCPT TO, grr.la should work
- func TestAllowedHostsEvent(t *testing.T) {
- var err error
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- defer cleanTestArtifacts(t)
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- if err := ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
- t.Error(err)
- t.FailNow()
- }
- // start the server by emulating the serve command
- conf := &guerrilla.AppConfig{} // blank one
- if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
- t.Error(err)
- }
- cmd := &cobra.Command{}
- configPath = "configJsonD.json"
- go func() {
- serve(cmd, []string{})
- }()
- // wait for start
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:2552\"", 0); err != nil {
- t.Error("server didn't start")
- }
- // now connect and try RCPT TO with an invalid host
- if conn, buffin, err := test.Connect(conf.Servers[1], 20); err != nil {
- t.Error("Could not connect to new server", conf.Servers[1].ListenInterface, err)
- } else {
- if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
- expect := "250 secure.test.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- } else {
- if result, err = test.Command(conn, buffin, "RCPT TO:<[email protected]>"); err == nil {
- expect := "454 4.1.1 Error: Relay access denied: grr.la"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected:", expect, "but got:", result)
- }
- }
- }
- }
- _ = conn.Close()
- }
- // now change the config by adding a host to allowed hosts
- newConf := conf
- newConf.AllowedHosts = append(newConf.AllowedHosts, "grr.la")
- if jsonbytes, err := json.Marshal(newConf); err == nil {
- if err = ioutil.WriteFile("configJsonD.json", jsonbytes, 0644); err != nil {
- t.Error(err)
- }
- } else {
- t.Error(err)
- }
- // send a sighup signal to the server to reload config
- sigHup("pidfile.pid")
- if _, err := grepTestlog("allowed_hosts config changed", 0); err != nil {
- t.Error("allowed_hosts config not changed")
- t.FailNow()
- }
- // now repeat the same conversion, RCPT TO should be accepted
- if conn, buffin, err := test.Connect(conf.Servers[1], 20); err != nil {
- t.Error("Could not connect to new server", conf.Servers[1].ListenInterface, err)
- } else {
- if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
- expect := "250 secure.test.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- } else {
- if result, err = test.Command(conn, buffin, "RCPT TO:<[email protected]>"); err == nil {
- expect := "250 2.1.5 OK"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected:", expect, "but got:", result)
- }
- }
- }
- }
- _ = conn.Close()
- }
- // shutdown wait for exit
- d.Shutdown()
- // wait for shutdown
- if _, err := grepTestlog("backend shutdown completed", 0); err != nil {
- t.Error("server didn't stop")
- }
- }
- // Test TLS config change event
- // start with configJsonD
- // should be able to STARTTLS to 127.0.0.1:2525 with no problems
- // generate new certs & reload config
- // should get a new tls event & able to STARTTLS with no problem
- func TestTLSConfigEvent(t *testing.T) {
- var err error
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- defer cleanTestArtifacts(t)
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- if err := ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
- t.Error(err)
- t.FailNow()
- }
- conf := &guerrilla.AppConfig{} // blank one
- if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
- t.Error(err)
- t.FailNow()
- }
- cmd := &cobra.Command{}
- configPath = "configJsonD.json"
- go func() {
- serve(cmd, []string{})
- }()
- // wait for server to start
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:2552\"", 0); err != nil {
- t.Error("server didn't start")
- }
- // Test STARTTLS handshake
- testTlsHandshake := func() {
- if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
- t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
- } else {
- if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
- expect := "250 mail.test.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- } else {
- if result, err = test.Command(conn, buffin, "STARTTLS"); err == nil {
- expect := "220 2.0.0 Ready to start TLS"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected:", expect, "but got:", result)
- } else {
- tlsConn := tls.Client(conn, &tls.Config{
- InsecureSkipVerify: true,
- ServerName: "127.0.0.1",
- })
- if err := tlsConn.Handshake(); err != nil {
- t.Error("Failed to handshake", conf.Servers[0].ListenInterface)
- } else {
- conn = tlsConn
- mainlog.Info("TLS Handshake succeeded")
- }
- }
- }
- }
- }
- _ = conn.Close()
- }
- }
- testTlsHandshake()
- // TLS Handshake succeeded?
- if _, err := grepTestlog("TLS Handshake succeeded", 0); err != nil {
- t.Error("TLS Handshake did not succeed")
- t.FailNow()
- }
- // now delete old certs, configure new certs, and send a sighup to load them in
- if err := deleteIfExists("../../tests/mail2.guerrillamail.com.cert.pem"); err != nil {
- t.Error("could not delete ../../tests/mail2.guerrillamail.com.cert.pem", err)
- }
- if err := deleteIfExists("../../tests/mail2.guerrillamail.com.key.pem"); err != nil {
- t.Error("could not delete ../../tests/mail2.guerrillamail.com.key.pem", err)
- }
- time.Sleep(testPauseDuration) // need to pause so that the new certs have different timestamps!
- // generate a new cert
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- // pause for generated cert to output (don't need, since we've fsynced)
- // time.Sleep(testPauseDuration) // (don't need, since we've fsynced)
- // did cert output?
- if _, err := os.Stat("../../tests/mail2.guerrillamail.com.cert.pem"); err != nil {
- t.Error("Did not create cert ", err)
- }
- sigHup("pidfile.pid")
- // wait for config to reload
- if _, err := matchTestlog(1, "iface", "127.0.0.1:4655", "msg", "server re-opened"); err != nil {
- t.Error("server didn't catch sighup")
- }
- // did tls configuration reload as expected?
- if _, err := grepTestlog("new TLS configuration loaded", 0); err != nil {
- t.Error("server didn't catch sighup")
- }
- // test again
- testTlsHandshake()
- // after line 25
- if _, err := grepTestlog("TLS Handshake succeeded", 25); err != nil {
- t.Error("TLS Handshake did not succeed")
- t.FailNow()
- }
- d.Shutdown()
- // wait for shutdown
- if _, err := grepTestlog("backend shutdown completed", 0); err != nil {
- t.Error("server didn't stop")
- }
- }
- // Testing starting a server with a bad TLS config
- // It should not start, return exit code 1
- func TestBadTLSStart(t *testing.T) {
- var err error
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- // Need to run the test in a different process by executing a command
- // because the serve() does os.Exit when starting with a bad TLS config
- if os.Getenv("BE_CRASHER") == "1" {
- // do the test
- // first, remove the good certs, if any
- if err := deleteIfExists("../../tests/mail2.guerrillamail.com.cert.pem"); err != nil {
- t.Error("could not delete ../../tests/mail2.guerrillamail.com.cert.pem", err)
- }
- if err := deleteIfExists("../../tests/mail2.guerrillamail.com.key.pem"); err != nil {
- t.Error("could not delete ../../tests/mail2.guerrillamail.com.key.pem", err)
- }
- // next run the server
- if err = ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
- t.Error(err)
- }
- conf := &guerrilla.AppConfig{} // blank one
- if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
- t.Error(err)
- }
- cmd := &cobra.Command{}
- configPath = "configJsonD.json"
- var serveWG sync.WaitGroup
- serveWG.Add(1)
- go func() {
- serve(cmd, []string{})
- serveWG.Done()
- }()
- // it should exit by now because the TLS config is incorrect
- time.Sleep(testPauseDuration)
- sigKill("pidfile.pid")
- serveWG.Wait()
- return
- }
- defer cleanTestArtifacts(t)
- cmd := exec.Command(os.Args[0], "-test.run=TestBadTLSStart")
- cmd.Env = append(os.Environ(), "BE_CRASHER=1")
- err = cmd.Run()
- if e, ok := err.(*exec.ExitError); ok && !e.Success() {
- if _, err := grepTestlog("level=fatal", 0); err != nil {
- t.Error("server didn't exit with a fatal error")
- }
- return
- }
- t.Error("Server started with a bad TLS config, was expecting exit status 0")
- }
- // Test config reload with a bad TLS config
- // It should ignore the config reload, keep running with old settings
- func TestBadTLSReload(t *testing.T) {
- var err error
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- defer cleanTestArtifacts(t)
- // start with a good cert
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- // start the server by emulating the serve command
- if err = ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
- t.Error(err)
- t.FailNow()
- }
- conf := &guerrilla.AppConfig{} // blank one
- if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
- t.Error(err)
- t.FailNow()
- }
- cmd := &cobra.Command{}
- configPath = "configJsonD.json"
- go func() {
- serve(cmd, []string{})
- }()
- // wait for server to start
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:4655\"", 0); err != nil {
- t.Error("server didn't start")
- }
- if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
- t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
- } else {
- if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
- expect := "250 mail.test.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- }
- }
- }
- // write some trash data
- if err = ioutil.WriteFile("./../../tests/mail2.guerrillamail.com.cert.pem",
- []byte("trash data"),
- 0664); err != nil {
- t.Error(err)
- }
- if err = ioutil.WriteFile("./../../tests/mail2.guerrillamail.com.key.pem",
- []byte("trash data"),
- 0664); err != nil {
- t.Error(err)
- }
- newConf := conf // copy the cmdConfg
- if jsonbytes, err := json.Marshal(newConf); err == nil {
- if err = ioutil.WriteFile("configJsonD.json", jsonbytes, 0644); err != nil {
- t.Error(err)
- }
- } else {
- t.Error(err)
- }
- // send a sighup signal to the server to reload config
- sigHup("pidfile.pid")
- // did the config reload reload event fire? There should be config read error
- if _, err := grepTestlog("could not read config file", 0); err != nil {
- t.Error("was expecting an error reading config")
- }
- // we should still be able to to talk to it
- if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
- t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
- } else {
- if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
- expect := "250 mail.test.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- }
- }
- }
- // shutdown & wait for exit
- d.Shutdown()
- // wait for shutdown
- if _, err := grepTestlog("backend shutdown completed", 0); err != nil {
- t.Error("server didn't stop")
- }
- }
- // Test for when the server config Timeout value changes
- // Start with configJsonD.json
- func TestSetTimeoutEvent(t *testing.T) {
- var err error
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- defer cleanTestArtifacts(t)
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- // start the server by emulating the serve command
- if err = ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
- t.Error(err)
- }
- conf := &guerrilla.AppConfig{} // blank one
- if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
- t.Error(err)
- }
- cmd := &cobra.Command{}
- configPath = "configJsonD.json"
- go func() {
- serve(cmd, []string{})
- }()
- // wait for start
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:4655\"", 0); err != nil {
- t.Error("server didn't start")
- }
- // set the timeout to 1 second
- newConf := conf // copy the cmdConfg
- newConf.Servers[0].Timeout = 1
- if jsonbytes, err := json.Marshal(newConf); err == nil {
- if err = ioutil.WriteFile("configJsonD.json", jsonbytes, 0644); err != nil {
- t.Error(err)
- }
- } else {
- t.Error(err)
- }
- // send a sighup signal to the server to reload config
- sigHup("pidfile.pid")
- // did config update?
- if _, err := grepTestlog("a new config has been saved", 0); err != nil {
- t.Error("config didn't update")
- }
- var waitTimeout sync.WaitGroup
- if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
- t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
- } else {
- waitTimeout.Add(1)
- go func() {
- if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
- expect := "250 mail.test.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- } else {
- b := make([]byte, 1024)
- _, _ = conn.Read(b)
- }
- }
- waitTimeout.Done()
- }()
- }
- // wait for timeout
- waitTimeout.Wait()
- d.Shutdown()
- // wait for shutdown
- if _, err := grepTestlog("backend shutdown completed", 0); err != nil {
- t.Error("server didn't stop")
- }
- // so the connection we have opened should timeout by now
- // did we get timeout as expected?
- if _, err := grepTestlog("i/o timeout", 0); err != nil {
- t.Error("it looks like the timeout config didnt change")
- t.FailNow()
- }
- }
- // Test debug level config change
- // Start in log_level = debug
- // Load config & start server
- func TestDebugLevelChange(t *testing.T) {
- var err error
- mainlog, err = getTestLog()
- if err != nil {
- t.Error("could not get logger,", err)
- t.FailNow()
- }
- defer cleanTestArtifacts(t)
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- } // start the server by emulating the serve command
- if err = ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644); err != nil {
- t.Error(err)
- }
- conf := &guerrilla.AppConfig{} // blank one
- if err = conf.Load([]byte(configJsonD)); err != nil { // load configJsonD
- t.Error(err)
- }
- conf.LogLevel = "debug"
- cmd := &cobra.Command{}
- configPath = "configJsonD.json"
- go func() {
- serve(cmd, []string{})
- }()
- if _, err := grepTestlog("msg=\"listening on TCP\" iface=\"127.0.0.1:2552\"", 0); err != nil {
- t.Error("server didn't start")
- }
- if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
- t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
- } else {
- if result, err := test.Command(conn, buffin, "HELO example.com"); err == nil {
- expect := "250 mail.test.com Hello"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- }
- }
- _ = conn.Close()
- }
- // set the log_level to info
- newConf := conf // copy the cmdConfg
- newConf.LogLevel = log.InfoLevel.String()
- if jsonbytes, err := json.Marshal(newConf); err == nil {
- if err = ioutil.WriteFile("configJsonD.json", jsonbytes, 0644); err != nil {
- t.Error(err)
- }
- } else {
- t.Error(err)
- }
- // send a sighup signal to the server to reload config
- sigHup("pidfile.pid")
- // did the config reload?
- if _, err := grepTestlog("configuration was reloaded", 0); err != nil {
- t.Error("config did not reload")
- t.FailNow()
- }
- // connect again, this time we should see info
- if conn, buffin, err := test.Connect(conf.Servers[0], 20); err != nil {
- t.Error("Could not connect to server", conf.Servers[0].ListenInterface, err)
- } else {
- if result, err := test.Command(conn, buffin, "NOOP"); err == nil {
- expect := "200 2.0.0 OK"
- if strings.Index(result, expect) != 0 {
- t.Error("Expected", expect, "but got", result)
- }
- }
- _ = conn.Close()
- }
- d.Shutdown()
- if ok, err := matchTestlog(1, "msg", "log level changed"); !ok {
- t.Error("log level did not change", err)
- t.FailNow()
- }
- }
- // When reloading with a bad backend config, it should revert to old backend config
- // using the API way
- func TestBadBackendReload(t *testing.T) {
- defer cleanTestArtifacts(t)
- var err error
- err = testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
- if err != nil {
- t.Error("failed to generate a test certificate", err)
- t.FailNow()
- }
- if err = ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644); err != nil {
- t.Error(err)
- }
- d := guerrilla.Daemon{}
- _, err = d.LoadConfig("configJsonA.json")
- if err != nil {
- t.Error("ReadConfig error", err)
- return
- }
- err = d.Start()
- if err != nil {
- t.Error("start error", err)
- return
- } else {
- if err = ioutil.WriteFile("configJsonA.json", []byte(configJsonE), 0644); err != nil {
- t.Error(err)
- return
- }
- if err = d.ReloadConfigFile("configJsonA.json"); err != nil {
- t.Error(err)
- return
- }
- d.Shutdown()
- if _, err := grepTestlog("configuration was reloaded", 0); err != nil {
- t.Error("config didn't update")
- }
- // reverted to old gateway config?
- if _, err := grepTestlog("reverted to old gateway config", 0); err != nil {
- t.Error("Did not revert to old gateway config")
- }
- // did the pidfile change as expected?
- if _, err := grepTestlog("msg=\"pid_file written\" file=./pidfile2.pid", 0); err != nil {
- t.Error("pid_file (./pidfile2.pid) not written")
- }
- if _, err := os.Stat("./pidfile2.pid"); os.IsNotExist(err) {
- t.Error("pidfile not changed after sighup SIGHUP", err)
- }
- }
- }
|