2020-09-08 13:56:45 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2021-01-20 17:27:53 +03:00
|
|
|
"net"
|
2020-09-08 13:56:45 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-11 17:32:58 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-08 13:56:45 +03:00
|
|
|
)
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
func TestIsBlockedClientID(t *testing.T) {
|
|
|
|
clientID := "client-1"
|
|
|
|
clients := []string{clientID}
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
a, err := newAccessCtx(clients, nil, nil)
|
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
assert.False(t, a.isBlockedClientID(clientID))
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
a, err = newAccessCtx(nil, clients, nil)
|
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
assert.True(t, a.isBlockedClientID(clientID))
|
2020-09-08 13:56:45 +03:00
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
func TestIsBlockedHost(t *testing.T) {
|
|
|
|
a, err := newAccessCtx(nil, nil, []string{
|
2020-12-07 15:38:05 +03:00
|
|
|
"host1",
|
2020-09-08 13:56:45 +03:00
|
|
|
"*.host.com",
|
|
|
|
"||host3.com^",
|
2021-04-20 16:26:19 +03:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
testCases := []struct {
|
2021-06-29 15:53:28 +03:00
|
|
|
name string
|
|
|
|
host string
|
|
|
|
want bool
|
2021-03-11 17:32:58 +03:00
|
|
|
}{{
|
2021-06-29 15:53:28 +03:00
|
|
|
name: "plain_match",
|
|
|
|
host: "host1",
|
|
|
|
want: true,
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
2021-06-29 15:53:28 +03:00
|
|
|
name: "plain_mismatch",
|
|
|
|
host: "host2",
|
|
|
|
want: false,
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
2021-06-29 15:53:28 +03:00
|
|
|
name: "subdomain_match_short",
|
|
|
|
host: "asdf.host.com",
|
|
|
|
want: true,
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
2021-06-29 15:53:28 +03:00
|
|
|
name: "subdomain_match_long",
|
|
|
|
host: "qwer.asdf.host.com",
|
|
|
|
want: true,
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
2021-06-29 15:53:28 +03:00
|
|
|
name: "subdomain_mismatch_no_lead",
|
|
|
|
host: "host.com",
|
|
|
|
want: false,
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
2021-06-29 15:53:28 +03:00
|
|
|
name: "subdomain_mismatch_bad_asterisk",
|
|
|
|
host: "asdf.zhost.com",
|
|
|
|
want: false,
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
2021-06-29 15:53:28 +03:00
|
|
|
name: "rule_match_simple",
|
|
|
|
host: "host3.com",
|
|
|
|
want: true,
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
2021-06-29 15:53:28 +03:00
|
|
|
name: "rule_match_complex",
|
|
|
|
host: "asdf.host3.com",
|
|
|
|
want: true,
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
2021-06-29 15:53:28 +03:00
|
|
|
name: "rule_mismatch",
|
|
|
|
host: ".host3.com",
|
|
|
|
want: false,
|
2021-03-11 17:32:58 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-06-29 15:53:28 +03:00
|
|
|
assert.Equal(t, tc.want, a.isBlockedHost(tc.host))
|
2021-03-11 17:32:58 +03:00
|
|
|
})
|
|
|
|
}
|
2020-09-08 13:56:45 +03:00
|
|
|
}
|
2021-06-29 15:53:28 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|