syslog_test.go 3.2 KB

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