config_test.go 8.1 KB

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