api_test.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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 := truncateIfExists("tests/testlog"); err != nil {
  539. t.Error(err)
  540. }
  541. if err := truncateIfExists("tests/pidfilex.pid"); err != nil {
  542. t.Error(err)
  543. }
  544. d := Daemon{Config: &AppConfig{LogFile: "tests/testlog"}}
  545. if err := d.Start(); err != nil {
  546. t.Error(err)
  547. }
  548. defer d.Shutdown()
  549. // new config
  550. cfg := AppConfig{
  551. PidFile: "tests/pidfilex.pid",
  552. LogFile: "tests/testlog",
  553. AllowedHosts: []string{"grr.la"},
  554. BackendConfig: backends.BackendConfig{
  555. "save_process": "HeadersParser|Debugger|FunkyLogger",
  556. "validate_process": "FunkyLogger",
  557. },
  558. }
  559. var i = 0
  560. pidEvHandler := func(c *AppConfig) {
  561. i++
  562. if i > 1 {
  563. t.Error("number > 1, it means d.Unsubscribe didn't work")
  564. }
  565. d.Logger.Info("number", i)
  566. }
  567. if err := d.Subscribe(EventConfigPidFile, pidEvHandler); err != nil {
  568. t.Error(err)
  569. }
  570. if err := d.ReloadConfig(cfg); err != nil {
  571. t.Error(err)
  572. }
  573. if err := d.Unsubscribe(EventConfigPidFile, pidEvHandler); err != nil {
  574. t.Error(err)
  575. }
  576. cfg.PidFile = "tests/pidfile2.pid"
  577. d.Publish(EventConfigPidFile, &cfg)
  578. if err := d.ReloadConfig(cfg); err != nil {
  579. t.Error(err)
  580. }
  581. b, err := ioutil.ReadFile("tests/testlog")
  582. if err != nil {
  583. t.Error("could not read logfile")
  584. return
  585. }
  586. // lets interrogate the log
  587. if !strings.Contains(string(b), "number1") {
  588. t.Error("it lools like d.ReloadConfig(cfg) did not fire EventConfigPidFile, pidEvHandler not called")
  589. }
  590. }
  591. func TestAPILog(t *testing.T) {
  592. if err := os.Truncate("tests/testlog", 0); err != nil {
  593. t.Error(err)
  594. }
  595. d := Daemon{}
  596. l := d.Log()
  597. l.Info("logtest1") // to stderr
  598. if l.GetLevel() != log.InfoLevel.String() {
  599. t.Error("Log level does not eq info, it is ", l.GetLevel())
  600. }
  601. d.Logger = nil
  602. d.Config = &AppConfig{LogFile: "tests/testlog"}
  603. l = d.Log()
  604. l.Info("logtest1") // to tests/testlog
  605. //
  606. l = d.Log()
  607. if l.GetLogDest() != "tests/testlog" {
  608. t.Error("log dest is not tests/testlog, it was ", l.GetLogDest())
  609. }
  610. b, err := ioutil.ReadFile("tests/testlog")
  611. if err != nil {
  612. t.Error("could not read logfile")
  613. return
  614. }
  615. // lets interrogate the log
  616. if !strings.Contains(string(b), "logtest1") {
  617. t.Error("hai was not found in the log, it should have been in tests/testlog")
  618. }
  619. }
  620. // Test the allowed_hosts config option with a single entry of ".", which will allow all hosts.
  621. func TestSkipAllowsHost(t *testing.T) {
  622. d := Daemon{}
  623. defer d.Shutdown()
  624. // setting the allowed hosts to a single entry with a dot will let any host through
  625. d.Config = &AppConfig{AllowedHosts: []string{"."}, LogFile: "off"}
  626. if err := d.Start(); err != nil {
  627. t.Error(err)
  628. }
  629. conn, err := net.Dial("tcp", d.Config.Servers[0].ListenInterface)
  630. if err != nil {
  631. t.Error(t)
  632. return
  633. }
  634. in := bufio.NewReader(conn)
  635. if _, err := fmt.Fprint(conn, "HELO test\r\n"); err != nil {
  636. t.Error(err)
  637. }
  638. if _, err := fmt.Fprint(conn, "RCPT TO:<[email protected]>\r\n"); err != nil {
  639. t.Error(err)
  640. }
  641. if _, err := in.ReadString('\n'); err != nil {
  642. t.Error(err)
  643. }
  644. if _, err := in.ReadString('\n'); err != nil {
  645. t.Error(err)
  646. }
  647. str, _ := in.ReadString('\n')
  648. if strings.Index(str, "250") != 0 {
  649. t.Error("expected 250 reply, got:", str)
  650. }
  651. }
  652. var customBackend2 = func() backends.Decorator {
  653. return func(p backends.Processor) backends.Processor {
  654. return backends.ProcessWith(
  655. func(e *mail.Envelope, task backends.SelectTask) (backends.Result, error) {
  656. if task == backends.TaskValidateRcpt {
  657. return p.Process(e, task)
  658. } else if task == backends.TaskSaveMail {
  659. backends.Log().Info("Another funky email!")
  660. err := errors.New("system shock")
  661. return backends.NewResult(response.Canned.FailReadErrorDataCmd, response.SP, err), err
  662. }
  663. return p.Process(e, task)
  664. })
  665. }
  666. }
  667. // Test a custom backend response
  668. func TestCustomBackendResult(t *testing.T) {
  669. if err := os.Truncate("tests/testlog", 0); err != nil {
  670. t.Error(err)
  671. }
  672. cfg := &AppConfig{
  673. LogFile: "tests/testlog",
  674. AllowedHosts: []string{"grr.la"},
  675. BackendConfig: backends.BackendConfig{
  676. "save_process": "HeadersParser|Debugger|Custom",
  677. "validate_process": "Custom",
  678. },
  679. }
  680. d := Daemon{Config: cfg}
  681. d.AddProcessor("Custom", customBackend2)
  682. if err := d.Start(); err != nil {
  683. t.Error(err)
  684. }
  685. // lets have a talk with the server
  686. if err := talkToServer("127.0.0.1:2525", ""); err != nil {
  687. t.Error(err)
  688. }
  689. d.Shutdown()
  690. b, err := ioutil.ReadFile("tests/testlog")
  691. if err != nil {
  692. t.Error("could not read logfile")
  693. return
  694. }
  695. // lets check for fingerprints
  696. if !strings.Contains(string(b), "451 4.3.0 Error") {
  697. t.Error("did not log: 451 4.3.0 Error")
  698. }
  699. if !strings.Contains(string(b), "system shock") {
  700. t.Error("did not log: system shock")
  701. }
  702. }
  703. func TestStreamProcessor(t *testing.T) {
  704. if err := os.Truncate("tests/testlog", 0); err != nil {
  705. t.Error(err)
  706. }
  707. cfg := &AppConfig{
  708. LogFile: "tests/testlog",
  709. AllowedHosts: []string{"grr.la"},
  710. BackendConfig: backends.BackendConfig{
  711. "save_process": "HeadersParser|Debugger",
  712. "stream_save_process": "Header|headersparser|compress|Decompress|debug",
  713. },
  714. }
  715. d := Daemon{Config: cfg}
  716. if err := d.Start(); err != nil {
  717. t.Error(err)
  718. }
  719. body := "Subject: Test subject\r\n" +
  720. //"\r\n" +
  721. "A an email body.\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. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  732. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  733. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n"
  734. // lets have a talk with the server
  735. if err := talkToServer("127.0.0.1:2525", body); err != nil {
  736. t.Error(err)
  737. }
  738. d.Shutdown()
  739. b, err := ioutil.ReadFile("tests/testlog")
  740. if err != nil {
  741. t.Error("could not read logfile")
  742. return
  743. }
  744. // lets check for fingerprints
  745. if strings.Index(string(b), "Debug stream") < 0 {
  746. t.Error("did not log: Debug stream")
  747. }
  748. if strings.Index(string(b), "Error") != -1 {
  749. t.Error("There was an error", string(b))
  750. }
  751. }
  752. var mime0 = `MIME-Version: 1.0
  753. X-Mailer: MailBee.NET 8.0.4.428
  754. Subject: test
  755. subject
  756. To: [email protected]
  757. Content-Type: multipart/mixed;
  758. boundary="XXXXboundary text"
  759. --XXXXboundary text
  760. Content-Type: multipart/alternative;
  761. boundary="XXXXboundary text"
  762. --XXXXboundary text
  763. Content-Type: text/plain;
  764. charset="utf-8"
  765. Content-Transfer-Encoding: quoted-printable
  766. This is the body text of a sample message.
  767. --XXXXboundary text
  768. Content-Type: text/html;
  769. charset="utf-8"
  770. Content-Transfer-Encoding: quoted-printable
  771. <pre>This is the body text of a sample message.</pre>
  772. --XXXXboundary text
  773. Content-Type: text/plain;
  774. name="log_attachment.txt"
  775. Content-Disposition: attachment;
  776. filename="log_attachment.txt"
  777. Content-Transfer-Encoding: base64
  778. TUlNRS1WZXJzaW9uOiAxLjANClgtTWFpbGVyOiBNYWlsQmVlLk5FVCA4LjAuNC40MjgNClN1Ympl
  779. Y3Q6IHRlc3Qgc3ViamVjdA0KVG86IGtldmlubUBkYXRhbW90aW9uLmNvbQ0KQ29udGVudC1UeXBl
  780. OiBtdWx0aXBhcnQvYWx0ZXJuYXRpdmU7DQoJYm91bmRhcnk9Ii0tLS09X05leHRQYXJ0XzAwMF9B
  781. RTZCXzcyNUUwOUFGLjg4QjdGOTM0Ig0KDQoNCi0tLS0tLT1fTmV4dFBhcnRfMDAwX0FFNkJfNzI1
  782. RTA5QUYuODhCN0Y5MzQNCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsNCgljaGFyc2V0PSJ1dGYt
  783. OCINCkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6IHF1b3RlZC1wcmludGFibGUNCg0KdGVzdCBi
  784. b2R5DQotLS0tLS09X05leHRQYXJ0XzAwMF9BRTZCXzcyNUUwOUFGLjg4QjdGOTM0DQpDb250ZW50
  785. LVR5cGU6IHRleHQvaHRtbDsNCgljaGFyc2V0PSJ1dGYtOCINCkNvbnRlbnQtVHJhbnNmZXItRW5j
  786. b2Rpbmc6IHF1b3RlZC1wcmludGFibGUNCg0KPHByZT50ZXN0IGJvZHk8L3ByZT4NCi0tLS0tLT1f
  787. TmV4dFBhcnRfMDAwX0FFNkJfNzI1RTA5QUYuODhCN0Y5MzQtLQ0K
  788. --XXXXboundary text--
  789. `
  790. var mime2 = `From: [email protected]
  791. Content-Type: multipart/mixed;
  792. boundary="----_=_NextPart_001_01CBE273.65A0E7AA"
  793. To: [email protected]
  794. This is a multi-part message in MIME format.
  795. ------_=_NextPart_001_01CBE273.65A0E7AA
  796. Content-Type: multipart/alternative;
  797. boundary="----_=_NextPart_002_01CBE273.65A0E7AA"
  798. ------_=_NextPart_002_01CBE273.65A0E7AA
  799. Content-Type: text/plain;
  800. charset="UTF-8"
  801. Content-Transfer-Encoding: base64
  802. [base64-content]
  803. ------_=_NextPart_002_01CBE273.65A0E7AA
  804. Content-Type: text/html;
  805. charset="UTF-8"
  806. Content-Transfer-Encoding: base64
  807. [base64-content]
  808. ------_=_NextPart_002_01CBE273.65A0E7AA--
  809. ------_=_NextPart_001_01CBE273.65A0E7AA
  810. Content-Type: message/rfc822
  811. Content-Transfer-Encoding: 7bit
  812. X-MimeOLE: Produced By Microsoft Exchange V6.5
  813. Content-class: urn:content-classes:message
  814. MIME-Version: 1.0
  815. Content-Type: multipart/mixed;
  816. boundary="----_=_NextPart_003_01CBE272.13692C80"
  817. From: [email protected]
  818. To: [email protected]
  819. This is a multi-part message in MIME format.
  820. ------_=_NextPart_003_01CBE272.13692C80
  821. Content-Type: multipart/alternative;
  822. boundary="----_=_NextPart_004_01CBE272.13692C80"
  823. ------_=_NextPart_004_01CBE272.13692C80
  824. Content-Type: text/plain;
  825. charset="iso-8859-1"
  826. Content-Transfer-Encoding: quoted-printable
  827. =20
  828. Viele Gr=FC=DFe
  829. ------_=_NextPart_004_01CBE272.13692C80
  830. Content-Type: text/html;
  831. charset="iso-8859-1"
  832. Content-Transfer-Encoding: quoted-printable
  833. <html>...</html>
  834. ------_=_NextPart_004_01CBE272.13692C80--
  835. ------_=_NextPart_003_01CBE272.13692C80
  836. Content-Type: application/x-zip-compressed;
  837. name="abc.zip"
  838. Content-Transfer-Encoding: base64
  839. Content-Disposition: attachment;
  840. filename="abc.zip"
  841. [base64-content]
  842. ------_=_NextPart_003_01CBE272.13692C80--
  843. ------_=_NextPart_001_01CBE273.65A0E7AA--
  844. `
  845. var mime3 = `From [email protected] Mon Feb 19 22:24:21 2001
  846. Received: from [137.154.210.66] by hotmail.com (3.2) with ESMTP id MHotMailBC5B58230039400431D5899AD24289FA0; Mon Feb 19 22:22:29 2001
  847. Received: from lancelot.cit.nepean.uws.edu.au (lancelot.cit.uws.edu.au [137.154.148.30])
  848. by day.uws.edu.au (8.11.1/8.11.1) with ESMTP id f1K6MN404936;
  849. Tue, 20 Feb 2001 17:22:24 +1100 (EST)
  850. Received: from hotmail.com (law2-f35.hotmail.com [216.32.181.35])
  851. by lancelot.cit.nepean.uws.edu.au (8.10.0.Beta10/8.10.0.Beta10) with ESMTP id f1K6MJb13619;
  852. Tue, 20 Feb 2001 17:22:19 +1100 (EST)
  853. Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC;
  854. Mon, 19 Feb 2001 22:21:44 -0800
  855. Received: from 203.54.221.89 by lw2fd.hotmail.msn.com with HTTP; Tue, 20 Feb 2001 06:21:44 GMT
  856. X-Originating-IP: [203.54.221.89]
  857. From: "lara devine" <[email protected]>
  858. To: [email protected], [email protected],
  859. [email protected], [email protected],
  860. [email protected], [email protected],
  861. [email protected], [email protected],
  862. [email protected]
  863. Subject: Fwd: Goldfish
  864. Date: Tue, 20 Feb 2001 06:21:44
  865. Mime-Version: 1.0
  866. Content-Type: text/plain; format=flowed
  867. Message-ID: <[email protected]>
  868. X-OriginalArrivalTime: 20 Feb 2001 06:21:44.0718 (UTC) FILETIME=[658BDAE0:01C09B05]
  869. >> >Two builders (Chris and James) are seated either side of a table in a
  870. > > >rough
  871. > > >pub when a well-dressed man enters, orders beer and sits on a stool at
  872. > > >the bar.
  873. > > >The two builders start to speculate about the occupation of the suit.
  874. > > >
  875. > > >Chris: - I reckon he's an accountant.
  876. > > >
  877. > > >James: - No way - he's a stockbroker.
  878. > > >
  879. > > >Chris: - He ain't no stockbroker! A stockbroker wouldn't come in here!
  880. > > >
  881. > > >The argument repeats itself for some time until the volume of beer gets
  882. > > >the better of Chris and he makes for the toilet. On entering the toilet
  883. > > >he
  884. > > >sees that the suit is standing at a urinal. Curiosity and the several
  885. > > >beers
  886. > > >get the better of the builder...
  887. > > >
  888. > > >Chris: - 'scuse me.... no offence meant, but me and me mate were
  889. > > wondering
  890. > > >
  891. > > > what you do for a living?
  892. > > >
  893. > > >Suit: - No offence taken! I'm a Logical Scientist by profession!
  894. > > >
  895. > > >Chris: - Oh! What's that then?
  896. > > >
  897. > > >Suit:- I'll try to explain by example... Do you have a goldfish at
  898. >home?
  899. > > >
  900. > > >Chris:- Er...mmm... well yeah, I do as it happens!
  901. > > >
  902. > > >Suit: - Well, it's logical to follow that you keep it in a bowl or in a
  903. > > >pond. Which is it?
  904. > > >
  905. > > >Chris: - It's in a pond!
  906. > > >
  907. > > >Suit: - Well then it's reasonable to suppose that you have a large
  908. > > >garden
  909. > > >then?
  910. > > >
  911. > > >Chris: - As it happens, yes I have got a big garden!
  912. > > >
  913. > > >Suit: - Well then it's logical to assume that in this town that if you
  914. > > >have a large garden that you have a large house?
  915. > > >
  916. > > >Chris: - As it happens I've got a five bedroom house... built it
  917. >myself!
  918. > > >
  919. > > >Suit: - Well given that you've built a five-bedroom house it is logical
  920. > > >to asume that you haven't built it just for yourself and that you are
  921. > > >quite
  922. > > >probably married?
  923. > > >
  924. > > >Chris: - Yes I am married, I live with my wife and three children!
  925. > > >
  926. > > >Suit: - Well then it is logical to assume that you are sexually active
  927. > > >with your wife on a regular basis?
  928. > > >
  929. > > >Chris:- Yep! Four nights a week!
  930. > > >
  931. > > >Suit: - Well then it is logical to suggest that you do not masturbate
  932. > > >very often?
  933. > > >
  934. > > >Chris: - Me? Never.
  935. > > >
  936. > > >Suit: - Well there you are! That's logical science at work!
  937. > > >
  938. > > >Chris:- How's that then?
  939. > > >
  940. > > >Suit: - Well from finding out that you had a goldfish, I've told you
  941. > > >about the size of garden you have, size of house, your family and your
  942. > > >sex
  943. > > >life!
  944. > > >
  945. > > >Chris: - I see! That's pretty impressive... thanks mate!
  946. > > >
  947. > > >Both leave the toilet and Chris returns to his mate.
  948. > > >
  949. > > >James: - I see the suit was in there. Did you ask him what he does?
  950. > > >
  951. > > >Chris: - Yep! He's a logical scientist!
  952. > > >
  953. > > >James: What's a logical Scientist?
  954. > > >
  955. > > >Chris: - I'll try and explain. Do you have a goldfish?
  956. > > >
  957. > > >James: - Nope.
  958. > > >
  959. > > >Chris: - Well then, you're a wanker.
  960. >
  961. _________________________________________________________________________
  962. Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
  963. `
  964. /*
  965. 1 0 166 1514
  966. 1.1 186 260 259
  967. 1.2 280 374 416
  968. 1.3 437 530 584
  969. 1.4 605 769 1514
  970. */
  971. func TestStreamMimeProcessor(t *testing.T) {
  972. if err := os.Truncate("tests/testlog", 0); err != nil {
  973. t.Error(err)
  974. }
  975. cfg := &AppConfig{
  976. LogFile: "tests/testlog",
  977. AllowedHosts: []string{"grr.la"},
  978. BackendConfig: backends.BackendConfig{
  979. "save_process": "HeadersParser|Debugger",
  980. "stream_save_process": "mimeanalyzer|headersparser|compress|Decompress|debug",
  981. },
  982. }
  983. d := Daemon{Config: cfg}
  984. if err := d.Start(); err != nil {
  985. t.Error(err)
  986. }
  987. go func() {
  988. time.Sleep(time.Second * 15)
  989. // for debugging deadlocks
  990. //pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
  991. //os.Exit(1)
  992. }()
  993. // change \n to \r\n
  994. mime0 = strings.Replace(mime2, "\n", "\r\n", -1)
  995. // lets have a talk with the server
  996. if err := talkToServer("127.0.0.1:2525", mime0); err != nil {
  997. t.Error(err)
  998. }
  999. d.Shutdown()
  1000. b, err := ioutil.ReadFile("tests/testlog")
  1001. if err != nil {
  1002. t.Error("could not read logfile")
  1003. return
  1004. }
  1005. // lets check for fingerprints
  1006. if strings.Index(string(b), "Debug stream") < 0 {
  1007. t.Error("did not log: Debug stream")
  1008. }
  1009. if strings.Index(string(b), "Error") != -1 {
  1010. t.Error("There was an error", string(b))
  1011. }
  1012. }
  1013. var email = `From: Al Gore <[email protected]>
  1014. To: White House Transportation Coordinator <[email protected]>
  1015. Subject: [Fwd: Map of Argentina with Description]
  1016. MIME-Version: 1.0
  1017. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; s=ncr424; d=reliancegeneral.co.in;
  1018. h=List-Unsubscribe:MIME-Version:From:To:Reply-To:Date:Subject:Content-Type:Content-Transfer-Encoding:Message-ID; [email protected];
  1019. bh=F4UQPGEkpmh54C7v3DL8mm2db1QhZU4gRHR1jDqffG8=;
  1020. b=MVltcq6/I9b218a370fuNFLNinR9zQcdBSmzttFkZ7TvV2mOsGrzrwORT8PKYq4KNJNOLBahswXf
  1021. GwaMjDKT/5TXzegdX/L3f/X4bMAEO1einn+nUkVGLK4zVQus+KGqm4oP7uVXjqp70PWXScyWWkbT
  1022. 1PGUwRfPd/HTJG5IUqs=
  1023. Content-Type: multipart/mixed;
  1024. boundary="D7F------------D7FD5A0B8AB9C65CCDBFA872"
  1025. This is a multi-part message in MIME format.
  1026. --D7F------------D7FD5A0B8AB9C65CCDBFA872
  1027. Content-Type: text/plain; charset=us-ascii
  1028. Content-Transfer-Encoding: 7bit
  1029. Fred,
  1030. Fire up Air Force One! We're going South!
  1031. Thanks,
  1032. Al
  1033. --D7F------------D7FD5A0B8AB9C65CCDBFA872
  1034. Content-Type: message/rfc822
  1035. Content-Transfer-Encoding: 7bit
  1036. Content-Disposition: inline
  1037. Return-Path: <[email protected]>
  1038. Received: from mailhost.whitehouse.gov ([192.168.51.200])
  1039. by heartbeat.whitehouse.gov (8.8.8/8.8.8) with ESMTP id SAA22453
  1040. for <[email protected]>;
  1041. Mon, 13 Aug 1998 l8:14:23 +1000
  1042. Received: from the_big_box.whitehouse.gov ([192.168.51.50])
  1043. by mailhost.whitehouse.gov (8.8.8/8.8.7) with ESMTP id RAA20366
  1044. for [email protected]; Mon, 13 Aug 1998 17:42:41 +1000
  1045. Date: Mon, 13 Aug 1998 17:42:41 +1000
  1046. Message-Id: <[email protected]>
  1047. From: Bill Clinton <[email protected]>
  1048. To: A1 (The Enforcer) Gore <[email protected]>
  1049. Subject: Map of Argentina with Description
  1050. MIME-Version: 1.0
  1051. Content-Type: multipart/mixed;
  1052. boundary="DC8------------DC8638F443D87A7F0726DEF7"
  1053. This is a multi-part message in MIME format.
  1054. --DC8------------DC8638F443D87A7F0726DEF7
  1055. Content-Type: text/plain; charset=us-ascii
  1056. Content-Transfer-Encoding: 7bit
  1057. Hi A1,
  1058. I finally figured out this MIME thing. Pretty cool. I'll send you
  1059. some sax music in .au files next week!
  1060. Anyway, the attached image is really too small to get a good look at
  1061. Argentina. Try this for a much better map:
  1062. http://www.1one1yp1anet.com/dest/sam/graphics/map-arg.htm
  1063. Then again, shouldn't the CIA have something like that?
  1064. Bill
  1065. --DC8------------DC8638F443D87A7F0726DEF7
  1066. Content-Type: image/gif; name="map_of_Argentina.gif"
  1067. Content-Transfer-Encoding: base64
  1068. Content-Disposition: inline; filename="map_of_Argentina.gif"
  1069. R01GOD1hJQA1AKIAAP/////78P/omn19fQAAAAAAAAAAAAAAACwAAAAAJQA1AAAD7Qi63P5w
  1070. wEmjBCLrnQnhYCgM1wh+pkgqqeC9XrutmBm7hAK3tP31gFcAiFKVQrGFR6kscnonTe7FAAad
  1071. GugmRu3CmiBt57fsVq3Y0VFKnpYdxPC6M7Ze4crnnHum4oN6LFJ1bn5NXTN7OF5fQkN5WYow
  1072. BEN2dkGQGWJtSzqGTICJgnQuTJN/WJsojad9qXMuhIWdjXKjY4tenjo6tjVssk2gaWq3uGNX
  1073. U6ZGxseyk8SasGw3J9GRzdTQky1iHNvcPNNI4TLeKdfMvy0vMqLrItvuxfDW8ubjueDtJufz
  1074. 7itICBxISKDBgwgTKjyYAAA7
  1075. --DC8------------DC8638F443D87A7F0726DEF7--
  1076. --D7F------------D7FD5A0B8AB9C65CCDBFA872--
  1077. `
  1078. func TestStreamChunkSaver(t *testing.T) {
  1079. if err := os.Truncate("tests/testlog", 0); err != nil {
  1080. t.Error(err)
  1081. }
  1082. go func() {
  1083. time.Sleep(time.Second * 15)
  1084. // for debugging deadlocks
  1085. //pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
  1086. //os.Exit(1)
  1087. }()
  1088. cfg := &AppConfig{
  1089. LogFile: "tests/testlog",
  1090. AllowedHosts: []string{"grr.la"},
  1091. BackendConfig: backends.BackendConfig{
  1092. "stream_save_process": "mimeanalyzer|chunksaver",
  1093. "chunksaver_chunk_size": 1024 * 32,
  1094. "stream_buffer_size": 1024 * 16,
  1095. "chunksaver_storage_engine": "memory",
  1096. },
  1097. }
  1098. d := Daemon{Config: cfg}
  1099. if err := d.Start(); err != nil {
  1100. t.Error(err)
  1101. }
  1102. // change \n to \r\n
  1103. email = strings.Replace(email, "\n", "\r\n", -1)
  1104. // lets have a talk with the server
  1105. if err := talkToServer("127.0.0.1:2525", email); err != nil {
  1106. t.Error(err)
  1107. }
  1108. time.Sleep(time.Second * 1)
  1109. d.Shutdown()
  1110. b, err := ioutil.ReadFile("tests/testlog")
  1111. if err != nil {
  1112. t.Error("could not read logfile")
  1113. return
  1114. }
  1115. fmt.Println(string(b))
  1116. // lets check for fingerprints
  1117. if strings.Index(string(b), "Debug stream") < 0 {
  1118. // t.Error("did not log: Debug stream")
  1119. }
  1120. }