2021-02-02 12:05:47 +01:00
|
|
|
package geolocate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-03-02 12:08:24 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/resourcesmanager"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
asnDBPath = "../testdata/asn.mmdb"
|
|
|
|
countryDBPath = "../testdata/country.mmdb"
|
|
|
|
ipAddr = "35.204.49.125"
|
|
|
|
)
|
|
|
|
|
|
|
|
func maybeFetchResources(t *testing.T) {
|
2021-03-02 12:08:24 +01:00
|
|
|
c := &resourcesmanager.CopyWorker{DestDir: "../testdata/"}
|
|
|
|
if err := c.Ensure(); err != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupASN(t *testing.T) {
|
|
|
|
maybeFetchResources(t)
|
|
|
|
asn, org, err := LookupASN(asnDBPath, ipAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if asn <= 0 {
|
|
|
|
t.Fatal("unexpected ASN value")
|
|
|
|
}
|
|
|
|
if len(org) <= 0 {
|
|
|
|
t.Fatal("unexpected org value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupASNInvalidFile(t *testing.T) {
|
|
|
|
maybeFetchResources(t)
|
|
|
|
asn, org, err := LookupASN("/nonexistent", ipAddr)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error here")
|
|
|
|
}
|
|
|
|
if asn != DefaultProbeASN {
|
|
|
|
t.Fatal("expected a zero ASN")
|
|
|
|
}
|
|
|
|
if org != DefaultProbeNetworkName {
|
|
|
|
t.Fatal("expected an empty org")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupASNInvalidIP(t *testing.T) {
|
|
|
|
maybeFetchResources(t)
|
|
|
|
asn, org, err := LookupASN(asnDBPath, "xxx")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error here")
|
|
|
|
}
|
|
|
|
if asn != DefaultProbeASN {
|
|
|
|
t.Fatal("expected a zero ASN")
|
|
|
|
}
|
|
|
|
if org != DefaultProbeNetworkName {
|
|
|
|
t.Fatal("expected an empty org")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupCC(t *testing.T) {
|
|
|
|
maybeFetchResources(t)
|
|
|
|
cc, err := (mmdbLookupper{}).LookupCC(countryDBPath, ipAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(cc) != 2 {
|
|
|
|
t.Fatal("does not seem a country code")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupCCInvalidFile(t *testing.T) {
|
|
|
|
maybeFetchResources(t)
|
|
|
|
cc, err := (mmdbLookupper{}).LookupCC("/nonexistent", ipAddr)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error here")
|
|
|
|
}
|
|
|
|
if cc != DefaultProbeCC {
|
|
|
|
t.Fatal("expected an empty cc")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupCCInvalidIP(t *testing.T) {
|
|
|
|
maybeFetchResources(t)
|
|
|
|
cc, err := (mmdbLookupper{}).LookupCC(asnDBPath, "xxx")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error here")
|
|
|
|
}
|
|
|
|
if cc != DefaultProbeCC {
|
|
|
|
t.Fatal("expected an empty cc")
|
|
|
|
}
|
|
|
|
}
|