1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

test for presence of communties

This commit is contained in:
Matthias Hannig
2018-10-17 11:05:48 +02:00
parent e7edd6bad9
commit d5ea763399
2 changed files with 105 additions and 0 deletions

View File

@@ -188,6 +188,66 @@ type BgpInfo struct {
Med int `json:"med"`
}
func (bgp BgpInfo) HasCommunity(community Community) bool {
if len(community) != 2 {
return false // This can never match.
}
for _, com := range bgp.Communities {
if len(com) != len(community) {
continue // This can't match.
}
if com[0] == community[0] &&
com[1] == community[1] {
return true
}
}
return false
}
func (bgp BgpInfo) HasExtCommunity(community ExtCommunity) bool {
if len(community) != 3 {
return false // This can never match.
}
for _, com := range bgp.ExtCommunities {
if len(com) != len(community) {
continue // This can't match.
}
if com[0] == community[0] &&
com[1] == community[1] &&
com[2] == community[2] {
return true
}
}
return false
}
func (bgp BgpInfo) HasLargeCommunity(community Community) bool {
// TODO: This is an almost 1:1 match to the function above.
if len(community) != 3 {
return false // This can never match.
}
for _, com := range bgp.LargeCommunities {
if len(com) != len(community) {
continue // This can't match.
}
if com[0] == community[0] &&
com[1] == community[1] &&
com[2] == community[2] {
return true
}
}
return false
}
// Prefixes
type Route struct {
Id string `json:"id"`

View File

@@ -77,3 +77,48 @@ func TestCommunityStringify(t *testing.T) {
t.Error("Expected ro:42:123, but got:", extCom.String())
}
}
func TestHasCommunity(t *testing.T) {
com := Community{23, 42}
bgp := BgpInfo{
Communities: []Community{
Community{42, 123},
Community{23, 42},
Community{42, 23},
},
ExtCommunities: []ExtCommunity{
ExtCommunity{"rt", 23, 42},
ExtCommunity{"ro", 123, 456},
},
LargeCommunities: []Community{
Community{1000, 23, 42},
Community{2000, 123, 456},
},
}
if bgp.HasCommunity(com) == false {
t.Error("Expected community 23:42 to be present")
}
if bgp.HasCommunity(Community{111, 11}) != false {
t.Error("Expected community 111:11 to be not present")
}
if bgp.HasExtCommunity(ExtCommunity{"ro", 123, 456}) == false {
t.Error("Expected ro:123:456 in ext community set")
}
if bgp.HasExtCommunity(ExtCommunity{"ro", 111, 11}) != false {
t.Error("Expected ro:111:111 not in ext community set")
}
if bgp.HasLargeCommunity(Community{2000, 123, 456}) == false {
t.Error("Expected community 2000:123:456 present")
}
if bgp.HasLargeCommunity(Community{23, 42}) != false {
t.Error("23:42 should not be present in large commnuties")
}
}