| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package rewrite
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/pkg/dnsrecorder"
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/test"
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 	"golang.org/x/net/context"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func msgPrinter(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 	w.WriteMsg(r)
 | 
					
						
							|  |  |  | 	return 0, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | func TestRewrite(t *testing.T) {
 | 
					
						
							|  |  |  | 	rw := Rewrite{
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		Next: middleware.HandlerFunc(msgPrinter),
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Rules: []Rule{
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 			NewSimpleRule("from.nl.", "to.nl."),
 | 
					
						
							|  |  |  | 			NewSimpleRule("CH", "IN"),
 | 
					
						
							|  |  |  | 			NewSimpleRule("ANY", "HINFO"),
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		noRevert: true,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		from  string
 | 
					
						
							|  |  |  | 		fromT uint16
 | 
					
						
							|  |  |  | 		fromC uint16
 | 
					
						
							|  |  |  | 		to    string
 | 
					
						
							|  |  |  | 		toT   uint16
 | 
					
						
							|  |  |  | 		toC   uint16
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}{
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		{"from.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET},
 | 
					
						
							|  |  |  | 		{"a.nl.", dns.TypeA, dns.ClassINET, "a.nl.", dns.TypeA, dns.ClassINET},
 | 
					
						
							|  |  |  | 		{"a.nl.", dns.TypeA, dns.ClassCHAOS, "a.nl.", dns.TypeA, dns.ClassINET},
 | 
					
						
							|  |  |  | 		{"a.nl.", dns.TypeANY, dns.ClassINET, "a.nl.", dns.TypeHINFO, dns.ClassINET},
 | 
					
						
							|  |  |  | 		// name is rewritten, type is not.
 | 
					
						
							|  |  |  | 		{"from.nl.", dns.TypeANY, dns.ClassINET, "to.nl.", dns.TypeANY, dns.ClassINET},
 | 
					
						
							|  |  |  | 		// name is not, type is, but class is, because class is the 2nd rule.
 | 
					
						
							|  |  |  | 		{"a.nl.", dns.TypeANY, dns.ClassCHAOS, "a.nl.", dns.TypeANY, dns.ClassINET},
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 	ctx := context.TODO()
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 	for i, tc := range tests {
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		m := new(dns.Msg)
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		m.SetQuestion(tc.from, tc.fromT)
 | 
					
						
							|  |  |  | 		m.Question[0].Qclass = tc.fromC
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		rec := dnsrecorder.New(&test.ResponseWriter{})
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		rw.ServeDNS(ctx, rec, m)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		resp := rec.Msg
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		if resp.Question[0].Name != tc.to {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected Name to be '%s' but was '%s'", i, tc.to, resp.Question[0].Name)
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		if resp.Question[0].Qtype != tc.toT {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected Type to be '%d' but was '%d'", i, tc.toT, resp.Question[0].Qtype)
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		if resp.Question[0].Qclass != tc.toC {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected Class to be '%d' but was '%d'", i, tc.toC, resp.Question[0].Qclass)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 	/*
 | 
					
						
							|  |  |  | 		regexps := [][]string{
 | 
					
						
							|  |  |  | 			{"/reg/", ".*", "/to", ""},
 | 
					
						
							|  |  |  | 			{"/r/", "[a-z]+", "/toaz", "!.html|"},
 | 
					
						
							|  |  |  | 			{"/url/", "a([a-z0-9]*)s([A-Z]{2})", "/to/{path}", ""},
 | 
					
						
							|  |  |  | 			{"/ab/", "ab", "/ab?{query}", ".txt|"},
 | 
					
						
							|  |  |  | 			{"/ab/", "ab", "/ab?type=html&{query}", ".html|"},
 | 
					
						
							|  |  |  | 			{"/abc/", "ab", "/abc/{file}", ".html|"},
 | 
					
						
							|  |  |  | 			{"/abcd/", "ab", "/a/{dir}/{file}", ".html|"},
 | 
					
						
							|  |  |  | 			{"/abcde/", "ab", "/a#{fragment}", ".html|"},
 | 
					
						
							|  |  |  | 			{"/ab/", `.*\.jpg`, "/ajpg", ""},
 | 
					
						
							|  |  |  | 			{"/reggrp", `/ad/([0-9]+)([a-z]*)`, "/a{1}/{2}", ""},
 | 
					
						
							|  |  |  | 			{"/reg2grp", `(.*)`, "/{1}", ""},
 | 
					
						
							|  |  |  | 			{"/reg3grp", `(.*)/(.*)/(.*)`, "/{1}{2}{3}", ""},
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		for _, regexpRule := range regexps {
 | 
					
						
							|  |  |  | 			var ext []string
 | 
					
						
							|  |  |  | 			if s := strings.Split(regexpRule[3], "|"); len(s) > 1 {
 | 
					
						
							|  |  |  | 				ext = s[:len(s)-1]
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			rule, err := NewComplexRule(regexpRule[0], regexpRule[1], regexpRule[2], 0, ext, nil)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				t.Fatal(err)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			rw.Rules = append(rw.Rules, rule)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 	*/
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 		statusTests := []struct {
 | 
					
						
							|  |  |  | 			status         int
 | 
					
						
							|  |  |  | 			base           string
 | 
					
						
							|  |  |  | 			to             string
 | 
					
						
							|  |  |  | 			regexp         string
 | 
					
						
							|  |  |  | 			statusExpected bool
 | 
					
						
							|  |  |  | 		}{
 | 
					
						
							|  |  |  | 			{400, "/status", "", "", true},
 | 
					
						
							|  |  |  | 			{400, "/ignore", "", "", false},
 | 
					
						
							|  |  |  | 			{400, "/", "", "^/ignore", false},
 | 
					
						
							|  |  |  | 			{400, "/", "", "(.*)", true},
 | 
					
						
							|  |  |  | 			{400, "/status", "", "", true},
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		for i, s := range statusTests {
 | 
					
						
							|  |  |  | 			urlPath := fmt.Sprintf("/status%d", i)
 | 
					
						
							|  |  |  | 			rule, err := NewComplexRule(s.base, s.regexp, s.to, s.status, nil, nil)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				t.Fatalf("Test %d: No error expected for rule but found %v", i, err)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 			rw.Rules = []Rule{rule}
 | 
					
						
							|  |  |  | 			req, err := http.NewRequest("GET", urlPath, nil)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			rec := httptest.NewRecorder()
 | 
					
						
							|  |  |  | 			code, err := rw.ServeHTTP(rec, req)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				t.Fatalf("Test %d: No error expected for handler but found %v", i, err)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if s.statusExpected {
 | 
					
						
							|  |  |  | 				if rec.Body.String() != "" {
 | 
					
						
							|  |  |  | 					t.Errorf("Test %d: Expected empty body but found %s", i, rec.Body.String())
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				if code != s.status {
 | 
					
						
							|  |  |  | 					t.Errorf("Test %d: Expected status code %d found %d", i, s.status, code)
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			} else {
 | 
					
						
							|  |  |  | 				if code != 0 {
 | 
					
						
							|  |  |  | 					t.Errorf("Test %d: Expected no status code found %d", i, code)
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 	*/
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 |