| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | package test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Start 2 tests server, server A will proxy to B, server B is an CH server. | 
					
						
							|  |  |  | func TestProxyToChaosServer(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-01-12 08:13:50 +00:00
										 |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	corefile := `.:0 { | 
					
						
							|  |  |  | 	chaos CoreDNS-001 miek@miek.nl | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ` | 
					
						
							| 
									
										
										
										
											2017-08-24 11:35:14 +01:00
										 |  |  | 	chaos, udpChaos, _, err := CoreDNSServerAndPorts(corefile) | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err) | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	defer chaos.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	corefileProxy := `.:0 { | 
					
						
							| 
									
										
										
										
											2019-03-03 23:32:38 -08:00
										 |  |  | 		forward . ` + udpChaos + ` | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | } | 
					
						
							|  |  |  | ` | 
					
						
							| 
									
										
										
										
											2017-08-24 11:35:14 +01:00
										 |  |  | 	proxy, udp, _, err := CoreDNSServerAndPorts(corefileProxy) | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 		t.Fatalf("Could not get CoreDNS serving instance") | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	defer proxy.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	chaosTest(t, udpChaos) | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	chaosTest(t, udp) | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	// chaosTest(t, tcp, "tcp"), commented out because we use the original transport to reach the | 
					
						
							|  |  |  | 	// proxy and we only forward to the udp port. | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func chaosTest(t *testing.T, server string) { | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | 	m := new(dns.Msg) | 
					
						
							|  |  |  | 	m.Question = make([]dns.Question, 1) | 
					
						
							|  |  |  | 	m.Question[0] = dns.Question{Qclass: dns.ClassCHAOS, Name: "version.bind.", Qtype: dns.TypeTXT} | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	r, err := dns.Exchange(m, server) | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Could not send message: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if r.Rcode != dns.RcodeSuccess || len(r.Answer) == 0 { | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		t.Fatalf("Expected successful reply, got %s", dns.RcodeToString[r.Rcode]) | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if r.Answer[0].String() != `version.bind.	0	CH	TXT	"CoreDNS-001"` { | 
					
						
							|  |  |  | 		t.Fatalf("Expected version.bind. reply, got %s", r.Answer[0].String()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |