Modified clientconfig to match ndots0 (#590)

* Modified clientconfig to match ndots0

* Added Tests for reading resolv.conf

* Cleaned up and removed duplicated code in test

* Added test for ndots below 0

* Cleaned up test

* Clean up
This commit is contained in:
Marc Ende 2017-11-27 12:17:45 +01:00 committed by Miek Gieben
parent 57a0d1a2cf
commit 5ec0c6d20f
2 changed files with 55 additions and 5 deletions

View File

@ -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:":

View File

@ -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])
}
}