ooni-probe-cli/internal/engine/geolocate/iplookup_test.go
Simone Basso 2502a237fb
cleanup: netx does not use netxlite legacy names (#801)
This diff refactors netx and netxlite to ensure we're not using
netxlite legacy names inside of netx.

To this end, we're cheating a bit. We're exposing a new factory to
get an unwrapped stdlib resolver rather than defining a legacy name
to export the private name of the same factory.

This is actually a fine place to stop, for now, the next and
netxlite refactoring at https://github.com/ooni/probe/issues/2121.
2022-06-06 14:46:44 +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 != 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 != DefaultProbeIP {
t.Fatal("expected the default IP here")
}
}