diff options
author | Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp> | 2014-12-08 21:10:14 +0900 |
---|---|---|
committer | Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp> | 2014-12-08 21:10:14 +0900 |
commit | 8e3d8c97b898efc6b88313ea8475c37cabaf8cfe (patch) | |
tree | 31fdb06dec1b58a051a9981156862e2a374f7275 /table/table_manager_test.go | |
parent | ea42263b49d7dee42153d346172c1b275fc47b61 (diff) |
[bestpath_selection] fix bugs when handling withdraw routes
Diffstat (limited to 'table/table_manager_test.go')
-rw-r--r-- | table/table_manager_test.go | 1236 |
1 files changed, 1015 insertions, 221 deletions
diff --git a/table/table_manager_test.go b/table/table_manager_test.go index 8b1d7e2c..20d056b7 100644 --- a/table/table_manager_test.go +++ b/table/table_manager_test.go @@ -20,18 +20,17 @@ import ( log "github.com/Sirupsen/logrus" "github.com/osrg/gobgp/packet" "github.com/stretchr/testify/assert" - "net" "os" "reflect" "testing" ) -func getLogger() *log.Logger { +func getLogger(lv log.Level) *log.Logger { var l *log.Logger = &log.Logger{ Out: os.Stderr, Formatter: new(log.JSONFormatter), Hooks: make(map[log.Level][]log.Hook), - Level: log.InfoLevel, + Level: lv, } return l } @@ -74,47 +73,46 @@ func peerR3() *Peer { func TestProcessBGPUpdate_0_select_onlypath_ipv4(t *testing.T) { tm := NewTableManager() - setLogger(getLogger()) bgpMessage := update_fromR1() peer := peerR1() pList, wList, err := tm.ProcessUpdate(peer, bgpMessage) - assert.Equal(t, len(pList), 1, "pList length should be 1") - assert.Equal(t, len(wList), 0, "wList length should be 0") - assert.NoError(t, err, "err should be nil") + assert.Equal(t, len(pList), 1) + assert.Equal(t, len(wList), 0) + assert.NoError(t, err) // check type path := pList[0] expectedType := "*table.IPv4Path" - assert.Equal(t, reflect.TypeOf(path).String(), expectedType, "best path should be *table.IPv4Path") + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) // check PathAttribute pathAttributes := bgpMessage.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin, "PathAttributeOrigin should be ", expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath, "PathAttributeAsPath should be ", expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr, "PathAttributeNextHop should be ", expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) - assert.Equal(t, expectedMed, pathMed, "PathAttributeMed should be ", expectedMed) + assert.Equal(t, expectedMed, pathMed) // check PathAttribute length - assert.Equal(t, 4, path.getPathAttributeMap().Len(), "PathAttribute length should be ", 4) + assert.Equal(t, 4, path.getPathAttributeMap().Len()) // check destination expectedPrefix := "10.10.10.0" - assert.Equal(t, expectedPrefix, path.getPrefix().String(), "prefix should be ", expectedPrefix) + assert.Equal(t, expectedPrefix, path.getPrefix().String()) // check nexthop expectedNexthop := "192.168.50.1" - assert.Equal(t, expectedNexthop, path.getNexthop().String(), "nexthop should be ", expectedNexthop) + assert.Equal(t, expectedNexthop, path.getNexthop().String()) } @@ -122,7 +120,6 @@ func TestProcessBGPUpdate_0_select_onlypath_ipv4(t *testing.T) { func TestProcessBGPUpdate_0_select_onlypath_ipv6(t *testing.T) { tm := NewTableManager() - setLogger(getLogger()) bgpMessage := update_fromR1_ipv6() peer := peerR1() @@ -134,36 +131,36 @@ func TestProcessBGPUpdate_0_select_onlypath_ipv6(t *testing.T) { // check type path := pList[0] expectedType := "*table.IPv6Path" - assert.Equal(t, expectedType, reflect.TypeOf(path).String(), "best path should be *table.IPv6Path") + assert.Equal(t, expectedType, reflect.TypeOf(path).String()) // check PathAttribute pathAttributes := bgpMessage.Body.(*bgp.BGPUpdate).PathAttributes expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr, "PathAttributeNextHop should be ", expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin, "PathAttributeOrigin should be ", expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath, "PathAttributeAsPath should be ", expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) - assert.Equal(t, expectedMed, pathMed, "PathAttributeMed should be ", expectedMed) + assert.Equal(t, expectedMed, pathMed) // check PathAttribute length - assert.Equal(t, 4, path.getPathAttributeMap().Len(), "PathAttribute length should be ", 4) + assert.Equal(t, 4, path.getPathAttributeMap().Len()) // check destination expectedPrefix := "2001:123:123:1::" - assert.Equal(t, expectedPrefix, path.getPrefix().String(), "prefix should be ", expectedPrefix) + assert.Equal(t, expectedPrefix, path.getPrefix().String()) // check nexthop expectedNexthop := "2001::192:168:50:1" - assert.Equal(t, expectedNexthop, path.getNexthop().String(), "nexthop should be ", expectedNexthop) + assert.Equal(t, expectedNexthop, path.getNexthop().String()) } @@ -171,7 +168,8 @@ func TestProcessBGPUpdate_0_select_onlypath_ipv6(t *testing.T) { func TestProcessBGPUpdate_1_select_high_localpref_ipv4(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low localpref message @@ -204,60 +202,62 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv4(t *testing.T) { peer1 := peerR1() pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) - assert.Equal(t, 1, len(pList), "pList length should be 1") - assert.Equal(t, 0, len(wList), "wList length should be 0") - assert.NoError(t, err, "err should be nil") + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) - assert.Equal(t, 1, len(pList), "pList length should be 1") - assert.Equal(t, 0, len(wList), "wList length should be 0") - assert.NoError(t, err, "err should be nil") + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) // check type path := pList[0] expectedType := "*table.IPv4Path" - assert.Equal(t, reflect.TypeOf(path).String(), expectedType, "best path should be *table.IPv4Path") + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) // check PathAttribute pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin, "PathAttributeOrigin should be ", expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath, "PathAttributeAsPath should be ", expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr, "PathAttributeNextHop should be ", expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) - assert.Equal(t, expectedMed, pathMed, "PathAttributeMed should be ", expectedMed) + assert.Equal(t, expectedMed, pathMed) // check PathAttribute length - assert.Equal(t, len(pathAttributes2), path.getPathAttributeMap().Len(), "PathAttribute length should be ", 4) + assert.Equal(t, len(pathAttributes2), path.getPathAttributeMap().Len()) // check destination expectedPrefix := "10.10.10.0" - assert.Equal(t, expectedPrefix, path.getPrefix().String(), "prefix should be ", expectedPrefix) + assert.Equal(t, expectedPrefix, path.getPrefix().String()) // check nexthop expectedNexthop := "192.168.50.1" - assert.Equal(t, expectedNexthop, path.getNexthop().String(), "nexthop should be ", expectedNexthop) + assert.Equal(t, expectedNexthop, path.getNexthop().String()) } func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(0) aspath1 := createAsPathAttribute([]uint16{65000}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(100) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -271,7 +271,8 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{65100, 65000}) - mp_reach2 := createMpReach("2001::192:168:100:1", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(100) localpref2 := bgp.NewPathAttributeLocalPref(200) @@ -287,13 +288,13 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -305,15 +306,15 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -335,7 +336,8 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { func TestProcessBGPUpdate_2_select_local_origin_ipv4(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low localpref message @@ -387,15 +389,15 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv4(t *testing.T) { pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -416,12 +418,14 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv4(t *testing.T) { func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(0) aspath1 := createAsPathAttribute([]uint16{65000}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(100) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -435,7 +439,8 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{}) - mp_reach2 := createMpReach("::", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("::", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(100) localpref2 := bgp.NewPathAttributeLocalPref(100) @@ -451,13 +456,13 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) var peer2 *Peer = nil pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -469,15 +474,15 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -499,61 +504,63 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { func TestProcessBGPUpdate_3_select_aspath_ipv4(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error bgpMessage1 := update_fromR2viaR1() peer1 := peerR1() pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) - assert.Equal(t, 1, len(pList), "pList length should be 1") - assert.Equal(t, 0, len(wList), "wList length should be 0") - assert.NoError(t, err, "err should be nil") + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) bgpMessage2 := update_fromR2() peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) - assert.Equal(t, 1, len(pList), "pList length should be 1") - assert.Equal(t, 0, len(wList), "wList length should be 0") - assert.NoError(t, err, "err should be nil") + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) // check type path := pList[0] expectedType := "*table.IPv4Path" - assert.Equal(t, reflect.TypeOf(path).String(), expectedType, "best path should be *table.IPv4Path") + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) // check PathAttribute pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin, "PathAttributeOrigin should be ", expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath, "PathAttributeAsPath should be ", expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr, "PathAttributeNextHop should be ", expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) - assert.Equal(t, expectedMed, pathMed, "PathAttributeMed should be ", expectedMed) + assert.Equal(t, expectedMed, pathMed) // check PathAttribute length - assert.Equal(t, 4, path.getPathAttributeMap().Len(), "PathAttribute length should be ", 4) + assert.Equal(t, 4, path.getPathAttributeMap().Len()) // check destination expectedPrefix := "20.20.20.0" - assert.Equal(t, expectedPrefix, path.getPrefix().String(), "prefix should be ", expectedPrefix) + assert.Equal(t, expectedPrefix, path.getPrefix().String()) // check nexthop expectedNexthop := "192.168.100.1" - assert.Equal(t, expectedNexthop, path.getNexthop().String(), "nexthop should be ", expectedNexthop) + assert.Equal(t, expectedNexthop, path.getNexthop().String()) } func TestProcessBGPUpdate_3_select_aspath_ipv6(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error bgpMessage1 := update_fromR2viaR1_ipv6() @@ -561,47 +568,47 @@ func TestProcessBGPUpdate_3_select_aspath_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) bgpMessage2 := update_fromR2_ipv6() peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] expectedType := "*table.IPv6Path" - assert.Equal(t, reflect.TypeOf(path).String(), expectedType, "best path should be *table.IPv6Path") + assert.Equal(t, expectedType, reflect.TypeOf(path).String()) // check PathAttribute pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr, "PathAttributeNextHop should be ", expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin, "PathAttributeOrigin should be ", expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath, "PathAttributeAsPath should be ", expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) - assert.Equal(t, expectedMed, pathMed, "PathAttributeMed should be ", expectedMed) + assert.Equal(t, expectedMed, pathMed) // check PathAttribute length - assert.Equal(t, 4, path.getPathAttributeMap().Len(), "PathAttribute length should be ", 4) + assert.Equal(t, 4, path.getPathAttributeMap().Len()) // check destination expectedPrefix := "2002:223:123:1::" - assert.Equal(t, expectedPrefix, path.getPrefix().String(), "prefix should be ", expectedPrefix) + assert.Equal(t, expectedPrefix, path.getPrefix().String()) // check nexthop expectedNexthop := "2001::192:168:100:1" - assert.Equal(t, expectedNexthop, path.getNexthop().String(), "nexthop should be ", expectedNexthop) + assert.Equal(t, expectedNexthop, path.getNexthop().String()) } @@ -609,7 +616,8 @@ func TestProcessBGPUpdate_3_select_aspath_ipv6(t *testing.T) { func TestProcessBGPUpdate_4_select_low_origin_ipv4(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low origin message @@ -661,15 +669,15 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv4(t *testing.T) { pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -690,12 +698,14 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv4(t *testing.T) { func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(1) aspath1 := createAsPathAttribute([]uint16{65200, 65000}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(100) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -709,7 +719,8 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{65100, 65000}) - mp_reach2 := createMpReach("2001::192:168:100:1", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(100) localpref2 := bgp.NewPathAttributeLocalPref(200) @@ -725,13 +736,13 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -743,15 +754,15 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -773,7 +784,8 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { func TestProcessBGPUpdate_5_select_low_med_ipv4(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low origin message @@ -825,15 +837,15 @@ func TestProcessBGPUpdate_5_select_low_med_ipv4(t *testing.T) { pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -854,12 +866,14 @@ func TestProcessBGPUpdate_5_select_low_med_ipv4(t *testing.T) { func TestProcessBGPUpdate_5_select_low_med_ipv6(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(0) aspath1 := createAsPathAttribute([]uint16{65200, 65000}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(500) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -873,7 +887,8 @@ func TestProcessBGPUpdate_5_select_low_med_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{65100, 65000}) - mp_reach2 := createMpReach("2001::192:168:100:1", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(200) localpref2 := bgp.NewPathAttributeLocalPref(100) @@ -889,13 +904,13 @@ func TestProcessBGPUpdate_5_select_low_med_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -907,15 +922,15 @@ func TestProcessBGPUpdate_5_select_low_med_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -939,7 +954,8 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv4(t *testing.T) { tm := NewTableManager() tm.localAsn = uint32(65000) - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low origin message @@ -991,15 +1007,15 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv4(t *testing.T) { pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -1021,12 +1037,14 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv6(t *testing.T) { tm := NewTableManager() tm.localAsn = uint32(65000) - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(0) aspath1 := createAsPathAttribute([]uint16{65000, 65200}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(200) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -1040,7 +1058,8 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{65100, 65200}) - mp_reach2 := createMpReach("2001::192:168:100:1", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(200) localpref2 := bgp.NewPathAttributeLocalPref(100) @@ -1056,13 +1075,13 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -1074,15 +1093,15 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -1108,7 +1127,8 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv4(t *testing.T) { tm := NewTableManager() tm.localAsn = uint32(65000) - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low origin message @@ -1160,15 +1180,15 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv4(t *testing.T) { pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -1190,12 +1210,14 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { tm := NewTableManager() tm.localAsn = uint32(65000) - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(0) aspath1 := createAsPathAttribute([]uint16{65000, 65200}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(200) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -1209,7 +1231,8 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{65100, 65200}) - mp_reach2 := createMpReach("2001::192:168:100:1", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(200) localpref2 := bgp.NewPathAttributeLocalPref(100) @@ -1225,13 +1248,13 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) peer3 := peerR3() pList, wList, err = tm.ProcessUpdate(peer3, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -1243,15 +1266,15 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -1269,6 +1292,853 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { } +// test: withdraw and mpunreach path +func TestProcessBGPUpdate_8_withdraw_path_ipv4(t *testing.T) { + + tm := NewTableManager() + //setLogger(getLogger(log.DebugLevel)) + var pList []Path + var wList []Destination + var err error + + // path1 + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000}) + nexthop1 := bgp.NewPathAttributeNextHop("192.168.50.1") + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes1 := []bgp.PathAttributeInterface{ + origin1, aspath1, nexthop1, med1, localpref1, + } + nlri1 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")} + withdrawnRoutes1 := []bgp.WithdrawnRoute{} + bgpMessage1 := bgp.NewBGPUpdateMessage(withdrawnRoutes1, pathAttributes1, nlri1) + + // path 2 + origin2 := bgp.NewPathAttributeOrigin(0) + aspath2 := createAsPathAttribute([]uint16{65100, 65000}) + nexthop2 := bgp.NewPathAttributeNextHop("192.168.100.1") + med2 := bgp.NewPathAttributeMultiExitDisc(200) + localpref2 := bgp.NewPathAttributeLocalPref(200) + + pathAttributes2 := []bgp.PathAttributeInterface{ + origin2, aspath2, nexthop2, med2, localpref2, + } + nlri2 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")} + withdrawnRoutes2 := []bgp.WithdrawnRoute{} + bgpMessage2 := bgp.NewBGPUpdateMessage(withdrawnRoutes2, pathAttributes2, nlri2) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + peer2 := peerR2() + pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check type + path := pList[0] + expectedType := "*table.IPv4Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + expectedOrigin := pathAttributes[0] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[1] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedNexthopAttr := pathAttributes[2] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + checkPattr(bgpMessage2, path) + // check destination + expectedPrefix := "10.10.10.0" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "192.168.100.1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + + //withdraw path + w1 := bgp.WithdrawnRoute{*bgp.NewIPAddrPrefix(24, "10.10.10.0")} + w := []bgp.WithdrawnRoute{w1} + bgpMessage3 := bgp.NewBGPUpdateMessage(w, []bgp.PathAttributeInterface{}, []bgp.NLRInfo{}) + + pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage3) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + path = pList[0] + expectedType = "*table.IPv4Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + checkPattr(bgpMessage1, path) + // check destination + expectedPrefix = "10.10.10.0" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop = "192.168.50.1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) +} + +// TODO MP_UNREACH +func TestProcessBGPUpdate_8_mpunreach_path_ipv6(t *testing.T) { + + tm := NewTableManager() + var pList []Path + var wList []Destination + var err error + + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000}) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes1 := []bgp.PathAttributeInterface{ + mp_reach1, origin1, aspath1, med1, localpref1, + } + + nlri1 := []bgp.NLRInfo{} + withdrawnRoutes1 := []bgp.WithdrawnRoute{} + bgpMessage1 := bgp.NewBGPUpdateMessage(withdrawnRoutes1, pathAttributes1, nlri1) + + origin2 := bgp.NewPathAttributeOrigin(0) + aspath2 := createAsPathAttribute([]uint16{65100, 65000}) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) + med2 := bgp.NewPathAttributeMultiExitDisc(200) + localpref2 := bgp.NewPathAttributeLocalPref(200) + + pathAttributes2 := []bgp.PathAttributeInterface{ + mp_reach2, origin2, aspath2, med2, localpref2, + } + + nlri2 := []bgp.NLRInfo{} + withdrawnRoutes2 := []bgp.WithdrawnRoute{} + bgpMessage2 := bgp.NewBGPUpdateMessage(withdrawnRoutes2, pathAttributes2, nlri2) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + peer2 := peerR2() + pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check type + path := pList[0] + expectedType := "*table.IPv6Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes + + expectedNexthopAttr := pathAttributes[0] + pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[1] + pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[2] + pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[3] + pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + + expectedNexthopAttr := pathAttributes[0] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[1] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[2] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + + checkPattr(bgpMessage2, path) + + // check destination + expectedPrefix := "2001:123:123:1::" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "2001::192:168:100:1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + + //mpunreach path + mp_unreach := createMpUNReach("2001:123:123:1::", 64) + bgpMessage3 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, + []bgp.PathAttributeInterface{mp_unreach}, []bgp.NLRInfo{}) + + pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage3) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + path = pList[0] + expectedType = "*table.IPv6Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + checkPattr(bgpMessage1, path) + // check destination + expectedPrefix = "2001:123:123:1::" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop = "2001::192:168:50:1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + +} + +// handle bestpath lost +func TestProcessBGPUpdate_bestpath_lost_ipv4(t *testing.T) { + + tm := NewTableManager() + //setLogger(getLogger(log.DebugLevel)) + var pList []Path + var wList []Destination + var err error + + // path1 + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000}) + nexthop1 := bgp.NewPathAttributeNextHop("192.168.50.1") + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes1 := []bgp.PathAttributeInterface{ + origin1, aspath1, nexthop1, med1, localpref1, + } + nlri1 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")} + bgpMessage1 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes1, nlri1) + + // path 1 withdraw + w1 := bgp.WithdrawnRoute{*bgp.NewIPAddrPrefix(24, "10.10.10.0")} + w := []bgp.WithdrawnRoute{w1} + bgpMessage1_w := bgp.NewBGPUpdateMessage(w, []bgp.PathAttributeInterface{}, []bgp.NLRInfo{}) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1_w) + assert.Equal(t, 0, len(pList)) + assert.Equal(t, 1, len(wList)) + assert.NoError(t, err) + + // check old best path + path := wList[0].getOldBestPath() + expectedType := "*table.IPv4Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + expectedOrigin := pathAttributes[0] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[1] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedNexthopAttr := pathAttributes[2] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + + checkPattr(bgpMessage1, path) + // check destination + expectedPrefix := "10.10.10.0" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "192.168.50.1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + +} + +func TestProcessBGPUpdate_bestpath_lost_ipv6(t *testing.T) { + + tm := NewTableManager() + var pList []Path + var wList []Destination + var err error + + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000}) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes1 := []bgp.PathAttributeInterface{ + mp_reach1, origin1, aspath1, med1, localpref1, + } + + nlri1 := []bgp.NLRInfo{} + withdrawnRoutes1 := []bgp.WithdrawnRoute{} + bgpMessage1 := bgp.NewBGPUpdateMessage(withdrawnRoutes1, pathAttributes1, nlri1) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // path1 mpunreach + mp_unreach := createMpUNReach("2001:123:123:1::", 64) + bgpMessage1_w := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, + []bgp.PathAttributeInterface{mp_unreach}, []bgp.NLRInfo{}) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1_w) + assert.Equal(t, 0, len(pList)) + assert.Equal(t, 1, len(wList)) + assert.NoError(t, err) + + // check old best path + path := wList[0].getOldBestPath() + expectedType := "*table.IPv6Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + + expectedNexthopAttr := pathAttributes[0] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[1] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[2] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + + checkPattr(bgpMessage1, path) + + // check destination + expectedPrefix := "2001:123:123:1::" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "2001::192:168:50:1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + +} + +// test: implicit withdrawal case +func TestProcessBGPUpdate_implicit_withdrwal_ipv4(t *testing.T) { + + tm := NewTableManager() + //setLogger(getLogger(log.DebugLevel)) + var pList []Path + var wList []Destination + var err error + + // path1 + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000, 65100, 65200}) + nexthop1 := bgp.NewPathAttributeNextHop("192.168.50.1") + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes1 := []bgp.PathAttributeInterface{ + origin1, aspath1, nexthop1, med1, localpref1, + } + nlri1 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")} + withdrawnRoutes1 := []bgp.WithdrawnRoute{} + bgpMessage1 := bgp.NewBGPUpdateMessage(withdrawnRoutes1, pathAttributes1, nlri1) + + // path 1 from same peer but short AS_PATH + origin2 := bgp.NewPathAttributeOrigin(0) + aspath2 := createAsPathAttribute([]uint16{65000, 65100}) + nexthop2 := bgp.NewPathAttributeNextHop("192.168.50.1") + med2 := bgp.NewPathAttributeMultiExitDisc(200) + localpref2 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes2 := []bgp.PathAttributeInterface{ + origin2, aspath2, nexthop2, med2, localpref2, + } + nlri2 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")} + withdrawnRoutes2 := []bgp.WithdrawnRoute{} + bgpMessage2 := bgp.NewBGPUpdateMessage(withdrawnRoutes2, pathAttributes2, nlri2) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage2) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check type + path := pList[0] + expectedType := "*table.IPv4Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + expectedOrigin := pathAttributes[0] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[1] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedNexthopAttr := pathAttributes[2] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + checkPattr(bgpMessage2, path) + // check destination + expectedPrefix := "10.10.10.0" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "192.168.50.1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + +} + +func TestProcessBGPUpdate_implicit_withdrwal_ipv6(t *testing.T) { + + tm := NewTableManager() + var pList []Path + var wList []Destination + var err error + + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000, 65100, 65200}) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(200) + + pathAttributes1 := []bgp.PathAttributeInterface{ + mp_reach1, origin1, aspath1, med1, localpref1, + } + + nlri1 := []bgp.NLRInfo{} + withdrawnRoutes1 := []bgp.WithdrawnRoute{} + bgpMessage1 := bgp.NewBGPUpdateMessage(withdrawnRoutes1, pathAttributes1, nlri1) + + origin2 := bgp.NewPathAttributeOrigin(0) + aspath2 := createAsPathAttribute([]uint16{65000, 65100}) + mp_reach2 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) + med2 := bgp.NewPathAttributeMultiExitDisc(200) + localpref2 := bgp.NewPathAttributeLocalPref(200) + + pathAttributes2 := []bgp.PathAttributeInterface{ + mp_reach2, origin2, aspath2, med2, localpref2, + } + + nlri2 := []bgp.NLRInfo{} + withdrawnRoutes2 := []bgp.WithdrawnRoute{} + bgpMessage2 := bgp.NewBGPUpdateMessage(withdrawnRoutes2, pathAttributes2, nlri2) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage2) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check type + path := pList[0] + expectedType := "*table.IPv6Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes + + expectedNexthopAttr := pathAttributes[0] + pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[1] + pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[2] + pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[3] + pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + + expectedNexthopAttr := pathAttributes[0] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[1] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[2] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + + checkPattr(bgpMessage2, path) + + // check destination + expectedPrefix := "2001:123:123:1::" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "2001::192:168:50:1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + +} + +// check multiple paths +func TestProcessBGPUpdate_multiple_nlri_ipv4(t *testing.T) { + + tm := NewTableManager() + //setLogger(getLogger(log.DebugLevel)) + var pList []Path + var wList []Destination + var err error + + createPathAttr := func(aspaths []uint16, nh string) []bgp.PathAttributeInterface { + origin := bgp.NewPathAttributeOrigin(0) + aspath := createAsPathAttribute(aspaths) + nexthop := bgp.NewPathAttributeNextHop(nh) + med := bgp.NewPathAttributeMultiExitDisc(200) + localpref := bgp.NewPathAttributeLocalPref(100) + pathAttr := []bgp.PathAttributeInterface{ + origin, aspath, nexthop, med, localpref, + } + return pathAttr + } + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + expectedOrigin := pathAttributes[0] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[1] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedNexthopAttr := pathAttributes[2] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute length + assert.Equal(t, len(pathAttributes), actual.getPathAttributeMap().Len()) + } + + checkBestPathResult := func(pType, prefix, nexthop string, p Path, m *bgp.BGPMessage) { + expectedType := pType + assert.Equal(t, reflect.TypeOf(p).String(), expectedType) + checkPattr(m, p) + // check destination + assert.Equal(t, prefix, p.getPrefix().String()) + // check nexthop + assert.Equal(t, nexthop, p.getNexthop().String()) + } + + // path1 + pathAttributes1 := createPathAttr([]uint16{65000, 65100, 65200}, "192.168.50.1") + nlri1 := []bgp.NLRInfo{ + *bgp.NewNLRInfo(24, "10.10.10.0"), + *bgp.NewNLRInfo(24, "20.20.20.0"), + *bgp.NewNLRInfo(24, "30.30.30.0"), + *bgp.NewNLRInfo(24, "40.40.40.0"), + *bgp.NewNLRInfo(24, "50.50.50.0")} + bgpMessage1 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes1, nlri1) + + // path2 + pathAttributes2 := createPathAttr([]uint16{65000, 65100, 65300}, "192.168.50.1") + nlri2 := []bgp.NLRInfo{ + *bgp.NewNLRInfo(24, "11.11.11.0"), + *bgp.NewNLRInfo(24, "22.22.22.0"), + *bgp.NewNLRInfo(24, "33.33.33.0"), + *bgp.NewNLRInfo(24, "44.44.44.0"), + *bgp.NewNLRInfo(24, "55.55.55.0")} + bgpMessage2 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes2, nlri2) + + // path3 + pathAttributes3 := createPathAttr([]uint16{65000, 65100, 65400}, "192.168.50.1") + nlri3 := []bgp.NLRInfo{ + *bgp.NewNLRInfo(24, "77.77.77.0"), + *bgp.NewNLRInfo(24, "88.88.88.0"), + } + bgpMessage3 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes3, nlri3) + + // path4 + pathAttributes4 := createPathAttr([]uint16{65000, 65100, 65500}, "192.168.50.1") + nlri4 := []bgp.NLRInfo{ + *bgp.NewNLRInfo(24, "99.99.99.0"), + } + bgpMessage4 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes4, nlri4) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 5, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + checkBestPathResult("*table.IPv4Path", "10.10.10.0", "192.168.50.1", pList[0], bgpMessage1) + checkBestPathResult("*table.IPv4Path", "20.20.20.0", "192.168.50.1", pList[1], bgpMessage1) + checkBestPathResult("*table.IPv4Path", "30.30.30.0", "192.168.50.1", pList[2], bgpMessage1) + checkBestPathResult("*table.IPv4Path", "40.40.40.0", "192.168.50.1", pList[3], bgpMessage1) + checkBestPathResult("*table.IPv4Path", "50.50.50.0", "192.168.50.1", pList[4], bgpMessage1) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage2) + assert.Equal(t, 5, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + checkBestPathResult("*table.IPv4Path", "11.11.11.0", "192.168.50.1", pList[0], bgpMessage2) + checkBestPathResult("*table.IPv4Path", "22.22.22.0", "192.168.50.1", pList[1], bgpMessage2) + checkBestPathResult("*table.IPv4Path", "33.33.33.0", "192.168.50.1", pList[2], bgpMessage2) + checkBestPathResult("*table.IPv4Path", "44.44.44.0", "192.168.50.1", pList[3], bgpMessage2) + checkBestPathResult("*table.IPv4Path", "55.55.55.0", "192.168.50.1", pList[4], bgpMessage2) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage3) + assert.Equal(t, 2, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage4) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check table + table := tm.Tables[RF_IPv4_UC] + assert.Equal(t, 13, len(table.getDestinations())) + +} + +// check multiple paths +func TestProcessBGPUpdate_multiple_nlri_ipv6(t *testing.T) { + + tm := NewTableManager() + //setLogger(getLogger(log.DebugLevel)) + var pList []Path + var wList []Destination + var err error + + createPathAttr := func(aspaths []uint16) []bgp.PathAttributeInterface { + origin := bgp.NewPathAttributeOrigin(0) + aspath := createAsPathAttribute(aspaths) + med := bgp.NewPathAttributeMultiExitDisc(100) + localpref := bgp.NewPathAttributeLocalPref(100) + pathAttr := []bgp.PathAttributeInterface{ + origin, aspath, med, localpref, + } + return pathAttr + } + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + pathNexthop := pathAttributes[4] + expectedNexthopAttr := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[0] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[1] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[2] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + expectedLocalpref := pathAttributes[3] + localpref := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_LOCAL_PREF).(*bgp.PathAttributeLocalPref) + assert.Equal(t, expectedLocalpref, localpref) + + // check PathAttribute length + assert.Equal(t, len(pathAttributes), actual.getPathAttributeMap().Len()) + + } + + checkBestPathResult := func(pType, prefix, nexthop string, p Path, m *bgp.BGPMessage) { + expectedType := pType + assert.Equal(t, reflect.TypeOf(p).String(), expectedType) + checkPattr(m, p) + // check destination + assert.Equal(t, prefix, p.getPrefix().String()) + // check nexthop + assert.Equal(t, nexthop, p.getNexthop().String()) + } + + // path1 + pathAttributes1 := createPathAttr([]uint16{65000, 65100, 65200}) + mpreach1 := createMpReach("2001::192:168:50:1", []bgp.AddrPrefixInterface{ + bgp.NewIPv6AddrPrefix(64, "2001:123:1210:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1220:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1230:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1240:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1250:11::"), + }) + pathAttributes1 = append(pathAttributes1, mpreach1) + bgpMessage1 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes1, []bgp.NLRInfo{}) + + // path2 + pathAttributes2 := createPathAttr([]uint16{65000, 65100, 65300}) + mpreach2 := createMpReach("2001::192:168:50:1", []bgp.AddrPrefixInterface{ + bgp.NewIPv6AddrPrefix(64, "2001:123:1211:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1222:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1233:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1244:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1255:11::"), + }) + pathAttributes2 = append(pathAttributes2, mpreach2) + bgpMessage2 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes2, []bgp.NLRInfo{}) + + // path3 + pathAttributes3 := createPathAttr([]uint16{65000, 65100, 65400}) + mpreach3 := createMpReach("2001::192:168:50:1", []bgp.AddrPrefixInterface{ + bgp.NewIPv6AddrPrefix(64, "2001:123:1277:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1288:11::"), + }) + pathAttributes3 = append(pathAttributes3, mpreach3) + bgpMessage3 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes3, []bgp.NLRInfo{}) + + // path4 + pathAttributes4 := createPathAttr([]uint16{65000, 65100, 65500}) + mpreach4 := createMpReach("2001::192:168:50:1", []bgp.AddrPrefixInterface{ + bgp.NewIPv6AddrPrefix(64, "2001:123:1299:11::"), + }) + pathAttributes4 = append(pathAttributes4, mpreach4) + bgpMessage4 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes4, []bgp.NLRInfo{}) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 5, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + checkBestPathResult("*table.IPv6Path", "2001:123:1210:11::", "2001::192:168:50:1", pList[0], bgpMessage1) + checkBestPathResult("*table.IPv6Path", "2001:123:1220:11::", "2001::192:168:50:1", pList[1], bgpMessage1) + checkBestPathResult("*table.IPv6Path", "2001:123:1230:11::", "2001::192:168:50:1", pList[2], bgpMessage1) + checkBestPathResult("*table.IPv6Path", "2001:123:1240:11::", "2001::192:168:50:1", pList[3], bgpMessage1) + checkBestPathResult("*table.IPv6Path", "2001:123:1250:11::", "2001::192:168:50:1", pList[4], bgpMessage1) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage2) + assert.Equal(t, 5, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + checkBestPathResult("*table.IPv6Path", "2001:123:1211:11::", "2001::192:168:50:1", pList[0], bgpMessage2) + checkBestPathResult("*table.IPv6Path", "2001:123:1222:11::", "2001::192:168:50:1", pList[1], bgpMessage2) + checkBestPathResult("*table.IPv6Path", "2001:123:1233:11::", "2001::192:168:50:1", pList[2], bgpMessage2) + checkBestPathResult("*table.IPv6Path", "2001:123:1244:11::", "2001::192:168:50:1", pList[3], bgpMessage2) + checkBestPathResult("*table.IPv6Path", "2001:123:1255:11::", "2001::192:168:50:1", pList[4], bgpMessage2) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage3) + assert.Equal(t, 2, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage4) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check table + table := tm.Tables[RF_IPv6_UC] + assert.Equal(t, 13, len(table.getDestinations())) + +} + func update_fromR1() *bgp.BGPMessage { origin := bgp.NewPathAttributeOrigin(0) @@ -1335,7 +2205,8 @@ func update_fromR2_ipv6() *bgp.BGPMessage { origin := bgp.NewPathAttributeOrigin(0) aspath := createAsPathAttribute([]uint16{65100}) - mp_reach := createMpReach("2001::192:168:100:1", "2002:223:123:1::", 64) + mp_reach := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2002:223:123:1::")}) med := bgp.NewPathAttributeMultiExitDisc(100) pathAttributes := []bgp.PathAttributeInterface{ @@ -1355,12 +2226,17 @@ func createAsPathAttribute(ases []uint16) *bgp.PathAttributeAsPath { return aspath } -func createMpReach(nexthop, nlri string, len uint8) *bgp.PathAttributeMpReachNLRI { - mp_nlri := []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(len, nlri)} - mp_reach := bgp.NewPathAttributeMpReachNLRI(nexthop, mp_nlri) +func createMpReach(nexthop string, prefix []bgp.AddrPrefixInterface) *bgp.PathAttributeMpReachNLRI { + mp_reach := bgp.NewPathAttributeMpReachNLRI(nexthop, prefix) return mp_reach } +func createMpUNReach(nlri string, len uint8) *bgp.PathAttributeMpUnreachNLRI { + mp_nlri := []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(len, nlri)} + mp_unreach := bgp.NewPathAttributeMpUnreachNLRI(mp_nlri) + return mp_unreach +} + func update_fromR2viaR1() *bgp.BGPMessage { origin := bgp.NewPathAttributeOrigin(0) @@ -1383,7 +2259,8 @@ func update_fromR2viaR1_ipv6() *bgp.BGPMessage { origin := bgp.NewPathAttributeOrigin(0) aspath := createAsPathAttribute([]uint16{65000, 65100}) - mp_reach := createMpReach("2001::192:168:50:1", "2002:223:123:1::", 64) + mp_reach := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2002:223:123:1::")}) med := bgp.NewPathAttributeMultiExitDisc(100) pathAttributes := []bgp.PathAttributeInterface{ @@ -1397,86 +2274,3 @@ func update_fromR2viaR1_ipv6() *bgp.BGPMessage { return bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) } - -func update() *bgp.BGPMessage { - w1 := bgp.WithdrawnRoute{*bgp.NewIPAddrPrefix(23, "121.1.3.2")} - w2 := bgp.WithdrawnRoute{*bgp.NewIPAddrPrefix(17, "100.33.3.0")} - w := []bgp.WithdrawnRoute{w1, w2} - //w := []WithdrawnRoute{} - - aspath1 := []bgp.AsPathParamInterface{ - bgp.NewAsPathParam(2, []uint16{1000}), - bgp.NewAsPathParam(1, []uint16{1001, 1002}), - bgp.NewAsPathParam(2, []uint16{1003, 1004}), - } - - aspath2 := []bgp.AsPathParamInterface{ - bgp.NewAs4PathParam(2, []uint32{1000000}), - bgp.NewAs4PathParam(1, []uint32{1000001, 1002}), - bgp.NewAs4PathParam(2, []uint32{1003, 100004}), - } - - aspath3 := []*bgp.As4PathParam{ - bgp.NewAs4PathParam(2, []uint32{1000000}), - bgp.NewAs4PathParam(1, []uint32{1000001, 1002}), - bgp.NewAs4PathParam(2, []uint32{1003, 100004}), - } - - ecommunities := []bgp.ExtendedCommunityInterface{ - &bgp.TwoOctetAsSpecificExtended{SubType: 1, AS: 10003, LocalAdmin: 3 << 20}, - &bgp.FourOctetAsSpecificExtended{SubType: 2, AS: 1 << 20, LocalAdmin: 300}, - &bgp.IPv4AddressSpecificExtended{SubType: 3, IPv4: net.ParseIP("192.2.1.2").To4(), LocalAdmin: 3000}, - &bgp.OpaqueExtended{Value: []byte{0, 1, 2, 3, 4, 5, 6, 7}}, - &bgp.UnknownExtended{Type: 99, Value: []byte{0, 1, 2, 3, 4, 5, 6, 7}}, - } - - mp_nlri := []bgp.AddrPrefixInterface{ - bgp.NewLabelledVPNIPAddrPrefix(20, "192.0.9.0", *bgp.NewLabel(1, 2, 3), - bgp.NewRouteDistinguisherTwoOctetAS(256, 10000)), - bgp.NewLabelledVPNIPAddrPrefix(26, "192.10.8.192", *bgp.NewLabel(5, 6, 7, 8), - bgp.NewRouteDistinguisherIPAddressAS("10.0.1.1", 10001)), - } - - mp_nlri2 := []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(100, - "fe80:1234:1234:5667:8967:af12:8912:1023")} - - mp_nlri3 := []bgp.AddrPrefixInterface{bgp.NewLabelledVPNIPv6AddrPrefix(100, - "fe80:1234:1234:5667:8967:af12:1203:33a1", *bgp.NewLabel(5, 6), - bgp.NewRouteDistinguisherFourOctetAS(5, 6))} - - mp_nlri4 := []bgp.AddrPrefixInterface{bgp.NewLabelledIPAddrPrefix(25, "192.168.0.0", - *bgp.NewLabel(5, 6, 7))} - - p := []bgp.PathAttributeInterface{ - bgp.NewPathAttributeOrigin(3), - bgp.NewPathAttributeAsPath(aspath1), - bgp.NewPathAttributeAsPath(aspath2), - bgp.NewPathAttributeNextHop("129.1.1.2"), - bgp.NewPathAttributeMultiExitDisc(1 << 20), - bgp.NewPathAttributeLocalPref(1 << 22), - bgp.NewPathAttributeAtomicAggregate(), - bgp.NewPathAttributeAggregator(uint16(30002), "129.0.2.99"), - bgp.NewPathAttributeAggregator(uint32(30002), "129.0.2.99"), - bgp.NewPathAttributeAggregator(uint32(300020), "129.0.2.99"), - bgp.NewPathAttributeCommunities([]uint32{1, 3}), - bgp.NewPathAttributeOriginatorId("10.10.0.1"), - bgp.NewPathAttributeClusterList([]string{"10.10.0.2", "10.10.0.3"}), - bgp.NewPathAttributeExtendedCommunities(ecommunities), - bgp.NewPathAttributeAs4Path(aspath3), - bgp.NewPathAttributeAs4Aggregator(10000, "112.22.2.1"), - bgp.NewPathAttributeMpReachNLRI("112.22.2.0", mp_nlri), - bgp.NewPathAttributeMpReachNLRI("1023::", mp_nlri2), - bgp.NewPathAttributeMpReachNLRI("fe80::", mp_nlri3), - bgp.NewPathAttributeMpReachNLRI("129.1.1.1", mp_nlri4), - bgp.NewPathAttributeMpUnreachNLRI(mp_nlri), - &bgp.PathAttributeUnknown{ - PathAttribute: bgp.PathAttribute{ - Flags: 1, - Type: 100, - Value: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, - }, - }, - } - n := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "13.2.3.1")} - return bgp.NewBGPUpdateMessage(w, p, n) -} |