Skip to content
Snippets Groups Projects
Commit c4f38982 authored by Marcus Weiner's avatar Marcus Weiner
Browse files

Test decode of hello packets

parent e272abf5
No related branches found
No related tags found
1 merge request!2Packet/ospfv3
...@@ -7,8 +7,11 @@ import ( ...@@ -7,8 +7,11 @@ import (
"os" "os"
"testing" "testing"
"github.com/bio-routing/bio-rd/net"
ospf "github.com/bio-routing/bio-rd/protocols/ospf/packetv3" 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"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
var files = []string{ var files = []string{
...@@ -27,7 +30,7 @@ func init() { ...@@ -27,7 +30,7 @@ func init() {
dir = cwd + "/fixtures/" dir = cwd + "/fixtures/"
} }
func TestDecode(t *testing.T) { func TestDecodeDumps(t *testing.T) {
for _, path := range files { for _, path := range files {
t.Run(path, func(t *testing.T) { t.Run(path, func(t *testing.T) {
testDecodeFile(t, dir+path) testDecodeFile(t, dir+path)
...@@ -66,3 +69,251 @@ func testDecodeFile(t *testing.T, path string) { ...@@ -66,3 +69,251 @@ func testDecodeFile(t *testing.T, path string) {
packetCount++ packetCount++
} }
} }
type test struct {
name string
input []byte
wantFail bool
expected interface{}
}
func TestDecode(t *testing.T) {
tests := []test{
{
name: "Hello/Default",
input: []byte{
// Header
3, // Version
1, // Type: Hello
0, 36, // Length
3, 3, 3, 3, // Source Router
0, 0, 0, 0, // Area ID
0x94, 0x1c, // Checksum
0, // Instance ID
0, // Reserved
// Payload (Hello)
0, 0, 0, 6, // Interface ID
100, // Router Prio
0, // Reserved
0, 0x13, // Options: R, E, V6
0, 30, // Hello Interval
0, 120, // Dead Interval
0, 0, 0, 0, // Designated Router
0, 0, 0, 0, // Backup Designated Router
},
expected: &ospf.OSPFv3Message{
Version: 3,
Type: ospf.MsgTypeHello,
Checksum: 0x941c,
PacketLength: 36,
RouterID: ospf.ID(net.IPv4FromOctets(3, 3, 3, 3).Ptr().ToUint32()),
AreaID: 0,
InstanceID: 0,
Body: &ospf.Hello{
InterfaceID: 6,
RouterPriority: 100,
HelloInterval: 30,
RouterDeadInterval: 120,
Options: ospf.OptionsFromFlags(ospf.RouterOptR, ospf.RouterOptE, ospf.RouterOptV6),
},
},
},
{
name: "Hello/InvalidLength",
input: []byte{
// Header
3, // Version
1, // Type: Hello
0, 38, // Length (invalid, expecting 36)
3, 3, 3, 3, // Source Router
0, 0, 0, 0, // Area ID
0x94, 0x1a, // Checksum
0, // Instance ID
0, // Reserved
// Payload (20 bytes)
0, 0, 0, 6, 100, 0, 0, 0x13, 0, 30, 0, 120, 0, 0, 0, 0, 0, 0, 0, 0,
},
wantFail: true,
},
{
name: "Hello/InvalidChecksum",
input: []byte{
// Header
3, // Version
1, // Type: Hello
0, 36, // Length
3, 3, 3, 3, // Source Router
0, 0, 0, 0, // Area ID
0x94, 0x1d, // Checksum (invalid)
0, // Instance ID
0, // Reserved
// Payload (20 bytes)
0, 0, 0, 6, 100, 0, 0, 0x13, 0, 30, 0, 120, 0, 0, 0, 0, 0, 0, 0, 0,
},
wantFail: true,
},
{
name: "Hello/WithNeighbors",
input: []byte{
// Header
3, // Version
1, // Type: Hello
0, 44, // Length
3, 3, 3, 3, // Source Router
0, 0, 0, 0, // Area ID
0x8e, 0x06, // Checksum
0, // Instance ID
0, // Reserved
// Payload (Hello)
0, 0, 0, 6, // Interface ID
100, // Router Prio
0, // Reserved
0, 0x13, // Options: R, E, V6
0, 30, // Hello Interval
0, 120, // Dead Interval
0, 0, 0, 0, // Designated Router
0, 0, 0, 0, // Backup Designated Router
1, 1, 1, 1, // Neighbor 1
2, 2, 2, 2, // Neighbor 2
},
expected: &ospf.OSPFv3Message{
Version: 3,
Type: ospf.MsgTypeHello,
Checksum: 0x8e06,
PacketLength: 44,
RouterID: ospf.ID(net.IPv4FromOctets(3, 3, 3, 3).Ptr().ToUint32()),
AreaID: 0,
InstanceID: 0,
Body: &ospf.Hello{
InterfaceID: 6,
RouterPriority: 100,
HelloInterval: 30,
RouterDeadInterval: 120,
Options: ospf.OptionsFromFlags(ospf.RouterOptR, ospf.RouterOptE, ospf.RouterOptV6),
Neighbors: []ospf.ID{
ospf.ID(net.IPv4FromOctets(1, 1, 1, 1).Ptr().ToUint32()),
ospf.ID(net.IPv4FromOctets(2, 2, 2, 2).Ptr().ToUint32()),
},
},
},
},
{
name: "Hello/WithDR",
input: []byte{
// Header
3, // Version
1, // Type: Hello
0, 44, // Length
3, 3, 3, 3, // Source Router
0, 0, 0, 0, // Area ID
0x8c, 0x04, // Checksum
0, // Instance ID
0, // Reserved
// Payload (Hello)
0, 0, 0, 6, // Interface ID
100, // Router Prio
0, // Reserved
0, 0x13, // Options: R, E, V6
0, 30, // Hello Interval
0, 120, // Dead Interval
1, 1, 1, 1, // Designated Router
0, 0, 0, 0, // Backup Designated Router
1, 1, 1, 1, // Neighbor 1
2, 2, 2, 2, // Neighbor 2
},
expected: &ospf.OSPFv3Message{
Version: 3,
Type: ospf.MsgTypeHello,
Checksum: 0x8c04,
PacketLength: 44,
RouterID: ospf.ID(net.IPv4FromOctets(3, 3, 3, 3).Ptr().ToUint32()),
AreaID: 0,
InstanceID: 0,
Body: &ospf.Hello{
InterfaceID: 6,
RouterPriority: 100,
HelloInterval: 30,
RouterDeadInterval: 120,
Options: ospf.OptionsFromFlags(ospf.RouterOptR, ospf.RouterOptE, ospf.RouterOptV6),
DesignatedRouterID: ospf.ID(net.IPv4FromOctets(1, 1, 1, 1).Ptr().ToUint32()),
Neighbors: []ospf.ID{
ospf.ID(net.IPv4FromOctets(1, 1, 1, 1).Ptr().ToUint32()),
ospf.ID(net.IPv4FromOctets(2, 2, 2, 2).Ptr().ToUint32()),
},
},
},
},
{
name: "Hello/WithBDR",
input: []byte{
// Header
3, // Version
1, // Type: Hello
0, 44, // Length
3, 3, 3, 3, // Source Router
0, 0, 0, 0, // Area ID
0x88, 0x00, // Checksum
0, // Instance ID
0, // Reserved
// Payload (Hello)
0, 0, 0, 6, // Interface ID
100, // Router Prio
0, // Reserved
0, 0x13, // Options: R, E, V6
0, 30, // Hello Interval
0, 120, // Dead Interval
1, 1, 1, 1, // Designated Router
2, 2, 2, 2, // Backup Designated Router
1, 1, 1, 1, // Neighbor 1
2, 2, 2, 2, // Neighbor 2
},
expected: &ospf.OSPFv3Message{
Version: 3,
Type: ospf.MsgTypeHello,
Checksum: 0x8800,
PacketLength: 44,
RouterID: ospf.ID(net.IPv4FromOctets(3, 3, 3, 3).Ptr().ToUint32()),
AreaID: 0,
InstanceID: 0,
Body: &ospf.Hello{
InterfaceID: 6,
RouterPriority: 100,
HelloInterval: 30,
RouterDeadInterval: 120,
Options: ospf.OptionsFromFlags(ospf.RouterOptR, ospf.RouterOptE, ospf.RouterOptV6),
DesignatedRouterID: ospf.ID(net.IPv4FromOctets(1, 1, 1, 1).Ptr().ToUint32()),
BackupDesignatedRouterID: ospf.ID(net.IPv4FromOctets(2, 2, 2, 2).Ptr().ToUint32()),
Neighbors: []ospf.ID{
ospf.ID(net.IPv4FromOctets(1, 1, 1, 1).Ptr().ToUint32()),
ospf.ID(net.IPv4FromOctets(2, 2, 2, 2).Ptr().ToUint32()),
},
},
},
},
}
src, err := net.IPFromString("fe80::3")
require.NoError(t, err)
dst, err := net.IPFromString("ff02::5")
require.NoError(t, err)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
buf := bytes.NewBuffer(test.input)
msg, _, err := ospf.DeserializeOSPFv3Message(buf, src, dst)
if test.wantFail {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, test.expected, msg)
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment