config_test.go 8.3 KB

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