api_test.go 34 KB

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