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