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