syslog_test.go 2.2 KB

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