Skip to content
Snippets Groups Projects
Commit 5bf3640a authored by cedi's avatar cedi
Browse files

Merge branch 'feature/netlink' of github.com:bio-routing/bio-rd into feature/netlink

parents 1bb12c52 84fdb5ff
No related branches found
No related tags found
No related merge requests found
package net
import (
gonet "net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetIPNet(t *testing.T) {
tests := []struct {
name string
pfx Prefix
expected *gonet.IPNet
}{
{
name: "Some prefix IPv4",
pfx: NewPfx(IPv4FromOctets(127, 0, 0, 0), 8),
expected: &gonet.IPNet{
IP: gonet.IP{127, 0, 0, 0},
Mask: gonet.IPMask{255, 0, 0, 0},
},
},
{
name: "Some prefix IPv6",
pfx: NewPfx(IPv6FromBlocks(0xffff, 0xffff, 0xffff, 0xffff, 0x0, 0x0, 0x0, 0x0), 64),
expected: &gonet.IPNet{
IP: gonet.IP{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
Mask: gonet.IPMask{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0},
},
},
}
for _, test := range tests {
res := test.pfx.GetIPNet()
assert.Equal(t, test.expected, res, test.name)
}
}
func TestNewPfxFromIPNet(t *testing.T) {
tests := []struct {
name string
ipNet *gonet.IPNet
expected Prefix
}{
{
name: "Some Prefix",
ipNet: &gonet.IPNet{
IP: gonet.IP{127, 0, 0, 0},
Mask: gonet.IPMask{255, 0, 0, 0},
},
expected: NewPfx(IPv4FromOctets(127, 0, 0, 0), 8),
},
}
for _, test := range tests {
res := NewPfxFromIPNet(test.ipNet)
assert.Equal(t, test.expected, res, test.name)
}
}
func TestNewPfx(t *testing.T) {
p := NewPfx(IPv4(123), 11)
if p.addr != IPv4(123) || p.pfxlen != 11 {
......@@ -371,6 +424,11 @@ func TestStrToAddr(t *testing.T) {
wantFail bool
expected uint32
}{
{
name: "Non numeric",
input: "10.10.10.a",
wantFail: true,
},
{
name: "Invalid address #1",
input: "10.10.10",
......
......@@ -7,6 +7,121 @@ import (
"github.com/stretchr/testify/assert"
)
func TestBGPSelect(t *testing.T) {
tests := []struct {
name string
p *BGPPath
q *BGPPath
expected int8
}{
{
name: "Lpref",
p: &BGPPath{
LocalPref: 200,
},
q: &BGPPath{
LocalPref: 100,
},
expected: 1,
},
{
name: "Lpref #2",
p: &BGPPath{
LocalPref: 100,
},
q: &BGPPath{
LocalPref: 200,
},
expected: -1,
},
{
name: "AS Path Len",
p: &BGPPath{
ASPathLen: 100,
},
q: &BGPPath{
ASPathLen: 200,
},
expected: 1,
},
{
name: "AS Path Len #2",
p: &BGPPath{
ASPathLen: 200,
},
q: &BGPPath{
ASPathLen: 100,
},
expected: -1,
},
{
name: "Origin",
p: &BGPPath{
Origin: 1,
},
q: &BGPPath{
Origin: 2,
},
expected: 1,
},
{
name: "Origin #2",
p: &BGPPath{
Origin: 2,
},
q: &BGPPath{
Origin: 1,
},
expected: -1,
},
{
name: "MED",
p: &BGPPath{
MED: 1,
},
q: &BGPPath{
MED: 2,
},
expected: 1,
},
{
name: "MED #2",
p: &BGPPath{
MED: 2,
},
q: &BGPPath{
MED: 1,
},
expected: -1,
},
{
name: "EBGP",
p: &BGPPath{
EBGP: true,
},
q: &BGPPath{
EBGP: false,
},
expected: 1,
},
{
name: "EBGP #2",
p: &BGPPath{
EBGP: false,
},
q: &BGPPath{
EBGP: true,
},
expected: -1,
},
}
for _, test := range tests {
res := test.p.Select(test.q)
assert.Equal(t, test.expected, res, test.name)
}
}
func TestCommunitiesString(t *testing.T) {
tests := []struct {
name string
......@@ -65,6 +180,55 @@ func TestLargeCommunitiesString(t *testing.T) {
}
}
func TestBGPECMP(t *testing.T) {
tests := []struct {
name string
p *BGPPath
q *BGPPath
expected bool
}{
{
name: "Equal",
p: &BGPPath{},
q: &BGPPath{},
expected: true,
},
{
name: "Lpref",
p: &BGPPath{LocalPref: 200},
q: &BGPPath{},
expected: false,
},
{
name: "MED",
p: &BGPPath{MED: 200},
q: &BGPPath{},
expected: false,
},
{
name: "ASPath Len",
p: &BGPPath{
ASPathLen: 2,
},
q: &BGPPath{
ASPathLen: 1,
},
expected: false,
},
{
name: "Origin",
p: &BGPPath{Origin: 1},
q: &BGPPath{},
expected: false,
},
}
for _, test := range tests {
res := test.p.ECMP(test.q)
assert.Equal(t, test.expected, res, test.name)
}
}
func TestLength(t *testing.T) {
tests := []struct {
name string
......@@ -123,6 +287,26 @@ func TestLength(t *testing.T) {
},
expected: 71,
},
{
name: "Cluster list, unknown attr and originator",
path: &BGPPath{
ASPath: []types.ASPathSegment{
{
Type: types.ASSequence,
ASNs: []uint32{15169, 199714},
},
},
ClusterList: []uint32{10, 20, 30},
UnknownAttributes: []types.UnknownPathAttribute{
{
TypeCode: 100,
Value: []byte{1, 2, 3},
},
},
OriginatorID: 10,
},
expected: 54,
},
}
for _, test := range tests {
......
......@@ -2,11 +2,11 @@ package route
import (
"fmt"
"log"
bnet "github.com/bio-routing/bio-rd/net"
)
// Path represents a network path
type Path struct {
Type uint8
StaticPath *StaticPath
......@@ -46,6 +46,7 @@ func (p *Path) Select(q *Path) int8 {
panic("Unknown path type")
}
// ECMP checks if path p and q are equal enough to be considered for ECMP usage
func (p *Path) ECMP(q *Path) bool {
switch p.Type {
case BGPPathType:
......@@ -59,6 +60,7 @@ func (p *Path) ECMP(q *Path) bool {
panic("Unknown path type")
}
// Equal checks if paths p and q are equal
func (p *Path) Equal(q *Path) bool {
if p == nil || q == nil {
return false
......@@ -162,9 +164,7 @@ func (p *Path) NextHop() bnet.IP {
return p.StaticPath.NextHop
case NetlinkPathType:
return p.NetlinkPath.NextHop
default:
log.Panic("Type %d not implemented (yet)", p.Type)
}
return bnet.IP{}
panic("Unknown path type")
}
......@@ -3,9 +3,169 @@ package route
import (
"testing"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/stretchr/testify/assert"
)
func TestPathNextHop(t *testing.T) {
tests := []struct {
name string
p *Path
expected bnet.IP
}{
{
name: "BGP Path",
p: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{
NextHop: bnet.IPv4(123),
},
},
expected: bnet.IPv4(123),
},
{
name: "Static Path",
p: &Path{
Type: StaticPathType,
StaticPath: &StaticPath{
NextHop: bnet.IPv4(456),
},
},
expected: bnet.IPv4(456),
},
{
name: "Netlink Path",
p: &Path{
Type: NetlinkPathType,
NetlinkPath: &NetlinkPath{
NextHop: bnet.IPv4(1000),
},
},
expected: bnet.IPv4(1000),
},
}
for _, test := range tests {
res := test.p.NextHop()
assert.Equal(t, test.expected, res, test.name)
}
}
func TestPathCopy(t *testing.T) {
tests := []struct {
name string
p *Path
expected *Path
}{
{
name: "nil test",
},
}
for _, test := range tests {
res := test.p.Copy()
assert.Equal(t, test.expected, res, test.name)
}
}
func TestEqual(t *testing.T) {
tests := []struct {
name string
p *Path
q *Path
expected bool
}{
{
name: "Different types",
p: &Path{Type: 100},
q: &Path{Type: 200},
expected: false,
},
}
for _, test := range tests {
res := test.p.Equal(test.q)
assert.Equalf(t, test.expected, res, test.name)
}
}
func TestSelect(t *testing.T) {
tests := []struct {
name string
p *Path
q *Path
expected int8
}{
{
name: "All nil",
expected: 0,
},
{
name: "p nil",
q: &Path{},
expected: -1,
},
{
name: "q nil",
p: &Path{},
expected: 1,
},
{
name: "p > q",
p: &Path{Type: 20},
q: &Path{Type: 10},
expected: 1,
},
{
name: "p < q",
p: &Path{Type: 10},
q: &Path{Type: 20},
expected: -1,
},
{
name: "Static",
p: &Path{
Type: StaticPathType,
StaticPath: &StaticPath{},
},
q: &Path{
Type: StaticPathType,
StaticPath: &StaticPath{},
},
expected: 0,
},
{
name: "BGP",
p: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{},
},
q: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{},
},
expected: 0,
},
{
name: "Netlink",
p: &Path{
Type: NetlinkPathType,
NetlinkPath: &NetlinkPath{},
},
q: &Path{
Type: NetlinkPathType,
NetlinkPath: &NetlinkPath{},
},
expected: 0,
},
}
for _, test := range tests {
res := test.p.Select(test.q)
assert.Equalf(t, test.expected, res, "Test %q", test.name)
}
}
func TestPathsDiff(t *testing.T) {
tests := []struct {
name string
......
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