| 
									
										
										
										
											2017-06-01 12:33:40 +01:00
										 |  |  | package test
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							| 
									
										
										
										
											2019-06-29 22:22:34 +01:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2017-06-01 12:33:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2017-06-01 12:33:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestEmptySecondaryZone(t *testing.T) {
 | 
					
						
							|  |  |  | 	// Corefile that fails to transfer example.org.
 | 
					
						
							|  |  |  | 	corefile := `example.org:0 {
 | 
					
						
							|  |  |  | 		secondary {
 | 
					
						
							|  |  |  | 			transfer from 127.0.0.1:1717
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 	}`
 | 
					
						
							| 
									
										
										
										
											2017-06-01 12:33:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 11:35:14 +01:00
										 |  |  | 	i, udp, _, err := CoreDNSServerAndPorts(corefile)
 | 
					
						
							| 
									
										
										
										
											2017-06-01 12:33:40 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	defer i.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetQuestion("www.example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 	resp, err := dns.Exchange(m, udp)
 | 
					
						
							| 
									
										
										
										
											2017-06-01 12:33:40 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal("Expected to receive reply, but didn't")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if resp.Rcode != dns.RcodeServerFailure {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected reply to be a SERVFAIL, got %d", resp.Rcode)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-01-23 10:35:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestSecondaryZoneTransfer(t *testing.T) {
 | 
					
						
							|  |  |  | 	name, rm, err := test.TempFile(".", exampleOrg)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-06-01 15:12:49 +01:00
										 |  |  | 		t.Fatalf("Failed to create zone: %s", err)
 | 
					
						
							| 
									
										
										
										
											2018-01-23 10:35:10 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	defer rm()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	corefile := `example.org:0 {
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 		file ` + name + ` {
 | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		transfer {
 | 
					
						
							|  |  |  | 			to *
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}`
 | 
					
						
							| 
									
										
										
										
											2018-01-23 10:35:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	i, _, tcp, err := CoreDNSServerAndPorts(corefile)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	defer i.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	corefile = `example.org:0 {
 | 
					
						
							|  |  |  | 		secondary {
 | 
					
						
							|  |  |  | 			transfer from ` + tcp + `
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 	}`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 10:35:10 +00:00
										 |  |  | 	i1, udp, _, err := CoreDNSServerAndPorts(corefile)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	defer i1.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetQuestion("example.org.", dns.TypeSOA)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-29 22:22:34 +01:00
										 |  |  | 	var r *dns.Msg
 | 
					
						
							| 
									
										
										
										
											2019-08-21 16:08:55 -04:00
										 |  |  | 	// This is now async; we need to wait for it to be transferred.
 | 
					
						
							| 
									
										
										
										
											2019-06-29 22:22:34 +01:00
										 |  |  | 	for i := 0; i < 10; i++ {
 | 
					
						
							| 
									
										
										
										
											2019-08-24 18:14:25 +00:00
										 |  |  | 		r, _ = dns.Exchange(m, udp)
 | 
					
						
							| 
									
										
										
										
											2019-08-01 12:51:37 +00:00
										 |  |  | 		if len(r.Answer) != 0 {
 | 
					
						
							| 
									
										
										
										
											2019-06-29 22:22:34 +01:00
										 |  |  | 			break
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		time.Sleep(100 * time.Microsecond)
 | 
					
						
							| 
									
										
										
										
											2018-01-23 10:35:10 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2019-08-01 12:51:37 +00:00
										 |  |  | 	if len(r.Answer) == 0 {
 | 
					
						
							| 
									
										
										
										
											2018-01-23 10:35:10 +00:00
										 |  |  | 		t.Fatalf("Expected answer section")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2019-08-26 08:14:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestIxfrResponse(t *testing.T) {
 | 
					
						
							|  |  |  | 	// ixfr query with current soa should return single packet with that soa (no transfer needed).
 | 
					
						
							|  |  |  | 	name, rm, err := test.TempFile(".", exampleOrg)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create zone: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	defer rm()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	corefile := `example.org:0 {
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 		file ` + name + ` {
 | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		transfer {
 | 
					
						
							|  |  |  | 			to *
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}`
 | 
					
						
							| 
									
										
										
										
											2019-08-26 08:14:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-05 10:51:29 +01:00
										 |  |  | 	i, _, tcp, err := CoreDNSServerAndPorts(corefile)
 | 
					
						
							| 
									
										
										
										
											2019-08-26 08:14:43 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	defer i.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetQuestion("example.org.", dns.TypeIXFR)
 | 
					
						
							|  |  |  | 	m.Ns = []dns.RR{test.SOA("example.org. IN SOA sns.dns.icann.org. noc.dns.icann.org. 2015082541 7200 3600 1209600 3600")} // copied from exampleOrg
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var r *dns.Msg
 | 
					
						
							| 
									
										
										
										
											2021-02-05 10:51:29 +01:00
										 |  |  | 	c := new(dns.Client)
 | 
					
						
							|  |  |  | 	c.Net = "tcp"
 | 
					
						
							| 
									
										
										
										
											2019-08-26 08:14:43 +00:00
										 |  |  | 	// This is now async; we need to wait for it to be transferred.
 | 
					
						
							|  |  |  | 	for i := 0; i < 10; i++ {
 | 
					
						
							| 
									
										
										
										
											2021-02-05 10:51:29 +01:00
										 |  |  | 		r, _, _ = c.Exchange(m, tcp)
 | 
					
						
							| 
									
										
										
										
											2019-08-26 08:14:43 +00:00
										 |  |  | 		if len(r.Answer) != 0 {
 | 
					
						
							|  |  |  | 			break
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		time.Sleep(100 * time.Microsecond)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(r.Answer) != 1 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected answer section with single RR")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	soa, ok := r.Answer[0].(*dns.SOA)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected answer section with SOA RR")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if soa.Serial != 2015082541 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Serial should be %d, got %d", 2015082541, soa.Serial)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |