57a3919d2a
This change ensures that, in turn, we're able to "remote" all the traffic generated by the `geolocate` package, rather than missing some bits of it that were still using the standard library and caused _some_ geolocations to geolocate as the local host rather than as the remote host. Extracted from https://github.com/ooni/probe-cli/pull/969, where we tested this functionality. Closes https://github.com/ooni/probe/issues/1383 (which was long overdue). Part of https://github.com/ooni/probe/issues/2340, because it allows us to make progress with that.
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package geolocate
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
|
)
|
|
|
|
func TestLookupResolverIP(t *testing.T) {
|
|
rlc := resolverLookupClient{
|
|
Resolver: netxlite.NewStdlibResolver(model.DiscardLogger),
|
|
}
|
|
addr, err := rlc.LookupResolverIP(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if addr == "" {
|
|
t.Fatal("expected a non-empty string")
|
|
}
|
|
}
|
|
|
|
type brokenHostLookupper struct {
|
|
err error
|
|
}
|
|
|
|
func (bhl brokenHostLookupper) LookupHost(ctx context.Context, host string) ([]string, error) {
|
|
return nil, bhl.err
|
|
}
|
|
|
|
func TestLookupResolverIPFailure(t *testing.T) {
|
|
expected := errors.New("mocked error")
|
|
rlc := resolverLookupClient{
|
|
Resolver: netxlite.NewStdlibResolver(model.DiscardLogger),
|
|
}
|
|
addr, err := rlc.do(context.Background(), brokenHostLookupper{
|
|
err: expected,
|
|
})
|
|
if !errors.Is(err, expected) {
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
}
|
|
if len(addr) != 0 {
|
|
t.Fatal("expected an empty address")
|
|
}
|
|
}
|
|
|
|
func TestLookupResolverIPNoAddressReturned(t *testing.T) {
|
|
rlc := resolverLookupClient{
|
|
Resolver: netxlite.NewStdlibResolver(model.DiscardLogger),
|
|
}
|
|
addr, err := rlc.do(context.Background(), brokenHostLookupper{})
|
|
if !errors.Is(err, ErrNoIPAddressReturned) {
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
}
|
|
if len(addr) != 0 {
|
|
t.Fatal("expected an empty address")
|
|
}
|
|
}
|