config_test.go 8.1 KB

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