| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | package file
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"sort"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 	coretest "github.com/miekg/coredns/middleware/testing"
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 	"golang.org/x/net/context"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | var dnsTestCases = []coretest.Case{
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "miek.nl.", Qtype: dns.TypeSOA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 			coretest.SOA("miek.nl.	1800	IN	SOA	linode.atoom.net. miek.miek.nl. 1282630057 14400 3600 604800 14400"),
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "miek.nl.", Qtype: dns.TypeAAAA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 			coretest.AAAA("miek.nl.	1800	IN	AAAA	2a01:7e00::f03c:91ff:fef1:6735"),
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "miek.nl.", Qtype: dns.TypeMX,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 			coretest.MX("miek.nl.	1800	IN	MX	1 aspmx.l.google.com."),
 | 
					
						
							|  |  |  | 			coretest.MX("miek.nl.	1800	IN	MX	10 aspmx2.googlemail.com."),
 | 
					
						
							|  |  |  | 			coretest.MX("miek.nl.	1800	IN	MX	10 aspmx3.googlemail.com."),
 | 
					
						
							|  |  |  | 			coretest.MX("miek.nl.	1800	IN	MX	5 alt1.aspmx.l.google.com."),
 | 
					
						
							|  |  |  | 			coretest.MX("miek.nl.	1800	IN	MX	5 alt2.aspmx.l.google.com."),
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "www.miek.nl.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 			coretest.CNAME("www.miek.nl.	1800	IN	CNAME	a.miek.nl."),
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Extra: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 			coretest.A("a.miek.nl.	1800	IN	A	139.162.196.78"),
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "a.miek.nl.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Ns: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 			coretest.SOA("miek.nl.	1800	IN	SOA	linode.atoom.net. miek.miek.nl. 1282630057 14400 3600 604800 14400"),
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "b.miek.nl.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Ns: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 			coretest.SOA("miek.nl.	1800	IN	SOA	linode.atoom.net. miek.miek.nl. 1282630057 14400 3600 604800 14400"),
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const testzone = "miek.nl."
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestLookup(t *testing.T) {
 | 
					
						
							|  |  |  | 	zone, err := Parse(strings.NewReader(dbMiekNL), testzone, "stdin")
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("expect no error when reading zone, got %q", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 	fm := File{Next: coretest.ErrorHandler(), Zones: Zones{Z: map[string]*Zone{testzone: zone}, Names: []string{testzone}}}
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tc := range dnsTestCases {
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 		m := tc.Msg()
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		rec := middleware.NewResponseRecorder(&middleware.TestResponseWriter{})
 | 
					
						
							|  |  |  | 		_, err := fm.ServeDNS(ctx, rec, m)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			t.Errorf("expected no error, got %v\n", err)
 | 
					
						
							|  |  |  | 			return
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		resp := rec.Msg()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 		sort.Sort(coretest.RRSet(resp.Answer))
 | 
					
						
							|  |  |  | 		sort.Sort(coretest.RRSet(resp.Ns))
 | 
					
						
							|  |  |  | 		sort.Sort(coretest.RRSet(resp.Extra))
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if resp.Rcode != tc.Rcode {
 | 
					
						
							|  |  |  | 			t.Errorf("rcode is %q, expected %q", dns.RcodeToString[resp.Rcode], dns.RcodeToString[tc.Rcode])
 | 
					
						
							|  |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if len(resp.Answer) != len(tc.Answer) {
 | 
					
						
							|  |  |  | 			t.Errorf("answer for %q contained %d results, %d expected", tc.Qname, len(resp.Answer), len(tc.Answer))
 | 
					
						
							|  |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if len(resp.Ns) != len(tc.Ns) {
 | 
					
						
							|  |  |  | 			t.Errorf("authority for %q contained %d results, %d expected", tc.Qname, len(resp.Ns), len(tc.Ns))
 | 
					
						
							|  |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if len(resp.Extra) != len(tc.Extra) {
 | 
					
						
							|  |  |  | 			t.Errorf("additional for %q contained %d results, %d expected", tc.Qname, len(resp.Extra), len(tc.Extra))
 | 
					
						
							|  |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 		if !coretest.CheckSection(t, tc, coretest.Answer, resp.Answer) {
 | 
					
						
							|  |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if !coretest.CheckSection(t, tc, coretest.Ns, resp.Ns) {
 | 
					
						
							|  |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 10:49:28 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		if !coretest.CheckSection(t, tc, coretest.Extra, resp.Extra) {
 | 
					
						
							|  |  |  | 			t.Logf("%v\n", resp)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const dbMiekNL = `
 | 
					
						
							|  |  |  | $TTL    30M
 | 
					
						
							|  |  |  | $ORIGIN miek.nl.
 | 
					
						
							|  |  |  | @       IN      SOA     linode.atoom.net. miek.miek.nl. (
 | 
					
						
							|  |  |  |                              1282630057 ; Serial
 | 
					
						
							|  |  |  |                              4H         ; Refresh
 | 
					
						
							|  |  |  |                              1H         ; Retry
 | 
					
						
							|  |  |  |                              7D         ; Expire
 | 
					
						
							|  |  |  |                              4H )       ; Negative Cache TTL
 | 
					
						
							|  |  |  |                 IN      NS      linode.atoom.net.
 | 
					
						
							|  |  |  |                 IN      NS      ns-ext.nlnetlabs.nl.
 | 
					
						
							|  |  |  |                 IN      NS      omval.tednet.nl.
 | 
					
						
							|  |  |  |                 IN      NS      ext.ns.whyscream.net.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 IN      MX      1  aspmx.l.google.com.
 | 
					
						
							|  |  |  |                 IN      MX      5  alt1.aspmx.l.google.com.
 | 
					
						
							|  |  |  |                 IN      MX      5  alt2.aspmx.l.google.com.
 | 
					
						
							|  |  |  |                 IN      MX      10 aspmx2.googlemail.com.
 | 
					
						
							|  |  |  |                 IN      MX      10 aspmx3.googlemail.com.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 IN      A       139.162.196.78
 | 
					
						
							|  |  |  |                 IN      AAAA    2a01:7e00::f03c:91ff:fef1:6735
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | a               IN      A       139.162.196.78
 | 
					
						
							|  |  |  |                 IN      AAAA    2a01:7e00::f03c:91ff:fef1:6735
 | 
					
						
							|  |  |  | www             IN      CNAME   a
 | 
					
						
							|  |  |  | archive         IN      CNAME   a`
 |