mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 06:25:44 +03:00
e08a64ebe4
Updates #2624. Updates #3162. Squashed commit of the following: commit 68860da717a23a0bfeba14b7fe10b5e4ad38726d Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Jun 29 15:41:33 2021 +0300 all: imp types, names commit ebd4ec26636853d0d58c4e331e6a78feede20813 Merge: 239eb72116e5e09c
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Jun 29 15:14:33 2021 +0300 Merge branch 'master' into 2624-clientid-access commit 239eb7215abc47e99a0300a0f4cf56002689b1a9 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Jun 29 15:13:10 2021 +0300 all: fix client blocking check commit e6bece3ea8367b3cbe3d90702a3368c870ad4f13 Merge: 9935f2a39d1656b5
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Jun 29 13:12:28 2021 +0300 Merge branch 'master' into 2624-clientid-access commit 9935f2a30bcfae2b853f3ef610c0ab7a56a8f448 Author: Ildar Kamalov <ik@adguard.com> Date: Tue Jun 29 11:26:51 2021 +0300 client: show block button for client id commit ed786a6a74a081cd89e9d67df3537a4fadd54831 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Jun 25 15:56:23 2021 +0300 client: imp i18n commit 4fed21c68473ad408960c08a7d87624cabce1911 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Jun 25 15:34:09 2021 +0300 all: imp i18n, docs commit 55e65c0d6b939560c53dcb834a4557eb3853d194 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Jun 25 13:34:01 2021 +0300 all: fix cache, imp code, docs, tests commit c1e5a83e76deb44b1f92729bb9ddfcc6a96ac4a8 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Jun 24 19:27:12 2021 +0300 all: allow clientid in access settings
137 lines
2.7 KiB
Go
137 lines
2.7 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestIsBlockedClientID(t *testing.T) {
|
|
clientID := "client-1"
|
|
clients := []string{clientID}
|
|
|
|
a, err := newAccessCtx(clients, nil, nil)
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, a.isBlockedClientID(clientID))
|
|
|
|
a, err = newAccessCtx(nil, clients, nil)
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, a.isBlockedClientID(clientID))
|
|
}
|
|
|
|
func TestIsBlockedHost(t *testing.T) {
|
|
a, err := newAccessCtx(nil, nil, []string{
|
|
"host1",
|
|
"*.host.com",
|
|
"||host3.com^",
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
host string
|
|
want bool
|
|
}{{
|
|
name: "plain_match",
|
|
host: "host1",
|
|
want: true,
|
|
}, {
|
|
name: "plain_mismatch",
|
|
host: "host2",
|
|
want: false,
|
|
}, {
|
|
name: "subdomain_match_short",
|
|
host: "asdf.host.com",
|
|
want: true,
|
|
}, {
|
|
name: "subdomain_match_long",
|
|
host: "qwer.asdf.host.com",
|
|
want: true,
|
|
}, {
|
|
name: "subdomain_mismatch_no_lead",
|
|
host: "host.com",
|
|
want: false,
|
|
}, {
|
|
name: "subdomain_mismatch_bad_asterisk",
|
|
host: "asdf.zhost.com",
|
|
want: false,
|
|
}, {
|
|
name: "rule_match_simple",
|
|
host: "host3.com",
|
|
want: true,
|
|
}, {
|
|
name: "rule_match_complex",
|
|
host: "asdf.host3.com",
|
|
want: true,
|
|
}, {
|
|
name: "rule_mismatch",
|
|
host: ".host3.com",
|
|
want: false,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
assert.Equal(t, tc.want, a.isBlockedHost(tc.host))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsBlockedIP(t *testing.T) {
|
|
clients := []string{
|
|
"1.2.3.4",
|
|
"5.6.7.8/24",
|
|
}
|
|
|
|
allowCtx, err := newAccessCtx(clients, nil, nil)
|
|
require.NoError(t, err)
|
|
|
|
blockCtx, err := newAccessCtx(nil, clients, nil)
|
|
require.NoError(t, err)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
wantRule string
|
|
ip net.IP
|
|
wantBlocked bool
|
|
}{{
|
|
name: "match_ip",
|
|
wantRule: "1.2.3.4",
|
|
ip: net.IP{1, 2, 3, 4},
|
|
wantBlocked: true,
|
|
}, {
|
|
name: "match_cidr",
|
|
wantRule: "5.6.7.8/24",
|
|
ip: net.IP{5, 6, 7, 100},
|
|
wantBlocked: true,
|
|
}, {
|
|
name: "no_match_ip",
|
|
wantRule: "",
|
|
ip: net.IP{9, 2, 3, 4},
|
|
wantBlocked: false,
|
|
}, {
|
|
name: "no_match_cidr",
|
|
wantRule: "",
|
|
ip: net.IP{9, 6, 7, 100},
|
|
wantBlocked: false,
|
|
}}
|
|
|
|
t.Run("allow", func(t *testing.T) {
|
|
for _, tc := range testCases {
|
|
blocked, rule := allowCtx.isBlockedIP(tc.ip)
|
|
assert.Equal(t, !tc.wantBlocked, blocked)
|
|
assert.Equal(t, tc.wantRule, rule)
|
|
}
|
|
})
|
|
|
|
t.Run("block", func(t *testing.T) {
|
|
for _, tc := range testCases {
|
|
blocked, rule := blockCtx.isBlockedIP(tc.ip)
|
|
assert.Equal(t, tc.wantBlocked, blocked)
|
|
assert.Equal(t, tc.wantRule, rule)
|
|
}
|
|
})
|
|
}
|