Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpillora authored Mar 15, 2022
1 parent b7355e0 commit 4f13445
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions ipfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"github.com/stretchr/testify/assert"
)

const (
egUS = "52.92.180.128"
egAU = "49.189.50.1"
egCN = "116.31.116.51"
)

func TestSingleIP(t *testing.T) {
f := ipfilter.New(ipfilter.Options{
AllowedIPs: []string{"222.25.118.1"},
Expand All @@ -30,31 +36,31 @@ func TestSubnetIP(t *testing.T) {
}

func TestManualCountryCode(t *testing.T) {
assert.Equal(t, ipfilter.IPToCountry("203.25.111.68"), "AU")
assert.Equal(t, ipfilter.IPToCountry("216.58.199.67"), "US")
assert.Equal(t, ipfilter.IPToCountry(egAU), "AU")
assert.Equal(t, ipfilter.IPToCountry(egUS), "US")
}

func TestCountryCodeWhiteList(t *testing.T) {
f := ipfilter.New(ipfilter.Options{
AllowedCountries: []string{"AU"},
BlockByDefault: true,
})
assert.True(t, f.Allowed("203.25.111.68"), "[1] should be allowed")
assert.True(t, f.Blocked("216.58.199.67"), "[2] should be blocked")
assert.True(t, f.Allowed(egAU), "[1] should be allowed")
assert.True(t, f.Blocked(egUS), "[2] should be blocked")
}

func TestCountryCodeBlackList(t *testing.T) {
f := ipfilter.New(ipfilter.Options{
BlockedCountries: []string{"RU", "CN"},
})
assert.True(t, f.Allowed("203.25.111.68"), "[1] AU should be allowed")
assert.True(t, f.Allowed("216.58.199.67"), "[2] US should be allowed")
assert.True(t, f.Blocked("116.31.116.51"), "[3] CN should be blocked")
assert.True(t, f.Allowed(egAU), "[1] AU should be allowed")
assert.True(t, f.Allowed(egUS), "[2] US should be allowed")
assert.True(t, f.Blocked(egCN), "[3] CN should be blocked")
}

func TestDynamicList(t *testing.T) {
f := ipfilter.New(ipfilter.Options{})
assert.True(t, f.Allowed("116.31.116.51"), "[1] CN should be allowed")
assert.True(t, f.Allowed(egCN), "[1] CN should be allowed")
f.BlockCountry("CN")
assert.True(t, f.Blocked("116.31.116.51"), "[1] CN should be blocked")
assert.True(t, f.Blocked(egCN), "[1] CN should be blocked")
}

0 comments on commit 4f13445

Please sign in to comment.