mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-29 10:28:53 +03:00
298f74ba81
Merge in DNS/adguard-home from 2269-multiple-hosts to master
For #2269.
Squashed commit of the following:
commit f8ae452540b106f2d5b130b8edb08c4e76b003f4
Merge: 8dd06f7cc 3e1f92225
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Nov 6 17:28:12 2020 +0300
Merge branch 'master' into 2269-multiple-hosts
commit 8dd06f7cca27ec32a4690e2673603b166f82af0a
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Nov 5 20:28:33 2020 +0300
* all: allow multiple hosts in reverse lookups
119 lines
3.1 KiB
Go
119 lines
3.1 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, 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"))
|
|
}
|