coredns-ldap/handler_test.go

85 lines
1.8 KiB
Go
Raw Permalink Normal View History

2020-06-10 07:41:44 +00:00
package ldap_test
2020-06-10 02:54:01 +00:00
import (
"context"
"testing"
"github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/fall"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
2020-06-10 07:41:44 +00:00
. "github.com/xoe-labs/ldap/v0"
2020-06-10 02:54:01 +00:00
)
2020-06-10 08:03:59 +00:00
// nolint: gochecknoglobals
var ldapHandlerTestCases = []test.Case{
2020-06-10 02:54:01 +00:00
{
// Simple case
Qname: "a.example.org.", Qtype: dns.TypeA,
Answer: []dns.RR{
test.A("a.example.org." + defaultA),
},
},
}
// Create a new Ldap Plugin. Use the test.ErrorHandler as the next plugin.
func newTestLdapHandler() *Ldap {
2020-06-10 02:54:01 +00:00
ldap := New([]string{"example.org.", "www.example.org.", "example.org.", "sample.example.org."})
ldap.Zones.Z = newTestLdapHandlerZones()
2020-06-10 02:54:01 +00:00
ldap.Fall = fall.Zero
ldap.Next = test.ErrorHandler()
2020-06-10 07:41:44 +00:00
2020-06-10 02:54:01 +00:00
return ldap
}
func newTestLdapHandlerZones() map[string]*file.Zone {
2020-06-10 02:54:01 +00:00
Zone := file.NewZone("example.org.", "")
2020-06-10 08:03:59 +00:00
if err := Zone.Insert(SOA("example.org.")); err != nil {
panic("omg")
}
2020-06-10 07:41:44 +00:00
2020-06-10 03:11:21 +00:00
for _, rr := range []string{
2020-06-10 07:16:34 +00:00
"example.org. " + defaultA,
"a.example.org. " + defaultA,
} {
2020-06-10 02:54:01 +00:00
r, _ := dns.NewRR(rr)
2020-06-10 08:03:59 +00:00
if err := Zone.Insert(r); err != nil {
panic("omg")
}
2020-06-10 02:54:01 +00:00
}
2020-06-10 07:41:44 +00:00
2020-06-10 02:54:01 +00:00
zones := make(map[string]*file.Zone)
zones["example.org."] = Zone
2020-06-10 07:41:44 +00:00
2020-06-10 02:54:01 +00:00
return zones
}
func TestServeDNS(t *testing.T) {
ldap := newTestLdapHandler()
2020-06-10 07:41:44 +00:00
for i, tc := range ldapHandlerTestCases {
2020-06-10 02:54:01 +00:00
req := tc.Msg()
rec := dnstest.NewRecorder(&test.ResponseWriter{})
2020-06-10 07:41:44 +00:00
2020-06-10 02:54:01 +00:00
_, err := ldap.ServeDNS(context.Background(), rec, req)
if err != nil {
t.Errorf("Expected no error, got %v", err)
continue
}
2020-06-10 07:41:44 +00:00
2020-06-10 02:54:01 +00:00
resp := rec.Msg
if resp == nil {
t.Fatalf("Test %d, got nil message and no error for %q", i, req.Question[0].Name)
}
2020-06-10 07:41:44 +00:00
2020-06-10 02:54:01 +00:00
if err := test.SortAndCheck(resp, tc); err != nil {
t.Error(err)
}
}
}
const defaultA = " 3600 IN A 1.2.3.4"