2021-05-28 13:02:59 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2024-02-08 20:39:18 +03:00
|
|
|
"slices"
|
2021-05-28 13:02:59 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAnyNameMatches(t *testing.T) {
|
|
|
|
dnsNames := []string{"host1", "*.host2", "1.2.3.4"}
|
2023-02-21 16:38:22 +03:00
|
|
|
slices.Sort(dnsNames)
|
2021-05-28 13:02:59 +03:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
dnsName string
|
|
|
|
want bool
|
|
|
|
}{{
|
|
|
|
name: "match",
|
|
|
|
dnsName: "host1",
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
name: "match",
|
|
|
|
dnsName: "a.host2",
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
name: "match",
|
|
|
|
dnsName: "b.a.host2",
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
name: "match",
|
|
|
|
dnsName: "1.2.3.4",
|
|
|
|
want: true,
|
2023-02-21 16:52:33 +03:00
|
|
|
}, {
|
|
|
|
name: "mismatch_bad_ip",
|
|
|
|
dnsName: "1.2.3.256",
|
|
|
|
want: false,
|
2021-05-28 13:02:59 +03:00
|
|
|
}, {
|
|
|
|
name: "mismatch",
|
|
|
|
dnsName: "host2",
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "mismatch",
|
|
|
|
dnsName: "",
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "mismatch",
|
|
|
|
dnsName: "*.host2",
|
|
|
|
want: false,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
assert.Equal(t, tc.want, anyNameMatches(dnsNames, tc.dnsName))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|