api_test.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. package guerrilla
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "os"
  9. "strings"
  10. "testing"
  11. "time"
  12. "github.com/flashmob/go-guerrilla/backends"
  13. _ "github.com/flashmob/go-guerrilla/chunk"
  14. "github.com/flashmob/go-guerrilla/log"
  15. "github.com/flashmob/go-guerrilla/mail"
  16. "github.com/flashmob/go-guerrilla/response"
  17. )
  18. // Test Starting smtp without setting up logger / backend
  19. func TestSMTP(t *testing.T) {
  20. done := make(chan bool)
  21. go func() {
  22. select {
  23. case <-time.After(time.Second * 40):
  24. t.Error("timeout")
  25. return
  26. case <-done:
  27. return
  28. }
  29. }()
  30. d := Daemon{}
  31. err := d.Start()
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. // it should set to stderr automatically
  36. if d.Config.LogFile != log.OutputStderr.String() {
  37. t.Error("smtp.config.LogFile is not", log.OutputStderr.String())
  38. }
  39. if len(d.Config.AllowedHosts) == 0 {
  40. t.Error("smtp.config.AllowedHosts len should be 1, not 0", d.Config.AllowedHosts)
  41. }
  42. if d.Config.LogLevel != "debug" {
  43. t.Error("smtp.config.LogLevel expected'debug', it is", d.Config.LogLevel)
  44. }
  45. if len(d.Config.Servers) != 1 {
  46. t.Error("len(smtp.config.Servers) should be 1, got", len(d.Config.Servers))
  47. }
  48. time.Sleep(time.Second * 2)
  49. d.Shutdown()
  50. done <- true
  51. }
  52. // Suppressing log output
  53. func TestSMTPNoLog(t *testing.T) {
  54. // configure a default server with no log output
  55. cfg := &AppConfig{LogFile: log.OutputOff.String()}
  56. d := Daemon{Config: cfg}
  57. err := d.Start()
  58. if err != nil {
  59. t.Error(err)
  60. }
  61. time.Sleep(time.Second * 2)
  62. d.Shutdown()
  63. }
  64. // our custom server
  65. func TestSMTPCustomServer(t *testing.T) {
  66. cfg := &AppConfig{LogFile: log.OutputOff.String()}
  67. sc := ServerConfig{
  68. ListenInterface: "127.0.0.1:2526",
  69. IsEnabled: true,
  70. }
  71. cfg.Servers = append(cfg.Servers, sc)
  72. d := Daemon{Config: cfg}
  73. err := d.Start()
  74. if err != nil {
  75. t.Error("start error", err)
  76. } else {
  77. time.Sleep(time.Second * 2)
  78. d.Shutdown()
  79. }
  80. }
  81. // with a backend config
  82. func TestSMTPCustomBackend(t *testing.T) {
  83. cfg := &AppConfig{LogFile: log.OutputOff.String()}
  84. sc := ServerConfig{
  85. ListenInterface: "127.0.0.1:2526",
  86. IsEnabled: true,
  87. }
  88. cfg.Servers = append(cfg.Servers, sc)
  89. bcfg := backends.BackendConfig{
  90. "save_workers_size": 3,
  91. "save_process": "HeadersParser|Header|Hasher|Debugger",
  92. "log_received_mails": true,
  93. "primary_mail_host": "example.com",
  94. }
  95. cfg.BackendConfig = bcfg
  96. d := Daemon{Config: cfg}
  97. err := d.Start()
  98. if err != nil {
  99. t.Error("start error", err)
  100. } else {
  101. time.Sleep(time.Second * 2)
  102. d.Shutdown()
  103. }
  104. }
  105. // with a config from a json file
  106. func TestSMTPLoadFile(t *testing.T) {
  107. json := `{
  108. "log_file" : "./tests/testlog",
  109. "log_level" : "debug",
  110. "pid_file" : "tests/go-guerrilla.pid",
  111. "allowed_hosts": ["spam4.me","grr.la"],
  112. "backend_config" :
  113. {
  114. "log_received_mails" : true,
  115. "save_process": "HeadersParser|Header|Hasher|Debugger",
  116. "save_workers_size": 3
  117. },
  118. "servers" : [
  119. {
  120. "is_enabled" : true,
  121. "host_name":"mail.guerrillamail.com",
  122. "max_size": 100017,
  123. "timeout":160,
  124. "listen_interface":"127.0.0.1:2526",
  125. "max_clients": 2,
  126. "tls" : {
  127. "private_key_file":"config_test.go",
  128. "public_key_file":"config_test.go",
  129. "start_tls_on":false,
  130. "tls_always_on":false
  131. }
  132. }
  133. ]
  134. }
  135. `
  136. json2 := `{
  137. "log_file" : "./tests/testlog2",
  138. "log_level" : "debug",
  139. "pid_file" : "tests/go-guerrilla2.pid",
  140. "allowed_hosts": ["spam4.me","grr.la"],
  141. "backend_config" :
  142. {
  143. "log_received_mails" : true,
  144. "save_process": "HeadersParser|Header|Hasher|Debugger",
  145. "save_workers_size": 3
  146. },
  147. "servers" : [
  148. {
  149. "is_enabled" : true,
  150. "host_name":"mail.guerrillamail.com",
  151. "max_size": 100017,
  152. "timeout":160,
  153. "listen_interface":"127.0.0.1:2526",
  154. "max_clients": 2,
  155. "tls" : {
  156. "private_key_file":"config_test.go",
  157. "public_key_file":"config_test.go",
  158. "start_tls_on":false,
  159. "tls_always_on":false
  160. }
  161. }
  162. ]
  163. }
  164. `
  165. err := ioutil.WriteFile("goguerrilla.conf.api", []byte(json), 0644)
  166. if err != nil {
  167. t.Error("could not write guerrilla.conf.api", err)
  168. return
  169. }
  170. d := Daemon{}
  171. _, err = d.LoadConfig("goguerrilla.conf.api")
  172. if err != nil {
  173. t.Error("ReadConfig error", err)
  174. return
  175. }
  176. err = d.Start()
  177. if err != nil {
  178. t.Error("start error", err)
  179. return
  180. } else {
  181. time.Sleep(time.Second * 2)
  182. if d.Config.LogFile != "./tests/testlog" {
  183. t.Error("d.Config.LogFile != \"./tests/testlog\"")
  184. }
  185. if d.Config.PidFile != "tests/go-guerrilla.pid" {
  186. t.Error("d.Config.LogFile != tests/go-guerrilla.pid")
  187. }
  188. err := ioutil.WriteFile("goguerrilla.conf.api", []byte(json2), 0644)
  189. if err != nil {
  190. t.Error("could not write guerrilla.conf.api", err)
  191. return
  192. }
  193. if err = d.ReloadConfigFile("goguerrilla.conf.api"); err != nil {
  194. t.Error(err)
  195. }
  196. if d.Config.LogFile != "./tests/testlog2" {
  197. t.Error("d.Config.LogFile != \"./tests/testlog\"")
  198. }
  199. if d.Config.PidFile != "tests/go-guerrilla2.pid" {
  200. t.Error("d.Config.LogFile != \"go-guerrilla.pid\"")
  201. }
  202. d.Shutdown()
  203. }
  204. }
  205. // test re-opening the main log
  206. func TestReopenLog(t *testing.T) {
  207. if err := os.Truncate("tests/testlog", 0); err != nil {
  208. t.Error(err)
  209. }
  210. cfg := &AppConfig{LogFile: "tests/testlog"}
  211. sc := ServerConfig{
  212. ListenInterface: "127.0.0.1:2526",
  213. IsEnabled: true,
  214. }
  215. cfg.Servers = append(cfg.Servers, sc)
  216. d := Daemon{Config: cfg}
  217. err := d.Start()
  218. if err != nil {
  219. t.Error("start error", err)
  220. } else {
  221. if err = d.ReopenLogs(); err != nil {
  222. t.Error(err)
  223. }
  224. time.Sleep(time.Second * 2)
  225. d.Shutdown()
  226. }
  227. b, err := ioutil.ReadFile("tests/testlog")
  228. if err != nil {
  229. t.Error("could not read logfile")
  230. return
  231. }
  232. if !strings.Contains(string(b), "re-opened log file") {
  233. t.Error("Server log did not re-opened, expecting \"re-opened log file\"")
  234. }
  235. if !strings.Contains(string(b), "re-opened main log file") {
  236. t.Error("Main log did not re-opened, expecting \"re-opened main log file\"")
  237. }
  238. }
  239. const testServerLog = "tests/testlog-server.log"
  240. // test re-opening the individual server log
  241. func TestReopenServerLog(t *testing.T) {
  242. if err := os.Truncate("tests/testlog", 0); err != nil {
  243. t.Error(err)
  244. }
  245. defer func() {
  246. if _, err := os.Stat(testServerLog); err == nil {
  247. if err = os.Remove(testServerLog); err != nil {
  248. t.Error(err)
  249. }
  250. }
  251. }()
  252. cfg := &AppConfig{LogFile: "tests/testlog", LogLevel: log.DebugLevel.String(), AllowedHosts: []string{"grr.la"}}
  253. sc := ServerConfig{
  254. ListenInterface: "127.0.0.1:2526",
  255. IsEnabled: true,
  256. LogFile: testServerLog,
  257. }
  258. cfg.Servers = append(cfg.Servers, sc)
  259. d := Daemon{Config: cfg}
  260. err := d.Start()
  261. if err != nil {
  262. t.Error("start error", err)
  263. } else {
  264. if err := talkToServer("127.0.0.1:2526"); err != nil {
  265. t.Error(err)
  266. }
  267. if err = d.ReopenLogs(); err != nil {
  268. t.Error(err)
  269. }
  270. time.Sleep(time.Second * 2)
  271. if err := talkToServer("127.0.0.1:2526"); err != nil {
  272. t.Error(err)
  273. }
  274. d.Shutdown()
  275. }
  276. b, err := ioutil.ReadFile("tests/testlog")
  277. if err != nil {
  278. t.Error("could not read logfile")
  279. return
  280. }
  281. if !strings.Contains(string(b), "re-opened log file") {
  282. t.Error("Server log did not re-opened, expecting \"re-opened log file\"")
  283. }
  284. if !strings.Contains(string(b), "re-opened main log file") {
  285. t.Error("Main log did not re-opened, expecting \"re-opened main log file\"")
  286. }
  287. b, err = ioutil.ReadFile(testServerLog)
  288. if err != nil {
  289. t.Error("could not read logfile")
  290. return
  291. }
  292. if !strings.Contains(string(b), "Handle client") {
  293. t.Error("server log does not contain \"handle client\"")
  294. }
  295. }
  296. func TestSetConfig(t *testing.T) {
  297. if err := os.Truncate("tests/testlog", 0); err != nil {
  298. t.Error(err)
  299. }
  300. cfg := AppConfig{LogFile: "tests/testlog"}
  301. sc := ServerConfig{
  302. ListenInterface: "127.0.0.1:2526",
  303. IsEnabled: true,
  304. }
  305. cfg.Servers = append(cfg.Servers, sc)
  306. d := Daemon{Config: &cfg}
  307. // lets add a new server
  308. sc.ListenInterface = "127.0.0.1:2527"
  309. cfg.Servers = append(cfg.Servers, sc)
  310. err := d.SetConfig(cfg)
  311. if err != nil {
  312. t.Error("SetConfig returned an error:", err)
  313. return
  314. }
  315. err = d.Start()
  316. if err != nil {
  317. t.Error("start error", err)
  318. } else {
  319. time.Sleep(time.Second * 2)
  320. d.Shutdown()
  321. }
  322. b, err := ioutil.ReadFile("tests/testlog")
  323. if err != nil {
  324. t.Error("could not read logfile")
  325. return
  326. }
  327. //fmt.Println(string(b))
  328. // has 127.0.0.1:2527 started?
  329. if !strings.Contains(string(b), "127.0.0.1:2527") {
  330. t.Error("expecting 127.0.0.1:2527 to start")
  331. }
  332. }
  333. func TestSetConfigError(t *testing.T) {
  334. if err := os.Truncate("tests/testlog", 0); err != nil {
  335. t.Error(err)
  336. }
  337. cfg := AppConfig{LogFile: "tests/testlog"}
  338. sc := ServerConfig{
  339. ListenInterface: "127.0.0.1:2526",
  340. IsEnabled: true,
  341. }
  342. cfg.Servers = append(cfg.Servers, sc)
  343. d := Daemon{Config: &cfg}
  344. // lets add a new server with bad TLS
  345. sc.ListenInterface = "127.0.0.1:2527"
  346. sc.TLS.StartTLSOn = true
  347. sc.TLS.PublicKeyFile = "tests/testlog" // totally wrong :->
  348. sc.TLS.PrivateKeyFile = "tests/testlog" // totally wrong :->
  349. cfg.Servers = append(cfg.Servers, sc)
  350. err := d.SetConfig(cfg)
  351. if err == nil {
  352. t.Error("SetConfig should have returned an error compalning about bad tls settings")
  353. return
  354. }
  355. }
  356. var funkyLogger = func() backends.Decorator {
  357. backends.Svc.AddInitializer(
  358. backends.InitializeWith(
  359. func(backendConfig backends.BackendConfig) error {
  360. backends.Log().Info("Funky logger is up & down to funk!")
  361. return nil
  362. }),
  363. )
  364. backends.Svc.AddShutdowner(
  365. backends.ShutdownWith(
  366. func() error {
  367. backends.Log().Info("The funk has been stopped!")
  368. return nil
  369. }),
  370. )
  371. return func(p backends.Processor) backends.Processor {
  372. return backends.ProcessWith(
  373. func(e *mail.Envelope, task backends.SelectTask) (backends.Result, error) {
  374. if task == backends.TaskValidateRcpt {
  375. // log the last recipient appended to e.Rcpt
  376. backends.Log().Infof(
  377. "another funky recipient [%s]",
  378. e.RcptTo[len(e.RcptTo)-1])
  379. // if valid then forward call to the next processor in the chain
  380. return p.Process(e, task)
  381. // if invalid, return a backend result
  382. //return backends.NewResult(response.Canned.FailRcptCmd), nil
  383. } else if task == backends.TaskSaveMail {
  384. backends.Log().Info("Another funky email!")
  385. }
  386. return p.Process(e, task)
  387. })
  388. }
  389. }
  390. // How about a custom processor?
  391. func TestSetAddProcessor(t *testing.T) {
  392. if err := os.Truncate("tests/testlog", 0); err != nil {
  393. t.Error(err)
  394. }
  395. cfg := &AppConfig{
  396. LogFile: "tests/testlog",
  397. AllowedHosts: []string{"grr.la"},
  398. BackendConfig: backends.BackendConfig{
  399. "save_process": "HeadersParser|Debugger|FunkyLogger",
  400. "validate_process": "FunkyLogger",
  401. },
  402. }
  403. d := Daemon{Config: cfg}
  404. d.AddProcessor("FunkyLogger", funkyLogger)
  405. if err := d.Start(); err != nil {
  406. t.Error(err)
  407. }
  408. // lets have a talk with the server
  409. if err := talkToServer("127.0.0.1:2525", ""); err != nil {
  410. t.Error(err)
  411. }
  412. d.Shutdown()
  413. b, err := ioutil.ReadFile("tests/testlog")
  414. if err != nil {
  415. t.Error("could not read logfile")
  416. return
  417. }
  418. // lets check for fingerprints
  419. if !strings.Contains(string(b), "another funky recipient") {
  420. t.Error("did not log: another funky recipient")
  421. }
  422. if !strings.Contains(string(b), "Another funky email!") {
  423. t.Error("Did not log: Another funky email!")
  424. }
  425. if !strings.Contains(string(b), "Funky logger is up & down to funk") {
  426. t.Error("Did not log: Funky logger is up & down to funk")
  427. }
  428. if !strings.Contains(string(b), "The funk has been stopped!") {
  429. t.Error("Did not log:The funk has been stopped!")
  430. }
  431. }
  432. func talkToServer(address string, body string) (err error) {
  433. conn, err := net.Dial("tcp", address)
  434. if err != nil {
  435. return
  436. }
  437. in := bufio.NewReader(conn)
  438. str, err := in.ReadString('\n')
  439. if err != nil {
  440. return err
  441. }
  442. _, err = fmt.Fprint(conn, "HELO maildiranasaurustester\r\n")
  443. if err != nil {
  444. return err
  445. }
  446. str, err = in.ReadString('\n')
  447. if err != nil {
  448. return err
  449. }
  450. _, err = fmt.Fprint(conn, "MAIL FROM:<[email protected]> BODY=8BITMIME\r\n")
  451. if err != nil {
  452. return err
  453. }
  454. str, err = in.ReadString('\n')
  455. if err != nil {
  456. return err
  457. }
  458. _, err = fmt.Fprint(conn, "RCPT TO:<[email protected]>\r\n")
  459. if err != nil {
  460. return err
  461. }
  462. str, err = in.ReadString('\n')
  463. if err != nil {
  464. return err
  465. }
  466. _, err = fmt.Fprint(conn, "DATA\r\n")
  467. if err != nil {
  468. return err
  469. }
  470. str, err = in.ReadString('\n')
  471. if err != nil {
  472. return err
  473. }
  474. if body == "" {
  475. _, err = fmt.Fprint(conn, "Subject: Test subject\r\n")
  476. if err != nil {
  477. return err
  478. }
  479. _, err = fmt.Fprint(conn, "\r\n")
  480. if err != nil {
  481. return err
  482. }
  483. _, err = fmt.Fprint(conn, "A an email body\r\n")
  484. if err != nil {
  485. return err
  486. }
  487. _, err = fmt.Fprint(conn, ".\r\n")
  488. if err != nil {
  489. return err
  490. }
  491. } else {
  492. _, err = fmt.Fprint(conn, body)
  493. if err != nil {
  494. return err
  495. }
  496. _, err = fmt.Fprint(conn, ".\r\n")
  497. if err != nil {
  498. return err
  499. }
  500. }
  501. str, err = in.ReadString('\n')
  502. if err != nil {
  503. return err
  504. }
  505. _, err = fmt.Fprint(conn, "QUIT\r\n")
  506. if err != nil {
  507. return err
  508. }
  509. _ = str
  510. return nil
  511. }
  512. // Test hot config reload
  513. // Here we forgot to add FunkyLogger so backend will fail to init
  514. // it will log to stderr at the beginning, but then change to tests/testlog
  515. func TestReloadConfig(t *testing.T) {
  516. if err := os.Truncate("tests/testlog", 0); err != nil {
  517. t.Error(err)
  518. }
  519. d := Daemon{}
  520. if err := d.Start(); err != nil {
  521. t.Error(err)
  522. }
  523. defer d.Shutdown()
  524. cfg := AppConfig{
  525. LogFile: "tests/testlog",
  526. AllowedHosts: []string{"grr.la"},
  527. BackendConfig: backends.BackendConfig{
  528. "save_process": "HeadersParser|Debugger|FunkyLogger",
  529. "validate_process": "FunkyLogger",
  530. },
  531. }
  532. // Look mom, reloading the config without shutting down!
  533. if err := d.ReloadConfig(cfg); err != nil {
  534. t.Error(err)
  535. }
  536. }
  537. func TestPubSubAPI(t *testing.T) {
  538. if err := os.Truncate("tests/testlog", 0); err != nil {
  539. t.Error(err)
  540. }
  541. d := Daemon{Config: &AppConfig{LogFile: "tests/testlog"}}
  542. if err := d.Start(); err != nil {
  543. t.Error(err)
  544. }
  545. defer d.Shutdown()
  546. // new config
  547. cfg := AppConfig{
  548. PidFile: "tests/pidfilex.pid",
  549. LogFile: "tests/testlog",
  550. AllowedHosts: []string{"grr.la"},
  551. BackendConfig: backends.BackendConfig{
  552. "save_process": "HeadersParser|Debugger|FunkyLogger",
  553. "validate_process": "FunkyLogger",
  554. },
  555. }
  556. var i = 0
  557. pidEvHandler := func(c *AppConfig) {
  558. i++
  559. if i > 1 {
  560. t.Error("number > 1, it means d.Unsubscribe didn't work")
  561. }
  562. d.Logger.Info("number", i)
  563. }
  564. if err := d.Subscribe(EventConfigPidFile, pidEvHandler); err != nil {
  565. t.Error(err)
  566. }
  567. if err := d.ReloadConfig(cfg); err != nil {
  568. t.Error(err)
  569. }
  570. if err := d.Unsubscribe(EventConfigPidFile, pidEvHandler); err != nil {
  571. t.Error(err)
  572. }
  573. cfg.PidFile = "tests/pidfile2.pid"
  574. d.Publish(EventConfigPidFile, &cfg)
  575. if err := d.ReloadConfig(cfg); err != nil {
  576. t.Error(err)
  577. }
  578. b, err := ioutil.ReadFile("tests/testlog")
  579. if err != nil {
  580. t.Error("could not read logfile")
  581. return
  582. }
  583. // lets interrogate the log
  584. if !strings.Contains(string(b), "number1") {
  585. t.Error("it lools like d.ReloadConfig(cfg) did not fire EventConfigPidFile, pidEvHandler not called")
  586. }
  587. }
  588. func TestAPILog(t *testing.T) {
  589. if err := os.Truncate("tests/testlog", 0); err != nil {
  590. t.Error(err)
  591. }
  592. d := Daemon{}
  593. l := d.Log()
  594. l.Info("logtest1") // to stderr
  595. if l.GetLevel() != log.InfoLevel.String() {
  596. t.Error("Log level does not eq info, it is ", l.GetLevel())
  597. }
  598. d.Logger = nil
  599. d.Config = &AppConfig{LogFile: "tests/testlog"}
  600. l = d.Log()
  601. l.Info("logtest1") // to tests/testlog
  602. //
  603. l = d.Log()
  604. if l.GetLogDest() != "tests/testlog" {
  605. t.Error("log dest is not tests/testlog, it was ", l.GetLogDest())
  606. }
  607. b, err := ioutil.ReadFile("tests/testlog")
  608. if err != nil {
  609. t.Error("could not read logfile")
  610. return
  611. }
  612. // lets interrogate the log
  613. if !strings.Contains(string(b), "logtest1") {
  614. t.Error("hai was not found in the log, it should have been in tests/testlog")
  615. }
  616. }
  617. // Test the allowed_hosts config option with a single entry of ".", which will allow all hosts.
  618. func TestSkipAllowsHost(t *testing.T) {
  619. d := Daemon{}
  620. defer d.Shutdown()
  621. // setting the allowed hosts to a single entry with a dot will let any host through
  622. d.Config = &AppConfig{AllowedHosts: []string{"."}, LogFile: "off"}
  623. if err := d.Start(); err != nil {
  624. t.Error(err)
  625. }
  626. conn, err := net.Dial("tcp", d.Config.Servers[0].ListenInterface)
  627. if err != nil {
  628. t.Error(t)
  629. return
  630. }
  631. in := bufio.NewReader(conn)
  632. if _, err := fmt.Fprint(conn, "HELO test\r\n"); err != nil {
  633. t.Error(err)
  634. }
  635. if _, err := fmt.Fprint(conn, "RCPT TO:<[email protected]>\r\n"); err != nil {
  636. t.Error(err)
  637. }
  638. if _, err := in.ReadString('\n'); err != nil {
  639. t.Error(err)
  640. }
  641. if _, err := in.ReadString('\n'); err != nil {
  642. t.Error(err)
  643. }
  644. str, _ := in.ReadString('\n')
  645. if strings.Index(str, "250") != 0 {
  646. t.Error("expected 250 reply, got:", str)
  647. }
  648. }
  649. var customBackend2 = func() backends.Decorator {
  650. return func(p backends.Processor) backends.Processor {
  651. return backends.ProcessWith(
  652. func(e *mail.Envelope, task backends.SelectTask) (backends.Result, error) {
  653. if task == backends.TaskValidateRcpt {
  654. return p.Process(e, task)
  655. } else if task == backends.TaskSaveMail {
  656. backends.Log().Info("Another funky email!")
  657. err := errors.New("system shock")
  658. return backends.NewResult(response.Canned.FailReadErrorDataCmd, response.SP, err), err
  659. }
  660. return p.Process(e, task)
  661. })
  662. }
  663. }
  664. // Test a custom backend response
  665. func TestCustomBackendResult(t *testing.T) {
  666. if err := os.Truncate("tests/testlog", 0); err != nil {
  667. t.Error(err)
  668. }
  669. cfg := &AppConfig{
  670. LogFile: "tests/testlog",
  671. AllowedHosts: []string{"grr.la"},
  672. BackendConfig: backends.BackendConfig{
  673. "save_process": "HeadersParser|Debugger|Custom",
  674. "validate_process": "Custom",
  675. },
  676. }
  677. d := Daemon{Config: cfg}
  678. d.AddProcessor("Custom", customBackend2)
  679. if err := d.Start(); err != nil {
  680. t.Error(err)
  681. }
  682. // lets have a talk with the server
  683. if err := talkToServer("127.0.0.1:2525", ""); err != nil {
  684. t.Error(err)
  685. }
  686. d.Shutdown()
  687. b, err := ioutil.ReadFile("tests/testlog")
  688. if err != nil {
  689. t.Error("could not read logfile")
  690. return
  691. }
  692. // lets check for fingerprints
  693. if !strings.Contains(string(b), "451 4.3.0 Error") {
  694. t.Error("did not log: 451 4.3.0 Error")
  695. }
  696. if !strings.Contains(string(b), "system shock") {
  697. t.Error("did not log: system shock")
  698. }
  699. }
  700. func TestStreamProcessor(t *testing.T) {
  701. if err := os.Truncate("tests/testlog", 0); err != nil {
  702. t.Error(err)
  703. }
  704. cfg := &AppConfig{
  705. LogFile: "tests/testlog",
  706. AllowedHosts: []string{"grr.la"},
  707. BackendConfig: backends.BackendConfig{
  708. "save_process": "HeadersParser|Debugger",
  709. "stream_save_process": "Header|headersparser|compress|Decompress|debug",
  710. },
  711. }
  712. d := Daemon{Config: cfg}
  713. if err := d.Start(); err != nil {
  714. t.Error(err)
  715. }
  716. body := "Subject: Test subject\r\n" +
  717. //"\r\n" +
  718. "A an email body.\r\n" +
  719. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  720. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  721. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  722. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  723. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  724. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  725. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  726. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  727. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  728. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  729. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  730. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n"
  731. // lets have a talk with the server
  732. if err := talkToServer("127.0.0.1:2525", body); err != nil {
  733. t.Error(err)
  734. }
  735. d.Shutdown()
  736. b, err := ioutil.ReadFile("tests/testlog")
  737. if err != nil {
  738. t.Error("could not read logfile")
  739. return
  740. }
  741. // lets check for fingerprints
  742. if strings.Index(string(b), "Debug stream") < 0 {
  743. t.Error("did not log: Debug stream")
  744. }
  745. if strings.Index(string(b), "Error") != -1 {
  746. t.Error("There was an error", string(b))
  747. }
  748. }
  749. var mime0 = `MIME-Version: 1.0
  750. X-Mailer: MailBee.NET 8.0.4.428
  751. Subject: test
  752. subject
  753. To: [email protected]
  754. Content-Type: multipart/mixed;
  755. boundary="XXXXboundary text"
  756. --XXXXboundary text
  757. Content-Type: multipart/alternative;
  758. boundary="XXXXboundary text"
  759. --XXXXboundary text
  760. Content-Type: text/plain;
  761. charset="utf-8"
  762. Content-Transfer-Encoding: quoted-printable
  763. This is the body text of a sample message.
  764. --XXXXboundary text
  765. Content-Type: text/html;
  766. charset="utf-8"
  767. Content-Transfer-Encoding: quoted-printable
  768. <pre>This is the body text of a sample message.</pre>
  769. --XXXXboundary text
  770. Content-Type: text/plain;
  771. name="log_attachment.txt"
  772. Content-Disposition: attachment;
  773. filename="log_attachment.txt"
  774. Content-Transfer-Encoding: base64
  775. TUlNRS1WZXJzaW9uOiAxLjANClgtTWFpbGVyOiBNYWlsQmVlLk5FVCA4LjAuNC40MjgNClN1Ympl
  776. Y3Q6IHRlc3Qgc3ViamVjdA0KVG86IGtldmlubUBkYXRhbW90aW9uLmNvbQ0KQ29udGVudC1UeXBl
  777. OiBtdWx0aXBhcnQvYWx0ZXJuYXRpdmU7DQoJYm91bmRhcnk9Ii0tLS09X05leHRQYXJ0XzAwMF9B
  778. RTZCXzcyNUUwOUFGLjg4QjdGOTM0Ig0KDQoNCi0tLS0tLT1fTmV4dFBhcnRfMDAwX0FFNkJfNzI1
  779. RTA5QUYuODhCN0Y5MzQNCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsNCgljaGFyc2V0PSJ1dGYt
  780. OCINCkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6IHF1b3RlZC1wcmludGFibGUNCg0KdGVzdCBi
  781. b2R5DQotLS0tLS09X05leHRQYXJ0XzAwMF9BRTZCXzcyNUUwOUFGLjg4QjdGOTM0DQpDb250ZW50
  782. LVR5cGU6IHRleHQvaHRtbDsNCgljaGFyc2V0PSJ1dGYtOCINCkNvbnRlbnQtVHJhbnNmZXItRW5j
  783. b2Rpbmc6IHF1b3RlZC1wcmludGFibGUNCg0KPHByZT50ZXN0IGJvZHk8L3ByZT4NCi0tLS0tLT1f
  784. TmV4dFBhcnRfMDAwX0FFNkJfNzI1RTA5QUYuODhCN0Y5MzQtLQ0K
  785. --XXXXboundary text--
  786. `
  787. var mime2 = `From: [email protected]
  788. Content-Type: multipart/mixed;
  789. boundary="----_=_NextPart_001_01CBE273.65A0E7AA"
  790. To: [email protected]
  791. This is a multi-part message in MIME format.
  792. ------_=_NextPart_001_01CBE273.65A0E7AA
  793. Content-Type: multipart/alternative;
  794. boundary="----_=_NextPart_002_01CBE273.65A0E7AA"
  795. ------_=_NextPart_002_01CBE273.65A0E7AA
  796. Content-Type: text/plain;
  797. charset="UTF-8"
  798. Content-Transfer-Encoding: base64
  799. [base64-content]
  800. ------_=_NextPart_002_01CBE273.65A0E7AA
  801. Content-Type: text/html;
  802. charset="UTF-8"
  803. Content-Transfer-Encoding: base64
  804. [base64-content]
  805. ------_=_NextPart_002_01CBE273.65A0E7AA--
  806. ------_=_NextPart_001_01CBE273.65A0E7AA
  807. Content-Type: message/rfc822
  808. Content-Transfer-Encoding: 7bit
  809. X-MimeOLE: Produced By Microsoft Exchange V6.5
  810. Content-class: urn:content-classes:message
  811. MIME-Version: 1.0
  812. Content-Type: multipart/mixed;
  813. boundary="----_=_NextPart_003_01CBE272.13692C80"
  814. From: [email protected]
  815. To: [email protected]
  816. This is a multi-part message in MIME format.
  817. ------_=_NextPart_003_01CBE272.13692C80
  818. Content-Type: multipart/alternative;
  819. boundary="----_=_NextPart_004_01CBE272.13692C80"
  820. ------_=_NextPart_004_01CBE272.13692C80
  821. Content-Type: text/plain;
  822. charset="iso-8859-1"
  823. Content-Transfer-Encoding: quoted-printable
  824. =20
  825. Viele Gr=FC=DFe
  826. ------_=_NextPart_004_01CBE272.13692C80
  827. Content-Type: text/html;
  828. charset="iso-8859-1"
  829. Content-Transfer-Encoding: quoted-printable
  830. <html>...</html>
  831. ------_=_NextPart_004_01CBE272.13692C80--
  832. ------_=_NextPart_003_01CBE272.13692C80
  833. Content-Type: application/x-zip-compressed;
  834. name="abc.zip"
  835. Content-Transfer-Encoding: base64
  836. Content-Disposition: attachment;
  837. filename="abc.zip"
  838. [base64-content]
  839. ------_=_NextPart_003_01CBE272.13692C80--
  840. ------_=_NextPart_001_01CBE273.65A0E7AA--
  841. `
  842. var mime3 = `From [email protected] Mon Feb 19 22:24:21 2001
  843. Received: from [137.154.210.66] by hotmail.com (3.2) with ESMTP id MHotMailBC5B58230039400431D5899AD24289FA0; Mon Feb 19 22:22:29 2001
  844. Received: from lancelot.cit.nepean.uws.edu.au (lancelot.cit.uws.edu.au [137.154.148.30])
  845. by day.uws.edu.au (8.11.1/8.11.1) with ESMTP id f1K6MN404936;
  846. Tue, 20 Feb 2001 17:22:24 +1100 (EST)
  847. Received: from hotmail.com (law2-f35.hotmail.com [216.32.181.35])
  848. by lancelot.cit.nepean.uws.edu.au (8.10.0.Beta10/8.10.0.Beta10) with ESMTP id f1K6MJb13619;
  849. Tue, 20 Feb 2001 17:22:19 +1100 (EST)
  850. Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC;
  851. Mon, 19 Feb 2001 22:21:44 -0800
  852. Received: from 203.54.221.89 by lw2fd.hotmail.msn.com with HTTP; Tue, 20 Feb 2001 06:21:44 GMT
  853. X-Originating-IP: [203.54.221.89]
  854. From: "lara devine" <[email protected]>
  855. To: [email protected], [email protected],
  856. [email protected], [email protected],
  857. [email protected], [email protected],
  858. [email protected], [email protected],
  859. [email protected]
  860. Subject: Fwd: Goldfish
  861. Date: Tue, 20 Feb 2001 06:21:44
  862. Mime-Version: 1.0
  863. Content-Type: text/plain; format=flowed
  864. Message-ID: <[email protected]>
  865. X-OriginalArrivalTime: 20 Feb 2001 06:21:44.0718 (UTC) FILETIME=[658BDAE0:01C09B05]
  866. >> >Two builders (Chris and James) are seated either side of a table in a
  867. > > >rough
  868. > > >pub when a well-dressed man enters, orders beer and sits on a stool at
  869. > > >the bar.
  870. > > >The two builders start to speculate about the occupation of the suit.
  871. > > >
  872. > > >Chris: - I reckon he's an accountant.
  873. > > >
  874. > > >James: - No way - he's a stockbroker.
  875. > > >
  876. > > >Chris: - He ain't no stockbroker! A stockbroker wouldn't come in here!
  877. > > >
  878. > > >The argument repeats itself for some time until the volume of beer gets
  879. > > >the better of Chris and he makes for the toilet. On entering the toilet
  880. > > >he
  881. > > >sees that the suit is standing at a urinal. Curiosity and the several
  882. > > >beers
  883. > > >get the better of the builder...
  884. > > >
  885. > > >Chris: - 'scuse me.... no offence meant, but me and me mate were
  886. > > wondering
  887. > > >
  888. > > > what you do for a living?
  889. > > >
  890. > > >Suit: - No offence taken! I'm a Logical Scientist by profession!
  891. > > >
  892. > > >Chris: - Oh! What's that then?
  893. > > >
  894. > > >Suit:- I'll try to explain by example... Do you have a goldfish at
  895. >home?
  896. > > >
  897. > > >Chris:- Er...mmm... well yeah, I do as it happens!
  898. > > >
  899. > > >Suit: - Well, it's logical to follow that you keep it in a bowl or in a
  900. > > >pond. Which is it?
  901. > > >
  902. > > >Chris: - It's in a pond!
  903. > > >
  904. > > >Suit: - Well then it's reasonable to suppose that you have a large
  905. > > >garden
  906. > > >then?
  907. > > >
  908. > > >Chris: - As it happens, yes I have got a big garden!
  909. > > >
  910. > > >Suit: - Well then it's logical to assume that in this town that if you
  911. > > >have a large garden that you have a large house?
  912. > > >
  913. > > >Chris: - As it happens I've got a five bedroom house... built it
  914. >myself!
  915. > > >
  916. > > >Suit: - Well given that you've built a five-bedroom house it is logical
  917. > > >to asume that you haven't built it just for yourself and that you are
  918. > > >quite
  919. > > >probably married?
  920. > > >
  921. > > >Chris: - Yes I am married, I live with my wife and three children!
  922. > > >
  923. > > >Suit: - Well then it is logical to assume that you are sexually active
  924. > > >with your wife on a regular basis?
  925. > > >
  926. > > >Chris:- Yep! Four nights a week!
  927. > > >
  928. > > >Suit: - Well then it is logical to suggest that you do not masturbate
  929. > > >very often?
  930. > > >
  931. > > >Chris: - Me? Never.
  932. > > >
  933. > > >Suit: - Well there you are! That's logical science at work!
  934. > > >
  935. > > >Chris:- How's that then?
  936. > > >
  937. > > >Suit: - Well from finding out that you had a goldfish, I've told you
  938. > > >about the size of garden you have, size of house, your family and your
  939. > > >sex
  940. > > >life!
  941. > > >
  942. > > >Chris: - I see! That's pretty impressive... thanks mate!
  943. > > >
  944. > > >Both leave the toilet and Chris returns to his mate.
  945. > > >
  946. > > >James: - I see the suit was in there. Did you ask him what he does?
  947. > > >
  948. > > >Chris: - Yep! He's a logical scientist!
  949. > > >
  950. > > >James: What's a logical Scientist?
  951. > > >
  952. > > >Chris: - I'll try and explain. Do you have a goldfish?
  953. > > >
  954. > > >James: - Nope.
  955. > > >
  956. > > >Chris: - Well then, you're a wanker.
  957. >
  958. _________________________________________________________________________
  959. Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
  960. `
  961. /*
  962. 1 0 166 1514
  963. 1.1 186 260 259
  964. 1.2 280 374 416
  965. 1.3 437 530 584
  966. 1.4 605 769 1514
  967. */
  968. func TestStreamMimeProcessor(t *testing.T) {
  969. if err := os.Truncate("tests/testlog", 0); err != nil {
  970. t.Error(err)
  971. }
  972. cfg := &AppConfig{
  973. LogFile: "tests/testlog",
  974. AllowedHosts: []string{"grr.la"},
  975. BackendConfig: backends.BackendConfig{
  976. "save_process": "HeadersParser|Debugger",
  977. "stream_save_process": "mimeanalyzer|headersparser|compress|Decompress|debug",
  978. },
  979. }
  980. d := Daemon{Config: cfg}
  981. if err := d.Start(); err != nil {
  982. t.Error(err)
  983. }
  984. go func() {
  985. time.Sleep(time.Second * 15)
  986. // for debugging deadlocks
  987. //pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
  988. //os.Exit(1)
  989. }()
  990. // change \n to \r\n
  991. mime0 = strings.Replace(mime2, "\n", "\r\n", -1)
  992. // lets have a talk with the server
  993. if err := talkToServer("127.0.0.1:2525", mime0); err != nil {
  994. t.Error(err)
  995. }
  996. d.Shutdown()
  997. b, err := ioutil.ReadFile("tests/testlog")
  998. if err != nil {
  999. t.Error("could not read logfile")
  1000. return
  1001. }
  1002. // lets check for fingerprints
  1003. if strings.Index(string(b), "Debug stream") < 0 {
  1004. t.Error("did not log: Debug stream")
  1005. }
  1006. if strings.Index(string(b), "Error") != -1 {
  1007. t.Error("There was an error", string(b))
  1008. }
  1009. }
  1010. var email = `From: Al Gore <[email protected]>
  1011. To: White House Transportation Coordinator <[email protected]>
  1012. Subject: [Fwd: Map of Argentina with Description]
  1013. MIME-Version: 1.0
  1014. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; s=ncr424; d=reliancegeneral.co.in;
  1015. h=List-Unsubscribe:MIME-Version:From:To:Reply-To:Date:Subject:Content-Type:Content-Transfer-Encoding:Message-ID; [email protected];
  1016. bh=F4UQPGEkpmh54C7v3DL8mm2db1QhZU4gRHR1jDqffG8=;
  1017. b=MVltcq6/I9b218a370fuNFLNinR9zQcdBSmzttFkZ7TvV2mOsGrzrwORT8PKYq4KNJNOLBahswXf
  1018. GwaMjDKT/5TXzegdX/L3f/X4bMAEO1einn+nUkVGLK4zVQus+KGqm4oP7uVXjqp70PWXScyWWkbT
  1019. 1PGUwRfPd/HTJG5IUqs=
  1020. Content-Type: multipart/mixed;
  1021. boundary="D7F------------D7FD5A0B8AB9C65CCDBFA872"
  1022. This is a multi-part message in MIME format.
  1023. --D7F------------D7FD5A0B8AB9C65CCDBFA872
  1024. Content-Type: text/plain; charset=us-ascii
  1025. Content-Transfer-Encoding: 7bit
  1026. Fred,
  1027. Fire up Air Force One! We're going South!
  1028. Thanks,
  1029. Al
  1030. --D7F------------D7FD5A0B8AB9C65CCDBFA872
  1031. Content-Type: message/rfc822
  1032. Content-Transfer-Encoding: 7bit
  1033. Content-Disposition: inline
  1034. Return-Path: <[email protected]>
  1035. Received: from mailhost.whitehouse.gov ([192.168.51.200])
  1036. by heartbeat.whitehouse.gov (8.8.8/8.8.8) with ESMTP id SAA22453
  1037. for <[email protected]>;
  1038. Mon, 13 Aug 1998 l8:14:23 +1000
  1039. Received: from the_big_box.whitehouse.gov ([192.168.51.50])
  1040. by mailhost.whitehouse.gov (8.8.8/8.8.7) with ESMTP id RAA20366
  1041. for [email protected]; Mon, 13 Aug 1998 17:42:41 +1000
  1042. Date: Mon, 13 Aug 1998 17:42:41 +1000
  1043. Message-Id: <[email protected]>
  1044. From: Bill Clinton <[email protected]>
  1045. To: A1 (The Enforcer) Gore <[email protected]>
  1046. Subject: Map of Argentina with Description
  1047. MIME-Version: 1.0
  1048. Content-Type: multipart/mixed;
  1049. boundary="DC8------------DC8638F443D87A7F0726DEF7"
  1050. This is a multi-part message in MIME format.
  1051. --DC8------------DC8638F443D87A7F0726DEF7
  1052. Content-Type: text/plain; charset=us-ascii
  1053. Content-Transfer-Encoding: 7bit
  1054. Hi A1,
  1055. I finally figured out this MIME thing. Pretty cool. I'll send you
  1056. some sax music in .au files next week!
  1057. Anyway, the attached image is really too small to get a good look at
  1058. Argentina. Try this for a much better map:
  1059. http://www.1one1yp1anet.com/dest/sam/graphics/map-arg.htm
  1060. Then again, shouldn't the CIA have something like that?
  1061. Bill
  1062. --DC8------------DC8638F443D87A7F0726DEF7
  1063. Content-Type: image/gif; name="map_of_Argentina.gif"
  1064. Content-Transfer-Encoding: base64
  1065. Content-Disposition: inline; filename="map_of_Argentina.gif"
  1066. R01GOD1hJQA1AKIAAP/////78P/omn19fQAAAAAAAAAAAAAAACwAAAAAJQA1AAAD7Qi63P5w
  1067. wEmjBCLrnQnhYCgM1wh+pkgqqeC9XrutmBm7hAK3tP31gFcAiFKVQrGFR6kscnonTe7FAAad
  1068. GugmRu3CmiBt57fsVq3Y0VFKnpYdxPC6M7Ze4crnnHum4oN6LFJ1bn5NXTN7OF5fQkN5WYow
  1069. BEN2dkGQGWJtSzqGTICJgnQuTJN/WJsojad9qXMuhIWdjXKjY4tenjo6tjVssk2gaWq3uGNX
  1070. U6ZGxseyk8SasGw3J9GRzdTQky1iHNvcPNNI4TLeKdfMvy0vMqLrItvuxfDW8ubjueDtJufz
  1071. 7itICBxISKDBgwgTKjyYAAA7
  1072. --DC8------------DC8638F443D87A7F0726DEF7--
  1073. --D7F------------D7FD5A0B8AB9C65CCDBFA872--
  1074. `
  1075. func TestStreamChunkSaver(t *testing.T) {
  1076. if err := os.Truncate("tests/testlog", 0); err != nil {
  1077. t.Error(err)
  1078. }
  1079. go func() {
  1080. time.Sleep(time.Second * 15)
  1081. // for debugging deadlocks
  1082. //pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
  1083. //os.Exit(1)
  1084. }()
  1085. cfg := &AppConfig{
  1086. LogFile: "tests/testlog",
  1087. AllowedHosts: []string{"grr.la"},
  1088. BackendConfig: backends.BackendConfig{
  1089. "stream_save_process": "mimeanalyzer|chunksaver",
  1090. "chunksaver_chunk_size": 1024 * 32,
  1091. "stream_buffer_size": 1024 * 16,
  1092. "chunksaver_storage_engine": "memory",
  1093. },
  1094. }
  1095. d := Daemon{Config: cfg}
  1096. if err := d.Start(); err != nil {
  1097. t.Error(err)
  1098. }
  1099. // change \n to \r\n
  1100. email = strings.Replace(email, "\n", "\r\n", -1)
  1101. // lets have a talk with the server
  1102. if err := talkToServer("127.0.0.1:2525", email); err != nil {
  1103. t.Error(err)
  1104. }
  1105. time.Sleep(time.Second * 1)
  1106. d.Shutdown()
  1107. b, err := ioutil.ReadFile("tests/testlog")
  1108. if err != nil {
  1109. t.Error("could not read logfile")
  1110. return
  1111. }
  1112. fmt.Println(string(b))
  1113. // lets check for fingerprints
  1114. if strings.Index(string(b), "Debug stream") < 0 {
  1115. // t.Error("did not log: Debug stream")
  1116. }
  1117. }