Skip to content
Snippets Groups Projects
Commit b5bc571f authored by Oliver Herms's avatar Oliver Herms
Browse files

More tests

parent b31a71be
No related branches found
No related tags found
No related merge requests found
...@@ -99,6 +99,4 @@ func Decode(msg []byte) (Msg, error) { ...@@ -99,6 +99,4 @@ func Decode(msg []byte) (Msg, error) {
return nil, fmt.Errorf("Unexpected message type: %d", ch.MsgType) return nil, fmt.Errorf("Unexpected message type: %d", ch.MsgType)
} }
return nil, fmt.Errorf("Unexpected message type: %d", ch.MsgType)
} }
...@@ -18,7 +18,8 @@ func (im *InitiationMessage) MsgType() uint8 { ...@@ -18,7 +18,8 @@ func (im *InitiationMessage) MsgType() uint8 {
func decodeInitiationMessage(buf *bytes.Buffer, ch *CommonHeader) (Msg, error) { func decodeInitiationMessage(buf *bytes.Buffer, ch *CommonHeader) (Msg, error) {
im := &InitiationMessage{ im := &InitiationMessage{
TLVs: make([]*InformationTLV, 0, 2), CommonHeader: ch,
TLVs: make([]*InformationTLV, 0, 2),
} }
read := uint32(0) read := uint32(0)
......
...@@ -2,15 +2,17 @@ package packet ...@@ -2,15 +2,17 @@ package packet
import ( import (
"bytes" "bytes"
"fmt"
"github.com/bio-routing/bio-rd/util/decoder" "github.com/bio-routing/bio-rd/util/decoder"
) )
// PeerDownNotification represents a peer down notification // PeerDownNotification represents a peer down notification
type PeerDownNotification struct { type PeerDownNotification struct {
CommonHeader *CommonHeader CommonHeader *CommonHeader
Reason uint8 PerPeerHeader *PerPeerHeader
Data []byte Reason uint8
Data []byte
} }
// MsgType returns the type of this message // MsgType returns the type of this message
...@@ -19,13 +21,22 @@ func (p *PeerDownNotification) MsgType() uint8 { ...@@ -19,13 +21,22 @@ func (p *PeerDownNotification) MsgType() uint8 {
} }
func decodePeerDownNotification(buf *bytes.Buffer, ch *CommonHeader) (*PeerDownNotification, error) { func decodePeerDownNotification(buf *bytes.Buffer, ch *CommonHeader) (*PeerDownNotification, error) {
p := &PeerDownNotification{} p := &PeerDownNotification{
CommonHeader: ch,
}
pph, err := decodePerPeerHeader(buf)
if err != nil {
return nil, fmt.Errorf("Unable to decode per peer header: %v", err)
}
p.PerPeerHeader = pph
fields := []interface{}{ fields := []interface{}{
&p.Reason, &p.Reason,
} }
err := decoder.Decode(buf, fields) err = decoder.Decode(buf, fields)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -34,7 +45,7 @@ func decodePeerDownNotification(buf *bytes.Buffer, ch *CommonHeader) (*PeerDownN ...@@ -34,7 +45,7 @@ func decodePeerDownNotification(buf *bytes.Buffer, ch *CommonHeader) (*PeerDownN
return p, nil return p, nil
} }
p.Data = make([]byte, ch.MsgLength-CommonHeaderLen-1) p.Data = make([]byte, ch.MsgLength-PerPeerHeaderLen-CommonHeaderLen-1)
fields = []interface{}{ fields = []interface{}{
p.Data, p.Data,
} }
......
...@@ -30,14 +30,36 @@ func TestDecodePeerDownNotification(t *testing.T) { ...@@ -30,14 +30,36 @@ func TestDecodePeerDownNotification(t *testing.T) {
{ {
name: "Full", name: "Full",
input: []byte{ input: []byte{
1,
2,
0, 0, 0, 3,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 200, 124,
0, 0, 0, 123,
0, 0, 0, 100,
0, 0, 0, 200,
1, 1,
1, 2, 3, 1, 2, 3,
}, },
ch: &CommonHeader{ ch: &CommonHeader{
MsgLength: CommonHeaderLen + 4, MsgLength: CommonHeaderLen + 4 + 38,
}, },
wantFail: false, wantFail: false,
expected: &PeerDownNotification{ expected: &PeerDownNotification{
CommonHeader: &CommonHeader{
MsgLength: CommonHeaderLen + 4 + 38,
},
PerPeerHeader: &PerPeerHeader{
PeerType: 1,
PeerFlags: 2,
PeerDistinguisher: 3,
PeerAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
PeerAS: 51324,
PeerBGPID: 123,
Timestamp: 100,
TimestampMicroSeconds: 200,
},
Reason: 1, Reason: 1,
Data: []byte{ Data: []byte{
1, 2, 3, 1, 2, 3,
...@@ -47,20 +69,65 @@ func TestDecodePeerDownNotification(t *testing.T) { ...@@ -47,20 +69,65 @@ func TestDecodePeerDownNotification(t *testing.T) {
{ {
name: "Full no data", name: "Full no data",
input: []byte{ input: []byte{
1,
2,
0, 0, 0, 3,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 200, 124,
0, 0, 0, 123,
0, 0, 0, 100,
0, 0, 0, 200,
4, 4,
}, },
ch: &CommonHeader{ ch: &CommonHeader{
MsgLength: CommonHeaderLen + 4, MsgLength: CommonHeaderLen + PerPeerHeaderLen + 4,
}, },
wantFail: false, wantFail: false,
expected: &PeerDownNotification{ expected: &PeerDownNotification{
CommonHeader: &CommonHeader{
MsgLength: CommonHeaderLen + PerPeerHeaderLen + 4,
},
PerPeerHeader: &PerPeerHeader{
PeerType: 1,
PeerFlags: 2,
PeerDistinguisher: 3,
PeerAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
PeerAS: 51324,
PeerBGPID: 123,
Timestamp: 100,
TimestampMicroSeconds: 200,
},
Reason: 4, Reason: 4,
Data: nil, Data: nil,
}, },
}, },
{
name: "Incomplete per peer header",
input: []byte{
1,
2,
0, 0, 0, 3,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 200, 124,
0, 0, 0,
},
ch: &CommonHeader{
MsgLength: CommonHeaderLen + 5,
},
wantFail: true,
},
{ {
name: "Incomplete data", name: "Incomplete data",
input: []byte{ input: []byte{
1,
2,
0, 0, 0, 3,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 200, 124,
0, 0, 0, 123,
0, 0, 0, 100,
0, 0, 0, 200,
1, 1,
1, 2, 3, 1, 2, 3,
}, },
......
...@@ -8,12 +8,14 @@ import ( ...@@ -8,12 +8,14 @@ import (
) )
const ( const (
// OpenMsgMinLen is the minimal length of a BGP open message
OpenMsgMinLen = 10 OpenMsgMinLen = 10
) )
// PeerUpNotification represents a peer up notification // PeerUpNotification represents a peer up notification
type PeerUpNotification struct { type PeerUpNotification struct {
CommonHeader *CommonHeader CommonHeader *CommonHeader
PerPeerHeader *PerPeerHeader
LocalAddress [16]byte LocalAddress [16]byte
LocalPort uint16 LocalPort uint16
RemotePort uint16 RemotePort uint16
...@@ -28,7 +30,16 @@ func (p *PeerUpNotification) MsgType() uint8 { ...@@ -28,7 +30,16 @@ func (p *PeerUpNotification) MsgType() uint8 {
} }
func decodePeerUpNotification(buf *bytes.Buffer, ch *CommonHeader) (*PeerUpNotification, error) { func decodePeerUpNotification(buf *bytes.Buffer, ch *CommonHeader) (*PeerUpNotification, error) {
p := &PeerUpNotification{} p := &PeerUpNotification{
CommonHeader: ch,
}
pph, err := decodePerPeerHeader(buf)
if err != nil {
return nil, fmt.Errorf("Unable to decode per peer header: %v", err)
}
p.PerPeerHeader = pph
fields := []interface{}{ fields := []interface{}{
&p.LocalAddress, &p.LocalAddress,
...@@ -36,7 +47,7 @@ func decodePeerUpNotification(buf *bytes.Buffer, ch *CommonHeader) (*PeerUpNotif ...@@ -36,7 +47,7 @@ func decodePeerUpNotification(buf *bytes.Buffer, ch *CommonHeader) (*PeerUpNotif
&p.RemotePort, &p.RemotePort,
} }
err := decoder.Decode(buf, fields) err = decoder.Decode(buf, fields)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -29,6 +29,15 @@ func TestDecodePeerUp(t *testing.T) { ...@@ -29,6 +29,15 @@ func TestDecodePeerUp(t *testing.T) {
{ {
name: "Full", name: "Full",
input: []byte{ input: []byte{
1,
2,
0, 0, 0, 3,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 200, 124,
0, 0, 0, 123,
0, 0, 0, 100,
0, 0, 0, 200,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 100, 0, 100,
0, 200, 0, 200,
...@@ -55,6 +64,19 @@ func TestDecodePeerUp(t *testing.T) { ...@@ -55,6 +64,19 @@ func TestDecodePeerUp(t *testing.T) {
}, },
wantFail: false, wantFail: false,
expected: &PeerUpNotification{ expected: &PeerUpNotification{
CommonHeader: &CommonHeader{
MsgLength: 47,
},
PerPeerHeader: &PerPeerHeader{
PeerType: 1,
PeerFlags: 2,
PeerDistinguisher: 3,
PeerAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
PeerAS: 51324,
PeerBGPID: 123,
Timestamp: 100,
TimestampMicroSeconds: 200,
},
LocalAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, LocalAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
LocalPort: 100, LocalPort: 100,
RemotePort: 200, RemotePort: 200,
...@@ -82,6 +104,15 @@ func TestDecodePeerUp(t *testing.T) { ...@@ -82,6 +104,15 @@ func TestDecodePeerUp(t *testing.T) {
{ {
name: "Full #2", name: "Full #2",
input: []byte{ input: []byte{
1,
2,
0, 0, 0, 3,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 200, 124,
0, 0, 0, 123,
0, 0, 0, 100,
0, 0, 0, 200,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 100, 0, 100,
0, 200, 0, 200,
...@@ -106,6 +137,19 @@ func TestDecodePeerUp(t *testing.T) { ...@@ -106,6 +137,19 @@ func TestDecodePeerUp(t *testing.T) {
}, },
wantFail: false, wantFail: false,
expected: &PeerUpNotification{ expected: &PeerUpNotification{
CommonHeader: &CommonHeader{
MsgLength: 44,
},
PerPeerHeader: &PerPeerHeader{
PeerType: 1,
PeerFlags: 2,
PeerDistinguisher: 3,
PeerAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
PeerAS: 51324,
PeerBGPID: 123,
Timestamp: 100,
TimestampMicroSeconds: 200,
},
LocalAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, LocalAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
LocalPort: 100, LocalPort: 100,
RemotePort: 200, RemotePort: 200,
...@@ -127,9 +171,33 @@ func TestDecodePeerUp(t *testing.T) { ...@@ -127,9 +171,33 @@ func TestDecodePeerUp(t *testing.T) {
}, },
}, },
}, },
{
name: "Incomplete #0",
input: []byte{
1,
2,
0, 0, 0, 3,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 200, 124,
0, 0, 0,
},
ch: &CommonHeader{
MsgLength: 47,
},
wantFail: true,
},
{ {
name: "Incomplete #1", name: "Incomplete #1",
input: []byte{ input: []byte{
1,
2,
0, 0, 0, 3,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 200, 124,
0, 0, 0, 123,
0, 0, 0, 100,
0, 0, 0, 200,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 100, 0, 100,
}, },
...@@ -141,6 +209,15 @@ func TestDecodePeerUp(t *testing.T) { ...@@ -141,6 +209,15 @@ func TestDecodePeerUp(t *testing.T) {
{ {
name: "Incomplete #2", name: "Incomplete #2",
input: []byte{ input: []byte{
1,
2,
0, 0, 0, 3,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 200, 124,
0, 0, 0, 123,
0, 0, 0, 100,
0, 0, 0, 200,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 100, 0, 100,
0, 200, 0, 200,
...@@ -158,6 +235,14 @@ func TestDecodePeerUp(t *testing.T) { ...@@ -158,6 +235,14 @@ func TestDecodePeerUp(t *testing.T) {
{ {
name: "Incomplete #3", name: "Incomplete #3",
input: []byte{ input: []byte{
1,
2,
0, 0, 0, 3,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 200, 124,
0, 0, 0, 123,
0, 0, 0, 100,
0, 0, 0, 200,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 100, 0, 100,
0, 200, 0, 200,
......
...@@ -18,7 +18,8 @@ func (t *TerminationMessage) MsgType() uint8 { ...@@ -18,7 +18,8 @@ func (t *TerminationMessage) MsgType() uint8 {
func decodeTerminationMessage(buf *bytes.Buffer, ch *CommonHeader) (*TerminationMessage, error) { func decodeTerminationMessage(buf *bytes.Buffer, ch *CommonHeader) (*TerminationMessage, error) {
tm := &TerminationMessage{ tm := &TerminationMessage{
TLVs: make([]*InformationTLV, 0, 2), CommonHeader: ch,
TLVs: make([]*InformationTLV, 0, 2),
} }
read := uint32(0) read := uint32(0)
......
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