2017-04-18 11:10:49 -04:00
|
|
|
package dnsserver
|
|
|
|
|
|
|
|
|
|
import (
|
2018-04-20 11:01:06 +01:00
|
|
|
"context"
|
2017-04-18 11:10:49 -04:00
|
|
|
"testing"
|
2017-10-21 09:30:59 +01:00
|
|
|
|
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
|
|
|
"github.com/coredns/coredns/plugin/test"
|
|
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2017-04-18 11:10:49 -04:00
|
|
|
)
|
|
|
|
|
|
2017-10-21 09:30:59 +01:00
|
|
|
type testPlugin struct{}
|
|
|
|
|
|
|
|
|
|
func (tp testPlugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (tp testPlugin) Name() string { return "testplugin" }
|
|
|
|
|
|
2017-10-24 10:16:03 +01:00
|
|
|
func testConfig(transport string, p plugin.Handler) *Config {
|
2017-10-21 09:30:59 +01:00
|
|
|
c := &Config{
|
2018-02-14 14:19:32 -05:00
|
|
|
Zone: "example.com.",
|
|
|
|
|
Transport: transport,
|
|
|
|
|
ListenHosts: []string{"127.0.0.1"},
|
|
|
|
|
Port: "53",
|
|
|
|
|
Debug: false,
|
2017-10-21 09:30:59 +01:00
|
|
|
}
|
|
|
|
|
|
2017-10-24 10:16:03 +01:00
|
|
|
c.AddPlugin(func(next plugin.Handler) plugin.Handler { return p })
|
2017-10-21 09:30:59 +01:00
|
|
|
return c
|
2017-04-18 11:10:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewServer(t *testing.T) {
|
2017-10-24 10:16:03 +01:00
|
|
|
_, err := NewServer("127.0.0.1:53", []*Config{testConfig("dns", testPlugin{})})
|
2017-04-18 11:10:49 -04:00
|
|
|
if err != nil {
|
2017-10-24 10:16:03 +01:00
|
|
|
t.Errorf("Expected no error for NewServer, got %s", err)
|
2017-04-18 11:10:49 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-24 10:16:03 +01:00
|
|
|
_, err = NewServergRPC("127.0.0.1:53", []*Config{testConfig("grpc", testPlugin{})})
|
2017-04-18 11:10:49 -04:00
|
|
|
if err != nil {
|
2017-10-24 10:16:03 +01:00
|
|
|
t.Errorf("Expected no error for NewServergRPC, got %s", err)
|
2017-04-18 11:10:49 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-24 10:16:03 +01:00
|
|
|
_, err = NewServerTLS("127.0.0.1:53", []*Config{testConfig("tls", testPlugin{})})
|
2017-04-18 11:10:49 -04:00
|
|
|
if err != nil {
|
2017-10-24 10:16:03 +01:00
|
|
|
t.Errorf("Expected no error for NewServerTLS, got %s", err)
|
2017-04-18 11:10:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
2017-10-21 09:30:59 +01:00
|
|
|
|
|
|
|
|
func BenchmarkCoreServeDNS(b *testing.B) {
|
2017-10-24 10:16:03 +01:00
|
|
|
s, err := NewServer("127.0.0.1:53", []*Config{testConfig("dns", testPlugin{})})
|
2017-10-21 09:30:59 +01:00
|
|
|
if err != nil {
|
2017-10-24 10:16:03 +01:00
|
|
|
b.Errorf("Expected no error for NewServer, got %s", err)
|
2017-10-21 09:30:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
w := &test.ResponseWriter{}
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
|
m.SetQuestion("aaa.example.com.", dns.TypeTXT)
|
|
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
s.ServeDNS(ctx, w, m)
|
|
|
|
|
}
|
|
|
|
|
}
|