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