| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | package hosts | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 16:32:59 +00:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/fall" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	"github.com/mholt/caddy" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestHostsParse(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		inputFileRules      string | 
					
						
							|  |  |  | 		shouldErr           bool | 
					
						
							|  |  |  | 		expectedPath        string | 
					
						
							|  |  |  | 		expectedOrigins     []string | 
					
						
							| 
									
										
										
										
											2018-01-07 14:51:32 -05:00
										 |  |  | 		expectedFallthrough fall.F | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts | 
					
						
							|  |  |  | `, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			false, "/etc/hosts", nil, fall.Zero, | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts /tmp`, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			false, "/tmp", nil, fall.Zero, | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts /etc/hosts miek.nl.`, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			false, "/etc/hosts", []string{"miek.nl."}, fall.Zero, | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts /etc/hosts miek.nl. pun.gent.`, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			false, "/etc/hosts", []string{"miek.nl.", "pun.gent."}, fall.Zero, | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts { | 
					
						
							|  |  |  | 				fallthrough | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			false, "/etc/hosts", nil, fall.Root, | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts /tmp { | 
					
						
							|  |  |  | 				fallthrough | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			false, "/tmp", nil, fall.Root, | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts /etc/hosts miek.nl. { | 
					
						
							|  |  |  | 				fallthrough | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			false, "/etc/hosts", []string{"miek.nl."}, fall.Root, | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-08-13 18:16:25 +01:00
										 |  |  | 			`hosts /etc/hosts miek.nl 10.0.0.9/8 { | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 				fallthrough | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			false, "/etc/hosts", []string{"miek.nl.", "10.in-addr.arpa."}, fall.Root, | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts /etc/hosts { | 
					
						
							|  |  |  | 				fallthrough | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			hosts /etc/hosts { | 
					
						
							|  |  |  | 				fallthrough | 
					
						
							|  |  |  | 			}`, | 
					
						
							|  |  |  | 			true, "/etc/hosts", nil, fall.Root, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range tests { | 
					
						
							|  |  |  | 		c := caddy.NewTestController("dns", test.inputFileRules) | 
					
						
							|  |  |  | 		h, err := hostsParse(c) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err == nil && test.shouldErr { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected errors, but got no error", i) | 
					
						
							|  |  |  | 		} else if err != nil && !test.shouldErr { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected no errors, but got '%v'", i, err) | 
					
						
							|  |  |  | 		} else if !test.shouldErr { | 
					
						
							|  |  |  | 			if h.path != test.expectedPath { | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected %v, got %v", i, test.expectedPath, h.path) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2018-01-07 16:32:59 +00:00
										 |  |  | 			if !h.Fall.Equal(test.expectedFallthrough) { | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected fallthrough of %v, got %v", i, test.expectedFallthrough, h.Fall) | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if len(h.Origins) != len(test.expectedOrigins) { | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected %v, got %v", i, test.expectedOrigins, h.Origins) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			for j, name := range test.expectedOrigins { | 
					
						
							|  |  |  | 				if h.Origins[j] != name { | 
					
						
							|  |  |  | 					t.Fatalf("Test %d expected %v for %d th zone, got %v", i, name, j, h.Origins[j]) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestHostsInlineParse(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		inputFileRules      string | 
					
						
							|  |  |  | 		shouldErr           bool | 
					
						
							|  |  |  | 		expectedbyAddr      map[string][]string | 
					
						
							| 
									
										
										
										
											2018-01-07 14:51:32 -05:00
										 |  |  | 		expectedFallthrough fall.F | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts highly_unlikely_to_exist_hosts_file example.org { | 
					
						
							|  |  |  |                                 10.0.0.1 example.org | 
					
						
							|  |  |  |                                 fallthrough | 
					
						
							|  |  |  |                         }`, | 
					
						
							|  |  |  | 			false, | 
					
						
							|  |  |  | 			map[string][]string{ | 
					
						
							|  |  |  | 				`10.0.0.1`: { | 
					
						
							|  |  |  | 					`example.org.`, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			fall.Root, | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts highly_unlikely_to_exist_hosts_file example.org { | 
					
						
							| 
									
										
										
										
											2018-01-07 16:32:59 +00:00
										 |  |  | 			                                10.0.0.1 example.org | 
					
						
							|  |  |  | 			                        }`, | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 			false, | 
					
						
							|  |  |  | 			map[string][]string{ | 
					
						
							|  |  |  | 				`10.0.0.1`: { | 
					
						
							|  |  |  | 					`example.org.`, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			fall.Zero, | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`hosts highly_unlikely_to_exist_hosts_file example.org { | 
					
						
							| 
									
										
										
										
											2018-01-07 16:32:59 +00:00
										 |  |  | 			                                fallthrough | 
					
						
							|  |  |  | 			                                10.0.0.1 example.org | 
					
						
							|  |  |  | 			                        }`, | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 			true, | 
					
						
							|  |  |  | 			map[string][]string{}, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:29:19 +00:00
										 |  |  | 			fall.Root, | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range tests { | 
					
						
							|  |  |  | 		c := caddy.NewTestController("dns", test.inputFileRules) | 
					
						
							|  |  |  | 		h, err := hostsParse(c) | 
					
						
							|  |  |  | 		if err == nil && test.shouldErr { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected errors, but got no error", i) | 
					
						
							|  |  |  | 		} else if err != nil && !test.shouldErr { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected no errors, but got '%v'", i, err) | 
					
						
							|  |  |  | 		} else if !test.shouldErr { | 
					
						
							| 
									
										
										
										
											2018-01-07 16:32:59 +00:00
										 |  |  | 			if !h.Fall.Equal(test.expectedFallthrough) { | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected fallthrough of %v, got %v", i, test.expectedFallthrough, h.Fall) | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			for k, expectedVal := range test.expectedbyAddr { | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 				if val, ok := h.hmap.byAddr[k]; !ok { | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 					t.Fatalf("Test %d expected %v, got no entry", i, k) | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					if len(expectedVal) != len(val) { | 
					
						
							|  |  |  | 						t.Fatalf("Test %d expected %v records for %v, got %v", i, len(expectedVal), k, len(val)) | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					for j := range expectedVal { | 
					
						
							|  |  |  | 						if expectedVal[j] != val[j] { | 
					
						
							|  |  |  | 							t.Fatalf("Test %d expected %v for %v, got %v", i, expectedVal[j], j, val[j]) | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |