ooni-probe-cli/internal/geoipx/geoipx_test.go
Simone Basso 110a11828b
refactor: spin geoipx off geolocate (#893)
A bunch of packages (including oohelperd) just need the ability to
use MaxMind-like databases. They don't need the additional functionality
implemented by the geolocate package. Such a package, in fact, is
mostly (if not only) needed by the engine package.

Therefore, move code to query MaxMind-like databases to a separate
package, and avoid depending on geolocate in all the packages for
which it's sufficient to use geoipx.

Part of https://github.com/ooni/probe/issues/2240
2022-08-28 20:00:25 +02:00

60 lines
1.1 KiB
Go

package geoipx
import (
"testing"
"github.com/ooni/probe-cli/v3/internal/model"
)
const ipAddr = "8.8.8.8"
func TestLookupASN(t *testing.T) {
t.Run("with valid IP address", func(t *testing.T) {
asn, org, err := LookupASN(ipAddr)
if err != nil {
t.Fatal(err)
}
if asn != 15169 {
t.Fatal("unexpected ASN value", asn)
}
if org != "Google LLC" {
t.Fatal("unexpected org value", org)
}
})
t.Run("with invalid IP address", func(t *testing.T) {
asn, org, err := LookupASN("xxx")
if err == nil {
t.Fatal("expected an error here")
}
if asn != model.DefaultProbeASN {
t.Fatal("expected a zero ASN")
}
if org != model.DefaultProbeNetworkName {
t.Fatal("expected an empty org")
}
})
}
func TestLookupCC(t *testing.T) {
t.Run("with valid IP address", func(t *testing.T) {
cc, err := LookupCC(ipAddr)
if err != nil {
t.Fatal(err)
}
if cc != "US" {
t.Fatal("invalid country code", cc)
}
})
t.Run("with invalid IP address", func(t *testing.T) {
cc, err := LookupCC("xxx")
if err == nil {
t.Fatal("expected an error here")
}
if cc != model.DefaultProbeCC {
t.Fatal("expected an empty cc")
}
})
}