mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 18:08:51 +03:00
5be0e84719
Squashed commit of the following: commit 081d10e6909def3a075707e75dbd0c5f63f91903 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Jul 20 14:17:01 2023 +0300 aghnet: fix docs commit 7433b72c0653cb33fe5ff810ae8a1346a6994f95 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Jul 20 14:03:16 2023 +0300 all: imp tests; upd golibs
144 lines
3.2 KiB
Go
144 lines
3.2 KiB
Go
package aghnet
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/netip"
|
|
"path"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/golibs/testutil/fakefs"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const nl = "\n"
|
|
|
|
func TestHostsContainer_PathsToPatterns(t *testing.T) {
|
|
gsfs := fstest.MapFS{
|
|
"dir_0/file_1": &fstest.MapFile{Data: []byte{1}},
|
|
"dir_0/file_2": &fstest.MapFile{Data: []byte{2}},
|
|
"dir_0/dir_1/file_3": &fstest.MapFile{Data: []byte{3}},
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
paths []string
|
|
want []string
|
|
}{{
|
|
name: "no_paths",
|
|
paths: nil,
|
|
want: nil,
|
|
}, {
|
|
name: "single_file",
|
|
paths: []string{"dir_0/file_1"},
|
|
want: []string{"dir_0/file_1"},
|
|
}, {
|
|
name: "several_files",
|
|
paths: []string{"dir_0/file_1", "dir_0/file_2"},
|
|
want: []string{"dir_0/file_1", "dir_0/file_2"},
|
|
}, {
|
|
name: "whole_dir",
|
|
paths: []string{"dir_0"},
|
|
want: []string{"dir_0/*"},
|
|
}, {
|
|
name: "file_and_dir",
|
|
paths: []string{"dir_0/file_1", "dir_0/dir_1"},
|
|
want: []string{"dir_0/file_1", "dir_0/dir_1/*"},
|
|
}, {
|
|
name: "non-existing",
|
|
paths: []string{path.Join("dir_0", "file_3")},
|
|
want: nil,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
patterns, err := pathsToPatterns(gsfs, tc.paths)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.want, patterns)
|
|
})
|
|
}
|
|
|
|
t.Run("bad_file", func(t *testing.T) {
|
|
const errStat errors.Error = "bad file"
|
|
|
|
badFS := &fakefs.StatFS{
|
|
OnOpen: func(_ string) (f fs.File, err error) { panic("not implemented") },
|
|
OnStat: func(name string) (fi fs.FileInfo, err error) {
|
|
return nil, errStat
|
|
},
|
|
}
|
|
|
|
_, err := pathsToPatterns(badFS, []string{""})
|
|
assert.ErrorIs(t, err, errStat)
|
|
})
|
|
}
|
|
|
|
func TestUniqueRules_ParseLine(t *testing.T) {
|
|
ip := netutil.IPv4Localhost()
|
|
ipStr := ip.String()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
line string
|
|
wantIP netip.Addr
|
|
wantHosts []string
|
|
}{{
|
|
name: "simple",
|
|
line: ipStr + ` hostname`,
|
|
wantIP: ip,
|
|
wantHosts: []string{"hostname"},
|
|
}, {
|
|
name: "aliases",
|
|
line: ipStr + ` hostname alias`,
|
|
wantIP: ip,
|
|
wantHosts: []string{"hostname", "alias"},
|
|
}, {
|
|
name: "invalid_line",
|
|
line: ipStr,
|
|
wantIP: netip.Addr{},
|
|
wantHosts: nil,
|
|
}, {
|
|
name: "invalid_line_hostname",
|
|
line: ipStr + ` # hostname`,
|
|
wantIP: ip,
|
|
wantHosts: nil,
|
|
}, {
|
|
name: "commented_aliases",
|
|
line: ipStr + ` hostname # alias`,
|
|
wantIP: ip,
|
|
wantHosts: []string{"hostname"},
|
|
}, {
|
|
name: "whole_comment",
|
|
line: `# ` + ipStr + ` hostname`,
|
|
wantIP: netip.Addr{},
|
|
wantHosts: nil,
|
|
}, {
|
|
name: "partial_comment",
|
|
line: ipStr + ` host#name`,
|
|
wantIP: ip,
|
|
wantHosts: []string{"host"},
|
|
}, {
|
|
name: "empty",
|
|
line: ``,
|
|
wantIP: netip.Addr{},
|
|
wantHosts: nil,
|
|
}, {
|
|
name: "bad_hosts",
|
|
line: ipStr + ` bad..host bad._tld empty.tld. ok.host`,
|
|
wantIP: ip,
|
|
wantHosts: []string{"ok.host"},
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
hp := hostsParser{}
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, hosts := hp.parseLine(tc.line)
|
|
assert.Equal(t, tc.wantIP, got)
|
|
assert.Equal(t, tc.wantHosts, hosts)
|
|
})
|
|
}
|
|
}
|