forked from flashmob/go-guerrilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
289 lines (271 loc) · 8.14 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package guerrilla
import (
"github.com/flashmob/go-guerrilla/backends"
"github.com/flashmob/go-guerrilla/log"
"github.com/flashmob/go-guerrilla/tests/testcert"
"io/ioutil"
"os"
"strings"
"testing"
"time"
)
func init() {
testcert.GenerateCert("mail2.guerrillamail.com", "", 365*24*time.Hour, false, 2048, "P256", "./tests/")
}
// a configuration file with a dummy backend
//
var configJsonA = `
{
"log_file" : "./tests/testlog",
"log_level" : "debug",
"pid_file" : "tests/go-guerrilla.pid",
"allowed_hosts": ["spam4.me","grr.la"],
"backend_config" :
{
"log_received_mails" : true
},
"servers" : [
{
"is_enabled" : true,
"host_name":"mail.guerrillamail.com",
"max_size": 100017,
"timeout":160,
"listen_interface":"127.0.0.1:2526",
"max_clients": 2,
"tls" : {
"start_tls_on":false,
"tls_always_on":false,
"private_key_file":"config_test.go",
"public_key_file":"config_test.go"
}
},
{
"is_enabled" : true,
"host_name":"mail2.guerrillamail.com",
"max_size":1000001,
"timeout":180,
"listen_interface":"127.0.0.1:2527",
"max_clients":1,
"tls" : {
"private_key_file":"./tests/mail2.guerrillamail.com.key.pem",
"public_key_file":"./tests/mail2.guerrillamail.com.cert.pem",
"tls_always_on":false,
"start_tls_on":true
}
},
{
"is_enabled" : true,
"host_name":"mail.stopme.com",
"max_size": 100017,
"timeout":160,
"listen_interface":"127.0.0.1:9999",
"max_clients": 2,
"tls" : {
"private_key_file":"config_test.go",
"public_key_file":"config_test.go",
"start_tls_on":false,
"tls_always_on":false
}
},
{
"is_enabled" : true,
"host_name":"mail.disableme.com",
"max_size": 100017,
"timeout":160,
"listen_interface":"127.0.0.1:3333",
"max_clients": 2,
"tls" : {
"private_key_file":"config_test.go",
"public_key_file":"config_test.go",
"start_tls_on":false,
"tls_always_on":false
}
}
]
}
`
// B is A's configuration with different values from B
// 127.0.0.1:4654 will be added
// A's 127.0.0.1:3333 is disabled
// B's 127.0.0.1:9999 is removed
var configJsonB = `
{
"log_file" : "./tests/testlog",
"log_level" : "debug",
"pid_file" : "tests/different-go-guerrilla.pid",
"allowed_hosts": ["spam4.me","grr.la","newhost.com"],
"backend_config" :
{
"log_received_mails" : true
},
"servers" : [
{
"is_enabled" : true,
"host_name":"mail.guerrillamail.com",
"max_size": 100017,
"timeout":161,
"listen_interface":"127.0.0.1:2526",
"max_clients": 3,
"tls" : {
"private_key_file":"./config_test.go",
"public_key_file":"./config_test.go",
"start_tls_on":false,
"tls_always_on":true
}
},
{
"is_enabled" : true,
"host_name":"mail2.guerrillamail.com",
"max_size": 100017,
"timeout":160,
"listen_interface":"127.0.0.1:2527",
"log_file" : "./tests/testlog",
"max_clients": 2,
"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.guerrillamail.com",
"max_size":1000001,
"timeout":180,
"listen_interface":"127.0.0.1:4654",
"max_clients":1,
"tls" : {
"private_key_file":"config_test.go",
"public_key_file":"config_test.go",
"start_tls_on":false,
"tls_always_on":false
}
},
{
"is_enabled" : false,
"host_name":"mail.disbaleme.com",
"max_size": 100017,
"timeout":160,
"listen_interface":"127.0.0.1:3333",
"max_clients": 2,
"tls" : {
"private_key_file":"config_test.go",
"public_key_file":"config_test.go",
"start_tls_on":true,
"tls_always_on":false
}
}
]
}
`
func TestConfigLoad(t *testing.T) {
ac := &AppConfig{}
if err := ac.Load([]byte(configJsonA)); err != nil {
t.Error("Cannot load config |", err)
t.SkipNow()
}
expectedLen := 4
if len(ac.Servers) != expectedLen {
t.Error("len(ac.Servers), expected", expectedLen, "got", len(ac.Servers))
t.SkipNow()
}
// did we got the timestamps?
if ac.Servers[0].TLS._privateKeyFile_mtime <= 0 {
t.Error("failed to read timestamp for _privateKeyFile_mtime, got", ac.Servers[0].TLS._privateKeyFile_mtime)
}
}
// Test the sample config to make sure a valid one is given!
func TestSampleConfig(t *testing.T) {
fileName := "goguerrilla.conf.sample"
if jsonBytes, err := ioutil.ReadFile(fileName); err == nil {
ac := &AppConfig{}
if err := ac.Load(jsonBytes); err != nil {
// sample config can have broken tls certs
if strings.Index(err.Error(), "cannot use TLS config for [127.0.0.1:25") != 0 {
t.Error("Cannot load config", fileName, "|", err)
t.FailNow()
}
}
} else {
t.Error("Error reading", fileName, "|", err)
}
}
// make sure that we get all the config change events
func TestConfigChangeEvents(t *testing.T) {
oldconf := &AppConfig{}
oldconf.Load([]byte(configJsonA))
logger, _ := log.GetLogger(oldconf.LogFile, oldconf.LogLevel)
bcfg := backends.BackendConfig{"log_received_mails": true}
backend, err := backends.New(bcfg, logger)
if err != nil {
t.Error("cannot create backend", err)
}
app, err := New(oldconf, backend, logger)
if err != nil {
t.Error("cannot create daemon", err)
}
// simulate timestamp change
time.Sleep(time.Second + time.Millisecond*500)
os.Chtimes(oldconf.Servers[1].TLS.PrivateKeyFile, time.Now(), time.Now())
os.Chtimes(oldconf.Servers[1].TLS.PublicKeyFile, time.Now(), time.Now())
newconf := &AppConfig{}
newconf.Load([]byte(configJsonB))
newconf.Servers[0].LogFile = log.OutputOff.String() // test for log file change
newconf.LogLevel = log.InfoLevel.String()
newconf.LogFile = "off"
expectedEvents := map[Event]bool{
EventConfigPidFile: false,
EventConfigLogFile: false,
EventConfigLogLevel: false,
EventConfigAllowedHosts: false,
EventConfigServerNew: false, // 127.0.0.1:4654 will be added
EventConfigServerRemove: false, // 127.0.0.1:9999 server removed
EventConfigServerStop: false, // 127.0.0.1:3333: server (disabled)
EventConfigServerLogFile: false, // 127.0.0.1:2526
EventConfigServerLogReopen: false, // 127.0.0.1:2527
EventConfigServerTimeout: false, // 127.0.0.1:2526 timeout
//"server_change:tls_config": false, // 127.0.0.1:2526
EventConfigServerMaxClients: false, // 127.0.0.1:2526
EventConfigServerTLSConfig: false, // 127.0.0.1:2527 timestamp changed on certificates
}
toUnsubscribe := map[Event]func(c *AppConfig){}
toUnsubscribeSrv := map[Event]func(c *ServerConfig){}
for event := range expectedEvents {
// Put in anon func since range is overwriting event
func(e Event) {
if strings.Index(e.String(), "config_change") != -1 {
f := func(c *AppConfig) {
expectedEvents[e] = true
}
app.Subscribe(event, f)
toUnsubscribe[event] = f
} else {
// must be a server config change then
f := func(c *ServerConfig) {
expectedEvents[e] = true
}
app.Subscribe(event, f)
toUnsubscribeSrv[event] = f
}
}(event)
}
// emit events
newconf.EmitChangeEvents(oldconf, app)
// unsubscribe
for unevent, unfun := range toUnsubscribe {
app.Unsubscribe(unevent, unfun)
}
for unevent, unfun := range toUnsubscribeSrv {
app.Unsubscribe(unevent, unfun)
}
for event, val := range expectedEvents {
if val == false {
t.Error("Did not fire config change event:", event)
t.FailNow()
break
}
}
// don't forget to reset
os.Truncate(oldconf.LogFile, 0)
}