This tests ensure parse does not crash when it meets a log line with too many separators.
(t *testing.T)
| 11 | // This tests ensure parse does not crash when it meets a log line |
| 12 | // with too many separators. |
| 13 | func TestLogLineParse_separators(t *testing.T) { |
| 14 | // Logline can has maximum 3000 fields and 3000 separator. |
| 15 | maxSeparators := int(LogLineNumFields) |
| 16 | tests := []struct { |
| 17 | name string |
| 18 | nseps int |
| 19 | reset bool // Whether the line should be zeroed out after parse. |
| 20 | }{ |
| 21 | { |
| 22 | name: "empty", |
| 23 | nseps: 0, |
| 24 | reset: true, |
| 25 | }, |
| 26 | { |
| 27 | name: "1-separator", |
| 28 | nseps: 1, |
| 29 | reset: false, |
| 30 | }, |
| 31 | { |
| 32 | name: "max-minus-1-separators", |
| 33 | nseps: maxSeparators - 1, |
| 34 | reset: false, |
| 35 | }, |
| 36 | { |
| 37 | name: "max-separators", |
| 38 | nseps: maxSeparators, |
| 39 | reset: false, |
| 40 | }, |
| 41 | { |
| 42 | name: "more-than-max-separators", |
| 43 | nseps: maxSeparators + 1, |
| 44 | reset: true, |
| 45 | }, |
| 46 | } |
| 47 | for _, tt := range tests { |
| 48 | t.Run(tt.name, func(t *testing.T) { |
| 49 | defer func() { |
| 50 | if r := recover(); r != nil { |
| 51 | t.Errorf("!!ALERT!! got panic in Parse: %v", r) |
| 52 | } |
| 53 | }() |
| 54 | |
| 55 | b := bytes.Buffer{} |
| 56 | for i := 0; i < tt.nseps; i++ { |
| 57 | b.WriteByte(DefaultLogLineFieldSeparator) |
| 58 | } |
| 59 | ll := LogLine{FieldSeparator: DefaultLogLineFieldSeparator} |
| 60 | ll.Parse(b.Bytes(), nil) |
| 61 | |
| 62 | if tt.reset && ll.data != nil { |
| 63 | t.Errorf("log line not zeroed out but it should") |
| 64 | } |
| 65 | if !tt.reset && ll.data == nil { |
| 66 | t.Errorf("log line got zeroed out but it shouldn't") |
| 67 | } |
| 68 | }) |
| 69 | } |
| 70 | } |