forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getaddrinfo_linux_test.go
181 lines (176 loc) · 3.55 KB
/
getaddrinfo_linux_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//go:build cgo && linux
package netxlite
import (
"errors"
"runtime"
"syscall"
"testing"
)
func TestGetaddrinfoAIFlags(t *testing.T) {
var wrong bool
switch runtime.GOOS {
case "android":
wrong = getaddrinfoAIFlags != aiCanonname
default:
wrong = getaddrinfoAIFlags != (aiCanonname | aiV4Mapped | aiAll)
}
if wrong {
t.Fatal("wrong flags for platform")
}
}
func TestGetaddrinfoGetPlatformSpecificAiFlags(t *testing.T) {
type args struct {
goos string
}
type expects struct {
flags int64
}
var inputs = []struct {
name string
args args
expects expects
}{{
name: "using the Android platform",
args: args{
goos: "android",
},
expects: expects{
flags: aiCanonname,
},
}, {
name: "using Linux",
args: args{
goos: "linux",
},
expects: expects{
flags: aiCanonname | aiV4Mapped | aiAll,
},
}, {
name: "when the platform name is empty",
args: args{
goos: "",
},
expects: expects{
flags: aiCanonname | aiV4Mapped | aiAll,
},
}}
for _, input := range inputs {
t.Run(input.name, func(t *testing.T) {
flags := getaddrinfoGetPlatformSpecificAIFlags(input.args.goos)
if int64(flags) != input.expects.flags {
t.Fatal("invalid flags")
}
})
}
}
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: "linux",
},
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: "linux",
},
expects: expects{
message: syscall.EMFILE.Error(),
code: eaiSystem,
err: syscall.EMFILE,
},
}, {
name: "with C.EAI_NONAME",
args: args{
code: eaiNoName,
err: nil,
goos: "linux",
},
expects: expects{
message: ErrOODNSNoSuchHost.Error(),
code: eaiNoName,
err: ErrOODNSNoSuchHost,
},
}, {
name: "with C.EAI_NODATA on Linux",
args: args{
code: eaiNoData,
err: nil,
goos: "linux",
},
expects: expects{
message: ErrOODNSNoAnswer.Error(),
code: eaiNoData,
err: ErrOODNSNoAnswer,
},
}, {
name: "with C.EAI_NODATA on Android",
args: args{
code: eaiNoData,
err: nil,
goos: "android",
},
expects: expects{
message: ErrAndroidDNSCacheNoData.Error(),
code: eaiNoData,
err: ErrAndroidDNSCacheNoData,
},
}, {
name: "with an unhandled error",
args: args{
code: eaiBadFlags,
err: nil,
goos: "linux",
},
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")
}
})
}
}