Skip to content
Snippets Groups Projects
Unverified Commit f7eed225 authored by Daniel Czerwonk's avatar Daniel Czerwonk Committed by GitHub
Browse files

Merge branch 'master' into feature/refactoring_bgp_path

parents 4014eca9 51103522
No related branches found
No related tags found
No related merge requests found
...@@ -56,6 +56,7 @@ go_test( ...@@ -56,6 +56,7 @@ go_test(
"//routingtable:go_default_library", "//routingtable:go_default_library",
"//routingtable/filter:go_default_library", "//routingtable/filter:go_default_library",
"//routingtable/locRIB:go_default_library", "//routingtable/locRIB:go_default_library",
"//testing:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library", "//vendor/github.com/stretchr/testify/assert:go_default_library",
], ],
) )
...@@ -134,7 +134,8 @@ func (s *openSentState) handleOpenMessage(openMsg *packet.BGPOpen) (state, strin ...@@ -134,7 +134,8 @@ func (s *openSentState) handleOpenMessage(openMsg *packet.BGPOpen) (state, strin
s.processOpenOptions(openMsg.OptParams) s.processOpenOptions(openMsg.OptParams)
if s.peerASNRcvd != s.fsm.peer.peerASN { if s.peerASNRcvd != s.fsm.peer.peerASN {
return newCeaseState(), fmt.Sprintf("Expected session from %d, got open message with ASN %d", s.fsm.peer.peerASN, s.peerASNRcvd) s.fsm.sendNotification(packet.OpenMessageError, packet.BadPeerAS)
return newCeaseState(), fmt.Sprintf("Bad Peer AS %d, expected: %d", s.peerASNRcvd, s.fsm.peer.peerASN)
} }
return newOpenConfirmState(s.fsm), "Received OPEN message" return newOpenConfirmState(s.fsm), "Received OPEN message"
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/bio-routing/bio-rd/protocols/bgp/packet" "github.com/bio-routing/bio-rd/protocols/bgp/packet"
btesting "github.com/bio-routing/bio-rd/testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
...@@ -68,6 +69,7 @@ func TestOpenMsgReceived(t *testing.T) { ...@@ -68,6 +69,7 @@ func TestOpenMsgReceived(t *testing.T) {
fsm := newFSM2(&peer{ fsm := newFSM2(&peer{
peerASN: test.asn, peerASN: test.asn,
}) })
fsm.con = &btesting.MockConn{}
s := &openSentState{ s := &openSentState{
fsm: fsm, fsm: fsm,
......
...@@ -65,6 +65,7 @@ func (fm *pathIDManager) releasePath(p *route.Path) (uint32, error) { ...@@ -65,6 +65,7 @@ func (fm *pathIDManager) releasePath(p *route.Path) (uint32, error) {
delete(fm.ids, fm.idByPath[hash]) delete(fm.ids, fm.idByPath[hash])
delete(fm.idByPath, hash) delete(fm.idByPath, hash)
} }
fm.used--
return id, nil return id, nil
} }
package adjRIBOut
import (
"testing"
"github.com/bio-routing/bio-rd/route"
"github.com/stretchr/testify/assert"
)
func TestAddPath(t *testing.T) {
tests := []struct {
name string
maxIDs uint32
count int
wantFail bool
}{
{
name: "Out of path IDs",
maxIDs: 10,
count: 11,
wantFail: true,
},
{
name: "Success",
maxIDs: 10,
count: 10,
wantFail: false,
},
}
X:
for _, test := range tests {
maxUint32 = test.maxIDs
m := newPathIDManager()
for i := 0; i < test.count; i++ {
_, err := m.addPath(&route.Path{BGPPath: &route.BGPPath{LocalPref: uint32(i)}})
if err != nil {
if test.wantFail {
continue X
}
t.Errorf("Unexpected failure for test %q: %v", test.name, err)
continue X
}
}
if test.wantFail {
t.Errorf("Unexpected success for test %q", test.name)
continue
}
}
}
func TestReleasePath(t *testing.T) {
tests := []struct {
name string
pm *pathIDManager
release *route.Path
expected *pathIDManager
wantFail bool
}{
{
name: "Release existent",
pm: &pathIDManager{
ids: map[uint32]uint64{
0: 1,
1: 1,
2: 1,
},
idByPath: map[route.BGPPath]uint32{
route.BGPPath{
LocalPref: 0,
}: 0,
route.BGPPath{
LocalPref: 1,
}: 1,
route.BGPPath{
LocalPref: 2,
}: 2,
},
last: 2,
used: 3,
},
release: &route.Path{BGPPath: &route.BGPPath{
LocalPref: 2,
}},
expected: &pathIDManager{
ids: map[uint32]uint64{
0: 1,
1: 1,
},
idByPath: map[route.BGPPath]uint32{
route.BGPPath{
LocalPref: 0,
}: 0,
route.BGPPath{
LocalPref: 1,
}: 1,
},
last: 2,
used: 2,
},
},
{
name: "Release non-existent",
pm: &pathIDManager{
ids: map[uint32]uint64{
0: 1,
1: 1,
2: 1,
},
idByPath: map[route.BGPPath]uint32{
route.BGPPath{
LocalPref: 0,
}: 0,
route.BGPPath{
LocalPref: 1,
}: 1,
route.BGPPath{
LocalPref: 2,
}: 2,
},
last: 2,
used: 3,
},
release: &route.Path{BGPPath: &route.BGPPath{
LocalPref: 4,
}},
expected: &pathIDManager{
ids: map[uint32]uint64{
0: 1,
1: 1,
2: 1,
},
idByPath: map[route.BGPPath]uint32{
route.BGPPath{
LocalPref: 0,
}: 0,
route.BGPPath{
LocalPref: 1,
}: 1,
route.BGPPath{
LocalPref: 2,
}: 2,
},
last: 2,
used: 3,
},
wantFail: true,
},
}
for _, test := range tests {
_, err := test.pm.releasePath(test.release)
if err != nil {
if test.wantFail {
continue
}
t.Errorf("Unexpected failure for test %q: %v", test.name, err)
continue
}
if test.wantFail {
t.Errorf("Unexpected success for test %q", test.name)
continue
}
assert.Equalf(t, test.expected, test.pm, "%s", test.name)
}
}
package filter package filter
/*func TestAddPath(t *testing.T) { import (
tests := []struct { "testing"
name string
prefix net.Prefix
path *route.Path
term *Term
exptectCalled bool
expectModified bool
}{
{
name: "accept",
prefix: net.NewPfx(0, 0),
path: &route.Path{},
term: &Term{
then: []FilterAction{
&actions.AcceptAction{},
},
},
exptectCalled: true,
expectModified: false,
},
{
name: "reject",
prefix: net.NewPfx(0, 0),
path: &route.Path{},
term: &Term{
then: []FilterAction{
&actions.RejectAction{},
},
},
exptectCalled: false,
expectModified: false,
},
{
name: "modified",
prefix: net.NewPfx(0, 0),
path: &route.Path{},
term: &Term{
then: []FilterAction{
&mockAction{},
&actions.AcceptAction{},
},
},
exptectCalled: true,
expectModified: true,
},
}
for _, test := range tests {
t.Run(test.name, func(te *testing.T) {
m := newClientMock()
f := NewFilter([]*Term{test.term})
f.Register(m)
f.AddPath(test.prefix, test.path) "github.com/bio-routing/bio-rd/net"
assert.Equal(te, test.exptectCalled, m.addPathCalled, "called") "github.com/bio-routing/bio-rd/route"
"github.com/bio-routing/bio-rd/routingtable/filter/actions"
"github.com/stretchr/testify/assert"
)
if !test.exptectCalled { func TestProcessTerms(t *testing.T) {
return
}
if m.path != test.path && !test.expectModified {
te.Fatal("expected path to be not modified but was")
}
if m.path == test.path && test.expectModified {
te.Fatal("expected path to be modified but was same reference")
}
})
}
}
func TestRemovePath(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
prefix net.Prefix prefix net.Prefix
path *route.Path path *route.Path
term *Term term *Term
exptectCalled bool exptectAccept bool
expectModified bool expectModified bool
}{ }{
{ {
...@@ -91,7 +27,7 @@ func TestRemovePath(t *testing.T) { ...@@ -91,7 +27,7 @@ func TestRemovePath(t *testing.T) {
&actions.AcceptAction{}, &actions.AcceptAction{},
}, },
}, },
exptectCalled: true, exptectAccept: true,
expectModified: false, expectModified: false,
}, },
{ {
...@@ -103,7 +39,7 @@ func TestRemovePath(t *testing.T) { ...@@ -103,7 +39,7 @@ func TestRemovePath(t *testing.T) {
&actions.RejectAction{}, &actions.RejectAction{},
}, },
}, },
exptectCalled: false, exptectAccept: false,
expectModified: false, expectModified: false,
}, },
{ {
...@@ -116,32 +52,21 @@ func TestRemovePath(t *testing.T) { ...@@ -116,32 +52,21 @@ func TestRemovePath(t *testing.T) {
&actions.AcceptAction{}, &actions.AcceptAction{},
}, },
}, },
exptectCalled: true, exptectAccept: true,
expectModified: true, expectModified: true,
}, },
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(te *testing.T) { t.Run(test.name, func(te *testing.T) {
m := newClientMock()
f := NewFilter([]*Term{test.term}) f := NewFilter([]*Term{test.term})
f.Register(m) p, reject := f.ProcessTerms(test.prefix, test.path)
f.RemovePath(test.prefix, test.path) assert.Equal(t, test.exptectAccept, !reject)
assert.Equal(te, test.exptectCalled, m.removePathCalled, "called")
if !test.exptectCalled {
return
}
if m.path != test.path && !test.expectModified { if test.expectModified {
te.Fatal("expected path to be not modified but was") assert.NotEqual(t, test.path, p)
}
if m.path == test.path && test.expectModified {
te.Fatal("expected path to be modified but was same reference")
} }
}) })
} }
}*/ }
package filter package filter
/*func TestNewAcceptAllFilter(t *testing.T) { import (
f := NewAcceptAllFilter() "testing"
m := &clientMock{} "github.com/bio-routing/bio-rd/net"
f.Register(m) "github.com/bio-routing/bio-rd/route"
"github.com/stretchr/testify/assert"
)
f.AddPath(net.NewPfx(0, 0), &route.Path{}) func TestNewAcceptAllFilter(t *testing.T) {
f := NewAcceptAllFilter()
if !m.addPathCalled { _, reject := f.ProcessTerms(net.NewPfx(0, 0), &route.Path{})
t.Fatalf("expected accepted, but was filtered") assert.Equal(t, false, reject)
}
} }
func TestNewDrainFilter(t *testing.T) { func TestNewDrainFilter(t *testing.T) {
f := NewDrainFilter() f := NewDrainFilter()
m := &clientMock{} _, reject := f.ProcessTerms(net.NewPfx(0, 0), &route.Path{})
f.Register(m) assert.Equal(t, true, reject)
}
f.AddPath(net.NewPfx(0, 0), &route.Path{})
if m.addPathCalled {
t.Fatalf("expected filtered, but was accepted")
}
}*/
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["conn_mock.go"],
importpath = "github.com/bio-routing/bio-rd/testing",
visibility = ["//visibility:public"],
)
package testing
import "net"
// MockConn mock an connection
type MockConn struct {
net.Conn
// Bytes are the bytes writen
Bytes []byte
}
func NewMockConn() *MockConn {
return &MockConn{
Bytes: make([]byte, 0),
}
}
func (m *MockConn) Write(b []byte) (int, error) {
m.Bytes = append(m.Bytes, b...)
return len(b), nil
}
func (m *MockConn) Read(b []byte) (n int, err error) {
count := len(b)
if count > len(m.Bytes) {
count = len(m.Bytes)
}
copy(m.Bytes[0:count], b)
return count, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment