-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_test.go
More file actions
88 lines (77 loc) · 1.9 KB
/
reader_test.go
File metadata and controls
88 lines (77 loc) · 1.9 KB
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
package l2
import (
"bytes"
"errors"
"fmt"
"strings"
"testing"
)
var (
defaultdest = macToBytesOrDie("ff:ff:ff:ff:ff:ff")
defaultframe = NewEthFrame(defaultdest, macToBytesOrDie("00:00:00:00:00:00"), 1, make([]byte, 100))
altframe = NewEthFrame(
macToBytesOrDie("aa:bb:cc:dd:ee:00"), macToBytesOrDie("00:00:00:00:00:00"), 1, make([]byte, 100))
)
type testReader []EthFrame
func (t *testReader) ReadFrame() (EthFrame, error) {
if len(*t) > 0 {
p := (*t)[0]
if len(*t) > 1 {
*t = (*t)[1:]
} else {
*t = nil
}
return p, nil
}
return nil, errors.New("Exhausted testing frames")
}
type readerTestCase struct {
create func(FrameReader) FrameReader
input []EthFrame
output []EthFrame
stringrep []string
}
func createFilter(r FrameReader) FrameReader {
return NewFilter(r, defaultdest)
}
func TestReaders(t *testing.T) {
testcases := []readerTestCase{
{NewLogger,
[]EthFrame{defaultframe},
[]EthFrame{defaultframe},
[]string{"Logger"},
},
{createFilter,
[]EthFrame{defaultframe, altframe},
[]EthFrame{defaultframe},
[]string{"Filter", "ffffffffffff"},
},
}
for _, tc := range testcases {
tr := testReader(tc.input)
reader := tc.create(&tr)
// Check for all expected output
for _, output := range tc.output {
p, err := reader.ReadFrame()
if err != nil {
t.Errorf("Reader %v Expected %v error: %v", reader, output, err)
}
if !bytes.Equal(output, p) {
t.Errorf("Reader %v Expected %v != %v", reader, output, p)
}
}
// Once no input is left, testReader throws an error which a sane reader
// should produce.
_, err := reader.ReadFrame()
if err == nil {
t.Errorf("Reader %v Expected error got %v", reader, err)
}
// Make sure the string represenation is sane
for _, r := range tc.stringrep {
o := fmt.Sprint(reader)
if !strings.Contains(o, r) {
t.Errorf("Reader %v should contain %s", reader, o)
}
}
}
}