acef18a955
The BogonResolver relied on its wrapper resolver to pass along the list of addresses _and_ the error. But the idiomatic thing to do is often to return `nil` when there is an error. I broke this very fragile assumption in https://github.com/ooni/probe-cli/pull/399. I could of course fix it, but this assumption is clearly wrong and we should not allow such fragile code in the tree. We are not using BogonIsError much in the tree. The only place in which we're using it for measuring seems to be dnscheck. It may be that this surprising behavior was what caused the issue at https://github.com/ooni/probe/issues/1510 in the first place. Regardless, let's remove fragile code and adjust the test that was failing. Also that test is quick so it can run in `-short` mode. Spotted while working on https://github.com/ooni/probe/issues/1505.
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package resolver_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
|
)
|
|
|
|
func TestResolverIsBogon(t *testing.T) {
|
|
if resolver.IsBogon("antani") != true {
|
|
t.Fatal("unexpected result")
|
|
}
|
|
if resolver.IsBogon("127.0.0.1") != true {
|
|
t.Fatal("unexpected result")
|
|
}
|
|
if resolver.IsBogon("1.1.1.1") != false {
|
|
t.Fatal("unexpected result")
|
|
}
|
|
if resolver.IsBogon("10.0.1.1") != true {
|
|
t.Fatal("unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestBogonAwareResolverWithBogon(t *testing.T) {
|
|
r := resolver.BogonResolver{
|
|
Resolver: resolver.NewFakeResolverWithResult([]string{"127.0.0.1"}),
|
|
}
|
|
addrs, err := r.LookupHost(context.Background(), "dns.google.com")
|
|
if !errors.Is(err, errorx.ErrDNSBogon) {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
if len(addrs) > 0 {
|
|
t.Fatal("expected to see nil here")
|
|
}
|
|
}
|
|
|
|
func TestBogonAwareResolverWithoutBogon(t *testing.T) {
|
|
orig := []string{"8.8.8.8"}
|
|
r := resolver.BogonResolver{
|
|
Resolver: resolver.NewFakeResolverWithResult(orig),
|
|
}
|
|
addrs, err := r.LookupHost(context.Background(), "dns.google.com")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(addrs) != len(orig) || addrs[0] != orig[0] {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
}
|