2015-02-04 00:27:39 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-10-15 14:18:44 +00:00
|
|
|
"strings"
|
2015-02-04 00:27:39 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const normal string = `
|
2015-02-04 06:55:03 +00:00
|
|
|
# Comment
|
2015-02-04 00:27:39 +00:00
|
|
|
domain somedomain.com
|
|
|
|
nameserver 10.28.10.2
|
|
|
|
nameserver 11.28.10.1
|
|
|
|
`
|
|
|
|
|
|
|
|
const missingNewline string = `
|
|
|
|
domain somedomain.com
|
|
|
|
nameserver 10.28.10.2
|
|
|
|
nameserver 11.28.10.1` // <- NOTE: NO newline.
|
|
|
|
|
|
|
|
func testConfig(t *testing.T, data string) {
|
2017-10-15 14:18:44 +00:00
|
|
|
cc, err := ClientConfigFromReader(strings.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error parsing resolv.conf: %v", 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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameserver(t *testing.T) { testConfig(t, normal) }
|
|
|
|
func TestMissingFinalNewLine(t *testing.T) { testConfig(t, missingNewline) }
|
|
|
|
|
|
|
|
func TestReadFromFile(t *testing.T) {
|
2015-02-04 00:27:39 +00:00
|
|
|
tempDir, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
2015-11-26 14:12:38 +00:00
|
|
|
t.Fatalf("tempDir: %v", err)
|
2015-02-04 00:27:39 +00:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
|
|
|
|
path := filepath.Join(tempDir, "resolv.conf")
|
2017-10-15 14:18:44 +00:00
|
|
|
if err := ioutil.WriteFile(path, []byte(normal), 0644); err != nil {
|
2015-11-26 14:12:38 +00:00
|
|
|
t.Fatalf("writeFile: %v", err)
|
2015-02-04 00:27:39 +00:00
|
|
|
}
|
|
|
|
cc, err := ClientConfigFromFile(path)
|
|
|
|
if err != nil {
|
2015-02-26 08:49:59 +00:00
|
|
|
t.Errorf("error parsing resolv.conf: %v", err)
|
2015-02-04 00:27:39 +00:00
|
|
|
}
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 20:16:50 +00:00
|
|
|
func TestNameList(t *testing.T) {
|
|
|
|
cfg := ClientConfig{
|
|
|
|
Ndots: 1,
|
|
|
|
}
|
|
|
|
// fqdn should be only result returned
|
|
|
|
names := cfg.NameList("miek.nl.")
|
|
|
|
if len(names) != 1 {
|
|
|
|
t.Errorf("NameList returned != 1 names: %v", names)
|
|
|
|
} else if names[0] != "miek.nl." {
|
|
|
|
t.Errorf("NameList didn't return sent fqdn domain: %v", names[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Search = []string{
|
|
|
|
"test",
|
|
|
|
}
|
|
|
|
// Sent domain has NDots and search
|
|
|
|
names = cfg.NameList("miek.nl")
|
|
|
|
if len(names) != 2 {
|
|
|
|
t.Errorf("NameList returned != 2 names: %v", names)
|
|
|
|
} else if names[0] != "miek.nl." {
|
|
|
|
t.Errorf("NameList didn't return sent domain first: %v", names[0])
|
|
|
|
} else if names[1] != "miek.nl.test." {
|
|
|
|
t.Errorf("NameList didn't return search last: %v", names[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Ndots = 2
|
|
|
|
// Sent domain has less than NDots and search
|
|
|
|
names = cfg.NameList("miek.nl")
|
|
|
|
if len(names) != 2 {
|
|
|
|
t.Errorf("NameList returned != 2 names: %v", names)
|
|
|
|
} else if names[0] != "miek.nl.test." {
|
|
|
|
t.Errorf("NameList didn't return search first: %v", names[0])
|
|
|
|
} else if names[1] != "miek.nl." {
|
|
|
|
t.Errorf("NameList didn't return sent domain last: %v", names[1])
|
|
|
|
}
|
|
|
|
}
|