AdGuardHome/util/auto_hosts_test.go
Simon Zolin 4870da7f94 * auto-hosts: support '#' comments after ip and hosts
Close #1807

Squashed commit of the following:

commit 9d3e2809df056354bb6195abf8106616c1e214b1
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Wed Jun 17 19:23:15 2020 +0300

    improve

commit 9b8c7104560c9c80f848f15818d49a065a38e498
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Wed Jun 17 11:07:56 2020 +0300

    * auto-hosts: support '#' comments after ip and hosts
2020-06-17 19:36:35 +03:00

115 lines
2.9 KiB
Go

package util
import (
"io/ioutil"
"net"
"os"
"strings"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
func prepareTestDir() string {
const dir = "./agh-test"
_ = os.RemoveAll(dir)
_ = os.MkdirAll(dir, 0755)
return dir
}
func TestAutoHostsResolution(t *testing.T) {
ah := AutoHosts{}
dir := prepareTestDir()
defer func() { _ = os.RemoveAll(dir) }()
f, _ := ioutil.TempFile(dir, "")
defer func() { _ = os.Remove(f.Name()) }()
defer f.Close()
_, _ = f.WriteString(" 127.0.0.1 host localhost # comment \n")
_, _ = f.WriteString(" ::1 localhost#comment \n")
ah.Init(f.Name())
// Update from the hosts file
ah.updateHosts()
// Existing host
ips := ah.Process("localhost", dns.TypeA)
assert.NotNil(t, ips)
assert.Equal(t, 1, len(ips))
assert.Equal(t, net.ParseIP("127.0.0.1"), ips[0])
// Unknown host
ips = ah.Process("newhost", dns.TypeA)
assert.Nil(t, ips)
// Unknown host (comment)
ips = ah.Process("comment", dns.TypeA)
assert.Nil(t, ips)
// Test hosts file
table := ah.List()
name, ok := table["127.0.0.1"]
assert.True(t, ok)
assert.Equal(t, "host", name)
// Test PTR
a, _ := dns.ReverseAddr("127.0.0.1")
a = strings.TrimSuffix(a, ".")
assert.True(t, ah.ProcessReverse(a, dns.TypePTR) == "host")
a, _ = dns.ReverseAddr("::1")
a = strings.TrimSuffix(a, ".")
assert.True(t, ah.ProcessReverse(a, dns.TypePTR) == "localhost")
}
func TestAutoHostsFSNotify(t *testing.T) {
ah := AutoHosts{}
dir := prepareTestDir()
defer func() { _ = os.RemoveAll(dir) }()
f, _ := ioutil.TempFile(dir, "")
defer func() { _ = os.Remove(f.Name()) }()
defer f.Close()
// Init
_, _ = f.WriteString(" 127.0.0.1 host localhost \n")
ah.Init(f.Name())
ah.updateHosts()
// Unknown host
ips := ah.Process("newhost", dns.TypeA)
assert.Nil(t, ips)
// Stat monitoring for changes
ah.Start()
defer ah.Close()
// Update file
_, _ = f.WriteString("127.0.0.2 newhost\n")
_ = f.Sync()
// wait until fsnotify has triggerred and processed the file-modification event
time.Sleep(50 * time.Millisecond)
// Check if we are notified about changes
ips = ah.Process("newhost", dns.TypeA)
assert.NotNil(t, ips)
assert.Equal(t, 1, len(ips))
assert.Equal(t, "127.0.0.2", ips[0].String())
}
func TestIP(t *testing.T) {
assert.True(t, dnsUnreverseAddr("1.0.0.127.in-addr.arpa").Equal(net.ParseIP("127.0.0.1").To4()))
assert.True(t, dnsUnreverseAddr("4.3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa").Equal(net.ParseIP("::abcd:1234")))
assert.True(t, dnsUnreverseAddr("1.0.0.127.in-addr.arpa.") == nil)
assert.True(t, dnsUnreverseAddr(".0.0.127.in-addr.arpa") == nil)
assert.True(t, dnsUnreverseAddr(".3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa") == nil)
assert.True(t, dnsUnreverseAddr("4.3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0..ip6.arpa") == nil)
}