| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func TestServeMux(t *testing.T) { |
| 10 |
mux := NewServeMux() |
| 11 |
noopHandler := func(ctx context.Context, w ResponseWriter, req *Msg) {} |
| 12 |
mux.Handle("grand.child.miek.nl.", HandlerFunc(noopHandler)) |
| 13 |
mux.Handle("child.miek.nl.", HandlerFunc(noopHandler)) |
| 14 |
mux.Handle("miek.nl.", HandlerFunc(noopHandler)) |
| 15 |
mux.Handle(".", HandlerFunc(noopHandler)) |
| 16 |
|
| 17 |
testcases := []struct { |
| 18 |
zone string |
| 19 |
typ uint16 |
| 20 |
exp string |
| 21 |
}{ |
| 22 |
{"child.miek.nl.", TypeDS, "miek.nl."}, |
| 23 |
{"grand.child.miek.nl.", TypeDS, "child.miek.nl."}, |
| 24 |
{"miek.nl.", TypeDS, "miek.nl."}, |
| 25 |
// |
| 26 |
{"child.miek.nl.", TypeA, "child.miek.nl."}, |
| 27 |
{"foo.child.miek.nl.", TypeA, "child.miek.nl."}, |
| 28 |
{"foo.miek.nl.", TypeA, "miek.nl."}, |
| 29 |
{"miek.nl.", TypeA, "miek.nl."}, |
| 30 |
{".", TypeA, "."}, |
| 31 |
{"bla.nl.", TypeA, ""}, |
| 32 |
} |
| 33 |
for _, tc := range testcases { |
| 34 |
t.Run(fmt.Sprintf("%s/%s", tc.zone, typeToString(tc.typ)), func(t *testing.T) { |
| 35 |
_, zone := mux.match(tc.zone, tc.typ, ClassINET) |
| 36 |
if zone != tc.exp { |
| 37 |
t.Errorf("expected %s for %d, got %s", tc.exp, tc.typ, zone) |
| 38 |
} |
| 39 |
}) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func BenchmarkServeMux(b *testing.B) { |
| 44 |
mux := NewServeMux() |
| 45 |
noopHandler := func(ctx context.Context, w ResponseWriter, req *Msg) {} |
| 46 |
mux.Handle("_udp.example.org.", HandlerFunc(noopHandler)) |
| 47 |
|
| 48 |
for b.Loop() { |
| 49 |
mux.match("_dns._udp.example.com.", TypeSRV, ClassINET) |
| 50 |
mux.match("miek.nl.", TypeSRV, ClassINET) |
| 51 |
} |
| 52 |
} |