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