2023-07-20 14:26:35 +03:00
|
|
|
package aghnet_test
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
import (
|
2023-08-22 16:45:11 +03:00
|
|
|
"net/netip"
|
2021-10-14 19:39:21 +03:00
|
|
|
"path"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"testing/fstest"
|
2022-01-12 12:14:59 +03:00
|
|
|
"time"
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-07-20 14:26:35 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-10-14 19:39:21 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2023-08-22 16:45:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/hostsfile"
|
2022-10-25 15:08:12 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2022-01-27 20:18:52 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-10-14 19:39:21 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewHostsContainer(t *testing.T) {
|
|
|
|
const dirname = "dir"
|
|
|
|
const filename = "file1"
|
|
|
|
|
|
|
|
p := path.Join(dirname, filename)
|
|
|
|
|
|
|
|
testFS := fstest.MapFS{
|
|
|
|
p: &fstest.MapFile{Data: []byte("127.0.0.1 localhost")},
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2021-11-17 17:21:10 +03:00
|
|
|
wantErr error
|
|
|
|
name string
|
|
|
|
paths []string
|
2021-10-14 19:39:21 +03:00
|
|
|
}{{
|
2021-11-17 17:21:10 +03:00
|
|
|
wantErr: nil,
|
|
|
|
name: "one_file",
|
|
|
|
paths: []string{p},
|
2021-10-14 19:39:21 +03:00
|
|
|
}, {
|
2023-07-20 14:26:35 +03:00
|
|
|
wantErr: aghnet.ErrNoHostsPaths,
|
2021-11-17 17:21:10 +03:00
|
|
|
name: "no_files",
|
|
|
|
paths: []string{},
|
2021-10-14 19:39:21 +03:00
|
|
|
}, {
|
2023-07-20 14:26:35 +03:00
|
|
|
wantErr: aghnet.ErrNoHostsPaths,
|
2021-11-17 17:21:10 +03:00
|
|
|
name: "non-existent_file",
|
|
|
|
paths: []string{path.Join(dirname, filename+"2")},
|
2021-10-14 19:39:21 +03:00
|
|
|
}, {
|
2021-11-17 17:21:10 +03:00
|
|
|
wantErr: nil,
|
|
|
|
name: "whole_dir",
|
|
|
|
paths: []string{dirname},
|
2021-10-14 19:39:21 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
onAdd := func(name string) (err error) {
|
|
|
|
assert.Contains(t, tc.paths, name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var eventsCalledCounter uint32
|
|
|
|
eventsCh := make(chan struct{})
|
|
|
|
onEvents := func() (e <-chan struct{}) {
|
|
|
|
assert.Equal(t, uint32(1), atomic.AddUint32(&eventsCalledCounter, 1))
|
|
|
|
|
|
|
|
return eventsCh
|
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
hc, err := aghnet.NewHostsContainer(testFS, &aghtest.FSWatcher{
|
2024-02-13 13:19:22 +03:00
|
|
|
OnStart: func() (_ error) { panic("not implemented") },
|
2021-10-14 19:39:21 +03:00
|
|
|
OnEvents: onEvents,
|
|
|
|
OnAdd: onAdd,
|
2023-03-28 13:27:46 +03:00
|
|
|
OnClose: func() (err error) { return nil },
|
2021-10-14 19:39:21 +03:00
|
|
|
}, tc.paths...)
|
|
|
|
if tc.wantErr != nil {
|
|
|
|
require.ErrorIs(t, err, tc.wantErr)
|
|
|
|
|
|
|
|
assert.Nil(t, hc)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-01-27 20:18:52 +03:00
|
|
|
testutil.CleanupAndRequireSuccess(t, hc.Close)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, hc)
|
|
|
|
|
|
|
|
assert.NotNil(t, <-hc.Upd())
|
|
|
|
|
|
|
|
eventsCh <- struct{}{}
|
|
|
|
assert.Equal(t, uint32(1), atomic.LoadUint32(&eventsCalledCounter))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("nil_fs", func(t *testing.T) {
|
|
|
|
require.Panics(t, func() {
|
2023-08-22 16:45:11 +03:00
|
|
|
_, _ = aghnet.NewHostsContainer(nil, &aghtest.FSWatcher{
|
2024-02-13 13:19:22 +03:00
|
|
|
OnStart: func() (_ error) { panic("not implemented") },
|
2021-10-14 19:39:21 +03:00
|
|
|
// Those shouldn't panic.
|
|
|
|
OnEvents: func() (e <-chan struct{}) { return nil },
|
|
|
|
OnAdd: func(name string) (err error) { return nil },
|
|
|
|
OnClose: func() (err error) { return nil },
|
|
|
|
}, p)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil_watcher", func(t *testing.T) {
|
|
|
|
require.Panics(t, func() {
|
2023-08-22 16:45:11 +03:00
|
|
|
_, _ = aghnet.NewHostsContainer(testFS, nil, p)
|
2021-10-14 19:39:21 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("err_watcher", func(t *testing.T) {
|
|
|
|
const errOnAdd errors.Error = "error"
|
|
|
|
|
|
|
|
errWatcher := &aghtest.FSWatcher{
|
2024-02-13 13:19:22 +03:00
|
|
|
OnStart: func() (_ error) { panic("not implemented") },
|
2021-10-14 19:39:21 +03:00
|
|
|
OnEvents: func() (e <-chan struct{}) { panic("not implemented") },
|
|
|
|
OnAdd: func(name string) (err error) { return errOnAdd },
|
2023-03-28 13:27:46 +03:00
|
|
|
OnClose: func() (err error) { return nil },
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
hc, err := aghnet.NewHostsContainer(testFS, errWatcher, p)
|
2021-10-14 19:39:21 +03:00
|
|
|
require.ErrorIs(t, err, errOnAdd)
|
|
|
|
|
|
|
|
assert.Nil(t, hc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-12 12:14:59 +03:00
|
|
|
func TestHostsContainer_refresh(t *testing.T) {
|
|
|
|
// TODO(e.burkov): Test the case with no actual updates.
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2022-10-25 15:08:12 +03:00
|
|
|
ip := netutil.IPv4Localhost()
|
2022-01-12 12:14:59 +03:00
|
|
|
ipStr := ip.String()
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
anotherIPStr := "1.2.3.4"
|
|
|
|
anotherIP := netip.MustParseAddr(anotherIPStr)
|
|
|
|
|
2023-12-07 16:48:55 +03:00
|
|
|
r1 := &hostsfile.Record{
|
|
|
|
Addr: ip,
|
|
|
|
Source: "file1",
|
|
|
|
Names: []string{"hostname"},
|
|
|
|
}
|
|
|
|
r2 := &hostsfile.Record{
|
|
|
|
Addr: anotherIP,
|
|
|
|
Source: "file2",
|
|
|
|
Names: []string{"alias"},
|
|
|
|
}
|
|
|
|
|
|
|
|
r1Data, _ := r1.MarshalText()
|
|
|
|
r2Data, _ := r2.MarshalText()
|
|
|
|
|
|
|
|
testFS := fstest.MapFS{"dir/file1": &fstest.MapFile{Data: r1Data}}
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2021-11-16 16:16:38 +03:00
|
|
|
// event is a convenient alias for an empty struct{} to emit test events.
|
|
|
|
type event = struct{}
|
|
|
|
|
|
|
|
eventsCh := make(chan event, 1)
|
2021-10-14 19:39:21 +03:00
|
|
|
t.Cleanup(func() { close(eventsCh) })
|
|
|
|
|
|
|
|
w := &aghtest.FSWatcher{
|
2024-02-13 13:19:22 +03:00
|
|
|
OnStart: func() (_ error) { panic("not implemented") },
|
2021-11-16 16:16:38 +03:00
|
|
|
OnEvents: func() (e <-chan event) { return eventsCh },
|
2021-10-14 19:39:21 +03:00
|
|
|
OnAdd: func(name string) (err error) {
|
2022-01-12 12:14:59 +03:00
|
|
|
assert.Equal(t, "dir", name)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2023-03-28 13:27:46 +03:00
|
|
|
OnClose: func() (err error) { return nil },
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:45:11 +03:00
|
|
|
hc, err := aghnet.NewHostsContainer(testFS, w, "dir")
|
2021-10-14 19:39:21 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-27 20:18:52 +03:00
|
|
|
testutil.CleanupAndRequireSuccess(t, hc.Close)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-12-07 16:48:55 +03:00
|
|
|
strg, _ := hostsfile.NewDefaultStorage()
|
|
|
|
strg.Add(r1)
|
2022-07-07 19:33:32 +03:00
|
|
|
|
2023-12-07 16:48:55 +03:00
|
|
|
t.Run("initial_refresh", func(t *testing.T) {
|
|
|
|
upd, ok := testutil.RequireReceive(t, hc.Upd(), 1*time.Second)
|
2022-10-04 16:02:55 +03:00
|
|
|
require.True(t, ok)
|
2021-10-14 19:39:21 +03:00
|
|
|
|
2023-12-07 16:48:55 +03:00
|
|
|
assert.True(t, strg.Equal(upd))
|
2021-10-14 19:39:21 +03:00
|
|
|
})
|
|
|
|
|
2023-12-07 16:48:55 +03:00
|
|
|
strg.Add(r2)
|
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
t.Run("second_refresh", func(t *testing.T) {
|
2023-12-07 16:48:55 +03:00
|
|
|
testFS["dir/file2"] = &fstest.MapFile{Data: r2Data}
|
2022-01-12 12:14:59 +03:00
|
|
|
eventsCh <- event{}
|
2022-07-07 19:33:32 +03:00
|
|
|
|
2023-12-07 16:48:55 +03:00
|
|
|
upd, ok := testutil.RequireReceive(t, hc.Upd(), 1*time.Second)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.True(t, strg.Equal(upd))
|
2021-11-16 16:16:38 +03:00
|
|
|
})
|
|
|
|
|
2022-01-12 12:14:59 +03:00
|
|
|
t.Run("double_refresh", func(t *testing.T) {
|
|
|
|
// Make a change once.
|
2023-12-07 16:48:55 +03:00
|
|
|
testFS["dir/file1"] = &fstest.MapFile{Data: []byte(ipStr + " alias\n")}
|
2022-01-12 12:14:59 +03:00
|
|
|
eventsCh <- event{}
|
|
|
|
|
|
|
|
// Require the changes are written.
|
2023-12-07 16:48:55 +03:00
|
|
|
current, ok := testutil.RequireReceive(t, hc.Upd(), 1*time.Second)
|
|
|
|
require.True(t, ok)
|
2021-11-16 16:16:38 +03:00
|
|
|
|
2023-12-07 16:48:55 +03:00
|
|
|
require.Empty(t, current.ByName("hostname"))
|
2022-01-12 12:14:59 +03:00
|
|
|
|
|
|
|
// Make a change again.
|
2023-12-07 16:48:55 +03:00
|
|
|
testFS["dir/file2"] = &fstest.MapFile{Data: []byte(ipStr + " hostname\n")}
|
2022-01-12 12:14:59 +03:00
|
|
|
eventsCh <- event{}
|
|
|
|
|
|
|
|
// Require the changes are written.
|
2023-12-07 16:48:55 +03:00
|
|
|
current, ok = testutil.RequireReceive(t, hc.Upd(), 1*time.Second)
|
|
|
|
require.True(t, ok)
|
2022-01-12 12:14:59 +03:00
|
|
|
|
2023-12-07 16:48:55 +03:00
|
|
|
require.NotEmpty(t, current.ByName("hostname"))
|
2021-10-14 19:39:21 +03:00
|
|
|
})
|
|
|
|
}
|