|
@@ -3,7 +3,6 @@ package main
|
|
import (
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
- "fmt"
|
|
|
|
"github.com/flashmob/go-guerrilla"
|
|
"github.com/flashmob/go-guerrilla"
|
|
"github.com/flashmob/go-guerrilla/backends"
|
|
"github.com/flashmob/go-guerrilla/backends"
|
|
"github.com/flashmob/go-guerrilla/log"
|
|
"github.com/flashmob/go-guerrilla/log"
|
|
@@ -33,8 +32,9 @@ var configJsonA = `
|
|
"guerrillamail.net",
|
|
"guerrillamail.net",
|
|
"guerrillamail.org"
|
|
"guerrillamail.org"
|
|
],
|
|
],
|
|
- "backend_name": "dummy",
|
|
|
|
"backend_config": {
|
|
"backend_config": {
|
|
|
|
+ "save_workers_size" : 1,
|
|
|
|
+ "save_process": "HeadersParser|Debugger",
|
|
"log_received_mails": true
|
|
"log_received_mails": true
|
|
},
|
|
},
|
|
"servers" : [
|
|
"servers" : [
|
|
@@ -45,7 +45,7 @@ var configJsonA = `
|
|
"private_key_file":"../..//tests/mail2.guerrillamail.com.key.pem",
|
|
"private_key_file":"../..//tests/mail2.guerrillamail.com.key.pem",
|
|
"public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
|
|
"public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
|
|
"timeout":180,
|
|
"timeout":180,
|
|
- "listen_interface":"127.0.0.1:25",
|
|
|
|
|
|
+ "listen_interface":"127.0.0.1:3536",
|
|
"start_tls_on":true,
|
|
"start_tls_on":true,
|
|
"tls_always_on":false,
|
|
"tls_always_on":false,
|
|
"max_clients": 1000,
|
|
"max_clients": 1000,
|
|
@@ -81,8 +81,9 @@ var configJsonB = `
|
|
"guerrillamail.net",
|
|
"guerrillamail.net",
|
|
"guerrillamail.org"
|
|
"guerrillamail.org"
|
|
],
|
|
],
|
|
- "backend_name": "dummy",
|
|
|
|
"backend_config": {
|
|
"backend_config": {
|
|
|
|
+ "save_workers_size" : 1,
|
|
|
|
+ "save_process": "HeadersParser|Debugger",
|
|
"log_received_mails": false
|
|
"log_received_mails": false
|
|
},
|
|
},
|
|
"servers" : [
|
|
"servers" : [
|
|
@@ -93,7 +94,7 @@ var configJsonB = `
|
|
"private_key_file":"../..//tests/mail2.guerrillamail.com.key.pem",
|
|
"private_key_file":"../..//tests/mail2.guerrillamail.com.key.pem",
|
|
"public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
|
|
"public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
|
|
"timeout":180,
|
|
"timeout":180,
|
|
- "listen_interface":"127.0.0.1:25",
|
|
|
|
|
|
+ "listen_interface":"127.0.0.1:3536",
|
|
"start_tls_on":true,
|
|
"start_tls_on":true,
|
|
"tls_always_on":false,
|
|
"tls_always_on":false,
|
|
"max_clients": 1000,
|
|
"max_clients": 1000,
|
|
@@ -127,7 +128,10 @@ var configJsonC = `
|
|
"redis_interface" : "127.0.0.1:6379",
|
|
"redis_interface" : "127.0.0.1:6379",
|
|
"redis_expire_seconds" : 7200,
|
|
"redis_expire_seconds" : 7200,
|
|
"save_workers_size" : 3,
|
|
"save_workers_size" : 3,
|
|
- "primary_mail_host":"sharklasers.com"
|
|
|
|
|
|
+ "primary_mail_host":"sharklasers.com",
|
|
|
|
+ "save_workers_size" : 1,
|
|
|
|
+ "save_process": "HeadersParser|Debugger",
|
|
|
|
+ "log_received_mails": true
|
|
},
|
|
},
|
|
"servers" : [
|
|
"servers" : [
|
|
{
|
|
{
|
|
@@ -173,8 +177,9 @@ var configJsonD = `
|
|
"guerrillamail.net",
|
|
"guerrillamail.net",
|
|
"guerrillamail.org"
|
|
"guerrillamail.org"
|
|
],
|
|
],
|
|
- "backend_name": "dummy",
|
|
|
|
"backend_config": {
|
|
"backend_config": {
|
|
|
|
+ "save_workers_size" : 1,
|
|
|
|
+ "save_process": "HeadersParser|Debugger",
|
|
"log_received_mails": false
|
|
"log_received_mails": false
|
|
},
|
|
},
|
|
"servers" : [
|
|
"servers" : [
|
|
@@ -208,6 +213,65 @@ var configJsonD = `
|
|
}
|
|
}
|
|
`
|
|
`
|
|
|
|
|
|
|
|
+// 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_config" :
|
|
|
|
+ {
|
|
|
|
+ "save_process_old": "HeadersParser|Debugger|Hasher|Header|Compressor|Redis|MySql",
|
|
|
|
+ "save_process": "GuerrillaRedisDB",
|
|
|
|
+ "log_received_mails" : true,
|
|
|
|
+ "mysql_db":"gmail_mail",
|
|
|
|
+ "mysql_host":"127.0.0.1:3306",
|
|
|
|
+ "mysql_pass":"secret",
|
|
|
|
+ "mysql_user":"root",
|
|
|
|
+ "mail_table":"new_mail",
|
|
|
|
+ "redis_interface" : "127.0.0.1:6379",
|
|
|
|
+ "redis_expire_seconds" : 7200,
|
|
|
|
+ "save_workers_size" : 3,
|
|
|
|
+ "primary_mail_host":"sharklasers.com"
|
|
|
|
+ },
|
|
|
|
+ "servers" : [
|
|
|
|
+ {
|
|
|
|
+ "is_enabled" : true,
|
|
|
|
+ "host_name":"mail.test.com",
|
|
|
|
+ "max_size": 1000000,
|
|
|
|
+ "private_key_file":"../..//tests/mail2.guerrillamail.com.key.pem",
|
|
|
|
+ "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
|
|
|
|
+ "timeout":180,
|
|
|
|
+ "listen_interface":"127.0.0.1:2552",
|
|
|
|
+ "start_tls_on":true,
|
|
|
|
+ "tls_always_on":false,
|
|
|
|
+ "max_clients": 1000,
|
|
|
|
+ "log_file" : "../../tests/testlog"
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ "is_enabled" : true,
|
|
|
|
+ "host_name":"secure.test.com",
|
|
|
|
+ "max_size":1000000,
|
|
|
|
+ "private_key_file":"../..//tests/mail2.guerrillamail.com.key.pem",
|
|
|
|
+ "public_key_file":"../../tests/mail2.guerrillamail.com.cert.pem",
|
|
|
|
+ "timeout":180,
|
|
|
|
+ "listen_interface":"127.0.0.1:4655",
|
|
|
|
+ "start_tls_on":false,
|
|
|
|
+ "tls_always_on":true,
|
|
|
|
+ "max_clients":500,
|
|
|
|
+ "log_file" : "../../tests/testlog"
|
|
|
|
+ }
|
|
|
|
+ ]
|
|
|
|
+}
|
|
|
|
+`
|
|
|
|
+
|
|
const testPauseDuration = time.Millisecond * 600
|
|
const testPauseDuration = time.Millisecond * 600
|
|
|
|
|
|
// reload config
|
|
// reload config
|
|
@@ -243,59 +307,66 @@ func sigKill() {
|
|
// make sure that we get all the config change events
|
|
// make sure that we get all the config change events
|
|
func TestCmdConfigChangeEvents(t *testing.T) {
|
|
func TestCmdConfigChangeEvents(t *testing.T) {
|
|
|
|
|
|
- oldconf := &CmdConfig{}
|
|
|
|
- oldconf.load([]byte(configJsonA))
|
|
|
|
|
|
+ oldconf := &guerrilla.AppConfig{}
|
|
|
|
+ if err := oldconf.Load([]byte(configJsonA)); err != nil {
|
|
|
|
+ t.Error("configJsonA is invalid", err)
|
|
|
|
+ }
|
|
|
|
|
|
- newconf := &CmdConfig{}
|
|
|
|
- newconf.load([]byte(configJsonB))
|
|
|
|
|
|
+ newconf := &guerrilla.AppConfig{}
|
|
|
|
+ if err := newconf.Load([]byte(configJsonB)); err != nil {
|
|
|
|
+ t.Error("configJsonB is invalid", err)
|
|
|
|
+ }
|
|
|
|
|
|
- newerconf := &CmdConfig{}
|
|
|
|
- newerconf.load([]byte(configJsonC))
|
|
|
|
|
|
+ newerconf := &guerrilla.AppConfig{}
|
|
|
|
+ if err := newerconf.Load([]byte(configJsonC)); err != nil {
|
|
|
|
+ t.Error("configJsonC is invalid", err)
|
|
|
|
+ }
|
|
|
|
|
|
expectedEvents := map[guerrilla.Event]bool{
|
|
expectedEvents := map[guerrilla.Event]bool{
|
|
- guerrilla.EvConfigBackendConfig: false,
|
|
|
|
- guerrilla.EvConfigBackendName: false,
|
|
|
|
- guerrilla.EvConfigEvServerNew: false,
|
|
|
|
|
|
+ guerrilla.EventConfigBackendConfig: false,
|
|
|
|
+ guerrilla.EventConfigServerNew: false,
|
|
}
|
|
}
|
|
- mainlog, _ = log.GetLogger("off")
|
|
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
|
|
|
|
bcfg := backends.BackendConfig{"log_received_mails": true}
|
|
bcfg := backends.BackendConfig{"log_received_mails": true}
|
|
- backend, err := backends.New("dummy", bcfg, mainlog)
|
|
|
|
- app, err := guerrilla.New(&oldconf.AppConfig, backend, mainlog)
|
|
|
|
|
|
+ backend, err := backends.New(bcfg, mainlog)
|
|
|
|
+ app, err := guerrilla.New(oldconf, backend, mainlog)
|
|
if err != nil {
|
|
if err != nil {
|
|
- //log.Info("Failed to create new app", err)
|
|
|
|
|
|
+ t.Error("Failed to create new app", err)
|
|
}
|
|
}
|
|
- toUnsubscribe := map[guerrilla.Event]func(c *CmdConfig){}
|
|
|
|
|
|
+ toUnsubscribe := map[guerrilla.Event]func(c *guerrilla.AppConfig){}
|
|
toUnsubscribeS := map[guerrilla.Event]func(c *guerrilla.ServerConfig){}
|
|
toUnsubscribeS := map[guerrilla.Event]func(c *guerrilla.ServerConfig){}
|
|
|
|
|
|
for event := range expectedEvents {
|
|
for event := range expectedEvents {
|
|
// Put in anon func since range is overwriting event
|
|
// Put in anon func since range is overwriting event
|
|
func(e guerrilla.Event) {
|
|
func(e guerrilla.Event) {
|
|
-
|
|
|
|
if strings.Index(e.String(), "server_change") == 0 {
|
|
if strings.Index(e.String(), "server_change") == 0 {
|
|
f := func(c *guerrilla.ServerConfig) {
|
|
f := func(c *guerrilla.ServerConfig) {
|
|
expectedEvents[e] = true
|
|
expectedEvents[e] = true
|
|
}
|
|
}
|
|
- app.Subscribe(event, f)
|
|
|
|
- toUnsubscribeS[event] = f
|
|
|
|
|
|
+ app.Subscribe(e, f)
|
|
|
|
+ toUnsubscribeS[e] = f
|
|
} else {
|
|
} else {
|
|
- f := func(c *CmdConfig) {
|
|
|
|
|
|
+ f := func(c *guerrilla.AppConfig) {
|
|
expectedEvents[e] = true
|
|
expectedEvents[e] = true
|
|
}
|
|
}
|
|
- app.Subscribe(event, f)
|
|
|
|
- toUnsubscribe[event] = f
|
|
|
|
|
|
+ app.Subscribe(e, f)
|
|
|
|
+ toUnsubscribe[e] = f
|
|
}
|
|
}
|
|
|
|
|
|
}(event)
|
|
}(event)
|
|
}
|
|
}
|
|
|
|
|
|
// emit events
|
|
// emit events
|
|
- newconf.emitChangeEvents(oldconf, app)
|
|
|
|
- newerconf.emitChangeEvents(newconf, app)
|
|
|
|
|
|
+ newconf.EmitChangeEvents(oldconf, app)
|
|
|
|
+ newerconf.EmitChangeEvents(newconf, app)
|
|
// unsubscribe
|
|
// unsubscribe
|
|
for unevent, unfun := range toUnsubscribe {
|
|
for unevent, unfun := range toUnsubscribe {
|
|
app.Unsubscribe(unevent, unfun)
|
|
app.Unsubscribe(unevent, unfun)
|
|
}
|
|
}
|
|
|
|
+ for unevent, unfun := range toUnsubscribeS {
|
|
|
|
+ app.Unsubscribe(unevent, unfun)
|
|
|
|
+ }
|
|
|
|
|
|
for event, val := range expectedEvents {
|
|
for event, val := range expectedEvents {
|
|
if val == false {
|
|
if val == false {
|
|
@@ -311,9 +382,10 @@ func TestCmdConfigChangeEvents(t *testing.T) {
|
|
|
|
|
|
// start server, change config, send SIG HUP, confirm that the pidfile changed & backend reloaded
|
|
// start server, change config, send SIG HUP, confirm that the pidfile changed & backend reloaded
|
|
func TestServe(t *testing.T) {
|
|
func TestServe(t *testing.T) {
|
|
|
|
+
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
|
|
|
|
- mainlog, _ = log.GetLogger("../../tests/testlog")
|
|
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
|
|
|
|
ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644)
|
|
ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644)
|
|
cmd := &cobra.Command{}
|
|
cmd := &cobra.Command{}
|
|
@@ -344,12 +416,7 @@ func TestServe(t *testing.T) {
|
|
// Would not work on windows as kill is not available.
|
|
// Would not work on windows as kill is not available.
|
|
// TODO: Implement an alternative test for windows.
|
|
// TODO: Implement an alternative test for windows.
|
|
if runtime.GOOS != "windows" {
|
|
if runtime.GOOS != "windows" {
|
|
- ecmd := exec.Command("kill", "-HUP", string(data))
|
|
|
|
- _, err = ecmd.Output()
|
|
|
|
- if err != nil {
|
|
|
|
- t.Error("could not SIGHUP", err)
|
|
|
|
- t.FailNow()
|
|
|
|
- }
|
|
|
|
|
|
+ sigHup()
|
|
time.Sleep(testPauseDuration) // allow sighup to do its job
|
|
time.Sleep(testPauseDuration) // allow sighup to do its job
|
|
// did the pidfile change as expected?
|
|
// did the pidfile change as expected?
|
|
if _, err := os.Stat("./pidfile2.pid"); os.IsNotExist(err) {
|
|
if _, err := os.Stat("./pidfile2.pid"); os.IsNotExist(err) {
|
|
@@ -358,6 +425,7 @@ func TestServe(t *testing.T) {
|
|
}
|
|
}
|
|
// send kill signal and wait for exit
|
|
// send kill signal and wait for exit
|
|
sigKill()
|
|
sigKill()
|
|
|
|
+ // wait for exit
|
|
serveWG.Wait()
|
|
serveWG.Wait()
|
|
|
|
|
|
// did backend started as expected?
|
|
// did backend started as expected?
|
|
@@ -367,7 +435,7 @@ func TestServe(t *testing.T) {
|
|
}
|
|
}
|
|
if read, err := ioutil.ReadAll(fd); err == nil {
|
|
if read, err := ioutil.ReadAll(fd); err == nil {
|
|
logOutput := string(read)
|
|
logOutput := string(read)
|
|
- if i := strings.Index(logOutput, "Backend started:dummy"); i < 0 {
|
|
|
|
|
|
+ if i := strings.Index(logOutput, "new backend started"); i < 0 {
|
|
t.Error("Dummy backend not restared")
|
|
t.Error("Dummy backend not restared")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -386,7 +454,7 @@ func TestServe(t *testing.T) {
|
|
// then connect to it & HELO.
|
|
// then connect to it & HELO.
|
|
func TestServerAddEvent(t *testing.T) {
|
|
func TestServerAddEvent(t *testing.T) {
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
- mainlog, _ = log.GetLogger("../../tests/testlog")
|
|
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
// start the server by emulating the serve command
|
|
// start the server by emulating the serve command
|
|
ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644)
|
|
ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644)
|
|
cmd := &cobra.Command{}
|
|
cmd := &cobra.Command{}
|
|
@@ -399,8 +467,8 @@ func TestServerAddEvent(t *testing.T) {
|
|
}()
|
|
}()
|
|
time.Sleep(testPauseDuration) // allow the server to start
|
|
time.Sleep(testPauseDuration) // allow the server to start
|
|
// now change the config by adding a server
|
|
// now change the config by adding a server
|
|
- conf := &CmdConfig{} // blank one
|
|
|
|
- conf.load([]byte(configJsonA)) // load configJsonA
|
|
|
|
|
|
+ conf := &guerrilla.AppConfig{} // blank one
|
|
|
|
+ conf.Load([]byte(configJsonA)) // load configJsonA
|
|
newServer := conf.Servers[0] // copy the first server config
|
|
newServer := conf.Servers[0] // copy the first server config
|
|
newServer.ListenInterface = "127.0.0.1:2526" // change it
|
|
newServer.ListenInterface = "127.0.0.1:2526" // change it
|
|
newConf := conf // copy the cmdConfg
|
|
newConf := conf // copy the cmdConfg
|
|
@@ -453,7 +521,7 @@ func TestServerAddEvent(t *testing.T) {
|
|
// then connect to 127.0.0.1:2228 & HELO.
|
|
// then connect to 127.0.0.1:2228 & HELO.
|
|
func TestServerStartEvent(t *testing.T) {
|
|
func TestServerStartEvent(t *testing.T) {
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
- mainlog, _ = log.GetLogger("../../tests/testlog")
|
|
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
// start the server by emulating the serve command
|
|
// start the server by emulating the serve command
|
|
ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644)
|
|
ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644)
|
|
cmd := &cobra.Command{}
|
|
cmd := &cobra.Command{}
|
|
@@ -466,8 +534,8 @@ func TestServerStartEvent(t *testing.T) {
|
|
}()
|
|
}()
|
|
time.Sleep(testPauseDuration)
|
|
time.Sleep(testPauseDuration)
|
|
// now change the config by adding a server
|
|
// now change the config by adding a server
|
|
- conf := &CmdConfig{} // blank one
|
|
|
|
- conf.load([]byte(configJsonA)) // load configJsonA
|
|
|
|
|
|
+ conf := &guerrilla.AppConfig{} // blank one
|
|
|
|
+ conf.Load([]byte(configJsonA)) // load configJsonA
|
|
|
|
|
|
newConf := conf // copy the cmdConfg
|
|
newConf := conf // copy the cmdConfg
|
|
newConf.Servers[1].IsEnabled = true
|
|
newConf.Servers[1].IsEnabled = true
|
|
@@ -523,7 +591,7 @@ func TestServerStartEvent(t *testing.T) {
|
|
|
|
|
|
func TestServerStopEvent(t *testing.T) {
|
|
func TestServerStopEvent(t *testing.T) {
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
- mainlog, _ = log.GetLogger("../../tests/testlog")
|
|
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
// start the server by emulating the serve command
|
|
// start the server by emulating the serve command
|
|
ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644)
|
|
ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644)
|
|
cmd := &cobra.Command{}
|
|
cmd := &cobra.Command{}
|
|
@@ -536,8 +604,8 @@ func TestServerStopEvent(t *testing.T) {
|
|
}()
|
|
}()
|
|
time.Sleep(testPauseDuration)
|
|
time.Sleep(testPauseDuration)
|
|
// now change the config by enabling a server
|
|
// now change the config by enabling a server
|
|
- conf := &CmdConfig{} // blank one
|
|
|
|
- conf.load([]byte(configJsonA)) // load configJsonA
|
|
|
|
|
|
+ conf := &guerrilla.AppConfig{} // blank one
|
|
|
|
+ conf.Load([]byte(configJsonA)) // load configJsonA
|
|
|
|
|
|
newConf := conf // copy the cmdConfg
|
|
newConf := conf // copy the cmdConfg
|
|
newConf.Servers[1].IsEnabled = true
|
|
newConf.Servers[1].IsEnabled = true
|
|
@@ -611,11 +679,11 @@ func TestServerStopEvent(t *testing.T) {
|
|
|
|
|
|
func TestAllowedHostsEvent(t *testing.T) {
|
|
func TestAllowedHostsEvent(t *testing.T) {
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
- mainlog, _ = log.GetLogger("../../tests/testlog")
|
|
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
// start the server by emulating the serve command
|
|
// start the server by emulating the serve command
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
- conf := &CmdConfig{} // blank one
|
|
|
|
- conf.load([]byte(configJsonD)) // load configJsonD
|
|
|
|
|
|
+ conf := &guerrilla.AppConfig{} // blank one
|
|
|
|
+ conf.Load([]byte(configJsonD)) // load configJsonD
|
|
cmd := &cobra.Command{}
|
|
cmd := &cobra.Command{}
|
|
configPath = "configJsonD.json"
|
|
configPath = "configJsonD.json"
|
|
var serveWG sync.WaitGroup
|
|
var serveWG sync.WaitGroup
|
|
@@ -628,8 +696,8 @@ func TestAllowedHostsEvent(t *testing.T) {
|
|
time.Sleep(testPauseDuration)
|
|
time.Sleep(testPauseDuration)
|
|
|
|
|
|
// now connect and try RCPT TO with an invalid host
|
|
// now connect and try RCPT TO with an invalid host
|
|
- if conn, buffin, err := test.Connect(conf.AppConfig.Servers[1], 20); err != nil {
|
|
|
|
- t.Error("Could not connect to new server", conf.AppConfig.Servers[1].ListenInterface, err)
|
|
|
|
|
|
+ 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 {
|
|
} else {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
expect := "250 secure.test.com Hello"
|
|
expect := "250 secure.test.com Hello"
|
|
@@ -649,7 +717,7 @@ func TestAllowedHostsEvent(t *testing.T) {
|
|
|
|
|
|
// now change the config by adding a host to allowed hosts
|
|
// now change the config by adding a host to allowed hosts
|
|
|
|
|
|
- newConf := conf // copy the cmdConfg
|
|
|
|
|
|
+ newConf := conf
|
|
newConf.AllowedHosts = append(newConf.AllowedHosts, "grr.la")
|
|
newConf.AllowedHosts = append(newConf.AllowedHosts, "grr.la")
|
|
if jsonbytes, err := json.Marshal(newConf); err == nil {
|
|
if jsonbytes, err := json.Marshal(newConf); err == nil {
|
|
ioutil.WriteFile("configJsonD.json", []byte(jsonbytes), 0644)
|
|
ioutil.WriteFile("configJsonD.json", []byte(jsonbytes), 0644)
|
|
@@ -661,8 +729,8 @@ func TestAllowedHostsEvent(t *testing.T) {
|
|
time.Sleep(testPauseDuration) // pause for config to reload
|
|
time.Sleep(testPauseDuration) // pause for config to reload
|
|
|
|
|
|
// now repeat the same conversion, RCPT TO should be accepted
|
|
// now repeat the same conversion, RCPT TO should be accepted
|
|
- if conn, buffin, err := test.Connect(conf.AppConfig.Servers[1], 20); err != nil {
|
|
|
|
- t.Error("Could not connect to new server", conf.AppConfig.Servers[1].ListenInterface, err)
|
|
|
|
|
|
+ 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 {
|
|
} else {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
expect := "250 secure.test.com Hello"
|
|
expect := "250 secure.test.com Hello"
|
|
@@ -690,7 +758,7 @@ func TestAllowedHostsEvent(t *testing.T) {
|
|
//fmt.Println(logOutput)
|
|
//fmt.Println(logOutput)
|
|
if i := strings.Index(logOutput, "allowed_hosts config changed, a new list was set"); i < 0 {
|
|
if i := strings.Index(logOutput, "allowed_hosts config changed, a new list was set"); i < 0 {
|
|
t.Errorf("did not change allowed_hosts, most likely because Bus.Subscribe(\"%s\" didnt fire",
|
|
t.Errorf("did not change allowed_hosts, most likely because Bus.Subscribe(\"%s\" didnt fire",
|
|
- guerrilla.EvConfigAllowedHosts)
|
|
|
|
|
|
+ guerrilla.EventConfigAllowedHosts)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// cleanup
|
|
// cleanup
|
|
@@ -714,11 +782,11 @@ func TestTLSConfigEvent(t *testing.T) {
|
|
if _, err := os.Stat("../../tests/mail2.guerrillamail.com.cert.pem"); err != nil {
|
|
if _, err := os.Stat("../../tests/mail2.guerrillamail.com.cert.pem"); err != nil {
|
|
t.Error("Did not create cert ", err)
|
|
t.Error("Did not create cert ", err)
|
|
}
|
|
}
|
|
- mainlog, _ = log.GetLogger("../../tests/testlog")
|
|
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
// start the server by emulating the serve command
|
|
// start the server by emulating the serve command
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
- conf := &CmdConfig{} // blank one
|
|
|
|
- conf.load([]byte(configJsonD)) // load configJsonD
|
|
|
|
|
|
+ conf := &guerrilla.AppConfig{} // blank one
|
|
|
|
+ conf.Load([]byte(configJsonD)) // load configJsonD
|
|
cmd := &cobra.Command{}
|
|
cmd := &cobra.Command{}
|
|
configPath = "configJsonD.json"
|
|
configPath = "configJsonD.json"
|
|
var serveWG sync.WaitGroup
|
|
var serveWG sync.WaitGroup
|
|
@@ -731,8 +799,8 @@ func TestTLSConfigEvent(t *testing.T) {
|
|
|
|
|
|
// Test STARTTLS handshake
|
|
// Test STARTTLS handshake
|
|
testTlsHandshake := func() {
|
|
testTlsHandshake := func() {
|
|
- if conn, buffin, err := test.Connect(conf.AppConfig.Servers[0], 20); err != nil {
|
|
|
|
- t.Error("Could not connect to server", conf.AppConfig.Servers[0].ListenInterface, err)
|
|
|
|
|
|
+ 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 {
|
|
} else {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
expect := "250 mail.test.com Hello"
|
|
expect := "250 mail.test.com Hello"
|
|
@@ -749,7 +817,7 @@ func TestTLSConfigEvent(t *testing.T) {
|
|
ServerName: "127.0.0.1",
|
|
ServerName: "127.0.0.1",
|
|
})
|
|
})
|
|
if err := tlsConn.Handshake(); err != nil {
|
|
if err := tlsConn.Handshake(); err != nil {
|
|
- t.Error("Failed to handshake", conf.AppConfig.Servers[0].ListenInterface)
|
|
|
|
|
|
+ t.Error("Failed to handshake", conf.Servers[0].ListenInterface)
|
|
} else {
|
|
} else {
|
|
conn = tlsConn
|
|
conn = tlsConn
|
|
mainlog.Info("TLS Handshake succeeded")
|
|
mainlog.Info("TLS Handshake succeeded")
|
|
@@ -821,8 +889,8 @@ func TestBadTLSStart(t *testing.T) {
|
|
}
|
|
}
|
|
// next run the server
|
|
// next run the server
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
- conf := &CmdConfig{} // blank one
|
|
|
|
- conf.load([]byte(configJsonD)) // load configJsonD
|
|
|
|
|
|
+ conf := &guerrilla.AppConfig{} // blank one
|
|
|
|
+ conf.Load([]byte(configJsonD)) // load configJsonD
|
|
|
|
|
|
cmd := &cobra.Command{}
|
|
cmd := &cobra.Command{}
|
|
configPath = "configJsonD.json"
|
|
configPath = "configJsonD.json"
|
|
@@ -856,13 +924,13 @@ func TestBadTLSStart(t *testing.T) {
|
|
// Test config reload with a bad TLS config
|
|
// Test config reload with a bad TLS config
|
|
// It should ignore the config reload, keep running with old settings
|
|
// It should ignore the config reload, keep running with old settings
|
|
func TestBadTLSReload(t *testing.T) {
|
|
func TestBadTLSReload(t *testing.T) {
|
|
- mainlog, _ = log.GetLogger("../../tests/testlog")
|
|
|
|
- // start with a good vert
|
|
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
|
|
+ // start with a good cert
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
// start the server by emulating the serve command
|
|
// start the server by emulating the serve command
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
- conf := &CmdConfig{} // blank one
|
|
|
|
- conf.load([]byte(configJsonD)) // load configJsonD
|
|
|
|
|
|
+ conf := &guerrilla.AppConfig{} // blank one
|
|
|
|
+ conf.Load([]byte(configJsonD)) // load configJsonD
|
|
cmd := &cobra.Command{}
|
|
cmd := &cobra.Command{}
|
|
configPath = "configJsonD.json"
|
|
configPath = "configJsonD.json"
|
|
var serveWG sync.WaitGroup
|
|
var serveWG sync.WaitGroup
|
|
@@ -874,8 +942,8 @@ func TestBadTLSReload(t *testing.T) {
|
|
}()
|
|
}()
|
|
time.Sleep(testPauseDuration)
|
|
time.Sleep(testPauseDuration)
|
|
|
|
|
|
- if conn, buffin, err := test.Connect(conf.AppConfig.Servers[0], 20); err != nil {
|
|
|
|
- t.Error("Could not connect to server", conf.AppConfig.Servers[0].ListenInterface, err)
|
|
|
|
|
|
+ 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 {
|
|
} else {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
expect := "250 mail.test.com Hello"
|
|
expect := "250 mail.test.com Hello"
|
|
@@ -901,8 +969,8 @@ func TestBadTLSReload(t *testing.T) {
|
|
|
|
|
|
// we should still be able to to talk to it
|
|
// we should still be able to to talk to it
|
|
|
|
|
|
- if conn, buffin, err := test.Connect(conf.AppConfig.Servers[0], 20); err != nil {
|
|
|
|
- t.Error("Could not connect to server", conf.AppConfig.Servers[0].ListenInterface, err)
|
|
|
|
|
|
+ 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 {
|
|
} else {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
expect := "250 mail.test.com Hello"
|
|
expect := "250 mail.test.com Hello"
|
|
@@ -934,12 +1002,12 @@ func TestBadTLSReload(t *testing.T) {
|
|
// Start with configJsonD.json
|
|
// Start with configJsonD.json
|
|
|
|
|
|
func TestSetTimeoutEvent(t *testing.T) {
|
|
func TestSetTimeoutEvent(t *testing.T) {
|
|
- mainlog, _ = log.GetLogger("../../tests/testlog")
|
|
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
// start the server by emulating the serve command
|
|
// start the server by emulating the serve command
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
- conf := &CmdConfig{} // blank one
|
|
|
|
- conf.load([]byte(configJsonD)) // load configJsonD
|
|
|
|
|
|
+ conf := &guerrilla.AppConfig{} // blank one
|
|
|
|
+ conf.Load([]byte(configJsonD)) // load configJsonD
|
|
cmd := &cobra.Command{}
|
|
cmd := &cobra.Command{}
|
|
configPath = "configJsonD.json"
|
|
configPath = "configJsonD.json"
|
|
var serveWG sync.WaitGroup
|
|
var serveWG sync.WaitGroup
|
|
@@ -966,8 +1034,8 @@ func TestSetTimeoutEvent(t *testing.T) {
|
|
time.Sleep(testPauseDuration) // config reload
|
|
time.Sleep(testPauseDuration) // config reload
|
|
|
|
|
|
var waitTimeout sync.WaitGroup
|
|
var waitTimeout sync.WaitGroup
|
|
- if conn, buffin, err := test.Connect(conf.AppConfig.Servers[0], 20); err != nil {
|
|
|
|
- t.Error("Could not connect to server", conf.AppConfig.Servers[0].ListenInterface, err)
|
|
|
|
|
|
+ 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 {
|
|
} else {
|
|
waitTimeout.Add(1)
|
|
waitTimeout.Add(1)
|
|
go func() {
|
|
go func() {
|
|
@@ -996,7 +1064,7 @@ func TestSetTimeoutEvent(t *testing.T) {
|
|
fd, _ := os.Open("../../tests/testlog")
|
|
fd, _ := os.Open("../../tests/testlog")
|
|
if read, err := ioutil.ReadAll(fd); err == nil {
|
|
if read, err := ioutil.ReadAll(fd); err == nil {
|
|
logOutput := string(read)
|
|
logOutput := string(read)
|
|
- fmt.Println(logOutput)
|
|
|
|
|
|
+ //fmt.Println(logOutput)
|
|
if i := strings.Index(logOutput, "i/o timeout"); i < 0 {
|
|
if i := strings.Index(logOutput, "i/o timeout"); i < 0 {
|
|
t.Error("Connection to 127.0.0.1:2552 didn't timeout as expected")
|
|
t.Error("Connection to 127.0.0.1:2552 didn't timeout as expected")
|
|
}
|
|
}
|
|
@@ -1012,12 +1080,12 @@ func TestSetTimeoutEvent(t *testing.T) {
|
|
// Start in log_level = debug
|
|
// Start in log_level = debug
|
|
// Load config & start server
|
|
// Load config & start server
|
|
func TestDebugLevelChange(t *testing.T) {
|
|
func TestDebugLevelChange(t *testing.T) {
|
|
- //mainlog, _ = log.GetLogger("../../tests/testlog")
|
|
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
// start the server by emulating the serve command
|
|
// start the server by emulating the serve command
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
ioutil.WriteFile("configJsonD.json", []byte(configJsonD), 0644)
|
|
- conf := &CmdConfig{} // blank one
|
|
|
|
- conf.load([]byte(configJsonD)) // load configJsonD
|
|
|
|
|
|
+ conf := &guerrilla.AppConfig{} // blank one
|
|
|
|
+ conf.Load([]byte(configJsonD)) // load configJsonD
|
|
conf.LogLevel = "debug"
|
|
conf.LogLevel = "debug"
|
|
cmd := &cobra.Command{}
|
|
cmd := &cobra.Command{}
|
|
configPath = "configJsonD.json"
|
|
configPath = "configJsonD.json"
|
|
@@ -1030,8 +1098,8 @@ func TestDebugLevelChange(t *testing.T) {
|
|
}()
|
|
}()
|
|
time.Sleep(testPauseDuration)
|
|
time.Sleep(testPauseDuration)
|
|
|
|
|
|
- if conn, buffin, err := test.Connect(conf.AppConfig.Servers[0], 20); err != nil {
|
|
|
|
- t.Error("Could not connect to server", conf.AppConfig.Servers[0].ListenInterface, err)
|
|
|
|
|
|
+ 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 {
|
|
} else {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
if result, err := test.Command(conn, buffin, "HELO"); err == nil {
|
|
expect := "250 mail.test.com Hello"
|
|
expect := "250 mail.test.com Hello"
|
|
@@ -1044,7 +1112,7 @@ func TestDebugLevelChange(t *testing.T) {
|
|
// set the log_level to info
|
|
// set the log_level to info
|
|
|
|
|
|
newConf := conf // copy the cmdConfg
|
|
newConf := conf // copy the cmdConfg
|
|
- newConf.LogLevel = "info"
|
|
|
|
|
|
+ newConf.LogLevel = log.InfoLevel.String()
|
|
if jsonbytes, err := json.Marshal(newConf); err == nil {
|
|
if jsonbytes, err := json.Marshal(newConf); err == nil {
|
|
ioutil.WriteFile("configJsonD.json", []byte(jsonbytes), 0644)
|
|
ioutil.WriteFile("configJsonD.json", []byte(jsonbytes), 0644)
|
|
} else {
|
|
} else {
|
|
@@ -1055,8 +1123,8 @@ func TestDebugLevelChange(t *testing.T) {
|
|
time.Sleep(testPauseDuration) // log to change
|
|
time.Sleep(testPauseDuration) // log to change
|
|
|
|
|
|
// connect again, this time we should see info
|
|
// connect again, this time we should see info
|
|
- if conn, buffin, err := test.Connect(conf.AppConfig.Servers[0], 20); err != nil {
|
|
|
|
- t.Error("Could not connect to server", conf.AppConfig.Servers[0].ListenInterface, err)
|
|
|
|
|
|
+ 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 {
|
|
} else {
|
|
if result, err := test.Command(conn, buffin, "NOOP"); err == nil {
|
|
if result, err := test.Command(conn, buffin, "NOOP"); err == nil {
|
|
expect := "200 2.0.0 OK"
|
|
expect := "200 2.0.0 OK"
|
|
@@ -1089,3 +1157,59 @@ func TestDebugLevelChange(t *testing.T) {
|
|
os.Remove("./pidfile.pid")
|
|
os.Remove("./pidfile.pid")
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// When reloading with a bad backend config, it should revert to old backend config
|
|
|
|
+func TestBadBackendReload(t *testing.T) {
|
|
|
|
+ testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "../../tests/")
|
|
|
|
+
|
|
|
|
+ mainlog, _ = log.GetLogger("../../tests/testlog", "debug")
|
|
|
|
+
|
|
|
|
+ ioutil.WriteFile("configJsonA.json", []byte(configJsonA), 0644)
|
|
|
|
+ cmd := &cobra.Command{}
|
|
|
|
+ configPath = "configJsonA.json"
|
|
|
|
+ var serveWG sync.WaitGroup
|
|
|
|
+ serveWG.Add(1)
|
|
|
|
+ go func() {
|
|
|
|
+ serve(cmd, []string{})
|
|
|
|
+ serveWG.Done()
|
|
|
|
+ }()
|
|
|
|
+ time.Sleep(testPauseDuration)
|
|
|
|
+
|
|
|
|
+ // change the config file to the one with a broken backend
|
|
|
|
+ ioutil.WriteFile("configJsonA.json", []byte(configJsonE), 0644)
|
|
|
|
+
|
|
|
|
+ // 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()
|
|
|
|
+ time.Sleep(testPauseDuration) // allow sighup to do its job
|
|
|
|
+ // did the pidfile change as expected?
|
|
|
|
+ if _, err := os.Stat("./pidfile2.pid"); os.IsNotExist(err) {
|
|
|
|
+ t.Error("pidfile not changed after sighup SIGHUP", err)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // send kill signal and wait for exit
|
|
|
|
+ sigKill()
|
|
|
|
+ serveWG.Wait()
|
|
|
|
+
|
|
|
|
+ // did backend started as expected?
|
|
|
|
+ fd, err := os.Open("../../tests/testlog")
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Error(err)
|
|
|
|
+ }
|
|
|
|
+ if read, err := ioutil.ReadAll(fd); err == nil {
|
|
|
|
+ logOutput := string(read)
|
|
|
|
+ if i := strings.Index(logOutput, "reverted to old backend config"); i < 0 {
|
|
|
|
+ t.Error("did not revert to old backend config")
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // cleanup
|
|
|
|
+ os.Truncate("../../tests/testlog", 0)
|
|
|
|
+ os.Remove("configJsonA.json")
|
|
|
|
+ os.Remove("./pidfile.pid")
|
|
|
|
+ os.Remove("./pidfile2.pid")
|
|
|
|
+
|
|
|
|
+}
|