config_test.go 8.0 KB

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