2021-02-02 12:05:47 +01:00
|
|
|
package geolocate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type taskProbeIPLookupper struct {
|
|
|
|
ip string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c taskProbeIPLookupper) LookupProbeIP(ctx context.Context) (string, error) {
|
|
|
|
return c.ip, c.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocationLookupCannotLookupProbeIP(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
op := Task{
|
|
|
|
probeIPLookupper: taskProbeIPLookupper{err: expected},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := op.Run(ctx)
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out.ASN != DefaultProbeASN {
|
|
|
|
t.Fatal("invalid ASN value")
|
|
|
|
}
|
|
|
|
if out.CountryCode != DefaultProbeCC {
|
|
|
|
t.Fatal("invalid CountryCode value")
|
|
|
|
}
|
|
|
|
if out.NetworkName != DefaultProbeNetworkName {
|
|
|
|
t.Fatal("invalid NetworkName value")
|
|
|
|
}
|
|
|
|
if out.ProbeIP != DefaultProbeIP {
|
|
|
|
t.Fatal("invalid ProbeIP value")
|
|
|
|
}
|
|
|
|
if out.ResolverASN != DefaultResolverASN {
|
|
|
|
t.Fatal("invalid ResolverASN value")
|
|
|
|
}
|
|
|
|
if out.ResolverIP != DefaultResolverIP {
|
|
|
|
t.Fatal("invalid ResolverIP value")
|
|
|
|
}
|
|
|
|
if out.ResolverNetworkName != DefaultResolverNetworkName {
|
|
|
|
t.Fatal("invalid ResolverNetworkName value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type taskASNLookupper struct {
|
|
|
|
err error
|
|
|
|
asn uint
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2021-04-01 16:57:31 +02:00
|
|
|
func (c taskASNLookupper) LookupASN(ip string) (uint, string, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return c.asn, c.name, c.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocationLookupCannotLookupProbeASN(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
op := Task{
|
|
|
|
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
|
|
|
probeASNLookupper: taskASNLookupper{err: expected},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := op.Run(ctx)
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out.ASN != DefaultProbeASN {
|
|
|
|
t.Fatal("invalid ASN value")
|
|
|
|
}
|
|
|
|
if out.CountryCode != DefaultProbeCC {
|
|
|
|
t.Fatal("invalid CountryCode value")
|
|
|
|
}
|
|
|
|
if out.NetworkName != DefaultProbeNetworkName {
|
|
|
|
t.Fatal("invalid NetworkName value")
|
|
|
|
}
|
|
|
|
if out.ProbeIP != "1.2.3.4" {
|
|
|
|
t.Fatal("invalid ProbeIP value")
|
|
|
|
}
|
|
|
|
if out.ResolverASN != DefaultResolverASN {
|
|
|
|
t.Fatal("invalid ResolverASN value")
|
|
|
|
}
|
|
|
|
if out.ResolverIP != DefaultResolverIP {
|
|
|
|
t.Fatal("invalid ResolverIP value")
|
|
|
|
}
|
|
|
|
if out.ResolverNetworkName != DefaultResolverNetworkName {
|
|
|
|
t.Fatal("invalid ResolverNetworkName value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type taskCCLookupper struct {
|
|
|
|
err error
|
|
|
|
cc string
|
|
|
|
}
|
|
|
|
|
2021-04-01 16:57:31 +02:00
|
|
|
func (c taskCCLookupper) LookupCC(ip string) (string, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return c.cc, c.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocationLookupCannotLookupProbeCC(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
op := Task{
|
|
|
|
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
|
|
|
probeASNLookupper: taskASNLookupper{asn: 1234, name: "1234.com"},
|
|
|
|
countryLookupper: taskCCLookupper{cc: "US", err: expected},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := op.Run(ctx)
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out.ASN != 1234 {
|
|
|
|
t.Fatal("invalid ASN value")
|
|
|
|
}
|
|
|
|
if out.CountryCode != DefaultProbeCC {
|
|
|
|
t.Fatal("invalid CountryCode value")
|
|
|
|
}
|
|
|
|
if out.NetworkName != "1234.com" {
|
|
|
|
t.Fatal("invalid NetworkName value")
|
|
|
|
}
|
|
|
|
if out.ProbeIP != "1.2.3.4" {
|
|
|
|
t.Fatal("invalid ProbeIP value")
|
|
|
|
}
|
|
|
|
if out.ResolverASN != DefaultResolverASN {
|
|
|
|
t.Fatal("invalid ResolverASN value")
|
|
|
|
}
|
|
|
|
if out.ResolverIP != DefaultResolverIP {
|
|
|
|
t.Fatal("invalid ResolverIP value")
|
|
|
|
}
|
|
|
|
if out.ResolverNetworkName != DefaultResolverNetworkName {
|
|
|
|
t.Fatal("invalid ResolverNetworkName value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type taskResolverIPLookupper struct {
|
|
|
|
ip string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c taskResolverIPLookupper) LookupResolverIP(ctx context.Context) (string, error) {
|
|
|
|
return c.ip, c.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocationLookupCannotLookupResolverIP(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
op := Task{
|
|
|
|
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
|
|
|
probeASNLookupper: taskASNLookupper{asn: 1234, name: "1234.com"},
|
|
|
|
countryLookupper: taskCCLookupper{cc: "IT"},
|
|
|
|
resolverIPLookupper: taskResolverIPLookupper{err: expected},
|
|
|
|
enableResolverLookup: true,
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := op.Run(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out.ASN != 1234 {
|
|
|
|
t.Fatal("invalid ASN value")
|
|
|
|
}
|
|
|
|
if out.CountryCode != "IT" {
|
|
|
|
t.Fatal("invalid CountryCode value")
|
|
|
|
}
|
|
|
|
if out.NetworkName != "1234.com" {
|
|
|
|
t.Fatal("invalid NetworkName value")
|
|
|
|
}
|
|
|
|
if out.ProbeIP != "1.2.3.4" {
|
|
|
|
t.Fatal("invalid ProbeIP value")
|
|
|
|
}
|
|
|
|
if out.DidResolverLookup != true {
|
|
|
|
t.Fatal("invalid DidResolverLookup value")
|
|
|
|
}
|
|
|
|
if out.ResolverASN != DefaultResolverASN {
|
|
|
|
t.Fatal("invalid ResolverASN value")
|
|
|
|
}
|
|
|
|
if out.ResolverIP != DefaultResolverIP {
|
|
|
|
t.Fatal("invalid ResolverIP value")
|
|
|
|
}
|
|
|
|
if out.ResolverNetworkName != DefaultResolverNetworkName {
|
|
|
|
t.Fatal("invalid ResolverNetworkName value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocationLookupCannotLookupResolverNetworkName(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
op := Task{
|
|
|
|
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
|
|
|
probeASNLookupper: taskASNLookupper{asn: 1234, name: "1234.com"},
|
|
|
|
countryLookupper: taskCCLookupper{cc: "IT"},
|
|
|
|
resolverIPLookupper: taskResolverIPLookupper{ip: "4.3.2.1"},
|
|
|
|
resolverASNLookupper: taskASNLookupper{err: expected},
|
|
|
|
enableResolverLookup: true,
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := op.Run(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out.ASN != 1234 {
|
|
|
|
t.Fatal("invalid ASN value")
|
|
|
|
}
|
|
|
|
if out.CountryCode != "IT" {
|
|
|
|
t.Fatal("invalid CountryCode value")
|
|
|
|
}
|
|
|
|
if out.NetworkName != "1234.com" {
|
|
|
|
t.Fatal("invalid NetworkName value")
|
|
|
|
}
|
|
|
|
if out.ProbeIP != "1.2.3.4" {
|
|
|
|
t.Fatal("invalid ProbeIP value")
|
|
|
|
}
|
|
|
|
if out.DidResolverLookup != true {
|
|
|
|
t.Fatal("invalid DidResolverLookup value")
|
|
|
|
}
|
|
|
|
if out.ResolverASN != DefaultResolverASN {
|
|
|
|
t.Fatalf("invalid ResolverASN value: %+v", out.ResolverASN)
|
|
|
|
}
|
|
|
|
if out.ResolverIP != "4.3.2.1" {
|
|
|
|
t.Fatalf("invalid ResolverIP value: %+v", out.ResolverIP)
|
|
|
|
}
|
|
|
|
if out.ResolverNetworkName != DefaultResolverNetworkName {
|
|
|
|
t.Fatal("invalid ResolverNetworkName value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocationLookupSuccessWithResolverLookup(t *testing.T) {
|
|
|
|
op := Task{
|
|
|
|
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
|
|
|
probeASNLookupper: taskASNLookupper{asn: 1234, name: "1234.com"},
|
|
|
|
countryLookupper: taskCCLookupper{cc: "IT"},
|
|
|
|
resolverIPLookupper: taskResolverIPLookupper{ip: "4.3.2.1"},
|
|
|
|
resolverASNLookupper: taskASNLookupper{asn: 4321, name: "4321.com"},
|
|
|
|
enableResolverLookup: true,
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := op.Run(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out.ASN != 1234 {
|
|
|
|
t.Fatal("invalid ASN value")
|
|
|
|
}
|
|
|
|
if out.CountryCode != "IT" {
|
|
|
|
t.Fatal("invalid CountryCode value")
|
|
|
|
}
|
|
|
|
if out.NetworkName != "1234.com" {
|
|
|
|
t.Fatal("invalid NetworkName value")
|
|
|
|
}
|
|
|
|
if out.ProbeIP != "1.2.3.4" {
|
|
|
|
t.Fatal("invalid ProbeIP value")
|
|
|
|
}
|
|
|
|
if out.DidResolverLookup != true {
|
|
|
|
t.Fatal("invalid DidResolverLookup value")
|
|
|
|
}
|
|
|
|
if out.ResolverASN != 4321 {
|
|
|
|
t.Fatalf("invalid ResolverASN value: %+v", out.ResolverASN)
|
|
|
|
}
|
|
|
|
if out.ResolverIP != "4.3.2.1" {
|
|
|
|
t.Fatalf("invalid ResolverIP value: %+v", out.ResolverIP)
|
|
|
|
}
|
|
|
|
if out.ResolverNetworkName != "4321.com" {
|
|
|
|
t.Fatal("invalid ResolverNetworkName value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocationLookupSuccessWithoutResolverLookup(t *testing.T) {
|
|
|
|
op := Task{
|
|
|
|
probeIPLookupper: taskProbeIPLookupper{ip: "1.2.3.4"},
|
|
|
|
probeASNLookupper: taskASNLookupper{asn: 1234, name: "1234.com"},
|
|
|
|
countryLookupper: taskCCLookupper{cc: "IT"},
|
|
|
|
resolverIPLookupper: taskResolverIPLookupper{ip: "4.3.2.1"},
|
|
|
|
resolverASNLookupper: taskASNLookupper{asn: 4321, name: "4321.com"},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := op.Run(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out.ASN != 1234 {
|
|
|
|
t.Fatal("invalid ASN value")
|
|
|
|
}
|
|
|
|
if out.CountryCode != "IT" {
|
|
|
|
t.Fatal("invalid CountryCode value")
|
|
|
|
}
|
|
|
|
if out.NetworkName != "1234.com" {
|
|
|
|
t.Fatal("invalid NetworkName value")
|
|
|
|
}
|
|
|
|
if out.ProbeIP != "1.2.3.4" {
|
|
|
|
t.Fatal("invalid ProbeIP value")
|
|
|
|
}
|
|
|
|
if out.DidResolverLookup != false {
|
|
|
|
t.Fatal("invalid DidResolverLookup value")
|
|
|
|
}
|
|
|
|
if out.ResolverASN != DefaultResolverASN {
|
|
|
|
t.Fatalf("invalid ResolverASN value: %+v", out.ResolverASN)
|
|
|
|
}
|
|
|
|
if out.ResolverIP != DefaultResolverIP {
|
|
|
|
t.Fatalf("invalid ResolverIP value: %+v", out.ResolverIP)
|
|
|
|
}
|
|
|
|
if out.ResolverNetworkName != DefaultResolverNetworkName {
|
|
|
|
t.Fatal("invalid ResolverNetworkName value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSmoke(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
EnableResolverLookup: true,
|
|
|
|
}
|
|
|
|
task := Must(NewTask(config))
|
|
|
|
result, err := task.Run(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("expected non nil result")
|
|
|
|
}
|
|
|
|
// we already checked above that the returned
|
|
|
|
// value is okay for all codepaths.
|
|
|
|
}
|
|
|
|
|
fix(geolocate): no proxy when discovering our IP address (#251)
* fix(geolocate): no proxy when discovering our IP address
The use case of --proxy is that you cannot contact the OONI
backend otherwise. It is wrong, though, using the proxy when
discovering our IP address. The measurement won't use the
proxy anyway. Therefore, we need to use the IP address that
is performing the measurement. Not the one of the proxy.
What's more, stun is not using a proxy. Therefore, it does
not make much sense that http IP resolvers use a proxy. This
leads to inconsistencies. So, here's anothe reason why this
patch is a good thing (TM).
Finally, because knowing the IP address enables us to sanitize
the data, it's important we discover the correct IP.
Now, up until this point, the `--proxy` option has mostly
been a developers toy. But, users have asked us to have the
possibility of configuring a proxy.
This explains why I have been looking into making `--proxy`
right for a couple of hours now.
See https://github.com/ooni/probe/issues/1382
* fix(session): properly configure the IP lookupper
2021-03-10 12:01:08 +01:00
|
|
|
func TestASNStringWorks(t *testing.T) {
|
|
|
|
r := Results{ASN: 1234}
|
|
|
|
if r.ASNString() != "AS1234" {
|
|
|
|
t.Fatal("unexpected result")
|
|
|
|
}
|
|
|
|
}
|