| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | package chaos
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/test"
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 	"golang.org/x/net/context"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestChaos(t *testing.T) {
 | 
					
						
							|  |  |  | 	em := Chaos{
 | 
					
						
							|  |  |  | 		Version: version,
 | 
					
						
							|  |  |  | 		Authors: map[string]bool{"Miek Gieben": true},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		next          middleware.Handler
 | 
					
						
							|  |  |  | 		qname         string
 | 
					
						
							|  |  |  | 		qtype         uint16
 | 
					
						
							|  |  |  | 		expectedCode  int
 | 
					
						
							|  |  |  | 		expectedReply string
 | 
					
						
							|  |  |  | 		expectedErr   error
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			next:          genHandler(dns.RcodeSuccess, nil),
 | 
					
						
							|  |  |  | 			qname:         "version.bind",
 | 
					
						
							|  |  |  | 			expectedCode:  dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			expectedReply: version,
 | 
					
						
							|  |  |  | 			expectedErr:   nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			next:          genHandler(dns.RcodeSuccess, nil),
 | 
					
						
							|  |  |  | 			qname:         "authors.bind",
 | 
					
						
							|  |  |  | 			expectedCode:  dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			expectedReply: "Miek Gieben",
 | 
					
						
							|  |  |  | 			expectedErr:   nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			next:         genHandler(dns.RcodeSuccess, nil),
 | 
					
						
							|  |  |  | 			qname:        "authors.bind",
 | 
					
						
							|  |  |  | 			qtype:        dns.TypeSRV,
 | 
					
						
							|  |  |  | 			expectedCode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 			expectedErr:  nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 	for i, tc := range tests {
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 		req := new(dns.Msg)
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		if tc.qtype == 0 {
 | 
					
						
							|  |  |  | 			tc.qtype = dns.TypeTXT
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		req.SetQuestion(dns.Fqdn(tc.qname), tc.qtype)
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:30:38 +00:00
										 |  |  | 		req.Question[0].Qclass = dns.ClassCHAOS
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		em.Next = tc.next
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		rec := middleware.NewResponseRecorder(&test.ResponseWriter{})
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 		code, err := em.ServeDNS(ctx, rec, req)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		if err != tc.expectedErr {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected error %v, but got %v", i, tc.expectedErr, err)
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		if code != int(tc.expectedCode) {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected status code %d, but got %d", i, tc.expectedCode, code)
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 		if tc.expectedReply != "" {
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 			answer := rec.Msg().Answer[0].(*dns.TXT).Txt[0]
 | 
					
						
							| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | 			if answer != tc.expectedReply {
 | 
					
						
							|  |  |  | 				t.Errorf("Test %d: Expected answer %s, but got %s", i, tc.expectedReply, answer)
 | 
					
						
							| 
									
										
										
										
											2016-03-25 10:32:12 +00:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func genHandler(rcode int, err error) middleware.Handler {
 | 
					
						
							|  |  |  | 	return middleware.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 		return rcode, err
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const version = "CoreDNS-001"
 |