Newer
Older
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
package packetv3_test
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"testing"
ospf "github.com/bio-routing/bio-rd/protocols/ospf/packetv3"
"github.com/bio-routing/bio-rd/protocols/ospf/packetv3/fixtures"
"github.com/bio-routing/bio-rd/protocols/ospf/packetv3/fixtures/packets"
"github.com/stretchr/testify/assert"
)
var files = []string{
"OSPFv3_multipoint_adjacencies.cap",
"OSPFv3_broadcast_adjacency.cap",
"OSPFv3_NBMA_adjacencies.cap",
}
var dir string
func init() {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
dir = cwd + "/fixtures/"
}
func TestDecode(t *testing.T) {
for _, path := range files {
t.Run(path, func(t *testing.T) {
testDecodeFile(t, dir+path)
})
}
}
func testDecodeFile(t *testing.T, path string) {
fmt.Printf("Testing on file: %s\n", path)
r, f := fixtures.PacketReader(t, path)
defer f.Close()
var packetCount int
for {
data, _, err := r.ReadPacketData()
if err == io.EOF {
break
}
if err != nil {
t.Error(err)
return
}
t.Run(fmt.Sprintf("Packet_%03d", packetCount+1), func(t *testing.T) {
payload, src, dst, err := fixtures.Payload(data)
if err != nil {
t.Error(err)
return
}
buf := bytes.NewBuffer(payload)
if _, _, err := ospf.DeserializeOSPFv3Message(buf, src, dst); err != nil {
t.Error(err)
}
})
packetCount++
}
}
func TestEncode(t *testing.T) {
for _, path := range files {
t.Run(path, func(t *testing.T) {
testEncodeFile(t, dir+path)
})
}
}
func testEncodeFile(t *testing.T, path string) {
fmt.Printf("Testing on file: %s\n", path)
r, f := fixtures.PacketReader(t, path)
defer f.Close()
packets, ok := packets.Packets[filepath.Base(path)]
if !ok {
t.Errorf("Raw Go values not found for file %s", filepath.Base(path))
}
var packetCount int
for {
data, _, err := r.ReadPacketData()
if err == io.EOF {
break
}
if err != nil {
t.Error(err)
return
}
pl, src, dst, err := fixtures.Payload(data)
if err != nil {
t.Error(err)
return
}
t.Run(fmt.Sprintf("Packet_%03d", packetCount+1), func(t *testing.T) {
buf := &bytes.Buffer{}
msg := packets[packetCount]
msg.Serialize(buf, src, dst)
assert.Equal(t, buf.Bytes(), pl)
})
packetCount++
}
}