| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | package header | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin" | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest" | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/test" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | func TestHeaderResponseRules(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 	wr := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							|  |  |  | 	next := plugin.HandlerFunc(func(ctx context.Context, writer dns.ResponseWriter, msg *dns.Msg) (int, error) { | 
					
						
							|  |  |  | 		writer.WriteMsg(msg) | 
					
						
							|  |  |  | 		return dns.RcodeSuccess, nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		handler  plugin.Handler | 
					
						
							|  |  |  | 		got      func(msg *dns.Msg) bool | 
					
						
							|  |  |  | 		expected bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			handler: Header{ | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 				ResponseRules: []Rule{{Flag: recursionAvailable, State: true}}, | 
					
						
							|  |  |  | 				Next:          next, | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			got: func(msg *dns.Msg) bool { | 
					
						
							|  |  |  | 				return msg.RecursionAvailable | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			handler: Header{ | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 				ResponseRules: []Rule{{Flag: recursionAvailable, State: false}}, | 
					
						
							|  |  |  | 				Next:          next, | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			got: func(msg *dns.Msg) bool { | 
					
						
							|  |  |  | 				return msg.RecursionAvailable | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 			expected: false, | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			handler: Header{ | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 				ResponseRules: []Rule{{Flag: recursionDesired, State: true}}, | 
					
						
							|  |  |  | 				Next:          next, | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			got: func(msg *dns.Msg) bool { | 
					
						
							|  |  |  | 				return msg.RecursionDesired | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			handler: Header{ | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 				ResponseRules: []Rule{{Flag: authoritative, State: true}}, | 
					
						
							|  |  |  | 				Next:          next, | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			got: func(msg *dns.Msg) bool { | 
					
						
							|  |  |  | 				return msg.Authoritative | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range tests { | 
					
						
							|  |  |  | 		m := new(dns.Msg) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_, err := test.handler.ServeDNS(context.TODO(), wr, m) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected no error, but got %s", i, err) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if test.got(m) != test.expected { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected flag state=%t, but got %t", i, test.expected, test.got(m)) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestHeaderQueryRules(t *testing.T) { | 
					
						
							|  |  |  | 	wr := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							|  |  |  | 	next := plugin.HandlerFunc(func(ctx context.Context, writer dns.ResponseWriter, msg *dns.Msg) (int, error) { | 
					
						
							|  |  |  | 		writer.WriteMsg(msg) | 
					
						
							|  |  |  | 		return dns.RcodeSuccess, nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		handler  plugin.Handler | 
					
						
							|  |  |  | 		got      func(msg *dns.Msg) bool | 
					
						
							|  |  |  | 		expected bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			handler: Header{ | 
					
						
							|  |  |  | 				QueryRules: []Rule{{Flag: recursionAvailable, State: true}}, | 
					
						
							|  |  |  | 				Next:       next, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			got: func(msg *dns.Msg) bool { | 
					
						
							|  |  |  | 				return msg.RecursionAvailable | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			handler: Header{ | 
					
						
							|  |  |  | 				QueryRules: []Rule{{Flag: recursionDesired, State: true}}, | 
					
						
							|  |  |  | 				Next:       next, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			got: func(msg *dns.Msg) bool { | 
					
						
							|  |  |  | 				return msg.RecursionDesired | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			handler: Header{ | 
					
						
							|  |  |  | 				QueryRules: []Rule{{Flag: recursionDesired, State: false}}, | 
					
						
							|  |  |  | 				Next:       next, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			got: func(msg *dns.Msg) bool { | 
					
						
							|  |  |  | 				return msg.RecursionDesired | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			handler: Header{ | 
					
						
							|  |  |  | 				QueryRules: []Rule{{Flag: authoritative, State: true}}, | 
					
						
							|  |  |  | 				Next:       next, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			got: func(msg *dns.Msg) bool { | 
					
						
							|  |  |  | 				return msg.Authoritative | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, tc := range tests { | 
					
						
							|  |  |  | 		m := new(dns.Msg) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_, err := tc.handler.ServeDNS(context.TODO(), wr, m) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected no error, but got %s", i, err) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if tc.got(m) != tc.expected { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected flag state=%t, but got %t", i, tc.expected, tc.got(m)) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |