ooni-probe-cli/internal/netxlite/getaddrinfo_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

81 lines
1.5 KiB
Go

package netxlite
import (
"context"
"errors"
"io"
"testing"
)
func TestGetaddrinfoLookupHost(t *testing.T) {
addrs, err := getaddrinfoLookupHost(context.Background(), "127.0.0.1")
if err != nil {
t.Fatal(err)
}
if len(addrs) != 1 || addrs[0] != "127.0.0.1" {
t.Fatal("unexpected addrs", addrs)
}
}
func TestErrorToGetaddrinfoRetval(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
want int64
}{{
name: "with valid getaddrinfo error",
args: args{
newErrGetaddrinfo(144, nil),
},
want: 144,
}, {
name: "with another kind of error",
args: args{io.EOF},
want: 0,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ErrorToGetaddrinfoRetval(tt.args.err); got != tt.want {
t.Errorf("ErrorToGetaddrinfoRetval() = %v, want %v", got, tt.want)
}
})
}
}
func Test_newErrGetaddrinfo(t *testing.T) {
type args struct {
code int64
err error
}
tests := []struct {
name string
args args
}{{
name: "common case",
args: args{
code: 17,
err: io.EOF,
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := newErrGetaddrinfo(tt.args.code, tt.args.err)
if err == nil {
t.Fatal("expected non-nil error")
}
if !errors.Is(err, tt.args.err) {
t.Fatal("Unwrap() is not working correctly")
}
if err.Error() != tt.args.err.Error() {
t.Fatal("Error() is not working correctly")
}
if err.Code != tt.args.code {
t.Fatal("Code has not been copied correctly")
}
})
}
}