2020-03-20 12:05:43 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
2020-04-16 15:56:47 +00:00
|
|
|
"strings"
|
2020-03-20 12:05:43 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-11-16 12:52:05 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/testutil"
|
2020-04-16 15:56:47 +00:00
|
|
|
"github.com/miekg/dns"
|
2020-03-20 12:05:43 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-11-16 12:52:05 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
testutil.DiscardLogOutput(m)
|
|
|
|
}
|
|
|
|
|
2020-03-20 12:05:43 +00:00
|
|
|
func prepareTestDir() string {
|
|
|
|
const dir = "./agh-test"
|
|
|
|
_ = os.RemoveAll(dir)
|
2020-11-06 14:34:40 +00:00
|
|
|
_ = os.MkdirAll(dir, 0o755)
|
2020-03-20 12:05:43 +00:00
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
func TestAutoHostsResolution(t *testing.T) {
|
2020-03-20 12:05:43 +00:00
|
|
|
ah := AutoHosts{}
|
|
|
|
|
|
|
|
dir := prepareTestDir()
|
|
|
|
defer func() { _ = os.RemoveAll(dir) }()
|
|
|
|
|
|
|
|
f, _ := ioutil.TempFile(dir, "")
|
2020-04-15 11:36:47 +00:00
|
|
|
defer func() { _ = os.Remove(f.Name()) }()
|
2020-03-20 12:05:43 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2020-06-17 16:36:35 +00:00
|
|
|
_, _ = f.WriteString(" 127.0.0.1 host localhost # comment \n")
|
|
|
|
_, _ = f.WriteString(" ::1 localhost#comment \n")
|
2020-04-16 15:56:47 +00:00
|
|
|
|
2020-03-20 12:05:43 +00:00
|
|
|
ah.Init(f.Name())
|
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
// Existing host
|
2020-04-16 15:56:47 +00:00
|
|
|
ips := ah.Process("localhost", dns.TypeA)
|
2020-04-15 11:36:47 +00:00
|
|
|
assert.NotNil(t, ips)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.Len(t, ips, 1)
|
2020-04-15 11:36:47 +00:00
|
|
|
assert.Equal(t, net.ParseIP("127.0.0.1"), ips[0])
|
|
|
|
|
|
|
|
// Unknown host
|
2020-04-16 15:56:47 +00:00
|
|
|
ips = ah.Process("newhost", dns.TypeA)
|
2020-04-15 11:36:47 +00:00
|
|
|
assert.Nil(t, ips)
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2020-06-17 16:36:35 +00:00
|
|
|
// Unknown host (comment)
|
|
|
|
ips = ah.Process("comment", dns.TypeA)
|
|
|
|
assert.Nil(t, ips)
|
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
// Test hosts file
|
2020-03-20 12:05:43 +00:00
|
|
|
table := ah.List()
|
2020-11-06 14:34:40 +00:00
|
|
|
names, ok := table["127.0.0.1"]
|
2020-06-11 08:11:52 +00:00
|
|
|
assert.True(t, ok)
|
2020-11-06 14:34:40 +00:00
|
|
|
assert.Equal(t, []string{"host", "localhost"}, names)
|
2020-04-16 15:56:47 +00:00
|
|
|
|
|
|
|
// Test PTR
|
|
|
|
a, _ := dns.ReverseAddr("127.0.0.1")
|
|
|
|
a = strings.TrimSuffix(a, ".")
|
2020-11-06 14:34:40 +00:00
|
|
|
hosts := ah.ProcessReverse(a, dns.TypePTR)
|
|
|
|
if assert.Len(t, hosts, 2) {
|
|
|
|
assert.Equal(t, hosts[0], "host")
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:56:47 +00:00
|
|
|
a, _ = dns.ReverseAddr("::1")
|
|
|
|
a = strings.TrimSuffix(a, ".")
|
2020-11-06 14:34:40 +00:00
|
|
|
hosts = ah.ProcessReverse(a, dns.TypePTR)
|
|
|
|
if assert.Len(t, hosts, 1) {
|
|
|
|
assert.Equal(t, hosts[0], "localhost")
|
|
|
|
}
|
2020-04-15 11:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoHostsFSNotify(t *testing.T) {
|
|
|
|
ah := AutoHosts{}
|
|
|
|
|
|
|
|
dir := prepareTestDir()
|
|
|
|
defer func() { _ = os.RemoveAll(dir) }()
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
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
|
2020-04-16 15:56:47 +00:00
|
|
|
ips := ah.Process("newhost", dns.TypeA)
|
2020-04-15 11:36:47 +00:00
|
|
|
assert.Nil(t, ips)
|
|
|
|
|
|
|
|
// Stat monitoring for changes
|
|
|
|
ah.Start()
|
|
|
|
defer ah.Close()
|
|
|
|
|
|
|
|
// Update file
|
2020-03-20 12:05:43 +00:00
|
|
|
_, _ = f.WriteString("127.0.0.2 newhost\n")
|
2020-04-15 11:36:47 +00:00
|
|
|
_ = f.Sync()
|
|
|
|
|
2020-03-20 12:05:43 +00:00
|
|
|
// wait until fsnotify has triggerred and processed the file-modification event
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
// Check if we are notified about changes
|
2020-04-16 15:56:47 +00:00
|
|
|
ips = ah.Process("newhost", dns.TypeA)
|
2020-04-15 11:36:47 +00:00
|
|
|
assert.NotNil(t, ips)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.Len(t, ips, 1)
|
2020-04-15 11:36:47 +00:00
|
|
|
assert.Equal(t, "127.0.0.2", ips[0].String())
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
2020-04-16 15:56:47 +00:00
|
|
|
|
|
|
|
func TestIP(t *testing.T) {
|
2020-06-23 09:13:13 +00:00
|
|
|
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"))
|
2020-04-16 15:56:47 +00:00
|
|
|
}
|