e8c1f5c8d3
Merge in DNS/adguard-home from 2508-ip-conversion to master Updates #2508. Squashed commit of the following: commit 3f64709fbc73ef74c11b910997be1e9bc337193c Merge: 5ac7faaaa0d67aa251
Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 16:21:34 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit 5ac7faaaa9dda570fdb872acad5d13d078f46b64 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 12:00:11 2021 +0300 all: replace conditions with appropriate functions in tests commit 9e3fa9a115ed23024c57dd5192d5173477ddbf71 Merge: db992a42abba74859e
Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 10:47:10 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit db992a42a2c6f315421e78a6a0492e2bfb3ce89d Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:55:53 2021 +0300 sysutil: fix linux tests commit f629b15d62349323ce2da05e68dc9cc0b5f6e194 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:41:20 2021 +0300 all: improve code quality commit 3bf03a75524040738562298bd1de6db536af130f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 17:33:26 2021 +0300 sysutil: fix linux net.IP conversion commit 5d5b6994916923636e635588631b63b7e7b74e5f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 14:57:26 2021 +0300 dnsforward: remove redundant net.IP <-> string conversion commit 0b955d99b7fad40942f21d1dd8734adb99126195 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 11 18:04:25 2021 +0300 dhcpd: remove net.IP <-> string conversion
125 lines
3.2 KiB
Go
125 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.Len(t, ips, 1)
|
|
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.Len(t, ips, 1)
|
|
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"))
|
|
}
|