| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package bind
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2025-05-23 19:23:14 +03:00
										 |  |  | 	"runtime"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy"
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 19:56:14 -08:00
										 |  |  | func TestSetup(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2025-05-23 19:23:14 +03:00
										 |  |  | 	// Skip on non-Linux systems as some tests refer to for e.g. loopback interfaces which
 | 
					
						
							|  |  |  | 	// are not present on all systems.
 | 
					
						
							|  |  |  | 	if runtime.GOOS != "linux" {
 | 
					
						
							|  |  |  | 		t.Skipf("Skipping bind test on %s", runtime.GOOS)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:19:32 -05:00
										 |  |  | 	for i, test := range []struct {
 | 
					
						
							|  |  |  | 		config   string
 | 
					
						
							|  |  |  | 		expected []string
 | 
					
						
							|  |  |  | 		failing  bool
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{`bind 1.2.3.4`, []string{"1.2.3.4"}, false},
 | 
					
						
							|  |  |  | 		{`bind`, nil, true},
 | 
					
						
							|  |  |  | 		{`bind 1.2.3.invalid`, nil, true},
 | 
					
						
							|  |  |  | 		{`bind 1.2.3.4 ::5`, []string{"1.2.3.4", "::5"}, false},
 | 
					
						
							|  |  |  | 		{`bind ::1 1.2.3.4 ::5 127.9.9.0`, []string{"::1", "1.2.3.4", "::5", "127.9.9.0"}, false},
 | 
					
						
							|  |  |  | 		{`bind ::1 1.2.3.4 ::5 127.9.9.0 noone`, nil, true},
 | 
					
						
							| 
									
										
										
										
											2021-03-18 10:08:48 +03:30
										 |  |  | 		{`bind 1.2.3.4 lo`, []string{"1.2.3.4", "127.0.0.1", "::1"}, false},
 | 
					
						
							| 
									
										
										
										
											2021-03-25 20:08:17 +04:30
										 |  |  | 		{"bind lo {\nexcept 127.0.0.1\n}\n", []string{"::1"}, false},
 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:19:32 -05:00
										 |  |  | 	} {
 | 
					
						
							|  |  |  | 		c := caddy.NewTestController("dns", test.config)
 | 
					
						
							| 
									
										
										
										
											2018-02-28 19:56:14 -08:00
										 |  |  | 		err := setup(c)
 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:19:32 -05:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			if !test.failing {
 | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 				t.Fatalf("Test %d, expected no errors, but got: %v", i, err)
 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:19:32 -05:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if test.failing {
 | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 			t.Fatalf("Test %d, expected to failed but did not, returned values", i)
 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:19:32 -05:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		cfg := dnsserver.GetConfig(c)
 | 
					
						
							|  |  |  | 		if len(cfg.ListenHosts) != len(test.expected) {
 | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 			t.Errorf("Test %d : expected the config's ListenHosts size to be %d, was %d", i, len(test.expected), len(cfg.ListenHosts))
 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:19:32 -05:00
										 |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		for i, v := range test.expected {
 | 
					
						
							|  |  |  | 			if got, want := cfg.ListenHosts[i], v; got != want {
 | 
					
						
							| 
									
										
										
										
											2018-05-07 22:47:25 +01:00
										 |  |  | 				t.Errorf("Test %d : expected the config's ListenHost to be %s, was %s", i, want, got)
 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:19:32 -05:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |