| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | package test
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	"path/filepath"
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAuto(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2017-01-12 08:13:50 +00:00
										 |  |  | 	t.Parallel()
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	tmpdir, err := ioutil.TempDir(os.TempDir(), "coredns")
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-07-02 15:10:36 +08:00
										 |  |  | 	defer os.RemoveAll(tmpdir)
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	corefile := `org:0 {
 | 
					
						
							|  |  |  | 		auto {
 | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			directory ` + tmpdir + ` db\.(.*) {1}
 | 
					
						
							|  |  |  | 			reload 1s
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 	}`
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 11:35:14 +01:00
										 |  |  | 	i, udp, _, err := CoreDNSServerAndPorts(corefile)
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +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)
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +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)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Write db.example.org to get example.org.
 | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	if err = ioutil.WriteFile(filepath.Join(tmpdir, "db.example.org"), []byte(zoneContent), 0644); err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 09:18:27 +01:00
										 |  |  | 	time.Sleep(1500 * time.Millisecond) // wait for it to be picked up
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	resp, err = dns.Exchange(m, udp)
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal("Expected to receive reply, but didn't")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(resp.Answer) != 1 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected 1 RR in the answer section, got %d", len(resp.Answer))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Remove db.example.org again.
 | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	os.Remove(filepath.Join(tmpdir, "db.example.org"))
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(1100 * time.Millisecond) // wait for it to be picked up
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	resp, err = dns.Exchange(m, udp)
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +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)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:12:20 +00:00
										 |  |  | func TestAutoNonExistentZone(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2017-01-12 08:13:50 +00:00
										 |  |  | 	t.Parallel()
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:12:20 +00:00
										 |  |  | 	tmpdir, err := ioutil.TempDir(os.TempDir(), "coredns")
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-07-02 15:10:36 +08:00
										 |  |  | 	defer os.RemoveAll(tmpdir)
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:12:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	corefile := `.:0 {
 | 
					
						
							|  |  |  | 		auto {
 | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			directory ` + tmpdir + ` (.*) {1}
 | 
					
						
							|  |  |  | 			reload 1s
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:12:20 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		errors stdout
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 	}`
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:12:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	i, err := CoreDNSServer(corefile)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	udp, _ := CoreDNSServerPorts(i, 0)
 | 
					
						
							|  |  |  | 	if udp == "" {
 | 
					
						
							| 
									
										
										
										
											2016-11-13 14:03:12 +00:00
										 |  |  | 		t.Fatal("Could not get UDP listening port")
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:12:20 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	defer i.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetQuestion("example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 	resp, err := dns.Exchange(m, udp)
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:12:20 +00: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)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | func TestAutoAXFR(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2017-01-12 08:13:50 +00:00
										 |  |  | 	t.Parallel()
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tmpdir, err := ioutil.TempDir(os.TempDir(), "coredns")
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-07-02 15:10:36 +08:00
										 |  |  | 	defer os.RemoveAll(tmpdir)
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	corefile := `org:0 {
 | 
					
						
							|  |  |  | 		auto {
 | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			directory ` + tmpdir + ` db\.(.*) {1}
 | 
					
						
							|  |  |  | 			reload 1s
 | 
					
						
							| 
									
										
										
										
											2020-07-08 09:00:26 -07:00
										 |  |  | 			transfer to *
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 	}`
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	i, err := CoreDNSServer(corefile)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-30 13:47:27 +01:00
										 |  |  | 	_, tcp := CoreDNSServerPorts(i, 0)
 | 
					
						
							|  |  |  | 	if tcp == "" {
 | 
					
						
							|  |  |  | 		t.Fatal("Could not get TCP listening port")
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	defer i.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Write db.example.org to get example.org.
 | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	if err = ioutil.WriteFile(filepath.Join(tmpdir, "db.example.org"), []byte(zoneContent), 0644); err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(1100 * time.Millisecond) // wait for it to be picked up
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-30 13:47:27 +01:00
										 |  |  | 	tr := new(dns.Transfer)
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetAxfr("example.org.")
 | 
					
						
							| 
									
										
										
										
											2019-08-30 13:47:27 +01:00
										 |  |  | 	c, err := tr.In(m, tcp)
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal("Expected to receive reply, but didn't")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2019-08-30 13:47:27 +01:00
										 |  |  | 	l := 0
 | 
					
						
							|  |  |  | 	for e := range c {
 | 
					
						
							|  |  |  | 		l += len(e.RR)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if l != 5 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected response with %d RRs, got %d", 5, l)
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:21:16 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | const zoneContent = `; testzone
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | @   IN SOA sns.dns.icann.org. noc.dns.icann.org. 2016082534 7200 3600 1209600 3600
 | 
					
						
							|  |  |  |     IN NS  a.iana-servers.net.
 | 
					
						
							|  |  |  |     IN NS  b.iana-servers.net.
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | www IN A   127.0.0.1
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | `
 |