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

49 lines
942 B
Go

package geolocate
import (
"context"
"io"
"net"
"net/http"
"strings"
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/model"
)
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,
)
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,
)
if err != nil {
t.Fatal(err)
}
if net.ParseIP(ip) == nil {
t.Fatalf("not an IP address: '%s'", ip)
}
}