syslog_test.go 3.3 KB

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