Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
szolin committed Mar 19, 2020
1 parent 9ddddf7 commit 4540aed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 3 additions & 2 deletions dnsfilter/auto_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion dnsfilter/dnsfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package dnsfilter

import (
"fmt"
"io/ioutil"
"net"
"os"
"path"
"runtime"
"testing"
Expand Down Expand Up @@ -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")))
}
Expand Down

0 comments on commit 4540aed

Please sign in to comment.