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.
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package geolocate
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/apex/log"
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
|
)
|
|
|
|
func TestUbuntuParseError(t *testing.T) {
|
|
ip, err := ubuntuIPLookup(
|
|
context.Background(),
|
|
&http.Client{Transport: FakeTransport{
|
|
Resp: &http.Response{
|
|
StatusCode: 200,
|
|
Body: io.NopCloser(strings.NewReader("<")),
|
|
},
|
|
}},
|
|
log.Log,
|
|
model.HTTPHeaderUserAgent,
|
|
netxlite.NewStdlibResolver(model.DiscardLogger),
|
|
)
|
|
if err == nil || !strings.HasPrefix(err.Error(), "XML syntax error") {
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
}
|
|
if ip != model.DefaultProbeIP {
|
|
t.Fatalf("not the expected IP address: %s", ip)
|
|
}
|
|
}
|
|
|
|
func TestIPLookupWorksUsingUbuntu(t *testing.T) {
|
|
ip, err := ubuntuIPLookup(
|
|
context.Background(),
|
|
http.DefaultClient,
|
|
log.Log,
|
|
model.HTTPHeaderUserAgent,
|
|
netxlite.NewStdlibResolver(model.DiscardLogger),
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if net.ParseIP(ip) == nil {
|
|
t.Fatalf("not an IP address: '%s'", ip)
|
|
}
|
|
}
|