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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsBlockedIPAllowed(t *testing.T) {
|
|
|
|
a := &accessCtx{}
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Nil(t, a.Init([]string{"1.1.1.1", "2.2.0.0/16"}, nil, nil))
|
2020-09-08 13:56:45 +03:00
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
disallowed, disallowedRule := a.IsBlockedIP(net.IPv4(1, 1, 1, 1))
|
2020-09-08 13:56:45 +03:00
|
|
|
assert.False(t, disallowed)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Empty(t, disallowedRule)
|
2020-09-08 13:56:45 +03:00
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
disallowed, disallowedRule = a.IsBlockedIP(net.IPv4(1, 1, 1, 2))
|
2020-09-08 13:56:45 +03:00
|
|
|
assert.True(t, disallowed)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Empty(t, disallowedRule)
|
2020-09-08 13:56:45 +03:00
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
disallowed, disallowedRule = a.IsBlockedIP(net.IPv4(2, 2, 1, 1))
|
2020-09-08 13:56:45 +03:00
|
|
|
assert.False(t, disallowed)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Empty(t, disallowedRule)
|
2020-09-08 13:56:45 +03:00
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
disallowed, disallowedRule = a.IsBlockedIP(net.IPv4(2, 3, 1, 1))
|
2020-09-08 13:56:45 +03:00
|
|
|
assert.True(t, disallowed)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Empty(t, disallowedRule)
|
2020-09-08 13:56:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsBlockedIPDisallowed(t *testing.T) {
|
|
|
|
a := &accessCtx{}
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Nil(t, a.Init(nil, []string{"1.1.1.1", "2.2.0.0/16"}, nil))
|
2020-09-08 13:56:45 +03:00
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
disallowed, disallowedRule := a.IsBlockedIP(net.IPv4(1, 1, 1, 1))
|
2020-09-08 13:56:45 +03:00
|
|
|
assert.True(t, disallowed)
|
|
|
|
assert.Equal(t, "1.1.1.1", disallowedRule)
|
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
disallowed, disallowedRule = a.IsBlockedIP(net.IPv4(1, 1, 1, 2))
|
2020-09-08 13:56:45 +03:00
|
|
|
assert.False(t, disallowed)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Empty(t, disallowedRule)
|
2020-09-08 13:56:45 +03:00
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
disallowed, disallowedRule = a.IsBlockedIP(net.IPv4(2, 2, 1, 1))
|
2020-09-08 13:56:45 +03:00
|
|
|
assert.True(t, disallowed)
|
|
|
|
assert.Equal(t, "2.2.0.0/16", disallowedRule)
|
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
disallowed, disallowedRule = a.IsBlockedIP(net.IPv4(2, 3, 1, 1))
|
2020-09-08 13:56:45 +03:00
|
|
|
assert.False(t, disallowed)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Empty(t, disallowedRule)
|
2020-09-08 13:56:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsBlockedIPBlockedDomain(t *testing.T) {
|
|
|
|
a := &accessCtx{}
|
2020-12-07 15:38:05 +03:00
|
|
|
assert.True(t, a.Init(nil, nil, []string{
|
|
|
|
"host1",
|
2020-09-08 13:56:45 +03:00
|
|
|
"host2",
|
|
|
|
"*.host.com",
|
|
|
|
"||host3.com^",
|
|
|
|
}) == nil)
|
|
|
|
|
|
|
|
// match by "host2.com"
|
|
|
|
assert.True(t, a.IsBlockedDomain("host1"))
|
|
|
|
assert.True(t, a.IsBlockedDomain("host2"))
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.False(t, a.IsBlockedDomain("host3"))
|
2020-09-08 13:56:45 +03:00
|
|
|
|
|
|
|
// match by wildcard "*.host.com"
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.False(t, a.IsBlockedDomain("host.com"))
|
2020-09-08 13:56:45 +03:00
|
|
|
assert.True(t, a.IsBlockedDomain("asdf.host.com"))
|
|
|
|
assert.True(t, a.IsBlockedDomain("qwer.asdf.host.com"))
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.False(t, a.IsBlockedDomain("asdf.zhost.com"))
|
2020-09-08 13:56:45 +03:00
|
|
|
|
|
|
|
// match by wildcard "||host3.com^"
|
|
|
|
assert.True(t, a.IsBlockedDomain("host3.com"))
|
|
|
|
assert.True(t, a.IsBlockedDomain("asdf.host3.com"))
|
|
|
|
}
|