| 
									
										
										
										
											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) {
 | 
					
						
							|  |  |  | 	corefile := `.:0 {
 | 
					
						
							|  |  |  | 	chaos CoreDNS-001 miek@miek.nl
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | `
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	chaos, err := CoreDNSServer(corefile)
 | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07: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
										 |  |  | 
 | 
					
						
							|  |  |  | 	udpChaos, tcpChaos := CoreDNSServerPorts(chaos, 0)
 | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	defer chaos.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	corefileProxy := `.:0 {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		proxy . ` + udpChaos + `
 | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | `
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	proxy, err := CoreDNSServer(corefileProxy)
 | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		t.Fatalf("could not get CoreDNS serving instance")
 | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	udp, _ := CoreDNSServerPorts(proxy, 0)
 | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	defer proxy.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	chaosTest(t, udpChaos, "udp")
 | 
					
						
							|  |  |  | 	chaosTest(t, tcpChaos, "tcp")
 | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	chaosTest(t, udp, "udp")
 | 
					
						
							|  |  |  | 	// chaosTest(t, tcp, "tcp"), commented out because we use the original transport to reach the
 | 
					
						
							|  |  |  | 	// proxy and we only forward to the udp port.
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func chaosTest(t *testing.T, server, net string) {
 | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	m := Msg("version.bind.", dns.TypeTXT, nil)
 | 
					
						
							| 
									
										
										
										
											2016-04-10 18:50:11 +01:00
										 |  |  | 	m.Question[0].Qclass = dns.ClassCHAOS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	r, err := Exchange(m, server, net)
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected successful reply on %s, got %s", net, dns.RcodeToString[r.Rcode])
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if r.Answer[0].String() != `version.bind.	0	CH	TXT	"CoreDNS-001"` {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected version.bind. reply, got %s", r.Answer[0].String())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |