| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package rewrite | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin" | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metadata" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 	"github.com/coredns/coredns/request" | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func msgPrinter(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 	if len(r.Answer) == 0 { | 
					
						
							|  |  |  | 		r.Answer = []dns.RR{ | 
					
						
							|  |  |  | 			test.A(fmt.Sprintf("%s  5   IN  A  10.0.0.1", r.Question[0].Name)), | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 	w.WriteMsg(r) | 
					
						
							|  |  |  | 	return 0, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | func TestNewRule(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		args        []string | 
					
						
							|  |  |  | 		shouldError bool | 
					
						
							|  |  |  | 		expType     reflect.Type | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{[]string{}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"foo"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"name"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"name", "a.com"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"name", "a.com", "b.com", "c.com"}, true, nil}, | 
					
						
							| 
									
										
										
										
											2018-10-23 16:59:59 -04:00
										 |  |  | 		{[]string{"name", "a.com", "b.com"}, false, reflect.TypeOf(&exactNameRule{})}, | 
					
						
							|  |  |  | 		{[]string{"name", "exact", "a.com", "b.com"}, false, reflect.TypeOf(&exactNameRule{})}, | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 		{[]string{"name", "prefix", "a.com", "b.com"}, false, reflect.TypeOf(&prefixNameRule{})}, | 
					
						
							|  |  |  | 		{[]string{"name", "suffix", "a.com", "b.com"}, false, reflect.TypeOf(&suffixNameRule{})}, | 
					
						
							|  |  |  | 		{[]string{"name", "substring", "a.com", "b.com"}, false, reflect.TypeOf(&substringNameRule{})}, | 
					
						
							|  |  |  | 		{[]string{"name", "regex", "([a])\\.com", "new-{1}.com"}, false, reflect.TypeOf(®exNameRule{})}, | 
					
						
							|  |  |  | 		{[]string{"name", "regex", "([a]\\.com", "new-{1}.com"}, true, nil}, | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 		{[]string{"name", "regex", "(dns)\\.(core)\\.(rocks)", "{2}.{1}.{3}", "answer", "name", "(core)\\.(dns)\\.(rocks)", "{2}.{1}.{3}"}, false, reflect.TypeOf(®exNameRule{})}, | 
					
						
							|  |  |  | 		{[]string{"name", "regex", "(adns)\\.(core)\\.(rocks)", "{2}.{1}.{3}", "answer", "name", "(core)\\.(adns)\\.(rocks)", "{2}.{1}.{3}", "too.long", "way.too.long"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"name", "regex", "(bdns)\\.(core)\\.(rocks)", "{2}.{1}.{3}", "NoAnswer", "name", "(core)\\.(bdns)\\.(rocks)", "{2}.{1}.{3}"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"name", "regex", "(cdns)\\.(core)\\.(rocks)", "{2}.{1}.{3}", "answer", "ttl", "(core)\\.(cdns)\\.(rocks)", "{2}.{1}.{3}"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"name", "regex", "(ddns)\\.(core)\\.(rocks)", "{2}.{1}.{3}", "answer", "name", "\xecore\\.(ddns)\\.(rocks)", "{2}.{1}.{3}"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"name", "regex", "\xedns\\.(core)\\.(rocks)", "{2}.{1}.{3}", "answer", "name", "(core)\\.(edns)\\.(rocks)", "{2}.{1}.{3}"}, true, nil}, | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 		{[]string{"name", "substring", "fcore.dns.rocks", "dns.fcore.rocks", "answer", "name", "(fcore)\\.(dns)\\.(rocks)", "{2}.{1}.{3}"}, false, reflect.TypeOf(&substringNameRule{})}, | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 		{[]string{"name", "substring", "a.com", "b.com", "c.com"}, true, nil}, | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		{[]string{"type"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"type", "a"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"type", "any", "a", "a"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"type", "any", "a"}, false, reflect.TypeOf(&typeRule{})}, | 
					
						
							|  |  |  | 		{[]string{"type", "XY", "WV"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"type", "ANY", "WV"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"class"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"class", "IN"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"class", "ch", "in", "in"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"class", "ch", "in"}, false, reflect.TypeOf(&classRule{})}, | 
					
						
							|  |  |  | 		{[]string{"class", "XY", "WV"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"class", "IN", "WV"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set", "0xffee"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set", "65518", "abcdefg"}, false, reflect.TypeOf(&edns0LocalRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set", "0xffee", "abcdefg"}, false, reflect.TypeOf(&edns0LocalRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "append", "0xffee", "abcdefg"}, false, reflect.TypeOf(&edns0LocalRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "replace", "0xffee", "abcdefg"}, false, reflect.TypeOf(&edns0LocalRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "foo", "0xffee", "abcdefg"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set", "0xffee", "0xabcdefg"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "nsid", "set", "junk"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "nsid", "set"}, false, reflect.TypeOf(&edns0NsidRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "nsid", "append"}, false, reflect.TypeOf(&edns0NsidRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "nsid", "replace"}, false, reflect.TypeOf(&edns0NsidRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "nsid", "foo"}, true, nil}, | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | 		{[]string{"edns0", "local", "set", "0xffee", "{dummy}"}, true, nil}, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		{[]string{"edns0", "local", "set", "0xffee", "{qname}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set", "0xffee", "{qtype}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set", "0xffee", "{client_ip}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set", "0xffee", "{client_port}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set", "0xffee", "{protocol}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set", "0xffee", "{server_ip}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "set", "0xffee", "{server_port}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | 		{[]string{"edns0", "local", "append", "0xffee", "{dummy}"}, true, nil}, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		{[]string{"edns0", "local", "append", "0xffee", "{qname}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "append", "0xffee", "{qtype}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "append", "0xffee", "{client_ip}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "append", "0xffee", "{client_port}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "append", "0xffee", "{protocol}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "append", "0xffee", "{server_ip}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "append", "0xffee", "{server_port}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | 		{[]string{"edns0", "local", "replace", "0xffee", "{dummy}"}, true, nil}, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		{[]string{"edns0", "local", "replace", "0xffee", "{qname}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "replace", "0xffee", "{qtype}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "replace", "0xffee", "{client_ip}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "replace", "0xffee", "{client_port}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "replace", "0xffee", "{protocol}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "replace", "0xffee", "{server_ip}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "local", "replace", "0xffee", "{server_port}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		{[]string{"edns0", "subnet", "set", "-1", "56"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "subnet", "set", "24", "-56"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "subnet", "set", "33", "56"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "subnet", "set", "24", "129"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "subnet", "set", "24", "56"}, false, reflect.TypeOf(&edns0SubnetRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "subnet", "append", "24", "56"}, false, reflect.TypeOf(&edns0SubnetRule{})}, | 
					
						
							|  |  |  | 		{[]string{"edns0", "subnet", "replace", "24", "56"}, false, reflect.TypeOf(&edns0SubnetRule{})}, | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | 		{[]string{"unknown-action", "name", "a.com", "b.com"}, true, nil}, | 
					
						
							| 
									
										
										
										
											2018-10-23 16:59:59 -04:00
										 |  |  | 		{[]string{"stop", "name", "a.com", "b.com"}, false, reflect.TypeOf(&exactNameRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "name", "a.com", "b.com"}, false, reflect.TypeOf(&exactNameRule{})}, | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | 		{[]string{"unknown-action", "type", "any", "a"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"stop", "type", "any", "a"}, false, reflect.TypeOf(&typeRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "type", "any", "a"}, false, reflect.TypeOf(&typeRule{})}, | 
					
						
							|  |  |  | 		{[]string{"unknown-action", "class", "ch", "in"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"stop", "class", "ch", "in"}, false, reflect.TypeOf(&classRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "class", "ch", "in"}, false, reflect.TypeOf(&classRule{})}, | 
					
						
							|  |  |  | 		{[]string{"unknown-action", "edns0", "local", "set", "0xffee", "abcedef"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "local", "set", "0xffee", "abcdefg"}, false, reflect.TypeOf(&edns0LocalRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "local", "set", "0xffee", "abcdefg"}, false, reflect.TypeOf(&edns0LocalRule{})}, | 
					
						
							|  |  |  | 		{[]string{"unknown-action", "edns0", "nsid", "set"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "nsid", "set"}, false, reflect.TypeOf(&edns0NsidRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "nsid", "set"}, false, reflect.TypeOf(&edns0NsidRule{})}, | 
					
						
							|  |  |  | 		{[]string{"unknown-action", "edns0", "local", "set", "0xffee", "{qname}"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "local", "set", "0xffee", "{qname}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "local", "set", "0xffee", "{qtype}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "local", "set", "0xffee", "{client_ip}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "local", "set", "0xffee", "{client_port}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "local", "set", "0xffee", "{protocol}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "local", "set", "0xffee", "{server_ip}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "local", "set", "0xffee", "{server_port}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "local", "set", "0xffee", "{qname}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "local", "set", "0xffee", "{qtype}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "local", "set", "0xffee", "{client_ip}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "local", "set", "0xffee", "{client_port}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "local", "set", "0xffee", "{protocol}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "local", "set", "0xffee", "{server_ip}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "local", "set", "0xffee", "{server_port}"}, false, reflect.TypeOf(&edns0VariableRule{})}, | 
					
						
							|  |  |  | 		{[]string{"unknown-action", "edns0", "subnet", "set", "24", "64"}, true, nil}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "subnet", "set", "24", "56"}, false, reflect.TypeOf(&edns0SubnetRule{})}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "subnet", "append", "24", "56"}, false, reflect.TypeOf(&edns0SubnetRule{})}, | 
					
						
							|  |  |  | 		{[]string{"stop", "edns0", "subnet", "replace", "24", "56"}, false, reflect.TypeOf(&edns0SubnetRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "subnet", "set", "24", "56"}, false, reflect.TypeOf(&edns0SubnetRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "subnet", "append", "24", "56"}, false, reflect.TypeOf(&edns0SubnetRule{})}, | 
					
						
							|  |  |  | 		{[]string{"continue", "edns0", "subnet", "replace", "24", "56"}, false, reflect.TypeOf(&edns0SubnetRule{})}, | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, tc := range tests { | 
					
						
							|  |  |  | 		r, err := newRule(tc.args...) | 
					
						
							|  |  |  | 		if err == nil && tc.shouldError { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: expected error but got success", i) | 
					
						
							|  |  |  | 		} else if err != nil && !tc.shouldError { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: expected success but got error: %s", i, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !tc.shouldError && reflect.TypeOf(r) != tc.expType { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: expected %q but got %q", i, tc.expType, r) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 04:34:26 -04:00
										 |  |  | func TestRewriteDefaultRevertPolicy(t *testing.T) { | 
					
						
							|  |  |  | 	rules := []Rule{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	r, _ := newNameRule("stop", "prefix", "prefix", "to") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 	r, _ = newNameRule("stop", "suffix", ".suffix.", ".nl.") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 	r, _ = newNameRule("stop", "substring", "from.substring", "to") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 	r, _ = newNameRule("stop", "regex", "(f.*m)\\.regex\\.(nl)", "to.{2}") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rw := Rewrite{ | 
					
						
							|  |  |  | 		Next:  plugin.HandlerFunc(msgPrinter), | 
					
						
							|  |  |  | 		Rules: rules, | 
					
						
							|  |  |  | 		// use production (default) RevertPolicy | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		from  string | 
					
						
							|  |  |  | 		fromT uint16 | 
					
						
							|  |  |  | 		fromC uint16 | 
					
						
							|  |  |  | 		to    string | 
					
						
							|  |  |  | 		toT   uint16 | 
					
						
							|  |  |  | 		toC   uint16 | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{"prefix.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, | 
					
						
							|  |  |  | 		{"to.suffix.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, | 
					
						
							|  |  |  | 		{"from.substring.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, | 
					
						
							|  |  |  | 		{"from.regex.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ctx := context.TODO() | 
					
						
							|  |  |  | 	for i, tc := range tests { | 
					
						
							|  |  |  | 		m := new(dns.Msg) | 
					
						
							|  |  |  | 		m.SetQuestion(tc.from, tc.fromT) | 
					
						
							|  |  |  | 		m.Question[0].Qclass = tc.fromC | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							|  |  |  | 		rw.ServeDNS(ctx, rec, m) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		resp := rec.Msg | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if resp.Question[0].Name != tc.from { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected Name in Question to be %q but was %q", i, tc.from, resp.Question[0].Name) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if resp.Answer[0].Header().Name != tc.to { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected Name in Answer to be %q but was %q", i, tc.to, resp.Answer[0].Header().Name) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | func TestRewrite(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	rules := []Rule{} | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 	r, _ := newNameRule("stop", "from.nl.", "to.nl.") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 	r, _ = newNameRule("stop", "regex", "(core)\\.(dns)\\.(rocks)\\.(nl)", "{2}.{1}.{3}.{4}", "answer", "name", "(dns)\\.(core)\\.(rocks)\\.(nl)", "{2}.{1}.{3}.{4}") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 	r, _ = newNameRule("stop", "exact", "from.exact.nl.", "to.nl.") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 	r, _ = newNameRule("stop", "prefix", "prefix", "to") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 	r, _ = newNameRule("stop", "suffix", ".suffix.", ".nl.") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 	r, _ = newNameRule("stop", "substring", "from.substring", "to") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 	r, _ = newNameRule("stop", "regex", "(f.*m)\\.regex\\.(nl)", "to.{2}") | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	rules = append(rules, r) | 
					
						
							| 
									
										
										
										
											2017-12-14 13:25:36 -05:00
										 |  |  | 	r, _ = newNameRule("continue", "regex", "consul\\.(rocks)", "core.dns.{1}") | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	rules = append(rules, r) | 
					
						
							| 
									
										
										
										
											2017-12-14 13:25:36 -05:00
										 |  |  | 	r, _ = newNameRule("stop", "core.dns.rocks", "to.nl.") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 	r, _ = newClassRule("continue", "HS", "CH") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 	r, _ = newClassRule("stop", "CH", "IN") | 
					
						
							|  |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 	r, _ = newTypeRule("stop", "ANY", "HINFO") | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	rw := Rewrite{ | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 		Next:         plugin.HandlerFunc(msgPrinter), | 
					
						
							|  |  |  | 		Rules:        rules, | 
					
						
							|  |  |  | 		RevertPolicy: NoRevertPolicy(), | 
					
						
							| 
									
										
										
										
											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}, | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 		{"from.exact.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, | 
					
						
							|  |  |  | 		{"prefix.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, | 
					
						
							|  |  |  | 		{"to.suffix.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, | 
					
						
							|  |  |  | 		{"from.substring.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, | 
					
						
							|  |  |  | 		{"from.regex.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, | 
					
						
							| 
									
										
										
										
											2017-12-14 13:25:36 -05:00
										 |  |  | 		{"consul.rocks.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		// 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}, | 
					
						
							| 
									
										
										
										
											2017-12-14 13:25:36 -05:00
										 |  |  | 		// class gets rewritten twice because of continue/stop logic: HS to CH, CH to IN | 
					
						
							|  |  |  | 		{"a.nl.", dns.TypeANY, 4, "a.nl.", dns.TypeANY, dns.ClassINET}, | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 		{"core.dns.rocks.nl.", dns.TypeA, dns.ClassINET, "dns.core.rocks.nl.", dns.TypeA, 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 		rec := dnstest.NewRecorder(&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 { | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 			t.Errorf("Test %d: Expected Name to be %q but was %q", 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
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 		if tc.fromT == dns.TypeA && tc.toT == dns.TypeA { | 
					
						
							|  |  |  | 			if len(resp.Answer) > 0 { | 
					
						
							|  |  |  | 				if resp.Answer[0].(*dns.A).Hdr.Name != tc.to { | 
					
						
							|  |  |  | 					t.Errorf("Test %d: Expected Answer Name to be %q but was %q", i, tc.to, resp.Answer[0].(*dns.A).Hdr.Name) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestRewriteEDNS0Local(t *testing.T) { | 
					
						
							|  |  |  | 	rw := Rewrite{ | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 		Next:         plugin.HandlerFunc(msgPrinter), | 
					
						
							|  |  |  | 		RevertPolicy: NoRevertPolicy(), | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		fromOpts []dns.EDNS0 | 
					
						
							|  |  |  | 		args     []string | 
					
						
							|  |  |  | 		toOpts   []dns.EDNS0 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 		doBool   bool | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "set", "0xffee", "0xabcdef"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte{0xab, 0xcd, 0xef}}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "append", "0xffee", "abcdefghijklmnop"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte("abcdefghijklmnop")}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "replace", "0xffee", "abcdefghijklmnop"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"nsid", "set"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_NSID{Code: dns.EDNS0NSID, Nsid: ""}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"nsid", "append"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_NSID{Code: dns.EDNS0NSID, Nsid: ""}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"nsid", "replace"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ctx := context.TODO() | 
					
						
							|  |  |  | 	for i, tc := range tests { | 
					
						
							|  |  |  | 		m := new(dns.Msg) | 
					
						
							|  |  |  | 		m.SetQuestion("example.com.", dns.TypeA) | 
					
						
							|  |  |  | 		m.Question[0].Qclass = dns.ClassINET | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | 		r, err := newEdns0Rule("stop", tc.args...) | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Errorf("Error creating test rule: %s", err) | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		rw.Rules = []Rule{r} | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		rw.ServeDNS(ctx, rec, m) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		resp := rec.Msg | 
					
						
							|  |  |  | 		o := resp.IsEdns0() | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 		o.SetDo(tc.doBool) | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		if o == nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: EDNS0 options not set", i) | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 		if o.Do() != tc.doBool { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %v but got %v", i, tc.doBool, o.Do()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		if !optsEqual(o.Option, tc.toOpts) { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %v but got %v", i, tc.toOpts, o) | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | func TestEdns0LocalMultiRule(t *testing.T) { | 
					
						
							|  |  |  | 	rules := []Rule{} | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | 	r, _ := newEdns0Rule("stop", "local", "replace", "0xffee", "abcdef") | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	rules = append(rules, r) | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | 	r, _ = newEdns0Rule("stop", "local", "set", "0xffee", "fedcba") | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	rules = append(rules, r) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rw := Rewrite{ | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 		Next:         plugin.HandlerFunc(msgPrinter), | 
					
						
							|  |  |  | 		Rules:        rules, | 
					
						
							|  |  |  | 		RevertPolicy: NoRevertPolicy(), | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		fromOpts []dns.EDNS0 | 
					
						
							|  |  |  | 		toOpts   []dns.EDNS0 | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte("fedcba")}}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte("foobar")}}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte("abcdef")}}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	ctx := context.TODO() | 
					
						
							|  |  |  | 	for i, tc := range tests { | 
					
						
							|  |  |  | 		m := new(dns.Msg) | 
					
						
							|  |  |  | 		m.SetQuestion("example.com.", dns.TypeA) | 
					
						
							|  |  |  | 		m.Question[0].Qclass = dns.ClassINET | 
					
						
							|  |  |  | 		if tc.fromOpts != nil { | 
					
						
							|  |  |  | 			o := m.IsEdns0() | 
					
						
							|  |  |  | 			if o == nil { | 
					
						
							|  |  |  | 				m.SetEdns0(4096, true) | 
					
						
							|  |  |  | 				o = m.IsEdns0() | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 			o.Option = append(o.Option, tc.fromOpts...) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		rw.ServeDNS(ctx, rec, m) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		resp := rec.Msg | 
					
						
							|  |  |  | 		o := resp.IsEdns0() | 
					
						
							|  |  |  | 		if o == nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: EDNS0 options not set", i) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !optsEqual(o.Option, tc.toOpts) { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %v but got %v", i, tc.toOpts, o) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func optsEqual(a, b []dns.EDNS0) bool { | 
					
						
							|  |  |  | 	if len(a) != len(b) { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i := range a { | 
					
						
							|  |  |  | 		switch aa := a[i].(type) { | 
					
						
							|  |  |  | 		case *dns.EDNS0_LOCAL: | 
					
						
							|  |  |  | 			if bb, ok := b[i].(*dns.EDNS0_LOCAL); ok { | 
					
						
							|  |  |  | 				if aa.Code != bb.Code { | 
					
						
							|  |  |  | 					return false | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 				if !bytes.Equal(aa.Data, bb.Data) { | 
					
						
							|  |  |  | 					return false | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 				return false | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		case *dns.EDNS0_NSID: | 
					
						
							|  |  |  | 			if bb, ok := b[i].(*dns.EDNS0_NSID); ok { | 
					
						
							|  |  |  | 				if aa.Nsid != bb.Nsid { | 
					
						
							|  |  |  | 					return false | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				return false | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		case *dns.EDNS0_SUBNET: | 
					
						
							|  |  |  | 			if bb, ok := b[i].(*dns.EDNS0_SUBNET); ok { | 
					
						
							|  |  |  | 				if aa.Code != bb.Code { | 
					
						
							|  |  |  | 					return false | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if aa.Family != bb.Family { | 
					
						
							|  |  |  | 					return false | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if aa.SourceNetmask != bb.SourceNetmask { | 
					
						
							|  |  |  | 					return false | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if aa.SourceScope != bb.SourceScope { | 
					
						
							|  |  |  | 					return false | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-09-27 18:10:34 +08:00
										 |  |  | 				if !aa.Address.Equal(bb.Address) { | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 					return false | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				return false | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 		default: | 
					
						
							|  |  |  | 			return false | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | type testProvider map[string]metadata.Func | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (tp testProvider) Metadata(ctx context.Context, state request.Request) context.Context { | 
					
						
							|  |  |  | 	for k, v := range tp { | 
					
						
							|  |  |  | 		metadata.SetValueFunc(ctx, k, v) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ctx | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | func TestRewriteEDNS0LocalVariable(t *testing.T) { | 
					
						
							|  |  |  | 	rw := Rewrite{ | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 		Next:         plugin.HandlerFunc(msgPrinter), | 
					
						
							|  |  |  | 		RevertPolicy: NoRevertPolicy(), | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 	expectedMetadata := []metadata.Provider{ | 
					
						
							|  |  |  | 		testProvider{"test/label": func() string { return "my-value" }}, | 
					
						
							|  |  |  | 		testProvider{"test/empty": func() string { return "" }}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	meta := metadata.Metadata{ | 
					
						
							|  |  |  | 		Zones:     []string{"."}, | 
					
						
							|  |  |  | 		Providers: expectedMetadata, | 
					
						
							|  |  |  | 		Next:      &rw, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 	// test.ResponseWriter has the following values: | 
					
						
							|  |  |  | 	// 		The remote will always be 10.240.0.1 and port 40212. | 
					
						
							|  |  |  | 	// 		The local address is always 127.0.0.1 and port 53. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		fromOpts []dns.EDNS0 | 
					
						
							|  |  |  | 		args     []string | 
					
						
							|  |  |  | 		toOpts   []dns.EDNS0 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 		doBool   bool | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "set", "0xffee", "{qname}"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte("example.com.")}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "set", "0xffee", "{qtype}"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte{0x00, 0x01}}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "set", "0xffee", "{client_ip}"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte{0x0A, 0xF0, 0x00, 0x01}}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "set", "0xffee", "{client_port}"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte{0x9D, 0x14}}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "set", "0xffee", "{protocol}"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte("udp")}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 			[]string{"local", "set", "0xffee", "{server_port}"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte{0x00, 0x35}}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 			[]string{"local", "set", "0xffee", "{server_ip}"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte{0x7F, 0x00, 0x00, 0x01}}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "set", "0xffee", "{test/label}"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte("my-value")}}, | 
					
						
							|  |  |  | 			true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "set", "0xffee", "{test/empty}"}, | 
					
						
							| 
									
										
										
										
											2018-07-08 17:38:46 -04:00
										 |  |  | 			nil, | 
					
						
							|  |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"local", "set", "0xffee", "{test/does-not-exist}"}, | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, tc := range tests { | 
					
						
							|  |  |  | 		m := new(dns.Msg) | 
					
						
							|  |  |  | 		m.SetQuestion("example.com.", dns.TypeA) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | 		r, err := newEdns0Rule("stop", tc.args...) | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Errorf("Error creating test rule: %s", err) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		rw.Rules = []Rule{r} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							| 
									
										
										
										
											2022-09-08 14:56:27 -04:00
										 |  |  | 		ctx := meta.Collect(context.TODO(), request.Request{W: rec, Req: m}) | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 		meta.ServeDNS(ctx, rec, m) | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		resp := rec.Msg | 
					
						
							|  |  |  | 		o := resp.IsEdns0() | 
					
						
							|  |  |  | 		if o == nil { | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 			if tc.toOpts != nil { | 
					
						
							|  |  |  | 				t.Errorf("Test %d: EDNS0 options not set", i) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 		o.SetDo(tc.doBool) | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 		if o.Do() != tc.doBool { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %v but got %v", i, tc.doBool, o.Do()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		if !optsEqual(o.Option, tc.toOpts) { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %v but got %v", i, tc.toOpts, o) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestRewriteEDNS0Subnet(t *testing.T) { | 
					
						
							|  |  |  | 	rw := Rewrite{ | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 		Next:         plugin.HandlerFunc(msgPrinter), | 
					
						
							|  |  |  | 		RevertPolicy: NoRevertPolicy(), | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		writer   dns.ResponseWriter | 
					
						
							|  |  |  | 		fromOpts []dns.EDNS0 | 
					
						
							|  |  |  | 		args     []string | 
					
						
							|  |  |  | 		toOpts   []dns.EDNS0 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 		doBool   bool | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			&test.ResponseWriter{}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"subnet", "set", "24", "56"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_SUBNET{Code: 0x8, | 
					
						
							|  |  |  | 				Family:        0x1, | 
					
						
							|  |  |  | 				SourceNetmask: 0x18, | 
					
						
							|  |  |  | 				SourceScope:   0x0, | 
					
						
							|  |  |  | 				Address:       []byte{0x0A, 0xF0, 0x00, 0x00}, | 
					
						
							| 
									
										
										
										
											2017-11-28 14:03:20 +00:00
										 |  |  | 			}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			&test.ResponseWriter{}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"subnet", "set", "32", "56"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_SUBNET{Code: 0x8, | 
					
						
							|  |  |  | 				Family:        0x1, | 
					
						
							|  |  |  | 				SourceNetmask: 0x20, | 
					
						
							|  |  |  | 				SourceScope:   0x0, | 
					
						
							|  |  |  | 				Address:       []byte{0x0A, 0xF0, 0x00, 0x01}, | 
					
						
							| 
									
										
										
										
											2017-11-28 14:03:20 +00:00
										 |  |  | 			}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			&test.ResponseWriter{}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"subnet", "set", "0", "56"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_SUBNET{Code: 0x8, | 
					
						
							|  |  |  | 				Family:        0x1, | 
					
						
							|  |  |  | 				SourceNetmask: 0x0, | 
					
						
							|  |  |  | 				SourceScope:   0x0, | 
					
						
							|  |  |  | 				Address:       []byte{0x00, 0x00, 0x00, 0x00}, | 
					
						
							| 
									
										
										
										
											2017-11-28 14:03:20 +00:00
										 |  |  | 			}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			&test.ResponseWriter6{}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"subnet", "set", "24", "56"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_SUBNET{Code: 0x8, | 
					
						
							|  |  |  | 				Family:        0x2, | 
					
						
							|  |  |  | 				SourceNetmask: 0x38, | 
					
						
							|  |  |  | 				SourceScope:   0x0, | 
					
						
							|  |  |  | 				Address: []byte{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
					
						
							|  |  |  | 					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, | 
					
						
							| 
									
										
										
										
											2017-11-28 14:03:20 +00:00
										 |  |  | 			}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			&test.ResponseWriter6{}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"subnet", "set", "24", "128"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_SUBNET{Code: 0x8, | 
					
						
							|  |  |  | 				Family:        0x2, | 
					
						
							|  |  |  | 				SourceNetmask: 0x80, | 
					
						
							|  |  |  | 				SourceScope:   0x0, | 
					
						
							|  |  |  | 				Address: []byte{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
					
						
							|  |  |  | 					0x00, 0x42, 0x00, 0xff, 0xfe, 0xca, 0x4c, 0x65}, | 
					
						
							| 
									
										
										
										
											2017-11-28 14:03:20 +00:00
										 |  |  | 			}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			&test.ResponseWriter6{}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{}, | 
					
						
							|  |  |  | 			[]string{"subnet", "set", "24", "0"}, | 
					
						
							|  |  |  | 			[]dns.EDNS0{&dns.EDNS0_SUBNET{Code: 0x8, | 
					
						
							|  |  |  | 				Family:        0x2, | 
					
						
							|  |  |  | 				SourceNetmask: 0x0, | 
					
						
							|  |  |  | 				SourceScope:   0x0, | 
					
						
							|  |  |  | 				Address: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
					
						
							|  |  |  | 					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, | 
					
						
							| 
									
										
										
										
											2017-11-28 14:03:20 +00:00
										 |  |  | 			}}, | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ctx := context.TODO() | 
					
						
							|  |  |  | 	for i, tc := range tests { | 
					
						
							|  |  |  | 		m := new(dns.Msg) | 
					
						
							|  |  |  | 		m.SetQuestion("example.com.", dns.TypeA) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | 		r, err := newEdns0Rule("stop", tc.args...) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Errorf("Error creating test rule: %s", err) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		rw.Rules = []Rule{r} | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 		rec := dnstest.NewRecorder(tc.writer) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		rw.ServeDNS(ctx, rec, m) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		resp := rec.Msg | 
					
						
							|  |  |  | 		o := resp.IsEdns0() | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 		o.SetDo(tc.doBool) | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		if o == nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: EDNS0 options not set", i) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-14 14:18:02 -05:00
										 |  |  | 		if o.Do() != tc.doBool { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %v but got %v", i, tc.doBool, o.Do()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 		if !optsEqual(o.Option, tc.toOpts) { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %v but got %v", i, tc.toOpts, o) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |