2021-04-12 18:31:45 +03:00
|
|
|
package aghnet
|
2020-03-20 15:05:43 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
2021-05-21 14:55:42 +03:00
|
|
|
f, err := os.CreateTemp(dir, "")
|
2021-05-31 20:11:06 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-09 19:38:31 +03:00
|
|
|
require.NotNil(t, f)
|
2021-05-31 20:11:06 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
t.Cleanup(func() {
|
2021-05-31 20:11:06 +03:00
|
|
|
assert.NoError(t, f.Close())
|
2021-02-09 19:38:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2021-05-31 20:11:06 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-09 19:38:31 +03:00
|
|
|
assert.Equal(t, n, len(str))
|
|
|
|
}
|
|
|
|
}
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
func TestEtcHostsContainerResolution(t *testing.T) {
|
|
|
|
ehc := &EtcHostsContainer{}
|
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",
|
|
|
|
)
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.Init(f.Name())
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
t.Run("existing_host", func(t *testing.T) {
|
2021-04-12 18:31:45 +03:00
|
|
|
ips := ehc.Process("localhost", dns.TypeA)
|
2021-02-09 19:38:31 +03: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 18:31:45 +03:00
|
|
|
ips := ehc.Process("newhost", dns.TypeA)
|
2021-02-09 19:38:31 +03:00
|
|
|
assert.Nil(t, ips)
|
|
|
|
|
|
|
|
// Comment.
|
2021-04-12 18:31:45 +03:00
|
|
|
ips = ehc.Process("comment", dns.TypeA)
|
2021-02-09 19:38:31 +03:00
|
|
|
assert.Nil(t, ips)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("hosts_file", func(t *testing.T) {
|
2021-04-12 18:31:45 +03:00
|
|
|
names, ok := ehc.List()["127.0.0.1"]
|
2021-02-09 19:38:31 +03: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 20:11:06 +03:00
|
|
|
wantLen int
|
2021-02-09 19:38:31 +03:00
|
|
|
}{
|
2021-05-31 20:11:06 +03:00
|
|
|
{wantIP: "127.0.0.1", wantHost: "host", wantLen: 2},
|
|
|
|
{wantIP: "::1", wantHost: "localhost", wantLen: 1},
|
2021-02-09 19:38:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
a, err := dns.ReverseAddr(tc.wantIP)
|
2021-05-31 20:11:06 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-09 19:38:31 +03:00
|
|
|
|
|
|
|
a = strings.TrimSuffix(a, ".")
|
2021-04-12 18:31:45 +03:00
|
|
|
hosts := ehc.ProcessReverse(a, dns.TypePTR)
|
2021-02-09 19:38:31 +03:00
|
|
|
require.Len(t, hosts, tc.wantLen)
|
|
|
|
assert.Equal(t, tc.wantHost, hosts[0])
|
|
|
|
}
|
|
|
|
})
|
2020-04-15 14:36:47 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
func TestEtcHostsContainerFSNotify(t *testing.T) {
|
|
|
|
ehc := &EtcHostsContainer{}
|
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")
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.Init(f.Name())
|
2020-04-15 14:36:47 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
t.Run("unknown_host", func(t *testing.T) {
|
2021-04-12 18:31:45 +03:00
|
|
|
ips := ehc.Process("newhost", dns.TypeA)
|
2021-02-09 19:38:31 +03:00
|
|
|
assert.Nil(t, ips)
|
|
|
|
})
|
2020-04-15 14:36:47 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
// Start monitoring for changes.
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.Start()
|
|
|
|
t.Cleanup(ehc.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")
|
2021-05-31 20:11:06 +03:00
|
|
|
require.NoError(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) {
|
2021-04-12 18:31:45 +03:00
|
|
|
ips := ehc.Process("newhost", dns.TypeA)
|
2021-02-09 19:38:31 +03: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 15:05:43 +03:00
|
|
|
}
|