2024-02-20 09:59:56 +01:00
|
|
|
package geolocation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"path"
|
2024-09-09 18:27:42 +02:00
|
|
|
"path/filepath"
|
2024-02-20 09:59:56 +01:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// from https://github.com/maxmind/MaxMind-DB/blob/main/test-data/GeoLite2-City-Test.mmdb
|
2024-09-09 18:27:42 +02:00
|
|
|
var mmdbPath = "../testdata/GeoLite2-City_20240305.mmdb"
|
2024-02-20 09:59:56 +01:00
|
|
|
|
|
|
|
func TestGeoLite_Lookup(t *testing.T) {
|
|
|
|
tempDir := t.TempDir()
|
2024-09-09 18:27:42 +02:00
|
|
|
filename := path.Join(tempDir, filepath.Base(mmdbPath))
|
2024-02-20 09:59:56 +01:00
|
|
|
err := util.CopyFileContents(mmdbPath, filename)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-09 18:27:42 +02:00
|
|
|
db, err := openDB(filename)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
geo := &Geolocation{
|
|
|
|
mux: sync.RWMutex{},
|
|
|
|
db: db,
|
|
|
|
stopCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
assert.NotNil(t, geo)
|
|
|
|
defer func() {
|
|
|
|
err = geo.Stop()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("geo.Stop: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
record, err := geo.Lookup(net.ParseIP("89.160.20.128"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, record)
|
|
|
|
assert.Equal(t, "SE", record.Country.ISOCode)
|
|
|
|
assert.Equal(t, uint(2661886), record.Country.GeonameID)
|
|
|
|
assert.Equal(t, "Linköping", record.City.Names.En)
|
|
|
|
assert.Equal(t, uint(2694762), record.City.GeonameID)
|
|
|
|
assert.Equal(t, "EU", record.Continent.Code)
|
|
|
|
assert.Equal(t, uint(6255148), record.Continent.GeonameID)
|
|
|
|
}
|