| 
									
										
										
										
											2017-07-11 00:49:24 -04:00
										 |  |  | package reverse
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2017-07-11 00:49:24 -04:00
										 |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"regexp"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											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-07-11 00:49:24 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestReverse(t *testing.T) {
 | 
					
						
							|  |  |  | 	_, net4, _ := net.ParseCIDR("10.1.1.0/24")
 | 
					
						
							|  |  |  | 	regexIP4, _ := regexp.Compile("^.*ip-" + regexMatchV4 + "\\.example\\.org\\.$")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	em := Reverse{
 | 
					
						
							|  |  |  | 		Networks: networks{network{
 | 
					
						
							|  |  |  | 			IPnet:        net4,
 | 
					
						
							| 
									
										
										
										
											2017-10-15 19:38:39 +02:00
										 |  |  | 			Zone:         "example.org.",
 | 
					
						
							| 
									
										
										
										
											2017-07-11 00:49:24 -04:00
										 |  |  | 			Template:     "ip-{ip}.example.org.",
 | 
					
						
							|  |  |  | 			RegexMatchIP: regexIP4,
 | 
					
						
							|  |  |  | 		}},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		next          plugin.Handler
 | 
					
						
							| 
									
										
										
										
											2017-07-11 00:49:24 -04:00
										 |  |  | 		qname         string
 | 
					
						
							|  |  |  | 		qtype         uint16
 | 
					
						
							|  |  |  | 		expectedCode  int
 | 
					
						
							|  |  |  | 		expectedReply string
 | 
					
						
							|  |  |  | 		expectedErr   error
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			next:          test.NextHandler(dns.RcodeSuccess, nil),
 | 
					
						
							| 
									
										
										
										
											2017-10-15 19:38:39 +02:00
										 |  |  | 			qname:         "test.ip-10.1.1.2.example.org.",
 | 
					
						
							| 
									
										
										
										
											2017-07-11 00:49:24 -04:00
										 |  |  | 			expectedCode:  dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			expectedReply: "10.1.1.2",
 | 
					
						
							|  |  |  | 			expectedErr:   nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, tr := range tests {
 | 
					
						
							|  |  |  | 		req := new(dns.Msg)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		tr.qtype = dns.TypeA
 | 
					
						
							| 
									
										
										
										
											2017-10-15 19:38:39 +02:00
										 |  |  | 		req.SetQuestion(tr.qname, tr.qtype)
 | 
					
						
							| 
									
										
										
										
											2017-07-11 00:49:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
					
						
							| 
									
										
										
										
											2017-07-11 00:49:24 -04:00
										 |  |  | 		code, err := em.ServeDNS(ctx, rec, req)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err != tr.expectedErr {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected error %v, but got %v", i, tr.expectedErr, err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if code != int(tr.expectedCode) {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected status code %d, but got %d", i, tr.expectedCode, code)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if tr.expectedReply != "" {
 | 
					
						
							|  |  |  | 			answer := rec.Msg.Answer[0].(*dns.A).A.String()
 | 
					
						
							|  |  |  | 			if answer != tr.expectedReply {
 | 
					
						
							|  |  |  | 				t.Errorf("Test %d: Expected answer %s, but got %s", i, tr.expectedReply, answer)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |