syslog_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package syslog
  2. import (
  3. "reflect"
  4. "testing"
  5. syslog "github.com/RackSec/srslog"
  6. )
  7. func functionMatches(expectedFun interface{}, actualFun interface{}) bool {
  8. return reflect.ValueOf(expectedFun).Pointer() == reflect.ValueOf(actualFun).Pointer()
  9. }
  10. func TestParseLogFormat(t *testing.T) {
  11. formatter, framer, err := parseLogFormat("rfc5424", "udp")
  12. if err != nil || !functionMatches(rfc5424formatterWithAppNameAsTag, formatter) ||
  13. !functionMatches(syslog.DefaultFramer, framer) {
  14. t.Fatal("Failed to parse rfc5424 format", err, formatter, framer)
  15. }
  16. formatter, framer, err = parseLogFormat("rfc5424", "tcp+tls")
  17. if err != nil || !functionMatches(rfc5424formatterWithAppNameAsTag, formatter) ||
  18. !functionMatches(syslog.RFC5425MessageLengthFramer, framer) {
  19. t.Fatal("Failed to parse rfc5424 format", err, formatter, framer)
  20. }
  21. formatter, framer, err = parseLogFormat("rfc5424micro", "udp")
  22. if err != nil || !functionMatches(rfc5424microformatterWithAppNameAsTag, formatter) ||
  23. !functionMatches(syslog.DefaultFramer, framer) {
  24. t.Fatal("Failed to parse rfc5424 (microsecond) format", err, formatter, framer)
  25. }
  26. formatter, framer, err = parseLogFormat("rfc5424micro", "tcp+tls")
  27. if err != nil || !functionMatches(rfc5424microformatterWithAppNameAsTag, formatter) ||
  28. !functionMatches(syslog.RFC5425MessageLengthFramer, framer) {
  29. t.Fatal("Failed to parse rfc5424 (microsecond) format", err, formatter, framer)
  30. }
  31. formatter, framer, err = parseLogFormat("rfc3164", "")
  32. if err != nil || !functionMatches(syslog.RFC3164Formatter, formatter) ||
  33. !functionMatches(syslog.DefaultFramer, framer) {
  34. t.Fatal("Failed to parse rfc3164 format", err, formatter, framer)
  35. }
  36. formatter, framer, err = parseLogFormat("", "")
  37. if err != nil || !functionMatches(syslog.UnixFormatter, formatter) ||
  38. !functionMatches(syslog.DefaultFramer, framer) {
  39. t.Fatal("Failed to parse empty format", err, formatter, framer)
  40. }
  41. formatter, framer, err = parseLogFormat("invalid", "")
  42. if err == nil {
  43. t.Fatal("Failed to parse invalid format", err, formatter, framer)
  44. }
  45. }
  46. func TestValidateLogOptEmpty(t *testing.T) {
  47. emptyConfig := make(map[string]string)
  48. if err := ValidateLogOpt(emptyConfig); err != nil {
  49. t.Fatal("Failed to parse empty config", err)
  50. }
  51. }