-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog_test.go
121 lines (115 loc) · 3.42 KB
/
syslog_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package logtap
import (
"bytes"
"fmt"
"reflect"
"strings"
"testing"
"testing/quick"
"time"
"unicode/utf8"
)
func TestEnsureUtf8(t *testing.T) {
// f checks the ensureUtf8's invariant: no matter how terribly
// non-utf8 the input is, the output will be a valid utf8 string.
f := func(b []byte) bool {
s := ensureUtf8(string(b))
return utf8.ValidString(s)
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestParseSyslogMessage(t *testing.T) {
_, err := ParseSyslogMessage([]byte(
"<45>1 2014-01-09T20:34:44.651004+00:00 host heroku api -" +
" Add ZOMGZOMG config by [email protected]"))
if err != nil {
t.Error(err)
}
_, err = ParseSyslogMessage([]byte(
"<45>1 2OI4-01-09T20:34:44.651004+00:00 host heroku api -" +
" Add ZOMGZOMG config by [email protected]"))
if err == nil {
t.Error("Expected to fail when given malformed timestamp!")
}
f := func(b []byte) bool {
// property: there's zero chance random bytes will parse as
// valid syslog message.
_, err := ParseSyslogMessage(b)
return err == ErrSyslogPatternMismatch
}
if err = quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestTokenize(t *testing.T) {
advance, data, err := tokenize(nil, true)
if advance != 0 || data != nil || err != nil {
t.Error("Expecting 0, nil, nil when tokenize is at EOF and has no more data")
}
advance, data, err = tokenize([]byte("65536 YOLO..."), true)
if advance != 0 || data != nil || err == nil {
t.Error("Expecting 0, nil, err when tokenize encounters message length bigger than uint16")
}
f := func(expected []byte) bool {
b := &bytes.Buffer{}
b.WriteString(fmt.Sprintf("%v ", len(expected)))
b.Write(expected)
advance, data, err = tokenize(b.Bytes(), false)
return advance == len(b.Bytes()) && reflect.DeepEqual(data, expected) && err == nil
}
if err = quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func testReadInvalidSyslogMessages(t *testing.T) {
f := func(b []byte) bool {
// property: totally random byte input will never produce messages.
xs, _ := ReadSyslogMessages(nil, bytes.NewBuffer(b))
if len(xs) != 0 {
t.Errorf("Got %v unexpected syslog messages", len(xs))
return false
}
return true
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestReadSyslogMessages(t *testing.T) {
utc, _ := time.LoadLocation("UTC")
expected := []*SyslogMessage{
&SyslogMessage{
Priority: "45",
Version: "1",
Timestamp: time.Date(2014, 1, 9, 20, 34, 44, 651004000, utc),
Hostname: "host",
Appname: "heroku",
Procid: "api",
Msgid: "-",
Text: "Add ZOMGZOMG config by [email protected]"},
&SyslogMessage{
Priority: "45",
Version: "1",
Timestamp: time.Date(2014, 1, 9, 20, 34, 44, 693891000, utc),
Hostname: "host",
Appname: "heroku",
Procid: "api",
Msgid: "-",
Text: "Release v1822 created by [email protected]"},
}
actual, errors := ReadSyslogMessages(nil, strings.NewReader(
`95 <45>1 2014-01-09T20:34:44.651004+00:00 host heroku api - Add ZOMGZOMG config by [email protected]`+
`97 <45>1 2014-01-09T20:34:44.693891+00:00 host heroku api - Release v1822 created by [email protected]`+
`zomg bogus`))
if len(actual) != len(expected) {
t.Errorf("Unexpected number of results: %v", len(actual))
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("%#v != %#v", actual, expected)
}
if len(errors) != 1 {
t.Errorf("Unexpected number of errors: %v", len(errors))
}
}