| 
									
										
										
										
											2022-07-15 17:00:42 +02:00
										 |  |  | package test
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"crypto/tls"
 | 
					
						
							| 
									
										
										
										
											2022-10-29 00:55:41 +08:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2022-07-15 17:00:42 +02:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 00:55:41 +08:00
										 |  |  | func TestTLS(t *testing.T) {
 | 
					
						
							|  |  |  | 	tempCorefile := `%s {
 | 
					
						
							| 
									
										
										
										
											2022-07-15 17:00:42 +02:00
										 |  |  |         tls ../plugin/tls/test_cert.pem ../plugin/tls/test_key.pem
 | 
					
						
							|  |  |  |         whoami
 | 
					
						
							|  |  |  |     }`
 | 
					
						
							| 
									
										
										
										
											2022-10-29 00:55:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	dot, doh := ":1053", ":8443"
 | 
					
						
							|  |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetQuestion("example.com.", dns.TypeA)
 | 
					
						
							| 
									
										
										
										
											2022-07-15 17:00:42 +02:00
										 |  |  | 	answerLength := 0
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 00:55:41 +08:00
										 |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		server    string
 | 
					
						
							|  |  |  | 		tlsConfig *tls.Config
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{fmt.Sprintf("tls://.%s", dot),
 | 
					
						
							|  |  |  | 			&tls.Config{InsecureSkipVerify: true},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{fmt.Sprintf("tls://.%s", dot),
 | 
					
						
							|  |  |  | 			&tls.Config{InsecureSkipVerify: true, NextProtos: []string{"dot"}},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{fmt.Sprintf("tls://.%s https://.%s", dot, doh),
 | 
					
						
							|  |  |  | 			&tls.Config{InsecureSkipVerify: true},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{fmt.Sprintf("tls://.%s https://.%s", dot, doh),
 | 
					
						
							|  |  |  | 			&tls.Config{InsecureSkipVerify: true, NextProtos: []string{"dot"}},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2022-07-15 17:00:42 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 00:55:41 +08:00
										 |  |  | 	for _, tc := range tests {
 | 
					
						
							|  |  |  | 		ex, _, _, err := CoreDNSServerAndPorts(fmt.Sprintf(tempCorefile, tc.server))
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2022-07-15 17:00:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 00:55:41 +08:00
										 |  |  | 		client := dns.Client{
 | 
					
						
							|  |  |  | 			Net:       "tcp-tls",
 | 
					
						
							|  |  |  | 			TLSConfig: tc.tlsConfig,
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		r, _, err := client.Exchange(m, dot)
 | 
					
						
							| 
									
										
										
										
											2022-07-15 17:00:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-29 00:55:41 +08:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			t.Fatalf("Could not exchange msg: %s", err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if n := len(r.Answer); n != answerLength {
 | 
					
						
							|  |  |  | 			t.Fatalf("Expected %v answers, got %v", answerLength, n)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if n := len(r.Extra); n != 2 {
 | 
					
						
							|  |  |  | 			t.Errorf("Expected 2 RRs in additional section, but got %d", n)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if r.Rcode != dns.RcodeSuccess {
 | 
					
						
							|  |  |  | 			t.Errorf("Expected success but got %d", r.Rcode)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		ex.Stop()
 | 
					
						
							| 
									
										
										
										
											2022-07-15 17:00:42 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |