Skip to content

Commit

Permalink
fix: panicing on options parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 1, 2018
1 parent 4d25966 commit 0079071
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clientconfig.go
Expand Up @@ -91,7 +91,7 @@ func ClientConfigFromReader(resolvconf io.Reader) (*ClientConfig, error) {
n = 1
}
c.Timeout = n
case len(s) >= 8 && s[:9] == "attempts:":
case len(s) >= 9 && s[:9] == "attempts:":
n, _ := strconv.Atoi(s[9:])
if n < 1 {
n = 1
Expand Down
28 changes: 28 additions & 0 deletions clientconfig_test.go
Expand Up @@ -59,7 +59,35 @@ func TestNdots(t *testing.T) {
t.Errorf("Ndots not properly parsed: (Expected: %d / Was: %d)", ndotsVariants[data], cc.Ndots)
}
}
}

func TestClientConfigFromReaderAttempts(t *testing.T) {
testCases := []struct {
data string
expected int
}{
{data: "options attempts:0", expected: 1},
{data: "options attempts:1", expected: 1},
{data: "options attempts:15", expected: 15},
{data: "options attempts:16", expected: 16},
{data: "options attempts:-1", expected: 1},
{data: "options attempt:", expected: 2},
}

for _, test := range testCases {
test := test
t.Run(strings.Replace(test.data, ":", " ", -1), func(t *testing.T) {
t.Parallel()

cc, err := ClientConfigFromReader(strings.NewReader(test.data))
if err != nil {
t.Errorf("error parsing resolv.conf: %v", err)
}
if cc.Attempts != test.expected {
t.Errorf("A attempts not properly parsed: (Expected: %d / Was: %d)", test.expected, cc.Attempts)
}
})
}
}

func TestReadFromFile(t *testing.T) {
Expand Down

0 comments on commit 0079071

Please sign in to comment.