Fix ignored last line of resolv.conf
When ClientConfigFromFile is given a file that is missing a newline before EOF, then the last directive is completely ignored. This also adds a very basic test for a normal resolv.conf parsing.
This commit is contained in:
parent
a76c0a363d
commit
80d2db2e86
|
@ -26,14 +26,19 @@ func ClientConfigFromFile(resolvconf string) (*ClientConfig, error) {
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
c := new(ClientConfig)
|
c := new(ClientConfig)
|
||||||
b := bufio.NewReader(file)
|
scanner := bufio.NewScanner(file)
|
||||||
c.Servers = make([]string, 0)
|
c.Servers = make([]string, 0)
|
||||||
c.Search = make([]string, 0)
|
c.Search = make([]string, 0)
|
||||||
c.Port = "53"
|
c.Port = "53"
|
||||||
c.Ndots = 1
|
c.Ndots = 1
|
||||||
c.Timeout = 5
|
c.Timeout = 5
|
||||||
c.Attempts = 2
|
c.Attempts = 2
|
||||||
for line, ok := b.ReadString('\n'); ok == nil; line, ok = b.ReadString('\n') {
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
line := scanner.Text()
|
||||||
f := strings.Fields(line)
|
f := strings.Fields(line)
|
||||||
if len(f) < 1 {
|
if len(f) < 1 {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const normal string = `
|
||||||
|
# This file is automatically generated.
|
||||||
|
#
|
||||||
|
domain somedomain.com
|
||||||
|
nameserver 10.28.10.2
|
||||||
|
nameserver 11.28.10.1
|
||||||
|
`
|
||||||
|
|
||||||
|
const missingNewline string = `
|
||||||
|
# This file is automatically generated.
|
||||||
|
#
|
||||||
|
domain somedomain.com
|
||||||
|
nameserver 10.28.10.2
|
||||||
|
nameserver 11.28.10.1` // <- NOTE: NO newline.
|
||||||
|
|
||||||
|
func testConfig(t *testing.T, data string) {
|
||||||
|
tempDir, err := ioutil.TempDir("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("TempDir: %v", err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tempDir)
|
||||||
|
|
||||||
|
path := filepath.Join(tempDir, "resolv.conf")
|
||||||
|
if err := ioutil.WriteFile(path, []byte(data), 0644); err != nil {
|
||||||
|
t.Fatalf("WriteFile: %v", err)
|
||||||
|
}
|
||||||
|
cc, err := ClientConfigFromFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error parsing resolv.conf: %s", err)
|
||||||
|
}
|
||||||
|
if l := len(cc.Servers); l != 2 {
|
||||||
|
t.Errorf("incorrect number of nameservers detected: %d", l)
|
||||||
|
}
|
||||||
|
if l := len(cc.Search); l != 1 {
|
||||||
|
t.Errorf("domain directive not parsed correctly: %v", cc.Search)
|
||||||
|
} else {
|
||||||
|
if cc.Search[0] != "somedomain.com" {
|
||||||
|
t.Errorf("domain is unexpected: %v", cc.Search[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test we can read a normally formed resolv.conf.
|
||||||
|
func TestNameserver(t *testing.T) {
|
||||||
|
testConfig(t, normal)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't drop the last line if there is no newline before EOF.
|
||||||
|
func TestMissingFinalNewLine(t *testing.T) {
|
||||||
|
testConfig(t, missingNewline)
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue