Skip to content

Commit

Permalink
Add test case for UDP
Browse files Browse the repository at this point in the history
Add a test case for the UDP parser.

Also change timeouts in the first test in the hope that this fixes a bogus
failure from Travis CI.
  • Loading branch information
abligh committed Jul 16, 2015
1 parent 1bef9ad commit 31402d4
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package syslog

import (
"fmt"
"io"
"net"
"testing"
Expand All @@ -17,6 +18,7 @@ type ServerSuite struct {

var _ = Suite(&ServerSuite{})
var exampleSyslog = "<31>Dec 26 05:08:46 hostname tag[296]: content"
var exampleRFC5424Syslog = "<34>1 2003-10-11T22:14:15.003Z mymachine.example.com su - ID47 - 'su root' failed for lonvick on /dev/pts/8"

func (s *ServerSuite) TestTailFile(c *C) {
handler := new(HandlerMock)
Expand All @@ -27,12 +29,12 @@ func (s *ServerSuite) TestTailFile(c *C) {
server.ListenTCP("0.0.0.0:5141")

go func(server *Server) {
time.Sleep(100 * time.Microsecond)
time.Sleep(100 * time.Millisecond)

serverAddr, _ := net.ResolveUDPAddr("udp", "localhost:5141")
con, _ := net.DialUDP("udp", nil, serverAddr)
con.Write([]byte(exampleSyslog))
time.Sleep(100 * time.Microsecond)
time.Sleep(100 * time.Millisecond)

server.Kill()
}(server)
Expand Down Expand Up @@ -146,3 +148,38 @@ func (s *ServerSuite) TestTcpTimeout(c *C) {
c.Check(handler.LastMessageLength, Equals, int64(0))
c.Check(handler.LastError, IsNil)
}

func (s *ServerSuite) TestUDP3164(c *C) {
handler := new(HandlerMock)
server := NewServer()
server.SetFormat(RFC3164)
server.SetHandler(handler)
server.SetTimeout(10)
server.goParseDatagrams()
server.datagramChannel <- DatagramMessage{[]byte(exampleSyslog), "0.0.0.0"}
close(server.datagramChannel)
server.Wait()
c.Check(handler.LastLogParts["hostname"], Equals, "hostname")
c.Check(handler.LastLogParts["tag"], Equals, "tag")
c.Check(handler.LastLogParts["content"], Equals, "content")
c.Check(handler.LastMessageLength, Equals, int64(len(exampleSyslog)))
c.Check(handler.LastError, IsNil)
}

func (s *ServerSuite) TestUDP6587(c *C) {
handler := new(HandlerMock)
server := NewServer()
server.SetFormat(RFC6587)
server.SetHandler(handler)
server.SetTimeout(10)
server.goParseDatagrams()
framedSyslog := []byte(fmt.Sprintf("%d %s", len(exampleRFC5424Syslog), exampleRFC5424Syslog))
server.datagramChannel <- DatagramMessage{[]byte(framedSyslog), "0.0.0.0"}
close(server.datagramChannel)
server.Wait()
c.Check(handler.LastLogParts["hostname"], Equals, "mymachine.example.com")
c.Check(handler.LastLogParts["facility"], Equals, 4)
c.Check(handler.LastLogParts["message"], Equals, "'su root' failed for lonvick on /dev/pts/8")
c.Check(handler.LastMessageLength, Equals, int64(len(exampleRFC5424Syslog)))
c.Check(handler.LastError, IsNil)
}

0 comments on commit 31402d4

Please sign in to comment.