Skip to content
Snippets Groups Projects
Unverified Commit dc43526d authored by takt's avatar takt Committed by GitHub
Browse files

Merge branch 'master' into coverage-badge

parents 8d5d69c8 d095a7a3
Branches
Tags
No related merge requests found
...@@ -497,14 +497,23 @@ func TestBitAtPosition(t *testing.T) { ...@@ -497,14 +497,23 @@ func TestBitAtPosition(t *testing.T) {
position: 1, position: 1,
expected: true, expected: true,
}, },
{
name: "IPv4: invalid position",
input: IPv4(0),
position: 33,
expected: false,
},
{
name: "IPv6: invalid position",
input: IPv6(0, 0),
position: 129,
expected: false,
},
} }
for _, test := range tests { for _, test := range tests {
b := test.input.BitAtPosition(test.position) b := test.input.BitAtPosition(test.position)
if b != test.expected { assert.Equal(t, test.expected, b, test.name)
t.Errorf("%s: Unexpected failure: Bit %d of %v is %v. Expected %v",
test.name, test.position, test.input, b, test.expected)
}
} }
} }
...@@ -550,3 +559,26 @@ func TestIPFromString(t *testing.T) { ...@@ -550,3 +559,26 @@ func TestIPFromString(t *testing.T) {
}) })
} }
} }
func TestSizeBytes(t *testing.T) {
tests := []struct {
name string
input IP
expected uint8
}{
{
name: "IPv4",
input: IPv4(0),
expected: 4,
},
{
name: "IPv6",
input: IPv6(0, 0),
expected: 16,
},
}
for _, test := range tests {
assert.Equal(t, test.expected, test.input.SizeBytes(), test.name)
}
}
...@@ -300,13 +300,11 @@ func TestGetSupernet(t *testing.T) { ...@@ -300,13 +300,11 @@ func TestGetSupernet(t *testing.T) {
func TestContains(t *testing.T) { func TestContains(t *testing.T) {
tests := []struct { tests := []struct {
name string
a Prefix a Prefix
b Prefix b Prefix
expected bool expected bool
}{ }{
{ {
name: "Test 1",
a: Prefix{ a: Prefix{
addr: IPv4(0), addr: IPv4(0),
pfxlen: 0, pfxlen: 0,
...@@ -318,7 +316,6 @@ func TestContains(t *testing.T) { ...@@ -318,7 +316,6 @@ func TestContains(t *testing.T) {
expected: true, expected: true,
}, },
{ {
name: "Test 2",
a: Prefix{ a: Prefix{
addr: IPv4(100), addr: IPv4(100),
pfxlen: 24, pfxlen: 24,
...@@ -330,7 +327,6 @@ func TestContains(t *testing.T) { ...@@ -330,7 +327,6 @@ func TestContains(t *testing.T) {
expected: false, expected: false,
}, },
{ {
name: "Test 3",
a: Prefix{ a: Prefix{
addr: IPv4(167772160), addr: IPv4(167772160),
pfxlen: 8, pfxlen: 8,
...@@ -342,7 +338,6 @@ func TestContains(t *testing.T) { ...@@ -342,7 +338,6 @@ func TestContains(t *testing.T) {
expected: true, expected: true,
}, },
{ {
name: "Test 4",
a: Prefix{ a: Prefix{
addr: IPv4(167772160), addr: IPv4(167772160),
pfxlen: 8, pfxlen: 8,
...@@ -354,7 +349,6 @@ func TestContains(t *testing.T) { ...@@ -354,7 +349,6 @@ func TestContains(t *testing.T) {
expected: true, expected: true,
}, },
{ {
name: "Test 5",
a: Prefix{ a: Prefix{
addr: IPv4(167772160), addr: IPv4(167772160),
pfxlen: 8, pfxlen: 8,
...@@ -366,7 +360,6 @@ func TestContains(t *testing.T) { ...@@ -366,7 +360,6 @@ func TestContains(t *testing.T) {
expected: false, expected: false,
}, },
{ {
name: "Test 6",
a: Prefix{ a: Prefix{
addr: IPv4(167772160), addr: IPv4(167772160),
pfxlen: 8, pfxlen: 8,
...@@ -378,7 +371,6 @@ func TestContains(t *testing.T) { ...@@ -378,7 +371,6 @@ func TestContains(t *testing.T) {
expected: false, expected: false,
}, },
{ {
name: "Test 7",
a: Prefix{ a: Prefix{
addr: IPv4FromOctets(169, 0, 0, 0), addr: IPv4FromOctets(169, 0, 0, 0),
pfxlen: 25, pfxlen: 25,
...@@ -390,7 +382,6 @@ func TestContains(t *testing.T) { ...@@ -390,7 +382,6 @@ func TestContains(t *testing.T) {
expected: false, expected: false,
}, },
{ {
name: "IPv6: 2001:678:1e0:100::/56 is subnet of 2001:678:1e0::/48",
a: Prefix{ a: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0), addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0),
pfxlen: 48, pfxlen: 48,
...@@ -402,7 +393,6 @@ func TestContains(t *testing.T) { ...@@ -402,7 +393,6 @@ func TestContains(t *testing.T) {
}, },
}, },
{ {
name: "IPv6: 2001:678:1e0:100::/56 is subnet of 2001:678:1e0::/48",
a: Prefix{ a: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x200, 0, 0, 0, 0), addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x200, 0, 0, 0, 0),
pfxlen: 56, pfxlen: 56,
...@@ -413,11 +403,22 @@ func TestContains(t *testing.T) { ...@@ -413,11 +403,22 @@ func TestContains(t *testing.T) {
}, },
expected: false, expected: false,
}, },
{
a: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x200, 0, 0, 0, 0),
pfxlen: 65,
},
b: Prefix{
addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x100, 0, 0, 0, 0),
pfxlen: 64,
},
expected: false,
},
} }
for _, test := range tests { for _, test := range tests {
res := test.a.Contains(test.b) res := test.a.Contains(test.b)
assert.Equal(t, res, test.expected, "Unexpected result %v for test %s: %s contains %s", res, test.name, test.a, test.b) assert.Equal(t, res, test.expected, "Subnet %s contains %s", test.a, test.b)
} }
} }
......
...@@ -236,6 +236,8 @@ func (b *BGPPath) better(c *BGPPath) bool { ...@@ -236,6 +236,8 @@ func (b *BGPPath) better(c *BGPPath) bool {
// Print all known information about a route in logfile friendly format // Print all known information about a route in logfile friendly format
func (b *BGPPath) String() string { func (b *BGPPath) String() string {
buf := &strings.Builder{}
origin := "" origin := ""
switch b.Origin { switch b.Origin {
case 0: case 0:
...@@ -251,30 +253,32 @@ func (b *BGPPath) String() string { ...@@ -251,30 +253,32 @@ func (b *BGPPath) String() string {
bgpType = "external" bgpType = "external"
} }
ret := fmt.Sprintf("Local Pref: %d, ", b.LocalPref) fmt.Fprintf(buf, "Local Pref: %d, ", b.LocalPref)
ret += fmt.Sprintf("Origin: %s, ", origin) fmt.Fprintf(buf, "Origin: %s, ", origin)
ret += fmt.Sprintf("AS Path: %v, ", b.ASPath) fmt.Fprintf(buf, "AS Path: %v, ", b.ASPath)
ret += fmt.Sprintf("BGP type: %s, ", bgpType) fmt.Fprintf(buf, "BGP type: %s, ", bgpType)
ret += fmt.Sprintf("NEXT HOP: %s, ", b.NextHop) fmt.Fprintf(buf, "NEXT HOP: %s, ", b.NextHop)
ret += fmt.Sprintf("MED: %d, ", b.MED) fmt.Fprintf(buf, "MED: %d, ", b.MED)
ret += fmt.Sprintf("Path ID: %d, ", b.PathIdentifier) fmt.Fprintf(buf, "Path ID: %d, ", b.PathIdentifier)
ret += fmt.Sprintf("Source: %s, ", b.Source) fmt.Fprintf(buf, "Source: %s, ", b.Source)
ret += fmt.Sprintf("Communities: %v, ", b.Communities) fmt.Fprintf(buf, "Communities: %v, ", b.Communities)
ret += fmt.Sprintf("LargeCommunities: %v, ", b.LargeCommunities) fmt.Fprintf(buf, "LargeCommunities: %v", b.LargeCommunities)
if b.OriginatorID != 0 { if b.OriginatorID != 0 {
oid := convert.Uint32Byte(b.OriginatorID) oid := convert.Uint32Byte(b.OriginatorID)
ret += fmt.Sprintf("OriginatorID: %d.%d.%d.%d, ", oid[0], oid[1], oid[2], oid[3]) fmt.Fprintf(buf, ", OriginatorID: %d.%d.%d.%d", oid[0], oid[1], oid[2], oid[3])
} }
if b.ClusterList != nil { if b.ClusterList != nil {
ret += fmt.Sprintf("ClusterList %s", b.ClusterListString()) fmt.Fprintf(buf, ", ClusterList %s", b.ClusterListString())
} }
return ret return buf.String()
} }
// Print all known information about a route in human readable form // Print all known information about a route in human readable form
func (b *BGPPath) Print() string { func (b *BGPPath) Print() string {
buf := &strings.Builder{}
origin := "" origin := ""
switch b.Origin { switch b.Origin {
case 0: case 0:
...@@ -290,26 +294,26 @@ func (b *BGPPath) Print() string { ...@@ -290,26 +294,26 @@ func (b *BGPPath) Print() string {
bgpType = "external" bgpType = "external"
} }
ret := fmt.Sprintf("\t\tLocal Pref: %d\n", b.LocalPref) fmt.Fprintf(buf, "\t\tLocal Pref: %d\n", b.LocalPref)
ret += fmt.Sprintf("\t\tOrigin: %s\n", origin) fmt.Fprintf(buf, "\t\tOrigin: %s\n", origin)
ret += fmt.Sprintf("\t\tAS Path: %v\n", b.ASPath) fmt.Fprintf(buf, "\t\tAS Path: %v\n", b.ASPath)
ret += fmt.Sprintf("\t\tBGP type: %s\n", bgpType) fmt.Fprintf(buf, "\t\tBGP type: %s\n", bgpType)
ret += fmt.Sprintf("\t\tNEXT HOP: %s\n", b.NextHop) fmt.Fprintf(buf, "\t\tNEXT HOP: %s\n", b.NextHop)
ret += fmt.Sprintf("\t\tMED: %d\n", b.MED) fmt.Fprintf(buf, "\t\tMED: %d\n", b.MED)
ret += fmt.Sprintf("\t\tPath ID: %d\n", b.PathIdentifier) fmt.Fprintf(buf, "\t\tPath ID: %d\n", b.PathIdentifier)
ret += fmt.Sprintf("\t\tSource: %s\n", b.Source) fmt.Fprintf(buf, "\t\tSource: %s\n", b.Source)
ret += fmt.Sprintf("\t\tCommunities: %v\n", b.Communities) fmt.Fprintf(buf, "\t\tCommunities: %v\n", b.Communities)
ret += fmt.Sprintf("\t\tLargeCommunities: %v\n", b.LargeCommunities) fmt.Fprintf(buf, "\t\tLargeCommunities: %v\n", b.LargeCommunities)
if b.OriginatorID != 0 { if b.OriginatorID != 0 {
oid := convert.Uint32Byte(b.OriginatorID) oid := convert.Uint32Byte(b.OriginatorID)
ret += fmt.Sprintf("\t\tOriginatorID: %d.%d.%d.%d\n", oid[0], oid[1], oid[2], oid[3]) fmt.Fprintf(buf, "\t\tOriginatorID: %d.%d.%d.%d\n", oid[0], oid[1], oid[2], oid[3])
} }
if b.ClusterList != nil { if b.ClusterList != nil {
ret += fmt.Sprintf("\t\tClusterList %s\n", b.ClusterListString()) fmt.Fprintf(buf, "\t\tClusterList %s\n", b.ClusterListString())
} }
return ret return buf.String()
} }
// Prepend the given BGPPath with the given ASN given times // Prepend the given BGPPath with the given ASN given times
...@@ -400,31 +404,44 @@ func (b *BGPPath) ComputeHash() string { ...@@ -400,31 +404,44 @@ func (b *BGPPath) ComputeHash() string {
// CommunitiesString returns the formated communities // CommunitiesString returns the formated communities
func (b *BGPPath) CommunitiesString() string { func (b *BGPPath) CommunitiesString() string {
str := "" str := &strings.Builder{}
for _, com := range b.Communities {
str += types.CommunityStringForUint32(com) + " " for i, com := range b.Communities {
if i > 0 {
str.WriteByte(' ')
}
str.WriteString(types.CommunityStringForUint32(com))
} }
return strings.TrimRight(str, " ") return str.String()
} }
// ClusterListString returns the formated ClusterList // ClusterListString returns the formated ClusterList
func (b *BGPPath) ClusterListString() string { func (b *BGPPath) ClusterListString() string {
str := "" str := &strings.Builder{}
for _, cid := range b.ClusterList {
for i, cid := range b.ClusterList {
if i > 0 {
str.WriteByte(' ')
}
octes := convert.Uint32Byte(cid) octes := convert.Uint32Byte(cid)
str += fmt.Sprintf("%d.%d.%d.%d ", octes[0], octes[1], octes[2], octes[3])
fmt.Fprintf(str, "%d.%d.%d.%d", octes[0], octes[1], octes[2], octes[3])
} }
return strings.TrimRight(str, " ") return str.String()
} }
// LargeCommunitiesString returns the formated communities // LargeCommunitiesString returns the formated communities
func (b *BGPPath) LargeCommunitiesString() string { func (b *BGPPath) LargeCommunitiesString() string {
str := "" str := &strings.Builder{}
for _, com := range b.LargeCommunities {
str += com.String() + " " for i, com := range b.LargeCommunities {
if i > 0 {
str.WriteByte(' ')
}
str.WriteString(com.String())
} }
return strings.TrimRight(str, " ") return str.String()
} }
...@@ -311,9 +311,40 @@ func TestLength(t *testing.T) { ...@@ -311,9 +311,40 @@ func TestLength(t *testing.T) {
for _, test := range tests { for _, test := range tests {
calcLen := test.path.Length() calcLen := test.path.Length()
assert.Equal(t, test.expected, calcLen, test.name)
}
}
func TestBGPPathString(t *testing.T) {
tests := []struct {
input BGPPath
expectedPrint string
expectedString string
}{
{
input: BGPPath{
EBGP: true,
OriginatorID: 23,
ClusterList: []uint32{10, 20},
},
expectedString: "Local Pref: 0, Origin: Incomplete, AS Path: , BGP type: external, NEXT HOP: 0:0:0:0:0:0:0:0, MED: 0, Path ID: 0, Source: 0:0:0:0:0:0:0:0, Communities: [], LargeCommunities: [], OriginatorID: 0.0.0.23, ClusterList 0.0.0.10 0.0.0.20",
expectedPrint: ` Local Pref: 0
Origin: Incomplete
AS Path:
BGP type: external
NEXT HOP: 0:0:0:0:0:0:0:0
MED: 0
Path ID: 0
Source: 0:0:0:0:0:0:0:0
Communities: []
LargeCommunities: []
OriginatorID: 0.0.0.23
ClusterList 0.0.0.10 0.0.0.20
`,
},
}
if calcLen != test.expected { for _, test := range tests {
t.Errorf("Unexpected result for test %q: Expected: %d Got: %d", test.name, test.expected, calcLen) assert.Equal(t, test.expectedString, test.input.String())
} assert.Equal(t, test.expectedPrint, test.input.Print())
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment