Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BTCORE-1100: Add new GeoDB to unit testing #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ jobs:
- checkout
- git/checkout-with-submodules
- setup_remote_docker:
version: 20.10.14
version: default
docker_layer_caching: true
- run:
name: Install Dagger CLI
command: cd /usr/local && { curl -L https://dl.dagger.io/dagger/install.sh | sudo sh; cd -; }
- run:
name: unit test
command: dagger run --progress plain go run ci/main.go --cov_file cov.out
- when:
condition:
equal: [master, << pipeline.git.branch >> ]
steps:
- codecov/upload:
file: cov.out
- codecov/upload:
file: cov.out

# Orchestrate jobs using workflows
# See: https://circleci.com/docs/configuration-reference/#workflows
Expand Down
84 changes: 83 additions & 1 deletion maxmind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"github.com/stretchr/testify/assert"
)

//go:embed test/MaxMind-DB/test-data/*.mmdb
//go:embed test/MaxMind-DB/test-data/*.mmdb test/*.mmdb
var embedFS embed.FS

const (
maxmindCountryTestDB string = "test/MaxMind-DB/test-data/GeoIP2-Country-Test.mmdb" // test database, only covers NA, EU, AS continents
dbipCountryTestDB string = "test/dbip-country-lite-2025-01.mmdb" // test database
)

type countryLookupTest struct {
Expand Down Expand Up @@ -107,3 +108,84 @@ func TestCountryLookupWithMaxmind(t *testing.T) {

assert.Nil(t, nil, maxmind.Close(), "close the reader should not generate an error")
}

func TestCountryLookupWithDBIP(t *testing.T) {

file, err := embedFS.Open(dbipCountryTestDB)
if err != nil {
t.Fatal(fmt.Errorf("embedFS.Open: %w", err))
}

maxmind, err := NewMaxMindFromReader(file)
if err != nil {
t.Fatal(fmt.Errorf("NewMaxMindn: %w", err))
}

//assert.Equal(t, true, maxmind.IsTestDB(), "not a test db")

ipTests := []countryLookupTest{
{
ip: "::149.101.100.0",
expectedCountryCode: "US",
expectedCountryAlpha3Code: "USA",
expectedContinentCode: "NA",
expectedUnknown: false,
expectedError: false,
},
{
ip: "81.2.69.142",
expectedCountryCode: "GB",
expectedCountryAlpha3Code: "GBR",
expectedContinentCode: "EU",
expectedUnknown: false,
expectedError: false,
},

{
ip: "2001:218::",
expectedCountryCode: "JP",
expectedCountryAlpha3Code: "JPN",
expectedContinentCode: "AS",
expectedUnknown: false,
expectedError: false,
},

{
ip: "1", // wrong ip
expectedCountryCode: "",
expectedContinentCode: "",
expectedCountryAlpha3Code: "",
expectedUnknown: false,
expectedError: true,
},

{
ip: "127.0.0.1", // local ip
expectedCountryCode: "ZZ",
expectedContinentCode: "ZZ",
expectedCountryAlpha3Code: "ZZZ",
expectedUnknown: true,
expectedError: false,
},
}

for _, test := range ipTests {
country, err := maxmind.CountryByIPString(test.ip)

if test.expectedError {
assert.NotNil(t, err, "expected error")
assert.Nil(t, country, "expected country to be nil ")
continue
}

assert.Nil(t, err, "expected no error")
assert.NotNil(t, country, "expected country to not be nil ")

assert.Equal(t, test.expectedUnknown, country.IsUnknown(), "expected country does not match unknown expectation")
assert.Equal(t, test.expectedCountryCode, country.CountryAlpha2Code(), "mismatch country code")
assert.Equal(t, test.expectedCountryAlpha3Code, country.CountryAlpha3Code(), "mismatch country alpha3 code")
assert.Equal(t, test.expectedContinentCode, country.ContinentCode(), "mismatch continent code")
}

assert.Nil(t, nil, maxmind.Close(), "close the reader should not generate an error")
}
Binary file added test/dbip-country-lite-2025-01.mmdb
Binary file not shown.