-
Notifications
You must be signed in to change notification settings - Fork 0
/
halfpike_test.go
406 lines (371 loc) · 9.5 KB
/
halfpike_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package halfpike
import (
"context"
"os"
"strings"
"testing"
"github.com/kylelemons/godebug/pretty"
)
const str = `
Peer: 10.10.10.2+179 AS 22 Local: 10.10.10.1+65406 AS 17
Type: External State: Established Flags: <Sync>
`
func TestLexer(t *testing.T) {
want := []Item{
{Type: ItemText, Val: "Peer:"},
{Type: ItemText, Val: "10.10.10.2+179"},
{Type: ItemText, Val: "AS"},
{Type: ItemInt, Val: "22"},
{Type: ItemText, Val: "Local:"},
{Type: ItemText, Val: "10.10.10.1+65406"},
{Type: ItemText, Val: "AS"},
{Type: ItemInt, Val: "17"},
{Type: ItemEOL, Val: "\n", lineNum: 1, raw: "\tPeer: 10.10.10.2+179 AS 22 Local: 10.10.10.1+65406 AS 17 \n"},
{Type: ItemText, Val: "Type:"},
{Type: ItemText, Val: "External"},
{Type: ItemText, Val: "State:"},
{Type: ItemText, Val: "Established"},
{Type: ItemText, Val: "Flags:"},
{Type: ItemText, Val: "<Sync>"},
{Type: ItemEOL, Val: "\n", lineNum: 2, raw: " Type: External State: Established Flags: <Sync>\n"},
{Type: ItemEOF, lineNum: 3, raw: ""},
}
config := pretty.Config{
IncludeUnexported: true,
PrintStringers: true,
}
l := newLexer(context.Background(), str, untilEOF)
go l.run()
got := []Item{}
for item := range l.items {
got = append(got, item)
}
if diff := config.Compare(want, got); diff != "" {
t.Errorf("TestLexer: -want/+got:\n%s", diff)
}
}
func TestNext(t *testing.T) {
want := []Line{
{
LineNum: 1,
Raw: "\tPeer: 10.10.10.2+179 AS 22 Local: 10.10.10.1+65406 AS 17 \n",
Items: []Item{
{Type: ItemText, Val: "Peer:"},
{Type: ItemText, Val: "10.10.10.2+179"},
{Type: ItemText, Val: "AS"},
{Type: ItemInt, Val: "22"},
{Type: ItemText, Val: "Local:"},
{Type: ItemText, Val: "10.10.10.1+65406"},
{Type: ItemText, Val: "AS"},
{Type: ItemInt, Val: "17"},
{Type: ItemEOL, Val: "\n"},
},
},
{
LineNum: 2,
Raw: " Type: External State: Established Flags: <Sync>\n",
Items: []Item{
{Type: ItemText, Val: "Type:"},
{Type: ItemText, Val: "External"},
{Type: ItemText, Val: "State:"},
{Type: ItemText, Val: "Established"},
{Type: ItemText, Val: "Flags:"},
{Type: ItemText, Val: "<Sync>"},
{Type: ItemEOL, Val: "\n"},
},
},
{
LineNum: 3,
Raw: "",
Items: []Item{
{Type: ItemEOF},
},
},
}
p, err := newParser(str)
if err != nil {
panic(err)
}
go p.lex.run()
got := []Line{}
for line := p.Next(); true; line = p.Next() {
got = append(got, line)
if p.EOF(line) {
break
}
}
if diff := pretty.Compare(want, got); diff != "" {
t.Errorf("TestNext: -want/+got:\n%s", diff)
}
}
func TestBackup(t *testing.T) {
p, err := newParser(str)
if err != nil {
panic(err)
}
go p.lex.run()
lines := []Line{}
for line := p.Next(); true; line = p.Next() {
lines = append(lines, line)
if p.EOF(line) {
break
}
}
reverse := make([]Line, len(lines))
for i := 0; i < len(lines); i++ {
reverse[len(lines)-1-i] = p.Backup()
}
if diff := pretty.Compare(lines, reverse); diff != "" {
t.Errorf("TestBackup: -want/+got:\n%s", diff)
}
}
func TestFindStart(t *testing.T) {
tests := []struct {
desc string
find []string
want []Item
err bool
reset bool
}{
{
desc: "Find entry toward the middle",
find: []string{"Local", "Interface:", "ge-1/2/0.0"},
want: []Item{
{Type: 2, Val: "Local"},
{Type: 2, Val: "Interface:"},
{Type: 2, Val: "ge-1/2/0.0"},
{Type: 5, Val: "\n"},
},
err: false,
},
{
desc: "Use Any to find the next entry",
find: []string{"Send", "state:", Skip, "sync"},
want: []Item{
{Type: 2, Val: "Send"},
{Type: 2, Val: "state:"},
{Type: 2, Val: "in"},
{Type: 2, Val: "sync"},
{Type: 5, Val: "\n"},
},
err: false,
},
{
desc: "Can't find the entry (we have already passed it in the input)",
find: []string{"Local", "Interface:", "ge-1/2/0.0"},
want: []Item{
{Type: 2, Val: "Send"},
{Type: 2, Val: "state:"},
{Type: 2, Val: "in"},
{Type: 2, Val: "sync"},
{Type: 5, Val: "\n"},
},
err: true,
},
{
desc: "Too many search parameters lead to no match",
find: []string{"Send", "state:", Skip, "sync", Skip, Skip}, // You have to also include a Skip for EOL or EOF
err: true,
reset: true,
},
}
var p *Parser
for _, test := range tests {
if test.reset || p == nil {
var err error
p, err = newParser(showBGPNeighbor)
if err != nil {
panic(err)
}
go p.lex.run()
}
got, err := p.FindStart(test.find)
switch {
case test.err && err == nil:
t.Fatalf("TestFindStart(%s): got err == nil, want err != nil", test.desc)
case !test.err && err != nil:
t.Fatalf("TestFindStart(%s): got err == %s, want err == nil", test.desc, err)
case err != nil:
continue
}
if diff := pretty.Compare(test.want, got.Items); diff != "" {
t.Fatalf("TestFindStart(%s): -want/+got:\n%s", test.find, diff)
}
}
}
func TestFindUntil(t *testing.T) {
p, err := newParser(showBGPNeighbor)
if err != nil {
panic(err)
}
go p.lex.run()
tests := []struct {
desc string
startFn bool
untilFn bool
find []string
until []string
want []Item
wantUntil bool
err bool
}{
{
desc: "Find record start",
find: peerRecStart,
startFn: true,
want: []Item{
{Type: 2, Val: "Peer:"},
{Type: 2, Val: "10.10.10.2+179"},
{Type: 2, Val: "AS"},
{Type: 3, Val: "22"},
{Type: 2, Val: "Local:"},
{Type: 2, Val: "10.10.10.1+65406"},
{Type: 2, Val: "AS"},
{Type: 3, Val: "17"},
{Type: 5, Val: "\n"},
},
err: false,
},
{
desc: "Find sub record",
untilFn: true,
find: []string{"Send", "state:", Skip, "sync"},
until: peerRecStart,
want: []Item{
{Type: 2, Val: "Send"},
{Type: 2, Val: "state:"},
{Type: 2, Val: "in"},
{Type: 2, Val: "sync"},
{Type: 5, Val: "\n"},
},
err: false,
},
{
desc: "Attempt to find sub record, but instead find next record",
untilFn: true,
find: []string{"Send", "state:", Skip, "sync"},
until: peerRecStart,
wantUntil: true,
err: false,
},
{
desc: "Find next record start",
find: peerRecStart,
startFn: true,
want: []Item{
{Type: 2, Val: "Peer:"},
{Type: 2, Val: "10.10.10.6+54781"},
{Type: 2, Val: "AS"},
{Type: 3, Val: "22"},
{Type: 2, Val: "Local:"},
{Type: 2, Val: "10.10.10.5+179"},
{Type: 2, Val: "AS"},
{Type: 3, Val: "17"},
{Type: 5, Val: "\n"},
},
err: false,
},
{
desc: "Find sub record",
untilFn: true,
find: []string{"Send", "state:", Skip, "sync"},
until: peerRecStart,
want: []Item{
{Type: 2, Val: "Send"},
{Type: 2, Val: "state:"},
{Type: 2, Val: "in"},
{Type: 2, Val: "sync"},
{Type: 5, Val: "\n"},
},
err: false,
},
{
desc: "Attempt to find sub record, but instead find EOF",
untilFn: true,
find: []string{"Send", "state:", Skip, "sync"},
until: peerRecStart,
err: true,
},
}
for _, test := range tests {
if test.startFn {
got, err := p.FindStart(test.find)
switch {
case test.err && err == nil:
t.Fatalf("TestFindUntil(%s): got err == nil, want err != nil", test.desc)
case !test.err && err != nil:
t.Fatalf("TestFindUntil(%s): got err == %s, want err == nil", test.desc, err)
case err != nil:
continue
}
if diff := pretty.Compare(test.want, got.Items); diff != "" {
t.Fatalf("TestFindUntil(%s): -want/+got:\n%s", test.find, diff)
}
} else {
got, until, err := p.FindUntil(test.find, test.until)
switch {
case test.err && err == nil:
t.Fatalf("TestFindUntil(%s): got err == nil, want err != nil", test.desc)
case !test.err && err != nil:
t.Fatalf("TestFindUntil(%s): got err == %s, want err == nil", test.desc, err)
case err != nil:
continue
case until != test.wantUntil:
t.Fatalf("TestFindUntil(%s): got until == %v, want until == %v", test.desc, until, !until)
}
if diff := pretty.Compare(test.want, got.Items); diff != "" {
t.Fatalf("TestFindUntil(%s): -want/+got:\n%s", test.find, diff)
}
}
}
}
type startWithCarriageObj struct{}
func (s *startWithCarriageObj) Start(ctx context.Context, p *Parser) ParseFn {
for {
line := p.Next()
if strings.HasPrefix(line.Raw, "\n") {
return p.Errorf("[LineNum %d]: line.Raw begins with \\n", line.LineNum)
}
if p.EOF(line) {
return nil
}
}
}
func (s *startWithCarriageObj) Validate() error {
return nil
}
func TestRegressionRawStartsWithCarriageReturn(t *testing.T) {
f, err := os.ReadFile("./testing/testfile.claw")
if err != nil {
panic(err)
}
obj := &startWithCarriageObj{}
if err := Parse(context.Background(), string(f), obj); err != nil {
t.Fatalf("TestRegressionRawStartsWithCarriageReturn: got err == %s", err)
}
}
// TestRegressionEOLOnLastLine tests a bug where if we have a EOF after a single character in a line,
// that the item comes out as a single item with type EOF, where it should be two items with EOF at the end.
func TestRegressionEOLOnLastLine(t *testing.T) {
text :=`a
}`
want := []Item{
{Type: ItemText, Val: "a"},
{Type: ItemEOL, Val: "\n"},
{Type: ItemText, Val: "}"},
{Type: ItemEOF},
}
config := pretty.Config{
IncludeUnexported: false,
PrintStringers: true,
}
l := newLexer(context.Background(), text, untilEOF)
go l.run()
got := []Item{}
for item := range l.items {
got = append(got, item)
}
if diff := config.Compare(want, got); diff != "" {
t.Errorf("TestRegressionEOLOnLastLine: -want/+got:\n%s", diff)
}
}