diff --git a/clientconfig.go b/clientconfig.go index 6479fef9..a606ef69 100644 --- a/clientconfig.go +++ b/clientconfig.go @@ -79,8 +79,10 @@ func ClientConfigFromReader(resolvconf io.Reader) (*ClientConfig, error) { switch { case len(s) >= 6 && s[:6] == "ndots:": n, _ := strconv.Atoi(s[6:]) - if n < 1 { - n = 1 + if n < 0 { + n = 0 + } else if n > 15 { + n = 15 } c.Ndots = n case len(s) >= 8 && s[:8] == "timeout:": diff --git a/clientconfig_test.go b/clientconfig_test.go index 4c5d1fb6..5c10360d 100644 --- a/clientconfig_test.go +++ b/clientconfig_test.go @@ -40,6 +40,28 @@ func testConfig(t *testing.T, data string) { func TestNameserver(t *testing.T) { testConfig(t, normal) } func TestMissingFinalNewLine(t *testing.T) { testConfig(t, missingNewline) } +func TestNdots(t *testing.T) { + ndotsVariants := map[string]int{ + "options ndots:0": 0, + "options ndots:1": 1, + "options ndots:15": 15, + "options ndots:16": 15, + "options ndots:-1": 0, + "": 1, + } + + for data := range ndotsVariants { + cc, err := ClientConfigFromReader(strings.NewReader(data)) + if err != nil { + t.Errorf("error parsing resolv.conf: %v", err) + } + if cc.Ndots != ndotsVariants[data] { + t.Errorf("Ndots not properly parsed: (Expected: %d / Was: %d)", ndotsVariants[data], cc.Ndots) + } + } + +} + func TestReadFromFile(t *testing.T) { tempDir, err := ioutil.TempDir("", "") if err != nil { @@ -67,7 +89,7 @@ func TestReadFromFile(t *testing.T) { } } -func TestNameList(t *testing.T) { +func TestNameListNdots1(t *testing.T) { cfg := ClientConfig{ Ndots: 1, } @@ -91,10 +113,18 @@ func TestNameList(t *testing.T) { } else if names[1] != "miek.nl.test." { t.Errorf("NameList didn't return search last: %v", names[1]) } +} +func TestNameListNdots2(t *testing.T) { + cfg := ClientConfig{ + Ndots: 2, + } - cfg.Ndots = 2 // Sent domain has less than NDots and search - names = cfg.NameList("miek.nl") + cfg.Search = []string{ + "test", + } + names := cfg.NameList("miek.nl") + if len(names) != 2 { t.Errorf("NameList returned != 2 names: %v", names) } else if names[0] != "miek.nl.test." { @@ -103,3 +133,21 @@ func TestNameList(t *testing.T) { t.Errorf("NameList didn't return sent domain last: %v", names[1]) } } + +func TestNameListNdots0(t *testing.T) { + cfg := ClientConfig{ + Ndots: 0, + } + cfg.Search = []string{ + "test", + } + // Sent domain has less than NDots and search + names := cfg.NameList("miek") + if len(names) != 2 { + t.Errorf("NameList returned != 2 names: %v", names) + } else if names[0] != "miek." { + t.Errorf("NameList didn't return search first: %v", names[0]) + } else if names[1] != "miek.test." { + t.Errorf("NameList didn't return sent domain last: %v", names[1]) + } +}