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

82 lines
1.7 KiB
Go

//go:build cgo && windows
package netxlite
import (
"errors"
"syscall"
"testing"
)
func TestGetaddrinfoAIFlags(t *testing.T) {
var wrong bool
wrong = getaddrinfoAIFlags != aiCanonname
if wrong {
t.Fatal("wrong flags for platform")
}
}
func TestGetaddrinfoStateToError(t *testing.T) {
type args struct {
code int64
err error
goos string
}
type expects struct {
message string // message obtained using .Error
code int64
err error
}
var inputs = []struct {
name string
args args
expects expects
}{{
name: "with nonzero return code and error",
args: args{
code: int64(WSAHOST_NOT_FOUND),
err: syscall.EAGAIN,
goos: "windows",
},
expects: expects{
message: syscall.EAGAIN.Error(),
code: int64(WSAHOST_NOT_FOUND),
err: syscall.EAGAIN,
},
}, {
name: "with return code and nil error",
args: args{
code: int64(WSAHOST_NOT_FOUND),
err: nil,
goos: "windows",
},
expects: expects{
message: WSAHOST_NOT_FOUND.Error(),
code: int64(WSAHOST_NOT_FOUND),
err: WSAHOST_NOT_FOUND,
},
}}
for _, input := range inputs {
t.Run(input.name, func(t *testing.T) {
state := newGetaddrinfoState(getaddrinfoNumSlots)
err := state.toError(input.args.code, input.args.err, input.args.goos)
if err == nil {
t.Fatal("expected non-nil error here")
}
if err.Error() != input.expects.message {
t.Fatal("unexpected error message")
}
var gaierr *ErrGetaddrinfo
if !errors.As(err, &gaierr) {
t.Fatal("cannot convert error to ErrGetaddrinfo")
}
if gaierr.Code != input.expects.code {
t.Fatal("unexpected code")
}
if !errors.Is(gaierr.Underlying, input.expects.err) {
t.Fatal("unexpected underlying error")
}
})
}
}