2020-03-20 15:05:43 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
2020-04-16 18:56:47 +03:00
|
|
|
"strings"
|
2020-03-20 15:05:43 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-04 20:35:13 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2020-04-16 18:56:47 +03:00
|
|
|
"github.com/miekg/dns"
|
2020-03-20 15:05:43 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-09 19:38:31 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-03-20 15:05:43 +03:00
|
|
|
)
|
|
|
|
|
2020-11-16 15:52:05 +03:00
|
|
|
func TestMain(m *testing.M) {
|
2021-02-04 20:35:13 +03:00
|
|
|
aghtest.DiscardLogOutput(m)
|
2020-11-16 15:52:05 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
func prepareTestFile(t *testing.T) (f *os.File) {
|
|
|
|
t.Helper()
|
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
dir := t.TempDir()
|
2021-02-09 19:38:31 +03:00
|
|
|
|
|
|
|
f, err := ioutil.TempFile(dir, "")
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, f)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
assert.Nil(t, f.Close())
|
|
|
|
})
|
|
|
|
|
|
|
|
return f
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
func assertWriting(t *testing.T, f *os.File, strs ...string) {
|
|
|
|
t.Helper()
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
for _, str := range strs {
|
|
|
|
n, err := f.WriteString(str)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, n, len(str))
|
|
|
|
}
|
|
|
|
}
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
func TestAutoHostsResolution(t *testing.T) {
|
|
|
|
ah := &AutoHosts{}
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
f := prepareTestFile(t)
|
2020-04-16 18:56:47 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
assertWriting(t, f,
|
|
|
|
" 127.0.0.1 host localhost # comment \n",
|
|
|
|
" ::1 localhost#comment \n",
|
|
|
|
)
|
2020-03-20 15:05:43 +03:00
|
|
|
ah.Init(f.Name())
|
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
t.Run("existing_host", func(t *testing.T) {
|
|
|
|
ips := ah.Process("localhost", dns.TypeA)
|
|
|
|
require.Len(t, ips, 1)
|
|
|
|
assert.Equal(t, net.IPv4(127, 0, 0, 1), ips[0])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unknown_host", func(t *testing.T) {
|
|
|
|
ips := ah.Process("newhost", dns.TypeA)
|
|
|
|
assert.Nil(t, ips)
|
|
|
|
|
|
|
|
// Comment.
|
|
|
|
ips = ah.Process("comment", dns.TypeA)
|
|
|
|
assert.Nil(t, ips)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("hosts_file", func(t *testing.T) {
|
|
|
|
names, ok := ah.List()["127.0.0.1"]
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, []string{"host", "localhost"}, names)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ptr", func(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
wantIP string
|
|
|
|
wantLen int
|
|
|
|
wantHost string
|
|
|
|
}{
|
|
|
|
{wantIP: "127.0.0.1", wantLen: 2, wantHost: "host"},
|
|
|
|
{wantIP: "::1", wantLen: 1, wantHost: "localhost"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
a, err := dns.ReverseAddr(tc.wantIP)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
a = strings.TrimSuffix(a, ".")
|
|
|
|
hosts := ah.ProcessReverse(a, dns.TypePTR)
|
|
|
|
require.Len(t, hosts, tc.wantLen)
|
|
|
|
assert.Equal(t, tc.wantHost, hosts[0])
|
|
|
|
}
|
|
|
|
})
|
2020-04-15 14:36:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoHostsFSNotify(t *testing.T) {
|
2021-02-09 19:38:31 +03:00
|
|
|
ah := &AutoHosts{}
|
2020-04-15 14:36:47 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
f := prepareTestFile(t)
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
assertWriting(t, f, " 127.0.0.1 host localhost \n")
|
2020-04-15 14:36:47 +03:00
|
|
|
ah.Init(f.Name())
|
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
t.Run("unknown_host", func(t *testing.T) {
|
|
|
|
ips := ah.Process("newhost", dns.TypeA)
|
|
|
|
assert.Nil(t, ips)
|
|
|
|
})
|
2020-04-15 14:36:47 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
// Start monitoring for changes.
|
2020-04-15 14:36:47 +03:00
|
|
|
ah.Start()
|
2021-02-09 19:38:31 +03:00
|
|
|
t.Cleanup(ah.Close)
|
2020-04-15 14:36:47 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
assertWriting(t, f, "127.0.0.2 newhost\n")
|
|
|
|
require.Nil(t, f.Sync())
|
2020-04-15 14:36:47 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
// Wait until fsnotify has triggerred and processed the
|
|
|
|
// file-modification event.
|
2020-03-20 15:05:43 +03:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
t.Run("notified", func(t *testing.T) {
|
|
|
|
ips := ah.Process("newhost", dns.TypeA)
|
|
|
|
assert.NotNil(t, ips)
|
|
|
|
require.Len(t, ips, 1)
|
|
|
|
assert.True(t, net.IP{127, 0, 0, 2}.Equal(ips[0]))
|
|
|
|
})
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|