aboutsummaryrefslogtreecommitdiff
path: root/plugin/transfer/select_test.go
blob: a064b00ca21b8266642aac09bb04b57c2b28ce2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package transfer

import (
	"context"
	"fmt"
	"testing"

	"github.com/coredns/coredns/plugin/pkg/dnstest"
	"github.com/coredns/coredns/plugin/test"

	"github.com/miekg/dns"
)

type (
	t1 struct{}
	t2 struct{}
)

func (t t1) Transfer(zone string, serial uint32) (<-chan []dns.RR, error) {
	const z = "example.org."
	if zone != z {
		return nil, ErrNotAuthoritative
	}
	return nil, fmt.Errorf(z)
}
func (t t2) Transfer(zone string, serial uint32) (<-chan []dns.RR, error) {
	const z = "sub.example.org."
	if zone != z {
		return nil, ErrNotAuthoritative
	}
	return nil, fmt.Errorf(z)
}

func TestZoneSelection(t *testing.T) {
	tr := &Transfer{
		Transferers: []Transferer{t1{}, t2{}},
		xfrs: []*xfr{
			{
				Zones: []string{"example.org."},
				to:    []string{"192.0.2.1"}, // RFC 5737 IP, no interface should have this address.
			},
			{
				Zones: []string{"sub.example.org."},
				to:    []string{"*"},
			},
		},
	}
	r := new(dns.Msg)
	r.SetAxfr("sub.example.org.")
	w := dnstest.NewRecorder(&test.ResponseWriter{TCP: true})
	_, err := tr.ServeDNS(context.TODO(), w, r)
	if err == nil {
		t.Fatal("Expected error, got nil")
	}
	if x := err.Error(); x != "sub.example.org." {
		t.Errorf("Expected transfer for zone %s, got %s", "sub.example.org", x)
	}
}