ooni-probe-cli/internal/engine/geolocate/iplookup_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

58 lines
1.4 KiB
Go

package geolocate
import (
"context"
"errors"
"net"
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestIPLookupGood(t *testing.T) {
ip, err := (ipLookupClient{
Logger: log.Log,
Resolver: netxlite.NewStdlibResolver(model.DiscardLogger),
UserAgent: "ooniprobe-engine/0.1.0",
}).LookupProbeIP(context.Background())
if err != nil {
t.Fatal(err)
}
if net.ParseIP(ip) == nil {
t.Fatal("not an IP address")
}
}
func TestIPLookupAllFailed(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // immediately cancel to cause Do() to fail
ip, err := (ipLookupClient{
Logger: log.Log,
Resolver: netxlite.NewStdlibResolver(model.DiscardLogger),
UserAgent: "ooniprobe-engine/0.1.0",
}).LookupProbeIP(ctx)
if !errors.Is(err, context.Canceled) {
t.Fatal("expected an error here")
}
if ip != model.DefaultProbeIP {
t.Fatal("expected the default IP here")
}
}
func TestIPLookupInvalidIP(t *testing.T) {
ctx := context.Background()
ip, err := (ipLookupClient{
Logger: log.Log,
Resolver: netxlite.NewStdlibResolver(model.DiscardLogger),
UserAgent: "ooniprobe-engine/0.1.0",
}).doWithCustomFunc(ctx, invalidIPLookup)
if !errors.Is(err, ErrInvalidIPAddress) {
t.Fatal("expected an error here")
}
if ip != model.DefaultProbeIP {
t.Fatal("expected the default IP here")
}
}