Skip to content
Snippets Groups Projects
Commit 5dab1a2c authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

prefix list now with matching strategy, next step in implementing terms

parent 2908db94
No related branches found
No related tags found
No related merge requests found
......@@ -6,11 +6,12 @@ import (
)
type From struct {
prefixLists []*PrefixList
prefixLists []*PrefixList
routeFilters []*RouteFilter
}
func (f *From) Matches(p net.Prefix, pa *route.Path) bool {
return f.matchesAnyPrefixList(p)
return f.matchesAnyPrefixList(p) || f.machtchesAnyRouteFilter(p)
}
func (t *From) matchesAnyPrefixList(p net.Prefix) bool {
......@@ -22,3 +23,13 @@ func (t *From) matchesAnyPrefixList(p net.Prefix) bool {
return false
}
func (t *From) machtchesAnyRouteFilter(p net.Prefix) bool {
for _, l := range t.routeFilters {
if l.Matches(p) {
return true
}
}
return false
}
package filter
import (
"testing"
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
"github.com/stretchr/testify/assert"
)
func TestMatches(t *testing.T) {
tests := []struct {
name string
prefix net.Prefix
prefixLists []*PrefixList
routeFilters []*RouteFilter
expected bool
}{
{
name: "one prefix matches in prefix list, no route filters set",
prefix: net.NewPfx(strAddr("127.0.0.1"), 8),
prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("127.0.0.1"), 8)),
},
routeFilters: []*RouteFilter{},
expected: true,
},
{
name: "one prefix in prefix list and no match, no route filters set",
prefix: net.NewPfx(strAddr("127.0.0.1"), 8),
prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(0, 32)),
},
routeFilters: []*RouteFilter{},
expected: false,
},
{
name: "one prefix of 2 matches in prefix list, no route filters set",
prefix: net.NewPfx(strAddr("127.0.0.1"), 8),
prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("10.0.0.0"), 8)),
NewPrefixList(net.NewPfx(strAddr("127.0.0.1"), 8)),
},
routeFilters: []*RouteFilter{},
expected: true,
},
{
name: "no prefixes in prefix list, only route filter matches",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
prefixLists: []*PrefixList{},
routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
},
expected: true,
},
{
name: "no prefixes in prefix list, one route filter matches",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
prefixLists: []*PrefixList{},
routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("8.0.0.0"), 8), Longer()),
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
},
expected: true,
},
{
name: "no prefixes in prefix list, one of many route filters matches",
prefix: net.NewPfx(strAddr("127.0.0.1"), 8),
prefixLists: []*PrefixList{},
routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
},
expected: false,
},
{
name: "no match in prefix list, no macht in route filter",
prefix: net.NewPfx(strAddr("9.9.9.0"), 24),
prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("8.0.0.0"), 8)),
},
routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
},
expected: false,
},
{
name: "one prefix in prefixlist, one route fitler, only prefix list matches",
prefix: net.NewPfx(strAddr("8.8.8.0"), 24),
prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("8.0.0.0"), 8)),
},
routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
},
expected: true,
},
{
name: "one prefix in prefixlist, one route fitler, only route filter matches",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("8.0.0.0"), 8)),
},
routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
},
expected: true,
},
}
for _, test := range tests {
t.Run(test.name, func(te *testing.T) {
f := &From{
prefixLists: test.prefixLists,
routeFilters: test.routeFilters,
}
assert.Equal(te, test.expected, f.Matches(test.prefix, &route.Path{}))
})
}
}
......@@ -4,21 +4,31 @@ import "github.com/bio-routing/bio-rd/net"
type PrefixList struct {
allowed []net.Prefix
matcher PrefixMatcher
}
func NewPrefixList(pfxs ...net.Prefix) *PrefixList {
l := &PrefixList{
allowed: pfxs,
matcher: Exact(),
}
return l
}
func NewPrefixListWithMatcher(matcher PrefixMatcher, pfxs ...net.Prefix) *PrefixList {
l := &PrefixList{
allowed: pfxs,
matcher: matcher,
}
return l
}
func (l *PrefixList) Matches(p net.Prefix) bool {
for _, a := range l.allowed {
if !a.Contains(p) {
return false
if a.Equal(p) {
return true
}
}
return true
return false
}
package filter
import "github.com/bio-routing/bio-rd/net"
type PrefixMatcher func(pattern, prefix net.Prefix) bool
func InRange(min, max uint8) PrefixMatcher {
return func(pattern, prefix net.Prefix) bool {
contains := pattern.Equal(prefix) || pattern.Contains(prefix)
return contains && prefix.Pfxlen() >= min && prefix.Pfxlen() <= max
}
}
func Exact() PrefixMatcher {
return func(pattern, prefix net.Prefix) bool {
return pattern.Equal(prefix)
}
}
func OrLonger() PrefixMatcher {
return func(pattern, prefix net.Prefix) bool {
return pattern.Equal(prefix) || pattern.Contains(prefix)
}
}
func Longer() PrefixMatcher {
return func(pattern, prefix net.Prefix) bool {
return pattern.Contains(prefix) && prefix.Pfxlen() > pattern.Pfxlen()
}
}
......@@ -9,33 +9,6 @@ type RouteFilter struct {
matcher PrefixMatcher
}
type PrefixMatcher func(pattern, prefix net.Prefix) bool
func InRange(min, max uint8) PrefixMatcher {
return func(pattern, prefix net.Prefix) bool {
contains := pattern.Equal(prefix) || pattern.Contains(prefix)
return contains && prefix.Pfxlen() >= min && prefix.Pfxlen() <= max
}
}
func Exact() PrefixMatcher {
return func(pattern, prefix net.Prefix) bool {
return pattern.Equal(prefix)
}
}
func OrLonger() PrefixMatcher {
return func(pattern, prefix net.Prefix) bool {
return pattern.Equal(prefix) || pattern.Contains(prefix)
}
}
func Longer() PrefixMatcher {
return func(pattern, prefix net.Prefix) bool {
return pattern.Contains(prefix) && prefix.Pfxlen() > pattern.Pfxlen()
}
}
func NewRouteFilter(pattern net.Prefix, matcher PrefixMatcher) *RouteFilter {
return &RouteFilter{
pattern: pattern,
......
......@@ -46,8 +46,8 @@ func TestProcess(t *testing.T) {
path: &route.Path{},
from: []*From{
{
[]*PrefixList{
NewPrefixList(net.NewPfx(0, 0)),
prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("100.64.0.1"), 8)),
},
},
},
......@@ -63,7 +63,7 @@ func TestProcess(t *testing.T) {
path: &route.Path{},
from: []*From{
{
[]*PrefixList{
prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(0, 32)),
},
},
......@@ -80,8 +80,8 @@ func TestProcess(t *testing.T) {
path: &route.Path{},
from: []*From{
{
[]*PrefixList{
NewPrefixList(net.NewPfx(0, 0)),
prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("100.64.0.1"), 8)),
},
},
},
......@@ -97,8 +97,8 @@ func TestProcess(t *testing.T) {
path: &route.Path{},
from: []*From{
{
[]*PrefixList{
NewPrefixList(net.NewPfx(0, 0)),
prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("100.64.0.1"), 8)),
},
},
},
......@@ -115,9 +115,9 @@ func TestProcess(t *testing.T) {
path: &route.Path{},
from: []*From{
{
[]*PrefixList{
NewPrefixList(net.NewPfx(0, 32)),
NewPrefixList(net.NewPfx(0, 0)),
prefixLists: []*PrefixList{
NewPrefixListWithMatcher(Exact(), net.NewPfx(0, 32)),
NewPrefixList(net.NewPfx(strAddr("100.64.0.1"), 8)),
},
},
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment