From d3997a2e6f01f66d2f109e0ffa3e478371ea4424 Mon Sep 17 00:00:00 2001 From: Oliver Herms <oliver.herms@exaring.de> Date: Fri, 5 Oct 2018 18:32:48 +0200 Subject: [PATCH] More tests --- protocols/bmp/packet/peer_up.go | 14 ++--- protocols/bmp/packet/peer_up_test.go | 92 ++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/protocols/bmp/packet/peer_up.go b/protocols/bmp/packet/peer_up.go index b8873b81..6f173d1e 100644 --- a/protocols/bmp/packet/peer_up.go +++ b/protocols/bmp/packet/peer_up.go @@ -62,12 +62,8 @@ func decodePeerUpNotification(buf *bytes.Buffer, ch *CommonHeader) (*PeerUpNotif &p.Information, } - err = decoder.Decode(buf, fields) - if err != nil { - return nil, err - } - - fmt.Printf("%v\n", p.Information) + // This can not fail as p.Information has exactly the size of what is left in buf + decoder.Decode(buf, fields) return p, nil } @@ -85,7 +81,11 @@ func getOpenMsg(buf *bytes.Buffer) ([]byte, error) { } optParams := make([]byte, msg[OpenMsgMinLen-1]) - _, err = buf.Read(optParams) + fields := []interface{}{ + &optParams, + } + + err = decoder.Decode(buf, fields) if err != nil { return nil, fmt.Errorf("Unable to read: %v", err) } diff --git a/protocols/bmp/packet/peer_up_test.go b/protocols/bmp/packet/peer_up_test.go index 240f38ab..6f7578fb 100644 --- a/protocols/bmp/packet/peer_up_test.go +++ b/protocols/bmp/packet/peer_up_test.go @@ -79,6 +79,54 @@ func TestDecodePeerUp(t *testing.T) { }, }, }, + { + name: "Full #2", + input: []byte{ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 0, 100, + 0, 200, + + // OPEN Sent + 4, // Version + 1, 0, // ASN + 2, 0, // Hold Time + 100, 110, 120, 130, // BGP Identifier + 5, // Opt Parm Len + 1, 2, 3, 4, 5, + + // OPEN Recv + 4, // Version + 1, 0, // ASN + 2, 0, // Hold Time + 100, 110, 120, 130, // BGP Identifier + 0, // Opt Parm Len + }, + ch: &CommonHeader{ + MsgLength: 44, + }, + wantFail: false, + expected: &PeerUpNotification{ + LocalAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, + LocalPort: 100, + RemotePort: 200, + SentOpenMsg: []byte{ + 4, // Version + 1, 0, // ASN + 2, 0, // Hold Time + 100, 110, 120, 130, // BGP Identifier + 5, // Opt Parm Len + 1, 2, 3, 4, 5, + }, + ReceivedOpenMsg: []byte{ + // OPEN Recv + 4, // Version + 1, 0, // ASN + 2, 0, // Hold Time + 100, 110, 120, 130, // BGP Identifier + 0, // Opt Parm Len + }, + }, + }, { name: "Incomplete #1", input: []byte{ @@ -107,6 +155,50 @@ func TestDecodePeerUp(t *testing.T) { }, wantFail: true, }, + { + name: "Incomplete #3", + input: []byte{ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 0, 100, + 0, 200, + + // OPEN Sent + 4, // Version + 1, 0, // ASN + 2, 0, // Hold Time + }, + ch: &CommonHeader{ + MsgLength: 47, + }, + wantFail: true, + }, + { + name: "Incomplete #4", + input: []byte{ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 0, 100, + 0, 200, + + // OPEN Sent + 4, // Version + 1, 0, // ASN + 2, 0, // Hold Time + 100, 110, 120, 130, // BGP Identifier + 5, // Opt Parm Len + 1, 2, 3, 4, 5, + + // OPEN Recv + 4, // Version + 1, 0, // ASN + 2, 0, // Hold Time + 100, 110, 120, 130, // BGP Identifier + 3, // Opt Parm Len + }, + ch: &CommonHeader{ + MsgLength: 47, + }, + wantFail: true, + }, } for _, test := range tests { -- GitLab