mirror of
https://github.com/coredns/coredns.git
synced 2026-07-15 04:00:11 -04:00
plugin/hosts: add wildcard support (#8185)
* plugin/hosts: add wildcard support for owner names Signed-off-by: youknowforsearch <amirhebrahimzader@gmail.com> * plugin/hosts: document wildcard owner name support Signed-off-by: youknowforsearch <amirhebrahimzader@gmail.com> * plugin/hosts: remove unused lookupStaticHost Signed-off-by: Amirhossein Ebrahimzade <amirhossein.e@smartech.ir> --------- Signed-off-by: youknowforsearch <amirhebrahimzader@gmail.com> Signed-off-by: Amirhossein Ebrahimzade <amirhossein.e@smartech.ir> Co-authored-by: Amirhossein Ebrahimzade <amirhossein.e@smartech.ir>
This commit is contained in:
committed by
GitHub
parent
e45ad5f87a
commit
6805f6f8a0
58
plugin/hosts/wildcard_test.go
Normal file
58
plugin/hosts/wildcard_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReplaceWithAsteriskLabel(t *testing.T) {
|
||||
tests := []struct {
|
||||
in, out string
|
||||
}{
|
||||
{".", ""},
|
||||
{"example.com.", "*.com."},
|
||||
{"foo.example.com.", "*.example.com."},
|
||||
{"bar.intern.example.com.", "*.intern.example.com."},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
got := replaceWithAsteriskLabel(tc.in)
|
||||
if got != tc.out {
|
||||
t.Errorf("replaceWithAsteriskLabel(%q) = %q, want %q", tc.in, got, tc.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplaceWithAsteriskLabelApex(t *testing.T) {
|
||||
// Apex names produce a *.parent pattern but must not match *.zone wildcards.
|
||||
if got := replaceWithAsteriskLabel("example.com."); got != "*.com." {
|
||||
t.Fatalf("replaceWithAsteriskLabel(example.com.) = %q, want *.com.", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookupWildcardHost(t *testing.T) {
|
||||
const hosts = `
|
||||
127.0.0.53 *.example.org
|
||||
127.0.1.52 *.intern.example.org
|
||||
127.0.0.54 foo.example.org
|
||||
192.168.33.10 *.example.com
|
||||
192.168.33.11 a.example.com
|
||||
192.168.33.12 b.example.com
|
||||
`
|
||||
|
||||
h := testHostsfile(hosts)
|
||||
|
||||
tests := []staticHostEntry{
|
||||
{"foo.example.org.", []string{"127.0.0.54"}, []string{}},
|
||||
{"bar.example.org.", []string{"127.0.0.53"}, []string{}},
|
||||
{"bar.intern.example.org.", []string{"127.0.1.52"}, []string{}},
|
||||
{"a.example.com.", []string{"192.168.33.11"}, []string{}},
|
||||
{"b.example.com.", []string{"192.168.33.12"}, []string{}},
|
||||
{"c.example.com.", []string{"192.168.33.10"}, []string{}},
|
||||
{"example.com.", []string{}, []string{}},
|
||||
{"deep.foo.example.org.", []string{}, []string{}},
|
||||
}
|
||||
|
||||
for _, ent := range tests {
|
||||
testStaticHost(t, ent, h)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user