<commit_before>package route53

import (
	"context"
	"errors"
	"reflect"
	"testing"
	"time"

	"github.com/coredns/coredns/plugin/pkg/dnstest"
	"github.com/coredns/coredns/plugin/pkg/fall"
	"github.com/coredns/coredns/plugin/test"
	crequest "github.com/coredns/coredns/request"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/aws/aws-sdk-go/service/route53"
	"github.com/aws/aws-sdk-go/service/route53/route53iface"
	"github.com/miekg/dns"
)

type fakeRoute53 struct {
	route53iface.Route53API
}

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")
	}
	rrsResponse := map[string][]*route53.ResourceRecordSet{}
	for _, r := range []struct {
		rType, name, value, hostedZoneID string
	}{
		{"A", "example.org.", "1.2.3.4", "1234567890"},
		{"A", "www.example.org", "1.2.3.4", "1234567890"},
		{"CNAME", `\052.www.example.org`, "www.example.org", "1234567890"},
		{"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"},
		{"A", "split-example.gov.", "1.2.3.4", "1234567890"},
		// Unsupported type should be ignored.
		{"YOLO", "swag.", "foobar", "1234567890"},
		// Hosted zone with the same name, but a different id.
		{"A", "other-example.org.", "3.5.7.9", "1357986420"},
		{"A", "split-example.org.", "1.2.3.4", "1357986420"},
		{"SOA", "org.", "ns-15.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400", "1357986420"},
		// Hosted zone without SOA.
	} {
		rrs, ok := rrsResponse[r.hostedZoneID]
		if !ok {
			rrs = make([]*route53.ResourceRecordSet, 0)
		}
		rrs = append(rrs, &route53.ResourceRecordSet{Type: aws.String(r.rType),
			Name: aws.String(r.name),
			ResourceRecords: []*route53.ResourceRecord{
				{
					Value: aws.String(r.value),
				},
			},
			TTL: aws.Int64(300),
		})
		rrsResponse[r.hostedZoneID] = rrs
	}

	if ok := fn(&route53.ListResourceRecordSetsOutput{
		ResourceRecordSets: rrsResponse[aws.StringValue(in.HostedZoneId)],
	}, true); !ok {
		return errors.New("paging function return false")
	}
	return nil
}

func TestRoute53(t *testing.T) {
	ctx := context.Background()

	r, err := New(ctx, fakeRoute53{}, map[string][]string{"bad.": {"0987654321"}}, time.Minute)
	if err != nil {
		t.Fatalf("Failed to create route53: %v", err)
	}
	if err = r.Run(ctx); err == nil {
		t.Fatalf("Expected errors for zone bad.")
	}

	r, err = New(ctx, fakeRoute53{}, map[string][]string{"org.": {"1357986420", "1234567890"}, "gov.": {"Z098765432", "1234567890"}}, 90*time.Second)
	if err != nil {
		t.Fatalf("Failed to create route53: %v", err)
	}
	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)<commit_msg>fix goroutine leaks in TestRoute53 (#5070)<commit_after>package route53

import (
	"context"
	"errors"
	"reflect"
	"testing"
	"time"

	"github.com/coredns/coredns/plugin/pkg/dnstest"
	"github.com/coredns/coredns/plugin/pkg/fall"
	"github.com/coredns/coredns/plugin/test"
	crequest "github.com/coredns/coredns/request"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/aws/aws-sdk-go/service/route53"
	"github.com/aws/aws-sdk-go/service/route53/route53iface"
	"github.com/miekg/dns"
)

type fakeRoute53 struct {
	route53iface.Route53API
}

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")
	}
	rrsResponse := map[string][]*route53.ResourceRecordSet{}
	for _, r := range []struct {
		rType, name, value, hostedZoneID string
	}{
		{"A", "example.org.", "172.16.17.32", "1234567890"},
		{"A", "www.example.org", "172.16.17.32", "1234567890"},
		{"CNAME", `\052.www.example.org`, "www.example.org", "1234567890"},
		{"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"},
		{"A", "split-example.gov.", "1.2.3.4", "1234567890"},
		// Unsupported type should be ignored.
		{"YOLO", "swag.", "foobar", "1234567890"},
		// Hosted zone with the same name, but a different id.
		{"A", "other-example.org.", "3.5.7.9", "1357986420"},
		{"A", "split-example.org.", "1.2.3.4", "1357986420"},
		{"SOA", "org.", "ns-15.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400", "1357986420"},
		// Hosted zone without SOA.
	} {
		rrs, ok := rrsResponse[r.hostedZoneID]
		if !ok {
			rrs = make([]*route53.ResourceRecordSet, 0)
		}
		rrs = append(rrs, &route53.ResourceRecordSet{Type: aws.String(r.rType),
			Name: aws.String(r.name),
			ResourceRecords: []*route53.ResourceRecord{
				{
					Value: aws.String(r.value),
				},
			},
			TTL: aws.Int64(300),
		})
		rrsResponse[r.hostedZoneID] = rrs
	}

	if ok := fn(&route53.ListResourceRecordSetsOutput{
		ResourceRecordSets: rrsResponse[aws.StringValue(in.HostedZoneId)],
	}, true); !ok {
		return errors.New("paging function return false")
	}
	return nil
}

func TestRoute53(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	r, err := New(ctx, fakeRoute53{}, map[string][]string{"bad.": {"0987654321"}}, time.Minute)
	if err != nil {
		t.Fatalf("Failed to create route53: %v", err)
	}
	if err = r.Run(ctx); err == nil {
		t.Fatalf("Expected errors for zone bad.")
	}

	r, err = New(ctx, fakeRoute53{}, map[string][]string{"org.": {"1357986420", "1234567890"}, "gov.": {"Z098765432", "1234567890"}}, 90*time.Second)
	if err != nil {
		t.Fatalf("Failed to create route53: %v", err)
	}
	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)
{'fraction_non_alphanumeric': 0.19481243899037792, 'fraction_numerical': 0.09677869195370241, 'mean_word_length': 4.462300076161462, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7634916', 'n_tokens_mistral': 3348, 'n_tokens_neox': 2836, 'n_words': 519}