Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate ndots:0, and improve validation #2212

Merged
merged 4 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sandbox_dns_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,13 @@ dnsOpt:
return fmt.Errorf("invalid ndots option %v", option)
}
if num, err := strconv.Atoi(parts[1]); err != nil {
return fmt.Errorf("invalid number for ndots option %v", option)
} else if num > 0 {
return fmt.Errorf("invalid number for ndots option: %v", parts[1])
} else if num >= 0 {
// if the user sets ndots, use the user setting
sb.ndotsSet = true
break dnsOpt
} else {
return fmt.Errorf("invalid number for ndots option: %v", num)
}
}
}
Expand Down
40 changes: 34 additions & 6 deletions service_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,51 @@ func TestDNSOptions(t *testing.T) {
currRC, err := resolvconf.GetSpecific(sb.(*sandbox).config.resolvConfPath)
require.NoError(t, err)
dnsOptionsList := resolvconf.GetOptions(currRC.Content)
assert.Equal(t, 1, len(dnsOptionsList), "There should be only 1 option instead:", dnsOptionsList)
assert.Equal(t, "ndots:0", dnsOptionsList[0], "The option must be ndots:0 instead:", dnsOptionsList[0])
assert.Equal(t, 1, len(dnsOptionsList))
assert.Equal(t, "ndots:0", dnsOptionsList[0])

sb.(*sandbox).config.dnsOptionsList = []string{"ndots:5"}
err = sb.(*sandbox).setupDNS()
require.NoError(t, err)
currRC, err = resolvconf.GetSpecific(sb.(*sandbox).config.resolvConfPath)
require.NoError(t, err)
dnsOptionsList = resolvconf.GetOptions(currRC.Content)
assert.Equal(t, 1, len(dnsOptionsList), "There should be only 1 option instead:", dnsOptionsList)
assert.Equal(t, "ndots:5", dnsOptionsList[0], "The option must be ndots:5 instead:", dnsOptionsList[0])
assert.Equal(t, 1, len(dnsOptionsList))
assert.Equal(t, "ndots:5", dnsOptionsList[0])

err = sb.(*sandbox).rebuildDNS()
require.NoError(t, err)
currRC, err = resolvconf.GetSpecific(sb.(*sandbox).config.resolvConfPath)
require.NoError(t, err)
dnsOptionsList = resolvconf.GetOptions(currRC.Content)
assert.Equal(t, 1, len(dnsOptionsList), "There should be only 1 option instead:", dnsOptionsList)
assert.Equal(t, "ndots:5", dnsOptionsList[0], "The option must be ndots:5 instead:", dnsOptionsList[0])
assert.Equal(t, 1, len(dnsOptionsList))
assert.Equal(t, "ndots:5", dnsOptionsList[0])

sb2, err := c.(*controller).NewSandbox("cnt2", nil)
require.NoError(t, err)
defer sb2.Delete()
sb2.(*sandbox).startResolver(false)

sb2.(*sandbox).config.dnsOptionsList = []string{"ndots:0"}
err = sb2.(*sandbox).setupDNS()
require.NoError(t, err)
err = sb2.(*sandbox).rebuildDNS()
require.NoError(t, err)
currRC, err = resolvconf.GetSpecific(sb2.(*sandbox).config.resolvConfPath)
require.NoError(t, err)
dnsOptionsList = resolvconf.GetOptions(currRC.Content)
assert.Equal(t, 1, len(dnsOptionsList))
assert.Equal(t, "ndots:0", dnsOptionsList[0])

sb2.(*sandbox).config.dnsOptionsList = []string{"ndots:foobar"}
err = sb2.(*sandbox).setupDNS()
require.NoError(t, err)
err = sb2.(*sandbox).rebuildDNS()
require.EqualError(t, err, "invalid number for ndots option: foobar")

sb2.(*sandbox).config.dnsOptionsList = []string{"ndots:-1"}
err = sb2.(*sandbox).setupDNS()
require.NoError(t, err)
err = sb2.(*sandbox).rebuildDNS()
require.EqualError(t, err, "invalid number for ndots option: -1")
}