ooni-probe-cli/internal/netxlite/getaddrinfo_cgo_test.go
Simone Basso cf6dbe48e0
netxlite: call getaddrinfo and handle platform-specific oddities (#764)
This commit changes our system resolver to call getaddrinfo directly when CGO is enabled. This change allows us to:

1. obtain the CNAME easily

2. obtain the real getaddrinfo retval

3. handle platform specific oddities such as `EAI_NODATA`
returned on Android devices

See https://github.com/ooni/probe/issues/2029 and https://github.com/ooni/probe/issues/2029#issuecomment-1140258729 in particular.

See https://github.com/ooni/probe/issues/2033 for documentation regarding the desire to see `getaddrinfo`'s retval.

See https://github.com/ooni/probe/issues/2118 for possible follow-up changes.
2022-05-28 15:10:30 +02:00

90 lines
2.2 KiB
Go

//go:build: cgo
package netxlite
import (
"context"
"errors"
"net"
"testing"
"time"
)
func TestGetaddrinfoStateAddrinfoToStringWithInvalidFamily(t *testing.T) {
aip := staticAddrinfoWithInvalidFamily()
state := newGetaddrinfoState(getaddrinfoNumSlots)
addr, err := state.addrinfoToString(aip)
if !errors.Is(err, errGetaddrinfoUnknownFamily) {
t.Fatal("unexpected err", err)
}
if addr != "" {
t.Fatal("expected empty addr here")
}
}
func TestGetaddrinfoStateIfnametoindex(t *testing.T) {
ifaces, err := net.Interfaces()
if err != nil {
t.Fatal(err)
}
state := newGetaddrinfoState(getaddrinfoNumSlots)
for _, iface := range ifaces {
name := state.ifnametoindex(iface.Index)
if name != iface.Name {
t.Fatal("unexpected name")
}
}
}
func TestGetaddrinfoStateLookupANYWithNoSlots(t *testing.T) {
const (
noslots = 0
timeout = 10 * time.Millisecond
)
state := newGetaddrinfoState(noslots)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
addresses, cname, err := state.LookupANY(ctx, "dns.google")
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatal("unexpected err", err)
}
if len(addresses) > 0 {
t.Fatal("expected no addresses", addresses)
}
if cname != "" {
t.Fatal("expected empty cname", cname)
}
}
func TestGetaddrinfoStateToAddressList(t *testing.T) {
t.Run("with invalid sockety type", func(t *testing.T) {
state := newGetaddrinfoState(0) // number of slots not relevant
aip := staticAddrinfoWithInvalidSocketType()
addresses, cname, err := state.toAddressList(aip)
if !errors.Is(err, ErrOODNSNoAnswer) {
t.Fatal("unexpected err", err)
}
if len(addresses) > 0 {
t.Fatal("expected no addresses", addresses)
}
if cname != "" {
t.Fatal("expected empty cname", cname)
}
})
t.Run("with invalid family", func(t *testing.T) {
state := newGetaddrinfoState(0) // number of slots not relevant
aip := staticAddrinfoWithInvalidFamily()
addresses, cname, err := state.toAddressList(aip)
if !errors.Is(err, ErrOODNSNoAnswer) {
t.Fatal("unexpected err", err)
}
if len(addresses) > 0 {
t.Fatal("expected no addresses", addresses)
}
if cname != "" {
t.Fatal("expected empty cname", cname)
}
})
}