From 4540aed9327566078e5087d43c30f4e8bffab7b9 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Thu, 19 Mar 2020 11:03:24 +0300 Subject: [PATCH] fix --- dnsfilter/auto_hosts.go | 5 +++-- dnsfilter/dnsfilter_test.go | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/dnsfilter/auto_hosts.go b/dnsfilter/auto_hosts.go index 5e78a9aae3b..6b4ea748369 100644 --- a/dnsfilter/auto_hosts.go +++ b/dnsfilter/auto_hosts.go @@ -98,10 +98,11 @@ func (a *AutoHosts) load(table map[string][]net.IP, fn string) { r := bufio.NewReader(f) log.Debug("AutoHosts: loading hosts from file %s", fn) - for { + finish := false + for !finish { line, err := r.ReadString('\n') if err == io.EOF { - break + finish = true } else if err != nil { log.Error("AutoHosts: %s", err) return diff --git a/dnsfilter/dnsfilter_test.go b/dnsfilter/dnsfilter_test.go index 9128eb47953..2df228e0578 100644 --- a/dnsfilter/dnsfilter_test.go +++ b/dnsfilter/dnsfilter_test.go @@ -2,7 +2,9 @@ package dnsfilter import ( "fmt" + "io/ioutil" "net" + "os" "path" "runtime" "testing" @@ -621,10 +623,27 @@ func TestRewrites(t *testing.T) { assert.True(t, r.IPList[0].Equal(net.ParseIP("1.2.3.4"))) } +func prepareTestDir() string { + const dir = "./agh-test" + _ = os.RemoveAll(dir) + _ = os.MkdirAll(dir, 0755) + return dir +} + func TestAutoHosts(t *testing.T) { ah := AutoHosts{} ah.table = make(map[string][]net.IP) - ah.load(ah.table, "/etc/hosts") + + dir := prepareTestDir() + defer func() { _ = os.RemoveAll(dir) }() + + f, _ := ioutil.TempFile(dir, "") + defer os.Remove(f.Name()) + defer f.Close() + + f.WriteString(" 127.0.0.1 host localhost ") + + ah.load(ah.table, f.Name()) ips := ah.Process("localhost") assert.True(t, ips[0].Equal(net.ParseIP("127.0.0.1"))) }