Skip to content

Commit

Permalink
Merge pull request #2212 from thaJeztah/fix_duplicate_ndots
Browse files Browse the repository at this point in the history
Fix duplicate ndots:0, and improve validation
  • Loading branch information
Flavio Crisciani authored Jun 29, 2018
2 parents e685c3a + cfa9afd commit 820ca50
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
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")
}

0 comments on commit 820ca50

Please sign in to comment.