Verified Commit 38f0e00e authored by shelikhoo's avatar shelikhoo
Browse files

Add Domain Name Matcher

Design difference from original vision: Skipped FQDN step to make it more generalized
#28651 (comment 2787394)
parent 5578b4dd
package namematcher
import "strings"
func NewNameMatcher(rule string) NameMatcher {
return NameMatcher{suffix: strings.TrimPrefix(rule, "^"), exact: strings.HasPrefix(rule, "^")}
}
type NameMatcher struct {
exact bool
suffix string
}
func (m *NameMatcher) IsSupersetOf(matcher NameMatcher) bool {
if m.exact {
return matcher.exact && m.suffix == matcher.suffix
}
return strings.HasSuffix(matcher.suffix, m.suffix)
}
func (m *NameMatcher) IsMember(s string) bool {
if m.exact {
return s == m.suffix
}
return strings.HasSuffix(s, m.suffix)
}
package namematcher
import "testing"
import . "github.com/smartystreets/goconvey/convey"
func TestMatchMember(t *testing.T) {
testingVector := []struct {
matcher string
target string
expects bool
}{
{matcher: "", target: "", expects: true},
{matcher: "^snowflake.torproject.net", target: "snowflake.torproject.net", expects: true},
{matcher: "^snowflake.torproject.net", target: "faketorproject.net", expects: false},
{matcher: "snowflake.torproject.net", target: "faketorproject.net", expects: false},
{matcher: "snowflake.torproject.net", target: "snowflake.torproject.net", expects: true},
{matcher: "snowflake.torproject.net", target: "imaginary-01-snowflake.torproject.net", expects: true},
{matcher: "snowflake.torproject.net", target: "imaginary-aaa-snowflake.torproject.net", expects: true},
{matcher: "snowflake.torproject.net", target: "imaginary-aaa-snowflake.faketorproject.net", expects: false},
}
for _, v := range testingVector {
t.Run(v.matcher+"<>"+v.target, func(t *testing.T) {
Convey("test", t, func() {
matcher := NewNameMatcher(v.matcher)
So(matcher.IsMember(v.target), ShouldEqual, v.expects)
})
})
}
}
func TestMatchSubset(t *testing.T) {
testingVector := []struct {
matcher string
target string
expects bool
}{
{matcher: "", target: "", expects: true},
{matcher: "^snowflake.torproject.net", target: "^snowflake.torproject.net", expects: true},
{matcher: "snowflake.torproject.net", target: "^snowflake.torproject.net", expects: true},
{matcher: "snowflake.torproject.net", target: "snowflake.torproject.net", expects: true},
{matcher: "snowflake.torproject.net", target: "testing-snowflake.torproject.net", expects: true},
{matcher: "snowflake.torproject.net", target: "^testing-snowflake.torproject.net", expects: true},
{matcher: "snowflake.torproject.net", target: "", expects: false},
}
for _, v := range testingVector {
t.Run(v.matcher+"<>"+v.target, func(t *testing.T) {
Convey("test", t, func() {
matcher := NewNameMatcher(v.matcher)
target := NewNameMatcher(v.target)
So(matcher.IsSupersetOf(target), ShouldEqual, v.expects)
})
})
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment