syslog_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package syslogacquisition
  2. import (
  3. "fmt"
  4. "net"
  5. "testing"
  6. "time"
  7. "github.com/crowdsecurity/crowdsec/pkg/cstest"
  8. "github.com/crowdsecurity/crowdsec/pkg/types"
  9. log "github.com/sirupsen/logrus"
  10. "gopkg.in/tomb.v2"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestConfigure(t *testing.T) {
  14. tests := []struct {
  15. config string
  16. expectedErr string
  17. }{
  18. {
  19. config: `
  20. foobar: bla
  21. source: syslog`,
  22. expectedErr: "line 2: field foobar not found in type syslogacquisition.SyslogConfiguration",
  23. },
  24. {
  25. config: `source: syslog`,
  26. expectedErr: "",
  27. },
  28. {
  29. config: `
  30. source: syslog
  31. listen_port: asd`,
  32. expectedErr: "cannot unmarshal !!str `asd` into int",
  33. },
  34. {
  35. config: `
  36. source: syslog
  37. listen_port: 424242`,
  38. expectedErr: "invalid port 424242",
  39. },
  40. {
  41. config: `
  42. source: syslog
  43. listen_addr: 10.0.0`,
  44. expectedErr: "invalid listen IP 10.0.0",
  45. },
  46. }
  47. subLogger := log.WithFields(log.Fields{
  48. "type": "syslog",
  49. })
  50. for _, test := range tests {
  51. s := SyslogSource{}
  52. err := s.Configure([]byte(test.config), subLogger)
  53. cstest.AssertErrorContains(t, err, test.expectedErr)
  54. }
  55. }
  56. func writeToSyslog(logs []string) {
  57. conn, err := net.Dial("udp", "127.0.0.1:4242")
  58. if err != nil {
  59. fmt.Printf("could not establish connection to syslog server : %s", err)
  60. return
  61. }
  62. for _, log := range logs {
  63. fmt.Fprint(conn, log)
  64. }
  65. }
  66. func TestStreamingAcquisition(t *testing.T) {
  67. tests := []struct {
  68. config string
  69. expectedErr string
  70. logs []string
  71. expectedLines int
  72. }{
  73. {
  74. config: `source: syslog`,
  75. expectedErr: "could not start syslog server: could not listen on port 514: listen udp 127.0.0.1:514: bind: permission denied",
  76. },
  77. {
  78. config: `
  79. source: syslog
  80. listen_port: 4242
  81. listen_addr: 127.0.0.1`,
  82. logs: []string{"foobar", "bla", "pouet"},
  83. },
  84. {
  85. config: `
  86. source: syslog
  87. listen_port: 4242
  88. listen_addr: 127.0.0.1`,
  89. expectedLines: 2,
  90. logs: []string{`<13>1 2021-05-18T11:58:40.828081+02:00 mantis sshd 49340 - [timeQuality isSynced="0" tzKnown="1"] blabla`,
  91. `<13>1 2021-05-18T12:12:37.560695+02:00 mantis sshd 49340 - [timeQuality isSynced="0" tzKnown="1"] blabla2[foobar]`},
  92. },
  93. {
  94. config: `
  95. source: syslog
  96. listen_port: 4242
  97. listen_addr: 127.0.0.1`,
  98. expectedLines: 3,
  99. logs: []string{`<13>May 18 12:37:56 mantis sshd[49340]: blabla2[foobar]`,
  100. `<13>May 18 12:37:56 mantis sshd[49340]: blabla2`,
  101. `<13>May 18 12:37:56 mantis sshd: blabla2`,
  102. `<13>May 18 12:37:56 mantis sshd`},
  103. },
  104. }
  105. for _, ts := range tests {
  106. subLogger := log.WithFields(log.Fields{
  107. "type": "syslog",
  108. })
  109. s := SyslogSource{}
  110. _ = s.Configure([]byte(ts.config), subLogger)
  111. tomb := tomb.Tomb{}
  112. out := make(chan types.Event)
  113. err := s.StreamingAcquisition(out, &tomb)
  114. cstest.AssertErrorContains(t, err, ts.expectedErr)
  115. if err != nil {
  116. continue
  117. }
  118. actualLines := 0
  119. go writeToSyslog(ts.logs)
  120. READLOOP:
  121. for {
  122. select {
  123. case <-out:
  124. actualLines++
  125. case <-time.After(2 * time.Second):
  126. break READLOOP
  127. }
  128. }
  129. assert.Equal(t, ts.expectedLines, actualLines)
  130. tomb.Kill(nil)
  131. tomb.Wait()
  132. }
  133. }