| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | package file | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 12:59:44 +00:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest" | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test" | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request" | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestLess(t *testing.T) { | 
					
						
							|  |  |  | 	const ( | 
					
						
							|  |  |  | 		min  = 0 | 
					
						
							|  |  |  | 		max  = 4294967295 | 
					
						
							|  |  |  | 		low  = 12345 | 
					
						
							|  |  |  | 		high = 4000000000 | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if less(min, max) { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("Less: should be false") | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if !less(max, min) { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("Less: should be true") | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if !less(high, low) { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("Less: should be true") | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if !less(7, 9) { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("Less; should be true") | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type soa struct { | 
					
						
							|  |  |  | 	serial uint32 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *soa) Handler(w dns.ResponseWriter, req *dns.Msg) { | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 	m := new(dns.Msg) | 
					
						
							|  |  |  | 	m.SetReply(req) | 
					
						
							|  |  |  | 	switch req.Question[0].Qtype { | 
					
						
							|  |  |  | 	case dns.TypeSOA: | 
					
						
							|  |  |  | 		m.Answer = make([]dns.RR, 1) | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		m.Answer[0] = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, s.serial)) | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 		w.WriteMsg(m) | 
					
						
							|  |  |  | 	case dns.TypeAXFR: | 
					
						
							|  |  |  | 		m.Answer = make([]dns.RR, 4) | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		m.Answer[0] = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, s.serial)) | 
					
						
							|  |  |  | 		m.Answer[1] = test.A(fmt.Sprintf("%s IN A 127.0.0.1", testZone)) | 
					
						
							|  |  |  | 		m.Answer[2] = test.A(fmt.Sprintf("%s IN A 127.0.0.1", testZone)) | 
					
						
							|  |  |  | 		m.Answer[3] = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, s.serial)) | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 		w.WriteMsg(m) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *soa) TransferHandler(w dns.ResponseWriter, req *dns.Msg) { | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	m := new(dns.Msg) | 
					
						
							|  |  |  | 	m.SetReply(req) | 
					
						
							|  |  |  | 	m.Answer = make([]dns.RR, 1) | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 	m.Answer[0] = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, s.serial)) | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	w.WriteMsg(m) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | const testZone = "secondary.miek.nl." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | func TestShouldTransfer(t *testing.T) { | 
					
						
							|  |  |  | 	soa := soa{250} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 12:59:44 +00:00
										 |  |  | 	s := dnstest.NewServer(soa.Handler) | 
					
						
							|  |  |  | 	defer s.Close() | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-29 22:22:34 +01:00
										 |  |  | 	z := NewZone("testzone", "test") | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 	z.origin = testZone | 
					
						
							| 
									
										
										
										
											2019-08-25 12:59:44 +00:00
										 |  |  | 	z.TransferFrom = []string{s.Addr} | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 10:51:20 -04:00
										 |  |  | 	// when we have a nil SOA (initial state) | 
					
						
							|  |  |  | 	should, err := z.shouldTransfer() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("Unable to run shouldTransfer: %v", err) | 
					
						
							| 
									
										
										
										
											2016-09-24 10:51:20 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if !should { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("ShouldTransfer should return true for serial: %d", soa.serial) | 
					
						
							| 
									
										
										
										
											2016-09-24 10:51:20 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	// Serial smaller | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 	z.Apex.SOA = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, soa.serial-1)) | 
					
						
							| 
									
										
										
										
											2016-09-24 10:51:20 -04:00
										 |  |  | 	should, err = z.shouldTransfer() | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("Unable to run shouldTransfer: %v", err) | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if !should { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("ShouldTransfer should return true for serial: %q", soa.serial-1) | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// Serial equal | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 	z.Apex.SOA = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, soa.serial)) | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	should, err = z.shouldTransfer() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("Unable to run shouldTransfer: %v", err) | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if should { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("ShouldTransfer should return false for serial: %d", soa.serial) | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestTransferIn(t *testing.T) { | 
					
						
							|  |  |  | 	soa := soa{250} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 12:59:44 +00:00
										 |  |  | 	s := dnstest.NewServer(soa.Handler) | 
					
						
							|  |  |  | 	defer s.Close() | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	z := new(Zone) | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 	z.origin = testZone | 
					
						
							| 
									
										
										
										
											2019-08-25 12:59:44 +00:00
										 |  |  | 	z.TransferFrom = []string{s.Addr} | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 12:59:44 +00:00
										 |  |  | 	if err := z.TransferIn(); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("Unable to run TransferIn: %v", err) | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 	if z.Apex.SOA.String() != fmt.Sprintf("%s	3600	IN	SOA	bla. bla. 250 0 0 0 0", testZone) { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatalf("Unknown SOA transferred") | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestIsNotify(t *testing.T) { | 
					
						
							|  |  |  | 	z := new(Zone) | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 	z.origin = testZone | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	state := newRequest(testZone, dns.TypeSOA) | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 	// need to set opcode | 
					
						
							|  |  |  | 	state.Req.Opcode = dns.OpcodeNotify | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-20 19:12:21 +07:00
										 |  |  | 	z.TransferFrom = []string{"10.240.0.1:53"} // IP from testing/responseWriter | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 	if !z.isNotify(state) { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatal("Should have been valid notify") | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-07 08:03:57 +01:00
										 |  |  | 	z.TransferFrom = []string{"10.240.0.2:53"} | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 	if z.isNotify(state) { | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 		t.Fatal("Should have been invalid notify") | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func newRequest(zone string, qtype uint16) request.Request { | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | 	m := new(dns.Msg) | 
					
						
							|  |  |  | 	m.SetQuestion("example.com.", dns.TypeA) | 
					
						
							|  |  |  | 	m.SetEdns0(4097, true) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	return request.Request{W: &test.ResponseWriter{}, Req: m} | 
					
						
							| 
									
										
										
										
											2016-04-07 07:42:58 +01:00
										 |  |  | } |