forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getaddrinfo_bsd_test.go
104 lines (100 loc) · 2.16 KB
/
getaddrinfo_bsd_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//go:build cgo && (darwin || dragonfly || freebsd || openbsd)
package netxlite
import (
"errors"
"syscall"
"testing"
)
func TestGetaddrinfoAIFlags(t *testing.T) {
wrong := getaddrinfoAIFlags != (aiCanonname|aiV4Mapped|aiAll)&aiMask
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 C.EAI_SYSTEM and non-nil error",
args: args{
code: eaiSystem,
err: syscall.EAGAIN,
goos: "darwin",
},
expects: expects{
message: syscall.EAGAIN.Error(),
code: eaiSystem,
err: syscall.EAGAIN,
},
}, {
name: "with C.EAI_SYSTEM and nil error",
args: args{
code: eaiSystem,
err: nil,
goos: "darwin",
},
expects: expects{
message: syscall.EMFILE.Error(),
code: eaiSystem,
err: syscall.EMFILE,
},
}, {
name: "with C.EAI_NONAME",
args: args{
code: eaiNoName,
err: nil,
goos: "darwin",
},
expects: expects{
message: ErrOODNSNoSuchHost.Error(),
code: eaiNoName,
err: ErrOODNSNoSuchHost,
},
}, {
name: "with an unhandled error",
args: args{
code: eaiBadFlags,
err: nil,
goos: "darwin",
},
expects: expects{
message: ErrOODNSMisbehaving.Error(),
code: eaiBadFlags,
err: ErrOODNSMisbehaving,
},
}}
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")
}
})
}
}