| 
									
										
										
										
											2018-09-19 08:16:04 +01:00
										 |  |  | package parse | 
					
						
							| 
									
										
										
										
											2016-11-24 16:57:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2018-09-19 08:16:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/transport" | 
					
						
							| 
									
										
										
										
											2016-11-24 16:57:20 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 08:16:04 +01:00
										 |  |  | func TestHostPortOrFile(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-11-24 16:57:20 +01:00
										 |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		in        string | 
					
						
							|  |  |  | 		expected  string | 
					
						
							|  |  |  | 		shouldErr bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"8.8.8.8", | 
					
						
							|  |  |  | 			"8.8.8.8:53", | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"8.8.8.8:153", | 
					
						
							|  |  |  | 			"8.8.8.8:153", | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"/etc/resolv.conf:53", | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"resolv.conf", | 
					
						
							|  |  |  | 			"127.0.0.1:53", | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-01-16 14:47:39 -05:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			"fe80::1", | 
					
						
							|  |  |  | 			"[fe80::1]:53", | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"fe80::1%ens3", | 
					
						
							|  |  |  | 			"[fe80::1%ens3]:53", | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"[fd01::1]:153", | 
					
						
							|  |  |  | 			"[fd01::1]:153", | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"[fd01::1%ens3]:153", | 
					
						
							|  |  |  | 			"[fd01::1%ens3]:153", | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-11-24 16:57:20 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err := ioutil.WriteFile("resolv.conf", []byte("nameserver 127.0.0.1\n"), 0600) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to write test resolv.conf") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer os.Remove("resolv.conf") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, tc := range tests { | 
					
						
							| 
									
										
										
										
											2018-09-19 08:16:04 +01:00
										 |  |  | 		got, err := HostPortOrFile(tc.in) | 
					
						
							| 
									
										
										
										
											2016-11-24 16:57:20 +01:00
										 |  |  | 		if err == nil && tc.shouldErr { | 
					
						
							|  |  |  | 			t.Errorf("Test %d, expected error, got nil", i) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if err != nil && tc.shouldErr { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if got[0] != tc.expected { | 
					
						
							|  |  |  | 			t.Errorf("Test %d, expected %q, got %q", i, tc.expected, got[0]) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestParseHostPort(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		in        string | 
					
						
							|  |  |  | 		expected  string | 
					
						
							|  |  |  | 		shouldErr bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{"8.8.8.8:53", "8.8.8.8:53", false}, | 
					
						
							|  |  |  | 		{"a.a.a.a:153", "", true}, | 
					
						
							|  |  |  | 		{"8.8.8.8", "8.8.8.8:53", false}, | 
					
						
							|  |  |  | 		{"8.8.8.8:", "8.8.8.8:53", false}, | 
					
						
							|  |  |  | 		{"8.8.8.8::53", "", true}, | 
					
						
							|  |  |  | 		{"resolv.conf", "", true}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, tc := range tests { | 
					
						
							| 
									
										
										
										
											2018-09-19 08:16:04 +01:00
										 |  |  | 		got, err := HostPort(tc.in, transport.Port) | 
					
						
							| 
									
										
										
										
											2016-11-24 16:57:20 +01:00
										 |  |  | 		if err == nil && tc.shouldErr { | 
					
						
							|  |  |  | 			t.Errorf("Test %d, expected error, got nil", i) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if err != nil && !tc.shouldErr { | 
					
						
							|  |  |  | 			t.Errorf("Test %d, expected no error, got %q", i, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if got != tc.expected { | 
					
						
							|  |  |  | 			t.Errorf("Test %d, expected %q, got %q", i, tc.expected, got) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |