AdGuardHome/internal/util/auto_hosts_test.go
Eugene Burkov 6358240e9b Pull request: 2273 clean tests output
Merge in DNS/adguard-home from 2273-clean-tests-output to master

Closes #2273.

Squashed commit of the following:

commit 7571a33fc1f76300bd256578b3afa95338e399c4
Merge: f17df0f9c a19523b25
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:45:30 2020 +0300

    Merge branch 'master' into 2273-clean-tests-output

commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:35:42 2020 +0300

    home: move build constraint on top line

commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:24:50 2020 +0300

    all: add improvements to changelog

commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56
Merge: 43d4f7acf 394fc5a9d
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:04:38 2020 +0300

    Merge branch 'master' into 2273-clean-tests-output

commit 43d4f7acf188e810aa7277cb6479110c9842e8be
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 13:38:13 2020 +0300

    dnsfilter: remove redundant test logging

commit 7194c1413006b8f52fc454e89ab052bf52e4e517
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 12:19:14 2020 +0300

    testutil: improve comments

commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Sat Nov 14 01:22:08 2020 +0300

    nclient4: fix wrong function name

commit f355749149b2a4485792ba2bdcbc0bb4b629d726
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Sat Nov 14 01:07:22 2020 +0300

    testutil: fix comments and naming

commit f8c50a260bfae08d594a7f37d603941d3680a45e
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Nov 13 14:09:50 2020 +0300

    testutil: create a package and include it

commit f169cdc4084b719de65aa0cdc65200b48785322e
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Nov 12 20:15:58 2020 +0300

    agherr: discard loggers output

commit 360e736b5a2a30f2c5350448234f14b841e3ea27
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Nov 12 20:09:55 2020 +0300

    all: replace default log writer with ioutil.Discard

    Closes #2273.
2020-11-16 15:52:05 +03:00

124 lines
3.2 KiB
Go

package util
import (
"io/ioutil"
"net"
"os"
"strings"
"testing"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/testutil"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
testutil.DiscardLogOutput(m)
}
func prepareTestDir() string {
const dir = "./agh-test"
_ = os.RemoveAll(dir)
_ = os.MkdirAll(dir, 0o755)
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())
// 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()
names, ok := table["127.0.0.1"]
assert.True(t, ok)
assert.Equal(t, []string{"host", "localhost"}, names)
// Test PTR
a, _ := dns.ReverseAddr("127.0.0.1")
a = strings.TrimSuffix(a, ".")
hosts := ah.ProcessReverse(a, dns.TypePTR)
if assert.Len(t, hosts, 2) {
assert.Equal(t, hosts[0], "host")
}
a, _ = dns.ReverseAddr("::1")
a = strings.TrimSuffix(a, ".")
hosts = ah.ProcessReverse(a, dns.TypePTR)
if assert.Len(t, hosts, 1) {
assert.Equal(t, hosts[0], "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())
// 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.Equal(t, "127.0.0.1", DNSUnreverseAddr("1.0.0.127.in-addr.arpa").String())
assert.Equal(t, "::abcd:1234", 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").String())
assert.Equal(t, "::abcd:1234", 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").String())
assert.Nil(t, DNSUnreverseAddr("1.0.0.127.in-addr.arpa."))
assert.Nil(t, DNSUnreverseAddr(".0.0.127.in-addr.arpa"))
assert.Nil(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"))
assert.Nil(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"))
assert.Nil(t, DNSUnreverseAddr("4.3.2.1.d.c.b. .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"))
}