| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | package test
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 	"io/ioutil"
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							|  |  |  | 	"path/filepath"
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | func setupProxyTargetCoreDNS(t *testing.T, fn func(string)) {
 | 
					
						
							|  |  |  | 	tmpdir, err := ioutil.TempDir(os.TempDir(), "coredns")
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-08-20 21:11:56 +08:00
										 |  |  | 	defer os.RemoveAll(tmpdir)
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	content := `
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | example.org. IN SOA sns.dns.icann.org. noc.dns.icann.org. 1 3600 3600 3600 3600
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | google.com. IN SOA ns1.google.com. dns-admin.google.com. 1 3600 3600 3600 3600
 | 
					
						
							|  |  |  | google.com. IN A 172.217.25.110
 | 
					
						
							|  |  |  | `
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	path := filepath.Join(tmpdir, "file")
 | 
					
						
							|  |  |  | 	if err = ioutil.WriteFile(path, []byte(content), 0644); err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Could not write to temp file: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	defer os.Remove(path)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 	corefile := `.:0 {
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 		file ` + path + `
 | 
					
						
							|  |  |  | 	}`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 11:35:14 +01:00
										 |  |  | 	i, udp, _, err := CoreDNSServerAndPorts(corefile)
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 		t.Fatalf("Could not get proxy target CoreDNS serving instance: %s", err)
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	defer i.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 	fn(udp)
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | func TestLookupAutoPathErratic(t *testing.T) {
 | 
					
						
							|  |  |  | 	setupProxyTargetCoreDNS(t, func(proxyPath string) {
 | 
					
						
							|  |  |  | 		corefile := `.:0 {
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 			erratic
 | 
					
						
							|  |  |  | 			autopath @erratic
 | 
					
						
							|  |  |  | 			forward . ` + proxyPath + `
 | 
					
						
							|  |  |  | 			debug
 | 
					
						
							|  |  |  | 		}`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 		i, udp, _, err := CoreDNSServerAndPorts(corefile)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 		defer i.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		tests := []struct {
 | 
					
						
							|  |  |  | 			qname          string
 | 
					
						
							|  |  |  | 			expectedAnswer string
 | 
					
						
							|  |  |  | 			expectedType   uint16
 | 
					
						
							|  |  |  | 		}{
 | 
					
						
							|  |  |  | 			{"google.com.a.example.org.", "google.com.a.example.org.", dns.TypeCNAME},
 | 
					
						
							|  |  |  | 			{"google.com.", "google.com.", dns.TypeA},
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for i, tc := range tests {
 | 
					
						
							|  |  |  | 			m := new(dns.Msg)
 | 
					
						
							|  |  |  | 			// erratic always returns this search path: "a.example.org.", "b.example.org.", "".
 | 
					
						
							|  |  |  | 			m.SetQuestion(tc.qname, dns.TypeA)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			r, err := dns.Exchange(m, udp)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				t.Fatalf("Test %d, failed to sent query: %q", i, err)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if len(r.Answer) == 0 {
 | 
					
						
							|  |  |  | 				t.Fatalf("Test %d, answer section should have RRs", i)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if x := r.Answer[0].Header().Name; x != tc.expectedAnswer {
 | 
					
						
							|  |  |  | 				t.Fatalf("Test %d, expected answer %s, got %s", i, tc.expectedAnswer, x)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if x := r.Answer[0].Header().Rrtype; x != tc.expectedType {
 | 
					
						
							|  |  |  | 				t.Fatalf("Test %d, expected answer type %d, got %d", i, tc.expectedType, x)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAutoPathErraticNotLoaded(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 	setupProxyTargetCoreDNS(t, func(proxyPath string) {
 | 
					
						
							|  |  |  | 		corefile := `.:0 {
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 			autopath @erratic
 | 
					
						
							|  |  |  | 			forward . ` + proxyPath + `
 | 
					
						
							|  |  |  | 			debug
 | 
					
						
							|  |  |  | 		}`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 		i, err := CoreDNSServer(corefile)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 		udp, _ := CoreDNSServerPorts(i, 0)
 | 
					
						
							|  |  |  | 		if udp == "" {
 | 
					
						
							|  |  |  | 			t.Fatalf("Could not get UDP listening port")
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		defer i.Stop()
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 17:33:51 +09:00
										 |  |  | 		m := new(dns.Msg)
 | 
					
						
							|  |  |  | 		m.SetQuestion("google.com.a.example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 		r, err := dns.Exchange(m, udp)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			t.Fatalf("Failed to sent query: %q", err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if r.Rcode != dns.RcodeNameError {
 | 
					
						
							|  |  |  | 			t.Fatalf("Expected NXDOMAIN, got %d", r.Rcode)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | }
 |