api_test.go 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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. backends.ConfigProcessors: {
  91. "debugger": {
  92. "log_received_mails": true,
  93. },
  94. "header": {
  95. "primary_mail_host": "example.com",
  96. },
  97. },
  98. backends.ConfigGateways: {
  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 complaining 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().Fields("recipient", e.RcptTo[len(e.RcptTo)-1]).Info(
  401. "another funky recipient")
  402. // if valid then forward call to the next processor in the chain
  403. return p.Process(e, task)
  404. } else if task == backends.TaskSaveMail {
  405. backends.Log().Info("Another funky email!")
  406. }
  407. return p.Process(e, task)
  408. })
  409. }
  410. }
  411. // How about a custom processor?
  412. func TestSetAddProcessor(t *testing.T) {
  413. if err := os.Truncate("tests/testlog", 0); err != nil {
  414. t.Error(err)
  415. }
  416. cfg := &AppConfig{
  417. LogFile: "tests/testlog",
  418. AllowedHosts: []string{"grr.la"},
  419. BackendConfig: backends.BackendConfig{
  420. backends.ConfigGateways: {
  421. "default": {
  422. "save_process": "HeadersParser|Debugger|FunkyLogger",
  423. "validate_process": "FunkyLogger",
  424. },
  425. },
  426. },
  427. }
  428. d := Daemon{Config: cfg}
  429. d.AddProcessor("FunkyLogger", funkyLogger)
  430. if err := d.Start(); err != nil {
  431. t.Error(err)
  432. }
  433. // lets have a talk with the server
  434. if err := talkToServer("127.0.0.1:2525", ""); err != nil {
  435. t.Error(err)
  436. }
  437. d.Shutdown()
  438. b, err := ioutil.ReadFile("tests/testlog")
  439. if err != nil {
  440. t.Error("could not read logfile")
  441. return
  442. }
  443. // lets check for fingerprints
  444. if !strings.Contains(string(b), "another funky recipient") {
  445. t.Error("did not log: another funky recipient")
  446. }
  447. if !strings.Contains(string(b), "Another funky email!") {
  448. t.Error("Did not log: Another funky email!")
  449. }
  450. if !strings.Contains(string(b), "Funky logger is up & down to funk") {
  451. t.Error("Did not log: Funky logger is up & down to funk")
  452. }
  453. if !strings.Contains(string(b), "The funk has been stopped!") {
  454. t.Error("Did not log:The funk has been stopped!")
  455. }
  456. }
  457. func talkToServer(address string, body string) (err error) {
  458. conn, err := net.Dial("tcp", address)
  459. if err != nil {
  460. return
  461. }
  462. in := bufio.NewReader(conn)
  463. str, err := in.ReadString('\n')
  464. if err != nil {
  465. return err
  466. }
  467. _, err = fmt.Fprint(conn, "HELO maildiranasaurustester\r\n")
  468. if err != nil {
  469. return err
  470. }
  471. str, err = in.ReadString('\n')
  472. if err != nil {
  473. return err
  474. }
  475. _, err = fmt.Fprint(conn, "MAIL FROM:<[email protected]> BODY=8BITMIME\r\n")
  476. if err != nil {
  477. return err
  478. }
  479. str, err = in.ReadString('\n')
  480. if err != nil {
  481. return err
  482. }
  483. _, err = fmt.Fprint(conn, "RCPT TO:<[email protected]>\r\n")
  484. if err != nil {
  485. return err
  486. }
  487. str, err = in.ReadString('\n')
  488. if err != nil {
  489. return err
  490. }
  491. _, err = fmt.Fprint(conn, "DATA\r\n")
  492. if err != nil {
  493. return err
  494. }
  495. str, err = in.ReadString('\n')
  496. if err != nil {
  497. return err
  498. }
  499. if body == "" {
  500. _, err = fmt.Fprint(conn, "Subject: Test subject\r\n")
  501. if err != nil {
  502. return err
  503. }
  504. _, err = fmt.Fprint(conn, "\r\n")
  505. if err != nil {
  506. return err
  507. }
  508. _, err = fmt.Fprint(conn, "A an email body\r\n")
  509. if err != nil {
  510. return err
  511. }
  512. _, err = fmt.Fprint(conn, ".\r\n")
  513. if err != nil {
  514. return err
  515. }
  516. } else {
  517. _, err = fmt.Fprint(conn, body)
  518. if err != nil {
  519. return err
  520. }
  521. _, err = fmt.Fprint(conn, ".\r\n")
  522. if err != nil {
  523. return err
  524. }
  525. }
  526. str, err = in.ReadString('\n')
  527. if err != nil {
  528. return err
  529. }
  530. _, err = fmt.Fprint(conn, "QUIT\r\n")
  531. if err != nil {
  532. return err
  533. }
  534. _ = str
  535. return nil
  536. }
  537. // Test hot config reload
  538. // Here we forgot to add FunkyLogger so backend will fail to init
  539. // it will log to stderr at the beginning, but then change to tests/testlog
  540. func TestReloadConfig(t *testing.T) {
  541. if err := os.Truncate("tests/testlog", 0); err != nil {
  542. t.Error(err)
  543. }
  544. d := Daemon{}
  545. if err := d.Start(); err != nil {
  546. t.Error(err)
  547. }
  548. defer d.Shutdown()
  549. cfg := AppConfig{
  550. LogFile: "tests/testlog",
  551. AllowedHosts: []string{"grr.la"},
  552. BackendConfig: backends.BackendConfig{
  553. backends.ConfigGateways: {
  554. "default": {
  555. "save_process": "HeadersParser|Debugger|FunkyLogger",
  556. "validate_process": "FunkyLogger",
  557. },
  558. },
  559. },
  560. }
  561. // Look mom, reloading the config without shutting down!
  562. if err := d.ReloadConfig(cfg); err != nil {
  563. t.Error(err)
  564. }
  565. }
  566. func TestPubSubAPI(t *testing.T) {
  567. if err := truncateIfExists("tests/testlog"); err != nil {
  568. t.Error(err)
  569. }
  570. if err := truncateIfExists("tests/pidfilex.pid"); err != nil {
  571. t.Error(err)
  572. }
  573. d := Daemon{Config: &AppConfig{LogFile: "tests/testlog"}}
  574. if err := d.Start(); err != nil {
  575. t.Error(err)
  576. }
  577. defer d.Shutdown()
  578. // new config
  579. cfg := AppConfig{
  580. PidFile: "tests/pidfilex.pid",
  581. LogFile: "tests/testlog",
  582. AllowedHosts: []string{"grr.la"},
  583. BackendConfig: backends.BackendConfig{
  584. backends.ConfigGateways: {
  585. "default": {
  586. "save_process": "HeadersParser|Debugger|FunkyLogger",
  587. "validate_process": "FunkyLogger",
  588. },
  589. },
  590. },
  591. }
  592. var i = 0
  593. pidEvHandler := func(c *AppConfig) {
  594. i++
  595. if i > 1 {
  596. t.Error("number > 1, it means d.Unsubscribe didn't work")
  597. }
  598. d.Logger.Info("number", i)
  599. }
  600. if err := d.Subscribe(EventConfigPidFile, pidEvHandler); err != nil {
  601. t.Error(err)
  602. }
  603. if err := d.ReloadConfig(cfg); err != nil {
  604. t.Error(err)
  605. }
  606. if err := d.Unsubscribe(EventConfigPidFile, pidEvHandler); err != nil {
  607. t.Error(err)
  608. }
  609. cfg.PidFile = "tests/pidfile2.pid"
  610. d.Publish(EventConfigPidFile, &cfg)
  611. if err := d.ReloadConfig(cfg); err != nil {
  612. t.Error(err)
  613. }
  614. b, err := ioutil.ReadFile("tests/testlog")
  615. if err != nil {
  616. t.Error("could not read logfile")
  617. return
  618. }
  619. // lets interrogate the log
  620. if !strings.Contains(string(b), "number1") {
  621. t.Error("it lools like d.ReloadConfig(cfg) did not fire EventConfigPidFile, pidEvHandler not called")
  622. }
  623. }
  624. func TestAPILog(t *testing.T) {
  625. if err := os.Truncate("tests/testlog", 0); err != nil {
  626. t.Error(err)
  627. }
  628. d := Daemon{}
  629. l := d.Log()
  630. l.Info("logtest1") // to stderr
  631. if l.GetLevel() != log.InfoLevel.String() {
  632. t.Error("Log level does not eq info, it is ", l.GetLevel())
  633. }
  634. d.Logger = nil
  635. d.Config = &AppConfig{LogFile: "tests/testlog"}
  636. l = d.Log()
  637. l.Info("logtest1") // to tests/testlog
  638. //
  639. l = d.Log()
  640. if l.GetLogDest() != "tests/testlog" {
  641. t.Error("log dest is not tests/testlog, it was ", l.GetLogDest())
  642. }
  643. b, err := ioutil.ReadFile("tests/testlog")
  644. if err != nil {
  645. t.Error("could not read logfile")
  646. return
  647. }
  648. // lets interrogate the log
  649. if !strings.Contains(string(b), "logtest1") {
  650. t.Error("hai was not found in the log, it should have been in tests/testlog")
  651. }
  652. }
  653. // Test the allowed_hosts config option with a single entry of ".", which will allow all hosts.
  654. func TestSkipAllowsHost(t *testing.T) {
  655. d := Daemon{}
  656. defer d.Shutdown()
  657. // setting the allowed hosts to a single entry with a dot will let any host through
  658. d.Config = &AppConfig{AllowedHosts: []string{"."}, LogFile: "off"}
  659. if err := d.Start(); err != nil {
  660. t.Error(err)
  661. }
  662. conn, err := net.Dial("tcp", d.Config.Servers[0].ListenInterface)
  663. if err != nil {
  664. t.Error(t)
  665. return
  666. }
  667. in := bufio.NewReader(conn)
  668. if _, err := fmt.Fprint(conn, "HELO test\r\n"); err != nil {
  669. t.Error(err)
  670. }
  671. if _, err := fmt.Fprint(conn, "RCPT TO:<[email protected]>\r\n"); err != nil {
  672. t.Error(err)
  673. }
  674. if _, err := in.ReadString('\n'); err != nil {
  675. t.Error(err)
  676. }
  677. if _, err := in.ReadString('\n'); err != nil {
  678. t.Error(err)
  679. }
  680. str, _ := in.ReadString('\n')
  681. if strings.Index(str, "250") != 0 {
  682. t.Error("expected 250 reply, got:", str)
  683. }
  684. }
  685. var customBackend2 = func() backends.Decorator {
  686. return func(p backends.Processor) backends.Processor {
  687. return backends.ProcessWith(
  688. func(e *mail.Envelope, task backends.SelectTask) (backends.Result, error) {
  689. if task == backends.TaskValidateRcpt {
  690. return p.Process(e, task)
  691. } else if task == backends.TaskSaveMail {
  692. backends.Log().Info("Another funky email!")
  693. err := errors.New("system shock")
  694. return backends.NewResult(response.Canned.FailReadErrorDataCmd, response.SP, err), err
  695. }
  696. return p.Process(e, task)
  697. })
  698. }
  699. }
  700. // Test a custom backend response
  701. func TestCustomBackendResult(t *testing.T) {
  702. if err := os.Truncate("tests/testlog", 0); err != nil {
  703. t.Error(err)
  704. }
  705. cfg := &AppConfig{
  706. LogFile: "tests/testlog",
  707. AllowedHosts: []string{"grr.la"},
  708. BackendConfig: backends.BackendConfig{
  709. backends.ConfigGateways: {
  710. "default": {
  711. "save_process": "HeadersParser|Debugger|Custom",
  712. "validate_process": "Custom",
  713. },
  714. },
  715. },
  716. }
  717. d := Daemon{Config: cfg}
  718. d.AddProcessor("Custom", customBackend2)
  719. if err := d.Start(); err != nil {
  720. t.Error(err)
  721. return
  722. }
  723. // lets have a talk with the server
  724. if err := talkToServer("127.0.0.1:2525", ""); err != nil {
  725. t.Error(err)
  726. }
  727. d.Shutdown()
  728. b, err := ioutil.ReadFile("tests/testlog")
  729. if err != nil {
  730. t.Error("could not read logfile")
  731. return
  732. }
  733. // lets check for fingerprints
  734. if !strings.Contains(string(b), "451 4.3.0 Error") {
  735. t.Error("did not log: 451 4.3.0 Error")
  736. }
  737. if !strings.Contains(string(b), "system shock") {
  738. t.Error("did not log: system shock")
  739. }
  740. }
  741. // Test a backends removed, 2 new backends added added
  742. func TestBackendAddRemove(t *testing.T) {
  743. if err := os.Truncate("tests/testlog", 0); err != nil {
  744. t.Error(err)
  745. }
  746. servers := []ServerConfig{
  747. 0: {
  748. IsEnabled: true,
  749. Hostname: "mail.guerrillamail.com",
  750. MaxSize: 100017,
  751. Timeout: 160,
  752. ListenInterface: "127.0.0.1:2526",
  753. MaxClients: 2,
  754. TLS: ServerTLSConfig{
  755. PrivateKeyFile: "",
  756. PublicKeyFile: "",
  757. StartTLSOn: false,
  758. AlwaysOn: false,
  759. },
  760. },
  761. }
  762. cfg := &AppConfig{
  763. LogFile: "tests/testlog",
  764. PidFile: "tests/go-guerrilla.pid",
  765. AllowedHosts: []string{"grr.la", "spam4.me"},
  766. BackendConfig: backends.BackendConfig{
  767. backends.ConfigGateways: {
  768. "default": {
  769. "save_process": "HeadersParser|Debugger|Custom",
  770. "validate_process": "Custom",
  771. },
  772. "temp": {
  773. "save_process": "HeadersParser|Debugger|Custom",
  774. "validate_process": "Custom",
  775. },
  776. },
  777. },
  778. Servers: servers,
  779. }
  780. d := Daemon{Config: cfg}
  781. d.AddProcessor("Custom", customBackend2)
  782. if err := d.Start(); err != nil {
  783. t.Error(err)
  784. return
  785. }
  786. cfg2 := *cfg
  787. cfg2.BackendConfig = backends.BackendConfig{
  788. backends.ConfigGateways: {
  789. "client1": {
  790. "save_process": "HeadersParser|Debugger|Custom",
  791. "validate_process": "Custom",
  792. },
  793. "client2": {
  794. "save_process": "HeadersParser|Debugger",
  795. "validate_process": "Custom",
  796. },
  797. },
  798. }
  799. eventFiredAdded := false
  800. _ = d.Subscribe(EventConfigBackendConfigAdded, backendEvent(func(appConfig *AppConfig, name string) {
  801. eventFiredAdded = true
  802. }))
  803. eventFiredRemoved := false
  804. _ = d.Subscribe(EventConfigBackendConfigRemoved, backendEvent(func(appConfig *AppConfig, name string) {
  805. eventFiredRemoved = true
  806. }))
  807. // default changed, temp removed, client1 and client2 added
  808. if err := d.ReloadConfig(cfg2); err != nil {
  809. t.Error(err)
  810. return
  811. }
  812. d.Shutdown()
  813. if eventFiredAdded == false {
  814. t.Error("EventConfigBackendConfigAdded did not fired")
  815. }
  816. if eventFiredRemoved == false {
  817. t.Error("EventConfigBackendConfigRemoved did not get fired")
  818. }
  819. }
  820. func TestStreamProcessorConfig(t *testing.T) {
  821. if err := os.Truncate("tests/testlog", 0); err != nil {
  822. t.Error(err)
  823. }
  824. servers := []ServerConfig{
  825. 0: {
  826. IsEnabled: true,
  827. Hostname: "mail.guerrillamail.com",
  828. MaxSize: 100017,
  829. Timeout: 160,
  830. ListenInterface: "127.0.0.1:2526",
  831. MaxClients: 2,
  832. TLS: ServerTLSConfig{
  833. PrivateKeyFile: "",
  834. PublicKeyFile: "",
  835. StartTLSOn: false,
  836. AlwaysOn: false,
  837. },
  838. },
  839. }
  840. cfg := &AppConfig{
  841. LogFile: "tests/testlog",
  842. PidFile: "tests/go-guerrilla.pid",
  843. AllowedHosts: []string{"grr.la", "spam4.me"},
  844. BackendConfig: backends.BackendConfig{
  845. backends.ConfigStreamProcessors: {
  846. "chunkSaver": { // note mixed case
  847. "chunk_size": 8000,
  848. "storage_engine": "memory",
  849. "compress_level": 0,
  850. },
  851. "test:chunksaver": {
  852. "chunk_size": 8000,
  853. "storage_engine": "memory",
  854. "compress_level": 0,
  855. },
  856. "debug": {
  857. "sleep_seconds": 2,
  858. "log_reads": true,
  859. },
  860. },
  861. backends.ConfigGateways: {
  862. "default": {
  863. "save_stream": "mimeanalyzer|chunksaver|test|debug",
  864. },
  865. },
  866. },
  867. Servers: servers,
  868. }
  869. d := Daemon{Config: cfg}
  870. if err := d.Start(); err != nil {
  871. t.Error(err)
  872. return
  873. }
  874. d.Shutdown()
  875. }
  876. func TestStreamProcessor(t *testing.T) {
  877. if err := os.Truncate("tests/testlog", 0); err != nil {
  878. t.Error(err)
  879. }
  880. cfg := &AppConfig{
  881. LogFile: "tests/testlog",
  882. AllowedHosts: []string{"grr.la"},
  883. BackendConfig: backends.BackendConfig{
  884. backends.ConfigStreamProcessors: {
  885. "debug": {
  886. "log_reads": true,
  887. },
  888. },
  889. backends.ConfigGateways: {
  890. "default": {
  891. "save_process": "HeadersParser|Debugger",
  892. "save_stream": "Header|headersparser|compress|Decompress|debug",
  893. "post_process": "Header|headersparser|compress|Decompress|debug",
  894. },
  895. },
  896. },
  897. }
  898. d := Daemon{Config: cfg}
  899. if err := d.Start(); err != nil {
  900. t.Error(err)
  901. }
  902. body := "Subject: Test subject\r\n" +
  903. //"\r\n" +
  904. "A an email body.\r\n" +
  905. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  906. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  907. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  908. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  909. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  910. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  911. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  912. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  913. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  914. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  915. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  916. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n"
  917. // lets have a talk with the server
  918. if err := talkToServer("127.0.0.1:2525", body); err != nil {
  919. t.Error(err)
  920. }
  921. d.Shutdown()
  922. b, err := ioutil.ReadFile("tests/testlog")
  923. if err != nil {
  924. t.Error("could not read logfile")
  925. return
  926. }
  927. // lets check for fingerprints
  928. if strings.Index(string(b), "debug stream") < 0 {
  929. t.Error("did not log: Debug stream")
  930. }
  931. if strings.Index(string(b), "Error") != -1 {
  932. t.Error("There was an error", string(b))
  933. }
  934. }
  935. func TestStreamProcessorBackground(t *testing.T) {
  936. if err := os.Truncate("tests/testlog", 0); err != nil {
  937. t.Error(err)
  938. }
  939. cfg := &AppConfig{
  940. LogFile: "tests/testlog",
  941. AllowedHosts: []string{"grr.la"},
  942. BackendConfig: backends.BackendConfig{
  943. backends.ConfigStreamProcessors: {
  944. "debug": {
  945. "log_reads": true,
  946. },
  947. "moo:chunksaver": {
  948. "chunk_size": 8000,
  949. "storage_engine": "memory",
  950. "compress_level": 0,
  951. },
  952. },
  953. backends.ConfigGateways: {
  954. "default": {
  955. "save_process": "",
  956. "save_stream": "mimeanalyzer|moo",
  957. "post_process_consumer": "Header|headersparser|compress|Decompress|debug",
  958. "post_process_producer": "moo",
  959. "post_process_size": 100,
  960. "stream_buffer_size": 1024,
  961. "save_workers_size": 8,
  962. "save_timeout": "20s",
  963. "val_rcpt_timeout": "2s",
  964. },
  965. },
  966. },
  967. }
  968. d := Daemon{Config: cfg}
  969. if err := d.Start(); err != nil {
  970. t.Error(err)
  971. }
  972. body := "Subject: Test subject\r\n" +
  973. "\r\n" +
  974. "A an email body.\r\n" +
  975. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  976. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  977. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  978. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  979. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  980. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  981. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  982. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  983. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  984. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  985. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n" +
  986. "Header|headersparser|compress|Decompress|debug Header|headersparser|compress|Decompress|debug.\r\n"
  987. // lets have a talk with the server
  988. if err := talkToServer("127.0.0.1:2525", body); err != nil {
  989. t.Error(err)
  990. }
  991. d.Shutdown()
  992. b, err := ioutil.ReadFile("tests/testlog")
  993. if err != nil {
  994. t.Error("could not read logfile")
  995. return
  996. }
  997. time.Sleep(time.Second * 2)
  998. // lets check for fingerprints
  999. if strings.Index(string(b), "debug stream") < 0 {
  1000. t.Error("did not log: Debug stream")
  1001. }
  1002. if strings.Index(string(b), "background process done") < 0 {
  1003. t.Error("did not log: background process done")
  1004. }
  1005. if strings.Index(string(b), "Error") != -1 {
  1006. t.Error("There was an error", string(b))
  1007. }
  1008. }
  1009. var mime0 = `MIME-Version: 1.0
  1010. X-Mailer: MailBee.NET 8.0.4.428
  1011. Subject: test
  1012. subject
  1013. To: [email protected]
  1014. Content-Type: multipart/mixed;
  1015. boundary="XXXXboundary text"
  1016. --XXXXboundary text
  1017. Content-Type: multipart/alternative;
  1018. boundary="XXXXboundary text"
  1019. --XXXXboundary text
  1020. Content-Type: text/plain;
  1021. charset="utf-8"
  1022. Content-Transfer-Encoding: quoted-printable
  1023. This is the body text of a sample message.
  1024. --XXXXboundary text
  1025. Content-Type: text/html;
  1026. charset="utf-8"
  1027. Content-Transfer-Encoding: quoted-printable
  1028. <pre>This is the body text of a sample message.</pre>
  1029. --XXXXboundary text
  1030. Content-Type: text/plain;
  1031. name="log_attachment.txt"
  1032. Content-Disposition: attachment;
  1033. filename="log_attachment.txt"
  1034. Content-Transfer-Encoding: base64
  1035. TUlNRS1WZXJzaW9uOiAxLjANClgtTWFpbGVyOiBNYWlsQmVlLk5FVCA4LjAuNC40MjgNClN1Ympl
  1036. Y3Q6IHRlc3Qgc3ViamVjdA0KVG86IGtldmlubUBkYXRhbW90aW9uLmNvbQ0KQ29udGVudC1UeXBl
  1037. OiBtdWx0aXBhcnQvYWx0ZXJuYXRpdmU7DQoJYm91bmRhcnk9Ii0tLS09X05leHRQYXJ0XzAwMF9B
  1038. RTZCXzcyNUUwOUFGLjg4QjdGOTM0Ig0KDQoNCi0tLS0tLT1fTmV4dFBhcnRfMDAwX0FFNkJfNzI1
  1039. RTA5QUYuODhCN0Y5MzQNCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsNCgljaGFyc2V0PSJ1dGYt
  1040. OCINCkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6IHF1b3RlZC1wcmludGFibGUNCg0KdGVzdCBi
  1041. b2R5DQotLS0tLS09X05leHRQYXJ0XzAwMF9BRTZCXzcyNUUwOUFGLjg4QjdGOTM0DQpDb250ZW50
  1042. LVR5cGU6IHRleHQvaHRtbDsNCgljaGFyc2V0PSJ1dGYtOCINCkNvbnRlbnQtVHJhbnNmZXItRW5j
  1043. b2Rpbmc6IHF1b3RlZC1wcmludGFibGUNCg0KPHByZT50ZXN0IGJvZHk8L3ByZT4NCi0tLS0tLT1f
  1044. TmV4dFBhcnRfMDAwX0FFNkJfNzI1RTA5QUYuODhCN0Y5MzQtLQ0K
  1045. --XXXXboundary text--
  1046. `
  1047. var mime2 = `From: [email protected]
  1048. Content-Type: multipart/mixed;
  1049. boundary="----_=_NextPart_001_01CBE273.65A0E7AA"
  1050. To: [email protected]
  1051. This is a multi-part message in MIME format.
  1052. ------_=_NextPart_001_01CBE273.65A0E7AA
  1053. Content-Type: multipart/alternative;
  1054. boundary="----_=_NextPart_002_01CBE273.65A0E7AA"
  1055. ------_=_NextPart_002_01CBE273.65A0E7AA
  1056. Content-Type: text/plain;
  1057. charset="UTF-8"
  1058. Content-Transfer-Encoding: base64
  1059. [base64-content]
  1060. ------_=_NextPart_002_01CBE273.65A0E7AA
  1061. Content-Type: text/html;
  1062. charset="UTF-8"
  1063. Content-Transfer-Encoding: base64
  1064. [base64-content]
  1065. ------_=_NextPart_002_01CBE273.65A0E7AA--
  1066. ------_=_NextPart_001_01CBE273.65A0E7AA
  1067. Content-Type: message/rfc822
  1068. Content-Transfer-Encoding: 7bit
  1069. X-MimeOLE: Produced By Microsoft Exchange V6.5
  1070. Content-class: urn:content-classes:message
  1071. MIME-Version: 1.0
  1072. Content-Type: multipart/mixed;
  1073. boundary="----_=_NextPart_003_01CBE272.13692C80"
  1074. From: [email protected]
  1075. To: [email protected]
  1076. This is a multi-part message in MIME format.
  1077. ------_=_NextPart_003_01CBE272.13692C80
  1078. Content-Type: multipart/alternative;
  1079. boundary="----_=_NextPart_004_01CBE272.13692C80"
  1080. ------_=_NextPart_004_01CBE272.13692C80
  1081. Content-Type: text/plain;
  1082. charset="iso-8859-1"
  1083. Content-Transfer-Encoding: quoted-printable
  1084. =20
  1085. Viele Gr=FC=DFe
  1086. ------_=_NextPart_004_01CBE272.13692C80
  1087. Content-Type: text/html;
  1088. charset="iso-8859-1"
  1089. Content-Transfer-Encoding: quoted-printable
  1090. <html>...</html>
  1091. ------_=_NextPart_004_01CBE272.13692C80--
  1092. ------_=_NextPart_003_01CBE272.13692C80
  1093. Content-Type: application/x-zip-compressed;
  1094. name="abc.zip"
  1095. Content-Transfer-Encoding: base64
  1096. Content-Disposition: attachment;
  1097. filename="abc.zip"
  1098. [base64-content]
  1099. ------_=_NextPart_003_01CBE272.13692C80--
  1100. ------_=_NextPart_001_01CBE273.65A0E7AA--
  1101. `
  1102. var mime3 = `From [email protected] Mon Feb 19 22:24:21 2001
  1103. Received: from [137.154.210.66] by hotmail.com (3.2) with ESMTP id MHotMailBC5B58230039400431D5899AD24289FA0; Mon Feb 19 22:22:29 2001
  1104. Received: from lancelot.cit.nepean.uws.edu.au (lancelot.cit.uws.edu.au [137.154.148.30])
  1105. by day.uws.edu.au (8.11.1/8.11.1) with ESMTP id f1K6MN404936;
  1106. Tue, 20 Feb 2001 17:22:24 +1100 (EST)
  1107. Received: from hotmail.com (law2-f35.hotmail.com [216.32.181.35])
  1108. by lancelot.cit.nepean.uws.edu.au (8.10.0.Beta10/8.10.0.Beta10) with ESMTP id f1K6MJb13619;
  1109. Tue, 20 Feb 2001 17:22:19 +1100 (EST)
  1110. Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC;
  1111. Mon, 19 Feb 2001 22:21:44 -0800
  1112. Received: from 203.54.221.89 by lw2fd.hotmail.msn.com with HTTP; Tue, 20 Feb 2001 06:21:44 GMT
  1113. X-Originating-IP: [203.54.221.89]
  1114. From: "lara devine" <[email protected]>
  1115. To: [email protected], [email protected],
  1116. [email protected], [email protected],
  1117. [email protected], [email protected],
  1118. [email protected], [email protected],
  1119. [email protected]
  1120. Subject: Fwd: Goldfish
  1121. Date: Tue, 20 Feb 2001 06:21:44
  1122. Mime-Version: 1.0
  1123. Content-Type: text/plain; format=flowed
  1124. Message-ID: <[email protected]>
  1125. X-OriginalArrivalTime: 20 Feb 2001 06:21:44.0718 (UTC) FILETIME=[658BDAE0:01C09B05]
  1126. >> >Two builders (Chris and James) are seated either side of a table in a
  1127. > > >rough
  1128. > > >pub when a well-dressed man enters, orders beer and sits on a stool at
  1129. > > >the bar.
  1130. > > >The two builders start to speculate about the occupation of the suit.
  1131. > > >
  1132. > > >Chris: - I reckon he's an accountant.
  1133. > > >
  1134. > > >James: - No way - he's a stockbroker.
  1135. > > >
  1136. > > >Chris: - He ain't no stockbroker! A stockbroker wouldn't come in here!
  1137. > > >
  1138. > > >The argument repeats itself for some time until the volume of beer gets
  1139. > > >the better of Chris and he makes for the toilet. On entering the toilet
  1140. > > >he
  1141. > > >sees that the suit is standing at a urinal. Curiosity and the several
  1142. > > >beers
  1143. > > >get the better of the builder...
  1144. > > >
  1145. > > >Chris: - 'scuse me.... no offence meant, but me and me mate were
  1146. > > wondering
  1147. > > >
  1148. > > > what you do for a living?
  1149. > > >
  1150. > > >Suit: - No offence taken! I'm a Logical Scientist by profession!
  1151. > > >
  1152. > > >Chris: - Oh! What's that then?
  1153. > > >
  1154. > > >Suit:- I'll try to explain by example... Do you have a goldfish at
  1155. >home?
  1156. > > >
  1157. > > >Chris:- Er...mmm... well yeah, I do as it happens!
  1158. > > >
  1159. > > >Suit: - Well, it's logical to follow that you keep it in a bowl or in a
  1160. > > >pond. Which is it?
  1161. > > >
  1162. > > >Chris: - It's in a pond!
  1163. > > >
  1164. > > >Suit: - Well then it's reasonable to suppose that you have a large
  1165. > > >garden
  1166. > > >then?
  1167. > > >
  1168. > > >Chris: - As it happens, yes I have got a big garden!
  1169. > > >
  1170. > > >Suit: - Well then it's logical to assume that in this town that if you
  1171. > > >have a large garden that you have a large house?
  1172. > > >
  1173. > > >Chris: - As it happens I've got a five bedroom house... built it
  1174. >myself!
  1175. > > >
  1176. > > >Suit: - Well given that you've built a five-bedroom house it is logical
  1177. > > >to asume that you haven't built it just for yourself and that you are
  1178. > > >quite
  1179. > > >probably married?
  1180. > > >
  1181. > > >Chris: - Yes I am married, I live with my wife and three children!
  1182. > > >
  1183. > > >Suit: - Well then it is logical to assume that you are sexually active
  1184. > > >with your wife on a regular basis?
  1185. > > >
  1186. > > >Chris:- Yep! Four nights a week!
  1187. > > >
  1188. > > >Suit: - Well then it is logical to suggest that you do not masturbate
  1189. > > >very often?
  1190. > > >
  1191. > > >Chris: - Me? Never.
  1192. > > >
  1193. > > >Suit: - Well there you are! That's logical science at work!
  1194. > > >
  1195. > > >Chris:- How's that then?
  1196. > > >
  1197. > > >Suit: - Well from finding out that you had a goldfish, I've told you
  1198. > > >about the size of garden you have, size of house, your family and your
  1199. > > >sex
  1200. > > >life!
  1201. > > >
  1202. > > >Chris: - I see! That's pretty impressive... thanks mate!
  1203. > > >
  1204. > > >Both leave the toilet and Chris returns to his mate.
  1205. > > >
  1206. > > >James: - I see the suit was in there. Did you ask him what he does?
  1207. > > >
  1208. > > >Chris: - Yep! He's a logical scientist!
  1209. > > >
  1210. > > >James: What's a logical Scientist?
  1211. > > >
  1212. > > >Chris: - I'll try and explain. Do you have a goldfish?
  1213. > > >
  1214. > > >James: - Nope.
  1215. > > >
  1216. > > >Chris: - Well then, you're a wanker.
  1217. >
  1218. _________________________________________________________________________
  1219. Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
  1220. `
  1221. /*
  1222. 1 0 166 1514
  1223. 1.1 186 260 259
  1224. 1.2 280 374 416
  1225. 1.3 437 530 584
  1226. 1.4 605 769 1514
  1227. */
  1228. func TestStreamMimeProcessor(t *testing.T) {
  1229. if err := os.Truncate("tests/testlog", 0); err != nil {
  1230. t.Error(err)
  1231. }
  1232. cfg := &AppConfig{
  1233. LogFile: "tests/testlog",
  1234. AllowedHosts: []string{"grr.la"},
  1235. BackendConfig: backends.BackendConfig{
  1236. backends.ConfigStreamProcessors: {
  1237. "debug": {
  1238. "log_reads": true,
  1239. },
  1240. },
  1241. backends.ConfigGateways: {
  1242. "default": {
  1243. "save_process": "HeadersParser|Debugger",
  1244. "save_stream": "mimeanalyzer|headersparser|compress|Decompress|debug",
  1245. },
  1246. },
  1247. },
  1248. }
  1249. d := Daemon{Config: cfg}
  1250. if err := d.Start(); err != nil {
  1251. t.Error(err)
  1252. }
  1253. go func() {
  1254. time.Sleep(time.Second * 15)
  1255. // for debugging deadlocks
  1256. //pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
  1257. //os.Exit(1)
  1258. }()
  1259. // change \n to \r\n
  1260. mime0 = strings.Replace(mime2, "\n", "\r\n", -1)
  1261. // lets have a talk with the server
  1262. if err := talkToServer("127.0.0.1:2525", mime0); err != nil {
  1263. t.Error(err)
  1264. }
  1265. d.Shutdown()
  1266. b, err := ioutil.ReadFile("tests/testlog")
  1267. if err != nil {
  1268. t.Error("could not read logfile")
  1269. return
  1270. }
  1271. // lets check for fingerprints
  1272. if strings.Index(string(b), "debug stream") < 0 {
  1273. t.Error("did not log: Debug stream")
  1274. }
  1275. if strings.Index(string(b), "Error") != -1 {
  1276. t.Error("There was an error", string(b))
  1277. }
  1278. }
  1279. var email = `From: Al Gore <[email protected]>
  1280. To: White House Transportation Coordinator <[email protected]>
  1281. Subject: [Fwd: Map of Argentina with Description]
  1282. MIME-Version: 1.0
  1283. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; s=ncr424; d=reliancegeneral.co.in;
  1284. h=List-Unsubscribe:MIME-Version:From:To:Reply-To:Date:Subject:Content-Type:Content-Transfer-Encoding:Message-ID; [email protected];
  1285. bh=F4UQPGEkpmh54C7v3DL8mm2db1QhZU4gRHR1jDqffG8=;
  1286. b=MVltcq6/I9b218a370fuNFLNinR9zQcdBSmzttFkZ7TvV2mOsGrzrwORT8PKYq4KNJNOLBahswXf
  1287. GwaMjDKT/5TXzegdX/L3f/X4bMAEO1einn+nUkVGLK4zVQus+KGqm4oP7uVXjqp70PWXScyWWkbT
  1288. 1PGUwRfPd/HTJG5IUqs=
  1289. Content-Type: multipart/mixed;
  1290. boundary="D7F------------D7FD5A0B8AB9C65CCDBFA872"
  1291. This is a multi-part message in MIME format.
  1292. --D7F------------D7FD5A0B8AB9C65CCDBFA872
  1293. Content-Type: text/plain; charset=us-ascii
  1294. Content-Transfer-Encoding: 7bit
  1295. Fred,
  1296. Fire up Air Force One! We're going South!
  1297. Thanks,
  1298. Al
  1299. --D7F------------D7FD5A0B8AB9C65CCDBFA872
  1300. Content-Type: message/rfc822
  1301. Content-Transfer-Encoding: 7bit
  1302. Content-Disposition: inline
  1303. Return-Path: <[email protected]>
  1304. Received: from mailhost.whitehouse.gov ([192.168.51.200])
  1305. by heartbeat.whitehouse.gov (8.8.8/8.8.8) with ESMTP id SAA22453
  1306. for <[email protected]>;
  1307. Mon, 13 Aug 1998 l8:14:23 +1000
  1308. Received: from the_big_box.whitehouse.gov ([192.168.51.50])
  1309. by mailhost.whitehouse.gov (8.8.8/8.8.7) with ESMTP id RAA20366
  1310. for [email protected]; Mon, 13 Aug 1998 17:42:41 +1000
  1311. Date: Mon, 13 Aug 1998 17:42:41 +1000
  1312. Message-Id: <[email protected]>
  1313. From: Bill Clinton <[email protected]>
  1314. To: A1 (The Enforcer) Gore <[email protected]>
  1315. Subject: Map of Argentina with Description
  1316. MIME-Version: 1.0
  1317. Content-Type: multipart/mixed;
  1318. boundary="DC8------------DC8638F443D87A7F0726DEF7"
  1319. This is a multi-part message in MIME format.
  1320. --DC8------------DC8638F443D87A7F0726DEF7
  1321. Content-Type: text/plain; charset=us-ascii
  1322. Content-Transfer-Encoding: 7bit
  1323. Hi A1,
  1324. I finally figured out this MIME thing. Pretty cool. I'll send you
  1325. some sax music in .au files next week!
  1326. Anyway, the attached image is really too small to get a good look at
  1327. Argentina. Try this for a much better map:
  1328. http://www.1one1yp1anet.com/dest/sam/graphics/map-arg.htm
  1329. Then again, shouldn't the CIA have something like that?
  1330. Bill
  1331. --DC8------------DC8638F443D87A7F0726DEF7
  1332. Content-Type: image/gif; name="map_of_Argentina.gif"
  1333. Content-Transfer-Encoding: base64
  1334. Content-Disposition: inline; filename="map_of_Argentina.gif"
  1335. R01GOD1hJQA1AKIAAP/////78P/omn19fQAAAAAAAAAAAAAAACwAAAAAJQA1AAAD7Qi63P5w
  1336. wEmjBCLrnQnhYCgM1wh+pkgqqeC9XrutmBm7hAK3tP31gFcAiFKVQrGFR6kscnonTe7FAAad
  1337. GugmRu3CmiBt57fsVq3Y0VFKnpYdxPC6M7Ze4crnnHum4oN6LFJ1bn5NXTN7OF5fQkN5WYow
  1338. BEN2dkGQGWJtSzqGTICJgnQuTJN/WJsojad9qXMuhIWdjXKjY4tenjo6tjVssk2gaWq3uGNX
  1339. U6ZGxseyk8SasGw3J9GRzdTQky1iHNvcPNNI4TLeKdfMvy0vMqLrItvuxfDW8ubjueDtJufz
  1340. 7itICBxISKDBgwgTKjyYAAA7
  1341. --DC8------------DC8638F443D87A7F0726DEF7--
  1342. --D7F------------D7FD5A0B8AB9C65CCDBFA872--
  1343. `
  1344. func TestStreamChunkSaver(t *testing.T) {
  1345. if err := os.Truncate("tests/testlog", 0); err != nil {
  1346. t.Error(err)
  1347. }
  1348. go func() {
  1349. time.Sleep(time.Second * 15)
  1350. // for debugging deadlocks
  1351. //pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
  1352. //os.Exit(1)
  1353. }()
  1354. cfg := &AppConfig{
  1355. LogFile: "tests/testlog",
  1356. AllowedHosts: []string{"grr.la"},
  1357. BackendConfig: backends.BackendConfig{
  1358. backends.ConfigStreamProcessors: {
  1359. "chunksaver": {
  1360. "chunk_size": 1024 * 32,
  1361. "stream_buffer_size": 1024 * 16,
  1362. "storage_engine": "memory",
  1363. },
  1364. },
  1365. backends.ConfigGateways: {
  1366. "default": {
  1367. "save_process": "HeadersParser|Debugger",
  1368. "save_stream": "mimeanalyzer|chunksaver",
  1369. "save_timeout": "5",
  1370. },
  1371. },
  1372. },
  1373. }
  1374. d := Daemon{Config: cfg}
  1375. if err := d.Start(); err != nil {
  1376. t.Error(err)
  1377. }
  1378. // change \n to \r\n
  1379. email = strings.Replace(email, "\n", "\r\n", -1)
  1380. // lets have a talk with the server
  1381. if err := talkToServer("127.0.0.1:2525", email); err != nil {
  1382. t.Error(err)
  1383. }
  1384. time.Sleep(time.Second * 1)
  1385. d.Shutdown()
  1386. b, err := ioutil.ReadFile("tests/testlog")
  1387. if err != nil {
  1388. t.Error("could not read logfile")
  1389. return
  1390. }
  1391. fmt.Println(string(b))
  1392. // lets check for fingerprints
  1393. if strings.Index(string(b), "Debug stream") < 0 {
  1394. // t.Error("did not log: Debug stream")
  1395. }
  1396. }