Debian Codegraph @

rfc3164/rfc3164_test.go

raw symbols SCIP
1 package rfc3164
2
3 import (
4 "bytes"
5 "strings"
6 "testing"
7 "time"
8
9 "github.com/jeromer/syslogparser"
10 "github.com/jeromer/syslogparser/parsercommon"
11 "github.com/stretchr/testify/require"
12 )
13
14 var (
15 // XXX : corresponds to the length of the last tried timestamp format
16 // XXX : Jan 2 15:04:05
17 lastTriedTimestampLen = 15
18 )
19
20 func TestParserValid(t *testing.T) {
21 buff := []byte(
22 "<34>Oct 11 22:14:15 mymachine very.large.syslog.message.tag: 'su root' failed for lonvick on /dev/pts/8",
23 )
24
25 p := NewParser(buff)
26
27 require.Equal(
28 t,
29 &Parser{
30 buff: buff,
31 cursor: 0,
32 l: len(buff),
33 location: time.UTC,
34 },
35 p,
36 )
37
38 err := p.Parse()
39
40 require.Nil(
41 t, err,
42 )
43
44 require.Equal(
45 t,
46 syslogparser.LogParts{
47 "timestamp": time.Date(
48 time.Now().Year(),
49 time.October,
50 11, 22, 14, 15, 0,
51 time.UTC,
52 ),
53 "hostname": "mymachine",
54 "tag": "very.large.syslog.message.tag",
55 "content": "'su root' failed for lonvick on /dev/pts/8",
56 "priority": 34,
57 "facility": 4,
58 "severity": 2,
59 },
60 p.Dump(),
61 )
62 }
63
64 func TestParserWithPriority(t *testing.T) {
65 buff := []byte(
66 "Oct 11 22:14:15 mymachine very.large.syslog.message.tag: 'su root' failed for lonvick on /dev/pts/8",
67 )
68
69 pri := parsercommon.NewPriority(0)
70
71 p := NewParser(buff)
72 p.WithPriority(pri)
73
74 require.Equal(
75 t,
76 &Parser{
77 buff: buff,
78 cursor: 0,
79 l: len(buff),
80 location: time.UTC,
81 priority: pri,
82 },
83 p,
84 )
85
86 err := p.Parse()
87
88 require.Nil(
89 t, err,
90 )
91
92 require.Equal(
93 t,
94 syslogparser.LogParts{
95 "timestamp": time.Date(
96 time.Now().Year(),
97 time.October,
98 11, 22, 14, 15, 0,
99 time.UTC,
100 ),
101 "hostname": "mymachine",
102 "tag": "very.large.syslog.message.tag",
103 "content": "'su root' failed for lonvick on /dev/pts/8",
104 "priority": 0,
105 "facility": 0,
106 "severity": 0,
107 },
108 p.Dump(),
109 )
110 }
111
112 func TestParserWithHostname(t *testing.T) {
113 buff := []byte(
114 "<30>Jun 23 13:17:42 chronyd[1119]: Selected source 192.168.65.1",
115 )
116
117 p := NewParser(buff)
118 p.WithHostname("dummy")
119
120 err := p.Parse()
121 require.Nil(t, err)
122
123 require.Equal(
124 t,
125 syslogparser.LogParts{
126 "timestamp": time.Date(
127 time.Now().Year(),
128 time.June,
129 23, 13, 17, 42, 0,
130 time.UTC,
131 ),
132 "hostname": "dummy",
133 "tag": "chronyd",
134 "content": "Selected source 192.168.65.1",
135 "priority": 30,
136 "facility": 3,
137 "severity": 6,
138 },
139 p.Dump(),
140 )
141 }
142
143 func TestParserWithTag(t *testing.T) {
144 buff := []byte(
145 "<30>Jun 23 13:17:42 localhost Selected source 192.168.65.1",
146 )
147
148 tag := "chronyd"
149 p := NewParser(buff)
150 p.WithTag(tag)
151
152 err := p.Parse()
153 require.Nil(t, err)
154
155 require.Equal(
156 t,
157 syslogparser.LogParts{
158 "timestamp": time.Date(
159 time.Now().Year(),
160 time.June,
161 23, 13, 17, 42, 0,
162 time.UTC,
163 ),
164 "hostname": "localhost",
165 "tag": "chronyd",
166 "content": "Selected source 192.168.65.1",
167 "priority": 30,
168 "facility": 3,
169 "severity": 6,
170 },
171 p.Dump(),
172 )
173 }
174
175 func TestParserWithLocation(t *testing.T) {
176 buff := []byte(
177 "<30>Jun 23 13:17:42 localhost foo: Selected source 192.168.65.1",
178 )
179
180 loc, err := time.LoadLocation("America/New_York")
181 require.Nil(t, err)
182
183 p := NewParser(buff)
184 p.WithLocation(loc)
185
186 err = p.Parse()
187 require.Nil(t, err)
188
189 require.Equal(
190 t,
191 syslogparser.LogParts{
192 "timestamp": time.Date(
193 time.Now().Year(),
194 time.June,
195 23, 13, 17, 42, 0,
196 loc,
197 ),
198 "hostname": "localhost",
199 "tag": "foo",
200 "content": "Selected source 192.168.65.1",
201 "priority": 30,
202 "facility": 3,
203 "severity": 6,
204 },
205 p.Dump(),
206 )
207 }
208
209 func TestParserWithTimestampFormat(t *testing.T) {
210 buff := []byte(
211 "<30>2006-01-02T15:04:05 localhost foo: Selected source 192.168.65.1",
212 )
213
214 p := NewParser(buff)
215 p.WithTimestampFormat(
216 "2006-01-02T15:04:05",
217 )
218
219 err := p.Parse()
220 require.Nil(t, err)
221
222 require.Equal(
223 t,
224 syslogparser.LogParts{
225 "timestamp": time.Date(
226 2006, time.January, 2,
227 15, 4, 5, 0,
228 time.UTC,
229 ),
230 "hostname": "localhost",
231 "tag": "foo",
232 "content": "Selected source 192.168.65.1",
233 "priority": 30,
234 "facility": 3,
235 "severity": 6,
236 },
237 p.Dump(),
238 )
239 }
240
241 func TestParserWithPriorityHostnameTag(t *testing.T) {
242 buff := []byte(
243 "Oct 11 22:14:15 'su root' failed for lonvick on /dev/pts/8",
244 )
245
246 pri := parsercommon.NewPriority(0)
247 h := "mymachine"
248 tag := "foo"
249
250 p := NewParser(buff)
251 p.WithPriority(pri)
252 p.WithHostname(h)
253 p.WithTag(tag)
254
255 require.Equal(
256 t,
257 &Parser{
258 buff: buff,
259 cursor: 0,
260 l: len(buff),
261 location: time.UTC,
262 priority: pri,
263 hostname: h,
264 customTag: tag,
265 },
266 p,
267 )
268
269 err := p.Parse()
270
271 require.Nil(
272 t, err,
273 )
274
275 require.Equal(
276 t,
277 syslogparser.LogParts{
278 "timestamp": time.Date(
279 time.Now().Year(),
280 time.October,
281 11, 22, 14, 15, 0,
282 time.UTC,
283 ),
284 "hostname": h,
285 "tag": tag,
286 "content": "'su root' failed for lonvick on /dev/pts/8",
287 "priority": 0,
288 "facility": 0,
289 "severity": 0,
290 },
291 p.Dump(),
292 )
293 }
294
295 func TestParseHeader(t *testing.T) {
296 date := time.Date(
297 time.Now().Year(),
298 time.October,
299 11, 22, 14, 15, 0,
300 time.UTC,
301 )
302
303 testCases := []struct {
304 description string
305 input string
306 expectedHdr *header
307 expectedCursorPos int
308 expectedErr error
309 }{
310 {
311 description: "valid headers",
312 input: "Oct 11 22:14:15 mymachine ",
313 expectedHdr: &header{
314 hostname: "mymachine",
315 timestamp: date,
316 },
317 expectedCursorPos: 25,
318 expectedErr: nil,
319 },
320 {
321 description: "valid headers with prepended space",
322 input: " Oct 11 22:14:15 mymachine ",
323 expectedHdr: &header{
324 hostname: "mymachine",
325 timestamp: date,
326 },
327 expectedCursorPos: 26,
328 expectedErr: nil,
329 },
330 {
331 description: "invalid timestamp",
332 input: "Oct 34 32:72:82 mymachine ",
333 expectedHdr: nil,
334 expectedCursorPos: lastTriedTimestampLen + 1,
335 expectedErr: parsercommon.ErrTimestampUnknownFormat,
336 },
337 }
338
339 for _, tc := range testCases {
340 p := NewParser([]byte(tc.input))
341 obtained, err := p.parseHeader()
342
343 require.Equal(
344 t, tc.expectedErr, err, tc.description,
345 )
346
347 require.Equal(
348 t, tc.expectedHdr, obtained, tc.description,
349 )
350
351 require.Equal(
352 t, tc.expectedCursorPos, p.cursor, tc.description,
353 )
354 }
355 }
356
357 func TestParsemessage(t *testing.T) {
358 content := "foo bar baz blah quux"
359
360 buff := []byte("sometag[123]: " + content)
361
362 msg := &message{
363 tag: "sometag",
364 content: content,
365 }
366
367 p := NewParser(buff)
368 obtained, err := p.parsemessage()
369
370 require.Equal(
371 t, parsercommon.ErrEOL, err,
372 )
373
374 require.Equal(
375 t, msg, obtained,
376 )
377
378 require.Equal(
379 t, len(buff), p.cursor,
380 )
381 }
382
383 func TestParseTimestamp(t *testing.T) {
384 testCases := []struct {
385 description string
386 input string
387 expectedTS time.Time
388 expectedCursorPos int
389 expectedErr error
390 }{
391 {
392 description: "invalid",
393 input: "Oct 34 32:72:82",
394 expectedCursorPos: lastTriedTimestampLen,
395 expectedErr: parsercommon.ErrTimestampUnknownFormat,
396 },
397 {
398 description: "trailing space",
399 input: "Oct 11 22:14:15 ",
400 expectedTS: time.Date(
401 time.Now().Year(),
402 time.October,
403 11, 22, 14, 15, 0,
404 time.UTC,
405 ),
406 expectedCursorPos: 16,
407 expectedErr: nil,
408 },
409 {
410 description: "one digit for month",
411 input: "Oct 1 22:14:15",
412 expectedTS: time.Date(
413 time.Now().Year(),
414 time.October,
415 1, 22, 14, 15, 0,
416 time.UTC,
417 ),
418 expectedCursorPos: 15,
419 expectedErr: nil,
420 },
421 {
422 description: "valid timestamp",
423 input: "Oct 11 22:14:15",
424 expectedTS: time.Date(
425 time.Now().Year(),
426 time.October,
427 11, 22, 14, 15, 0,
428 time.UTC,
429 ),
430 expectedCursorPos: 15,
431 expectedErr: nil,
432 },
433 }
434
435 for _, tc := range testCases {
436 p := NewParser([]byte(tc.input))
437 obtained, err := p.parseTimestamp()
438
439 require.Equal(
440 t, tc.expectedTS, obtained, tc.description,
441 )
442
443 require.Equal(
444 t, tc.expectedCursorPos, p.cursor, tc.description,
445 )
446
447 require.Equal(
448 t, tc.expectedErr, err, tc.description,
449 )
450 }
451 }
452
453 func TestParseTag(t *testing.T) {
454 testCases := []struct {
455 description string
456 input string
457 expectedTag string
458 expectedCursorPos int
459 expectedErr error
460 }{
461 {
462 description: "with pid",
463 input: "apache2[10]:",
464 expectedTag: "apache2",
465 expectedCursorPos: 12,
466 expectedErr: nil,
467 },
468 {
469 description: "without pid",
470 input: "apache2:",
471 expectedTag: "apache2",
472 expectedCursorPos: 8,
473 expectedErr: nil,
474 },
475 {
476 description: "trailing space",
477 input: "apache2: ",
478 expectedTag: "apache2",
479 expectedCursorPos: 9,
480 expectedErr: nil,
481 },
482 {
483 description: "super long",
484 input: strings.Repeat("a", 50) + "",
485 expectedTag: strings.Repeat("a", 32),
486 expectedCursorPos: 32,
487 expectedErr: nil,
488 },
489 }
490
491 for _, tc := range testCases {
492 p := NewParser([]byte(tc.input))
493 obtained, err := p.parseTag()
494
495 require.Equal(
496 t, obtained, tc.expectedTag, tc.description,
497 )
498
499 require.Equal(
500 t, tc.expectedCursorPos, p.cursor, tc.description,
501 )
502
503 require.Equal(
504 t, tc.expectedErr, err, tc.description,
505 )
506 }
507 }
508
509 func TestParseContent(t *testing.T) {
510 buff := []byte(" foo bar baz quux ")
511 content := string(bytes.Trim(buff, " "))
512
513 p := NewParser(buff)
514 obtained, err := p.parseContent()
515
516 require.Equal(
517 t, err, parsercommon.ErrEOL,
518 )
519
520 require.Equal(
521 t, content, obtained,
522 )
523
524 require.Equal(
525 t, len(content), p.cursor,
526 )
527 }
528
529 func TestParseMessageSizeChecks(t *testing.T) {
530 start := "<34>Oct 11 22:14:15 mymachine su: "
531 msg := start + strings.Repeat("a", MAX_PACKET_LEN)
532
533 p := NewParser([]byte(msg))
534 err := p.Parse()
535 fields := p.Dump()
536
537 require.Nil(
538 t, err,
539 )
540
541 require.Len(
542 t,
543 fields["content"],
544 MAX_PACKET_LEN-len(start),
545 )
546
547 // ---
548
549 msg = start + "hello"
550 p = NewParser([]byte(msg))
551 err = p.Parse()
552 fields = p.Dump()
553
554 require.Nil(
555 t, err,
556 )
557
558 require.Equal(
559 t, "hello", fields["content"],
560 )
561 }
562
563 func TestParseWithoutTag(t *testing.T) {
564 buff := []byte("<30>Jun 23 13:17:42 127.0.0.1 java.lang.NullPointerException")
565
566 p := NewParser(buff)
567
568 err := p.Parse()
569 require.Nil(t, err)
570
571 now := time.Now()
572
573 obtained := p.Dump()
574 expected := syslogparser.LogParts{
575 "timestamp": time.Date(
576 now.Year(), time.June, 23,
577 13, 17, 42, 0, time.UTC,
578 ),
579 "hostname": "127.0.0.1",
580 "tag": "java.lang.NullPointerException",
581 "content": "",
582 "priority": 30,
583 "facility": 3,
584 "severity": 6,
585 }
586
587 require.Equal(
588 t, expected, obtained,
589 )
590 }
591
592 func BenchmarkParseTimestamp(b *testing.B) {
593 buff := []byte("Oct 11 22:14:15")
594
595 p := NewParser(buff)
596
597 for i := 0; i < b.N; i++ {
598 _, err := p.parseTimestamp()
599 if err != nil {
600 panic(err)
601 }
602
603 p.cursor = 0
604 }
605 }
606
607 func BenchmarkParseHostname(b *testing.B) {
608 buff := []byte("gimli.local")
609
610 p := NewParser(buff)
611
612 for i := 0; i < b.N; i++ {
613 _, err := p.parseHostname()
614 if err != nil {
615 panic(err)
616 }
617
618 p.cursor = 0
619 }
620 }
621
622 func BenchmarkParseTag(b *testing.B) {
623 buff := []byte("apache2[10]:")
624
625 p := NewParser(buff)
626
627 for i := 0; i < b.N; i++ {
628 _, err := p.parseTag()
629 if err != nil {
630 panic(err)
631 }
632
633 p.cursor = 0
634 }
635 }
636
637 func BenchmarkParseHeader(b *testing.B) {
638 buff := []byte("Oct 11 22:14:15 mymachine ")
639
640 p := NewParser(buff)
641
642 for i := 0; i < b.N; i++ {
643 _, err := p.parseHeader()
644 if err != nil {
645 panic(err)
646 }
647
648 p.cursor = 0
649 }
650 }
651
652 func BenchmarkParsemessage(b *testing.B) {
653 buff := []byte("sometag[123]: foo bar baz blah quux")
654
655 p := NewParser(buff)
656
657 for i := 0; i < b.N; i++ {
658 _, err := p.parsemessage()
659 if err != parsercommon.ErrEOL {
660 panic(err)
661 }
662
663 p.cursor = 0
664 }
665 }
666
667 func BenchmarkParseFull(b *testing.B) {
668 msg := "<34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8"
669
670 for i := 0; i < b.N; i++ {
671 p := NewParser(
672 []byte(msg),
673 )
674
675 err := p.Parse()
676 if err != nil {
677 panic(err)
678 }
679 }
680 }
681
682 func TestBenchmarkParseTimestamp(t *testing.T) {
683 type args struct {
684 b *testing.B
685 }
686 tests := []struct {
687 name string
688 args args
689 }{
690 // TODO: Add test cases.
691 }
692 for _, tt := range tests {
693 t.Run(tt.name, func(t *testing.T) {
694 BenchmarkParseTimestamp(tt.args.b)
695 })
696 }
697 }
698
699 func TestBenchmarkParseHostname(t *testing.T) {
700 type args struct {
701 b *testing.B
702 }
703 tests := []struct {
704 name string
705 args args
706 }{
707 // TODO: Add test cases.
708 }
709 for _, tt := range tests {
710 t.Run(tt.name, func(t *testing.T) {
711 BenchmarkParseHostname(tt.args.b)
712 })
713 }
714 }
715
716 func TestBenchmarkParseTag(t *testing.T) {
717 type args struct {
718 b *testing.B
719 }
720 tests := []struct {
721 name string
722 args args
723 }{
724 // TODO: Add test cases.
725 }
726 for _, tt := range tests {
727 t.Run(tt.name, func(t *testing.T) {
728 BenchmarkParseTag(tt.args.b)
729 })
730 }
731 }
732
733 func TestBenchmarkParseHeader(t *testing.T) {
734 type args struct {
735 b *testing.B
736 }
737 tests := []struct {
738 name string
739 args args
740 }{
741 // TODO: Add test cases.
742 }
743 for _, tt := range tests {
744 t.Run(tt.name, func(t *testing.T) {
745 BenchmarkParseHeader(tt.args.b)
746 })
747 }
748 }
749
750 func TestBenchmarkParsemessage(t *testing.T) {
751 type args struct {
752 b *testing.B
753 }
754 tests := []struct {
755 name string
756 args args
757 }{
758 // TODO: Add test cases.
759 }
760 for _, tt := range tests {
761 t.Run(tt.name, func(t *testing.T) {
762 BenchmarkParsemessage(tt.args.b)
763 })
764 }
765 }
766
767 func TestBenchmarkParseFull(t *testing.T) {
768 type args struct {
769 b *testing.B
770 }
771 tests := []struct {
772 name string
773 args args
774 }{
775 // TODO: Add test cases.
776 }
777 for _, tt := range tests {
778 t.Run(tt.name, func(t *testing.T) {
779 BenchmarkParseFull(tt.args.b)
780 })
781 }
782 }