| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | package route53
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	"errors"
 | 
					
						
							|  |  |  | 	"reflect"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	"testing"
 | 
					
						
							| 
									
										
										
										
											2019-08-03 18:07:28 -07:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							| 
									
										
										
										
											2018-09-25 11:41:05 -07:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/fall"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2018-09-25 11:41:05 -07:00
										 |  |  | 	crequest "github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/aws/aws-sdk-go/aws"
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	"github.com/aws/aws-sdk-go/aws/request"
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	"github.com/aws/aws-sdk-go/service/route53"
 | 
					
						
							|  |  |  | 	"github.com/aws/aws-sdk-go/service/route53/route53iface"
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | type fakeRoute53 struct {
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	route53iface.Route53API
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | func (fakeRoute53) ListHostedZonesByNameWithContext(_ aws.Context, input *route53.ListHostedZonesByNameInput, _ ...request.Option) (*route53.ListHostedZonesByNameOutput, error) {
 | 
					
						
							|  |  |  | 	return nil, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (fakeRoute53) ListResourceRecordSetsPagesWithContext(_ aws.Context, in *route53.ListResourceRecordSetsInput, fn func(*route53.ListResourceRecordSetsOutput, bool) bool, _ ...request.Option) error {
 | 
					
						
							|  |  |  | 	if aws.StringValue(in.HostedZoneId) == "0987654321" {
 | 
					
						
							|  |  |  | 		return errors.New("bad. zone is bad")
 | 
					
						
							| 
									
										
										
										
											2018-03-13 00:06:50 -07:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 	rrsResponse := map[string][]*route53.ResourceRecordSet{}
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	for _, r := range []struct {
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 		rType, name, value, hostedZoneID string
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	}{
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 		{"A", "example.org.", "1.2.3.4", "1234567890"},
 | 
					
						
							| 
									
										
										
										
											2019-03-13 11:46:30 -07:00
										 |  |  | 		{"A", "www.example.org", "1.2.3.4", "1234567890"},
 | 
					
						
							| 
									
										
										
										
											2020-07-30 22:51:14 -07:00
										 |  |  | 		{"CNAME", `\052.www.example.org`, "www.example.org", "1234567890"},
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 		{"AAAA", "example.org.", "2001:db8:85a3::8a2e:370:7334", "1234567890"},
 | 
					
						
							|  |  |  | 		{"CNAME", "sample.example.org.", "example.org", "1234567890"},
 | 
					
						
							|  |  |  | 		{"PTR", "example.org.", "ptr.example.org.", "1234567890"},
 | 
					
						
							|  |  |  | 		{"SOA", "org.", "ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400", "1234567890"},
 | 
					
						
							|  |  |  | 		{"NS", "com.", "ns-1536.awsdns-00.co.uk.", "1234567890"},
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 		{"A", "split-example.gov.", "1.2.3.4", "1234567890"},
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		// Unsupported type should be ignored.
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 		{"YOLO", "swag.", "foobar", "1234567890"},
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 		// Hosted zone with the same name, but a different id.
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 		{"A", "other-example.org.", "3.5.7.9", "1357986420"},
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 		{"A", "split-example.org.", "1.2.3.4", "1357986420"},
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 		{"SOA", "org.", "ns-15.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400", "1357986420"},
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 		// Hosted zone without SOA.
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	} {
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 		rrs, ok := rrsResponse[r.hostedZoneID]
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			rrs = make([]*route53.ResourceRecordSet, 0)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		rrs = append(rrs, &route53.ResourceRecordSet{Type: aws.String(r.rType),
 | 
					
						
							|  |  |  | 			Name: aws.String(r.name),
 | 
					
						
							|  |  |  | 			ResourceRecords: []*route53.ResourceRecord{
 | 
					
						
							|  |  |  | 				{
 | 
					
						
							|  |  |  | 					Value: aws.String(r.value),
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 				},
 | 
					
						
							|  |  |  | 			},
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			TTL: aws.Int64(300),
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 		rrsResponse[r.hostedZoneID] = rrs
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	if ok := fn(&route53.ListResourceRecordSetsOutput{
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 		ResourceRecordSets: rrsResponse[aws.StringValue(in.HostedZoneId)],
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	}, true); !ok {
 | 
					
						
							|  |  |  | 		return errors.New("paging function return false")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestRoute53(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2022-01-05 21:20:32 +08:00
										 |  |  | 	ctx, cancel := context.WithCancel(context.Background())
 | 
					
						
							|  |  |  | 	defer cancel()
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 07:56:12 +00:00
										 |  |  | 	r, err := New(ctx, fakeRoute53{}, map[string][]string{"bad.": {"0987654321"}}, time.Minute)
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 		t.Fatalf("Failed to create route53: %v", err)
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	if err = r.Run(ctx); err == nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected errors for zone bad.")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 07:56:12 +00:00
										 |  |  | 	r, err = New(ctx, fakeRoute53{}, map[string][]string{"org.": {"1357986420", "1234567890"}, "gov.": {"Z098765432", "1234567890"}}, 90*time.Second)
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 		t.Fatalf("Failed to create route53: %v", err)
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-09-25 11:41:05 -07:00
										 |  |  | 	r.Fall = fall.Zero
 | 
					
						
							|  |  |  | 	r.Fall.SetZonesFromArgs([]string{"gov."})
 | 
					
						
							|  |  |  | 	r.Next = test.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 		state := crequest.Request{W: w, Req: r}
 | 
					
						
							|  |  |  | 		qname := state.Name()
 | 
					
						
							|  |  |  | 		m := new(dns.Msg)
 | 
					
						
							|  |  |  | 		rcode := dns.RcodeServerFailure
 | 
					
						
							|  |  |  | 		if qname == "example.gov." {
 | 
					
						
							|  |  |  | 			m.SetReply(r)
 | 
					
						
							|  |  |  | 			rr, err := dns.NewRR("example.gov.  300 IN  A   2.4.6.8")
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				t.Fatalf("Failed to create Resource Record: %v", err)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			m.Answer = []dns.RR{rr}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-30 17:05:08 +01:00
										 |  |  | 			m.Authoritative = true
 | 
					
						
							| 
									
										
										
										
											2018-09-25 11:41:05 -07:00
										 |  |  | 			rcode = dns.RcodeSuccess
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		m.SetRcode(r, rcode)
 | 
					
						
							|  |  |  | 		w.WriteMsg(m)
 | 
					
						
							|  |  |  | 		return rcode, nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	err = r.Run(ctx)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:39:01 +02:00
										 |  |  | 		t.Fatalf("Failed to initialize route53: %v", err)
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		qname        string
 | 
					
						
							|  |  |  | 		qtype        uint16
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 		wantRetCode  int
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		wantAnswer   []string // ownernames for the records in the additional section.
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 		wantMsgRCode int
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		wantNS       []string
 | 
					
						
							|  |  |  | 		expectedErr  error
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	}{
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		// 0. example.org A found - success.
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | 			qname:      "example.org",
 | 
					
						
							|  |  |  | 			qtype:      dns.TypeA,
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			wantAnswer: []string{"example.org.	300	IN	A	1.2.3.4"},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// 1. example.org AAAA found - success.
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | 			qname:      "example.org",
 | 
					
						
							|  |  |  | 			qtype:      dns.TypeAAAA,
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			wantAnswer: []string{"example.org.	300	IN	AAAA	2001:db8:85a3::8a2e:370:7334"},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// 2. exampled.org PTR found - success.
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | 			qname:      "example.org",
 | 
					
						
							|  |  |  | 			qtype:      dns.TypePTR,
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			wantAnswer: []string{"example.org.	300	IN	PTR	ptr.example.org."},
 | 
					
						
							| 
									
										
										
										
											2018-03-13 00:06:50 -07:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		// 3. sample.example.org points to example.org CNAME.
 | 
					
						
							|  |  |  | 		// Query must return both CNAME and A recs.
 | 
					
						
							| 
									
										
										
										
											2018-03-13 00:06:50 -07:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 			qname: "sample.example.org",
 | 
					
						
							|  |  |  | 			qtype: dns.TypeA,
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			wantAnswer: []string{
 | 
					
						
							|  |  |  | 				"sample.example.org.	300	IN	CNAME	example.org.",
 | 
					
						
							|  |  |  | 				"example.org.	300	IN	A	1.2.3.4",
 | 
					
						
							|  |  |  | 			},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// 4. Explicit CNAME query for sample.example.org.
 | 
					
						
							|  |  |  | 		// Query must return just CNAME.
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | 			qname:      "sample.example.org",
 | 
					
						
							|  |  |  | 			qtype:      dns.TypeCNAME,
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			wantAnswer: []string{"sample.example.org.	300	IN	CNAME	example.org."},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// 5. Explicit SOA query for example.org.
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | 			qname:  "example.org",
 | 
					
						
							|  |  |  | 			qtype:  dns.TypeNS,
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			wantNS: []string{"org.	300	IN	SOA	ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"},
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-05-11 16:51:02 +02:00
										 |  |  | 		// 6. AAAA query for split-example.org must return NODATA.
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 		{
 | 
					
						
							|  |  |  | 			qname:       "split-example.gov",
 | 
					
						
							|  |  |  | 			qtype:       dns.TypeAAAA,
 | 
					
						
							|  |  |  | 			wantRetCode: dns.RcodeSuccess,
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | 			wantNS:      []string{"org.	300	IN	SOA	ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"},
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-05-11 16:51:02 +02:00
										 |  |  | 		// 7. Zone not configured.
 | 
					
						
							| 
									
										
										
										
											2018-03-13 15:10:07 -07:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			qname:        "badexample.com",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeA,
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 			wantRetCode:  dns.RcodeServerFailure,
 | 
					
						
							|  |  |  | 			wantMsgRCode: dns.RcodeServerFailure,
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-05-11 16:51:02 +02:00
										 |  |  | 		// 8. No record found. Return SOA record.
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 		{
 | 
					
						
							|  |  |  | 			qname:        "bad.org",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeA,
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 			wantRetCode:  dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			wantMsgRCode: dns.RcodeNameError,
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | 			wantNS:       []string{"org.	300	IN	SOA	ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"},
 | 
					
						
							| 
									
										
										
										
											2018-03-13 15:10:07 -07:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-05-11 16:51:02 +02:00
										 |  |  | 		// 9. No record found. Fallthrough.
 | 
					
						
							| 
									
										
										
										
											2018-09-25 11:41:05 -07:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | 			qname:      "example.gov",
 | 
					
						
							|  |  |  | 			qtype:      dns.TypeA,
 | 
					
						
							| 
									
										
										
										
											2018-09-25 11:41:05 -07:00
										 |  |  | 			wantAnswer: []string{"example.gov.	300	IN	A	2.4.6.8"},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-05-11 16:51:02 +02:00
										 |  |  | 		// 10. other-zone.example.org is stored in a different hosted zone. success
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | 			qname:      "other-example.org",
 | 
					
						
							|  |  |  | 			qtype:      dns.TypeA,
 | 
					
						
							| 
									
										
										
										
											2018-10-10 10:55:54 -07:00
										 |  |  | 			wantAnswer: []string{"other-example.org.	300	IN	A	3.5.7.9"},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-05-11 16:51:02 +02:00
										 |  |  | 		// 11. split-example.org only has A record. Expect NODATA.
 | 
					
						
							| 
									
										
										
										
											2019-03-13 11:46:30 -07:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | 			qname:  "split-example.org",
 | 
					
						
							|  |  |  | 			qtype:  dns.TypeAAAA,
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 			wantNS: []string{"org.	300	IN	SOA	ns-15.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-05-11 16:51:02 +02:00
										 |  |  | 		// 12. *.www.example.org is a wildcard CNAME to www.example.org.
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 		{
 | 
					
						
							|  |  |  | 			qname: "a.www.example.org",
 | 
					
						
							|  |  |  | 			qtype: dns.TypeA,
 | 
					
						
							| 
									
										
										
										
											2019-03-13 11:46:30 -07:00
										 |  |  | 			wantAnswer: []string{
 | 
					
						
							|  |  |  | 				"a.www.example.org.	300	IN	CNAME	www.example.org.",
 | 
					
						
							|  |  |  | 				"www.example.org.	300	IN	A	1.2.3.4",
 | 
					
						
							|  |  |  | 			},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 	for ti, tc := range tests {
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		req := new(dns.Msg)
 | 
					
						
							|  |  |  | 		req.SetQuestion(dns.Fqdn(tc.qname), tc.qtype)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
					
						
							|  |  |  | 		code, err := r.ServeDNS(ctx, rec, req)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err != tc.expectedErr {
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 			t.Fatalf("Test %d: Expected error %v, but got %v", ti, tc.expectedErr, err)
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2023-08-14 21:14:09 +08:00
										 |  |  | 		if code != tc.wantRetCode {
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 			t.Fatalf("Test %d: Expected returned status code %s, but got %s", ti, dns.RcodeToString[tc.wantRetCode], dns.RcodeToString[code])
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if tc.wantMsgRCode != rec.Msg.Rcode {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Unexpected msg status code. Want: %s, got: %s", ti, dns.RcodeToString[tc.wantMsgRCode], dns.RcodeToString[rec.Msg.Rcode])
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if len(tc.wantAnswer) != len(rec.Msg.Answer) {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Unexpected number of Answers. Want: %d, got: %d", ti, len(tc.wantAnswer), len(rec.Msg.Answer))
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			for i, gotAnswer := range rec.Msg.Answer {
 | 
					
						
							|  |  |  | 				if gotAnswer.String() != tc.wantAnswer[i] {
 | 
					
						
							|  |  |  | 					t.Errorf("Test %d: Unexpected answer.\nWant:\n\t%s\nGot:\n\t%s", ti, tc.wantAnswer[i], gotAnswer)
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if len(tc.wantNS) != len(rec.Msg.Ns) {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Unexpected NS number. Want: %d, got: %d", ti, len(tc.wantNS), len(rec.Msg.Ns))
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			for i, ns := range rec.Msg.Ns {
 | 
					
						
							|  |  |  | 				got, ok := ns.(*dns.SOA)
 | 
					
						
							|  |  |  | 				if !ok {
 | 
					
						
							|  |  |  | 					t.Errorf("Test %d: Unexpected NS type. Want: SOA, got: %v", ti, reflect.TypeOf(got))
 | 
					
						
							| 
									
										
										
										
											2018-03-13 00:06:50 -07:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2018-09-17 11:19:07 -07:00
										 |  |  | 				if got.String() != tc.wantNS[i] {
 | 
					
						
							| 
									
										
										
										
											2019-03-31 10:12:33 -07:00
										 |  |  | 					t.Errorf("Test %d: Unexpected NS.\nWant: %v\nGot: %v", ti, tc.wantNS[i], got)
 | 
					
						
							| 
									
										
										
										
											2018-01-15 09:59:29 -08:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2019-03-13 11:46:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestMaybeUnescape(t *testing.T) {
 | 
					
						
							|  |  |  | 	for ti, tc := range []struct {
 | 
					
						
							|  |  |  | 		escaped, want string
 | 
					
						
							|  |  |  | 		wantErr       error
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		// 0. empty string is fine.
 | 
					
						
							|  |  |  | 		{escaped: "", want: ""},
 | 
					
						
							|  |  |  | 		// 1. non-escaped sequence.
 | 
					
						
							|  |  |  | 		{escaped: "example.com.", want: "example.com."},
 | 
					
						
							|  |  |  | 		// 2. escaped `*` as first label - OK.
 | 
					
						
							| 
									
										
										
										
											2020-07-30 22:51:14 -07:00
										 |  |  | 		{escaped: `\052.example.com`, want: "*.example.com"},
 | 
					
						
							| 
									
										
										
										
											2019-03-13 11:46:30 -07:00
										 |  |  | 		// 3. Escaped dot, 'a' and a hyphen. No idea why but we'll allow it.
 | 
					
						
							| 
									
										
										
										
											2020-07-30 22:51:14 -07:00
										 |  |  | 		{escaped: `weird\055ex\141mple\056com\056\056`, want: "weird-example.com.."},
 | 
					
						
							| 
									
										
										
										
											2019-03-13 11:46:30 -07:00
										 |  |  | 		// 4. escaped `*` in the middle - NOT OK.
 | 
					
						
							| 
									
										
										
										
											2020-07-30 22:51:14 -07:00
										 |  |  | 		{escaped: `e\052ample.com`, wantErr: errors.New("`*' only supported as wildcard (leftmost label)")},
 | 
					
						
							| 
									
										
										
										
											2019-03-13 11:46:30 -07:00
										 |  |  | 		// 5. Invalid character.
 | 
					
						
							| 
									
										
										
										
											2020-07-30 22:51:14 -07:00
										 |  |  | 		{escaped: `\000.example.com`, wantErr: errors.New(`invalid character: \000`)},
 | 
					
						
							| 
									
										
										
										
											2019-03-13 11:46:30 -07:00
										 |  |  | 		// 6. Invalid escape sequence in the middle.
 | 
					
						
							| 
									
										
										
										
											2020-07-30 22:51:14 -07:00
										 |  |  | 		{escaped: `example\0com`, wantErr: errors.New(`invalid escape sequence: '\0co'`)},
 | 
					
						
							| 
									
										
										
										
											2019-03-13 11:46:30 -07:00
										 |  |  | 		// 7. Invalid escape sequence at the end.
 | 
					
						
							| 
									
										
										
										
											2020-07-30 22:51:14 -07:00
										 |  |  | 		{escaped: `example.com\0`, wantErr: errors.New(`invalid escape sequence: '\0'`)},
 | 
					
						
							| 
									
										
										
										
											2019-03-13 11:46:30 -07:00
										 |  |  | 	} {
 | 
					
						
							|  |  |  | 		got, gotErr := maybeUnescape(tc.escaped)
 | 
					
						
							|  |  |  | 		if tc.wantErr != gotErr && !reflect.DeepEqual(tc.wantErr, gotErr) {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d: Expected error: `%v', but got: `%v'", ti, tc.wantErr, gotErr)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if tc.want != got {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected unescaped: `%s', but got: `%s'", ti, tc.want, got)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |