| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | package hosts
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2018-11-03 20:00:07 +00:00
										 |  |  | 	"io"
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	"strings"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | func (h *Hostsfile) parseReader(r io.Reader) {
 | 
					
						
							|  |  |  | 	h.hmap = h.parse(r)
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-11-03 20:00:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | func TestLookupA(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 	h := Hosts{
 | 
					
						
							|  |  |  | 		Next: test.ErrorHandler(),
 | 
					
						
							|  |  |  | 		Hostsfile: &Hostsfile{
 | 
					
						
							|  |  |  | 			Origins: []string{"."},
 | 
					
						
							|  |  |  | 			hmap:    newHostsMap(),
 | 
					
						
							|  |  |  | 			options: newOptions(),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 	h.parseReader(strings.NewReader(hostsExample))
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tc := range hostsTestCases {
 | 
					
						
							|  |  |  | 		m := tc.Msg()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 		_, err := h.ServeDNS(ctx, rec, m)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							| 
									
										
										
										
											2019-01-19 11:23:13 +00:00
										 |  |  | 			t.Errorf("Expected no error, got %v", err)
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 			return
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		resp := rec.Msg
 | 
					
						
							| 
									
										
										
										
											2019-01-19 11:23:13 +00:00
										 |  |  | 		if err := test.SortAndCheck(resp, tc); err != nil {
 | 
					
						
							|  |  |  | 			t.Error(err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var hostsTestCases = []test.Case{
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "example.org.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("example.org. 3600	IN	A 10.0.0.1"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							| 
									
										
										
										
											2019-01-28 16:36:34 +00:00
										 |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "example.com.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("example.com. 3600	IN	A 10.0.0.2"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "localhost.", Qtype: dns.TypeAAAA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.AAAA("localhost. 3600	IN	AAAA ::1"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "1.0.0.10.in-addr.arpa.", Qtype: dns.TypePTR,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.PTR("1.0.0.10.in-addr.arpa. 3600 PTR example.org."),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							| 
									
										
										
										
											2019-01-28 16:36:34 +00:00
										 |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "2.0.0.10.in-addr.arpa.", Qtype: dns.TypePTR,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.PTR("2.0.0.10.in-addr.arpa. 3600 PTR example.com."),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "1.0.0.127.in-addr.arpa.", Qtype: dns.TypePTR,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.PTR("1.0.0.127.in-addr.arpa. 3600 PTR localhost."),
 | 
					
						
							|  |  |  | 			test.PTR("1.0.0.127.in-addr.arpa. 3600 PTR localhost.domain."),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							| 
									
										
										
										
											2017-07-20 16:09:15 -06:00
										 |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "example.org.", Qtype: dns.TypeAAAA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "example.org.", Qtype: dns.TypeMX,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const hostsExample = `
 | 
					
						
							|  |  |  | 127.0.0.1 localhost localhost.domain
 | 
					
						
							|  |  |  | ::1 localhost localhost.domain
 | 
					
						
							| 
									
										
										
										
											2019-01-28 16:36:34 +00:00
										 |  |  | 10.0.0.1 example.org
 | 
					
						
							|  |  |  | ::FFFF:10.0.0.2 example.com
 | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | reload 5s
 | 
					
						
							|  |  |  | timeout 3600
 | 
					
						
							| 
									
										
										
										
											2019-01-28 16:36:34 +00:00
										 |  |  | `
 |