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