2021-04-12 15:31:45 +00:00
|
|
|
package aghnet
|
2020-03-20 12:05:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
2021-05-21 11:55:42 +00:00
|
|
|
f, err := os.CreateTemp(dir, "")
|
2021-05-31 17:11:06 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-09 16:38:31 +00:00
|
|
|
require.NotNil(t, f)
|
2021-05-31 17:11:06 +00:00
|
|
|
|
2021-02-09 16:38:31 +00:00
|
|
|
t.Cleanup(func() {
|
2021-05-31 17:11:06 +00:00
|
|
|
assert.NoError(t, f.Close())
|
2021-02-09 16:38:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2021-05-31 17:11:06 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-09 16:38:31 +00:00
|
|
|
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-06-29 12:53:28 +00:00
|
|
|
names, ok := ehc.List().Get(net.IP{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
|
|
|
|
wantHost string
|
2021-05-31 17:11:06 +00:00
|
|
|
wantLen int
|
2021-02-09 16:38:31 +00:00
|
|
|
}{
|
2021-05-31 17:11:06 +00:00
|
|
|
{wantIP: "127.0.0.1", wantHost: "host", wantLen: 2},
|
|
|
|
{wantIP: "::1", wantHost: "localhost", wantLen: 1},
|
2021-02-09 16:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
a, err := dns.ReverseAddr(tc.wantIP)
|
2021-05-31 17:11:06 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-09 16:38:31 +00:00
|
|
|
|
|
|
|
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")
|
2021-05-31 17:11:06 +00:00
|
|
|
require.NoError(t, f.Sync())
|
2020-04-15 11:36:47 +00:00
|
|
|
|
2021-09-13 13:00:36 +00:00
|
|
|
// Wait until fsnotify has triggered 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
|
|
|
}
|