5
5
from uvloop import _testbase as tb
6
6
7
7
8
+ def patched_getaddrinfo (* args , ** kwargs ):
9
+ # corrected socket.getaddrinfo() behavior: ai_canonname always follows the
10
+ # flag AI_CANONNAME, even if `host` is an IP
11
+ rv = []
12
+ result = socket .getaddrinfo (* args , ** kwargs )
13
+ for af , sk , proto , canon_name , addr in result :
14
+ if kwargs .get ('flags' , 0 ) & socket .AI_CANONNAME :
15
+ if not canon_name :
16
+ canon_name = args [0 ]
17
+ if not isinstance (canon_name , str ):
18
+ canon_name = canon_name .decode ('ascii' )
19
+ elif canon_name :
20
+ canon_name = ''
21
+ rv .append ((af , sk , proto , canon_name , addr ))
22
+ return rv
23
+
24
+
8
25
class BaseTestDNS :
9
26
10
- def _test_getaddrinfo (self , * args , ** kwargs ):
27
+ def _test_getaddrinfo (self , * args , _patch = False , ** kwargs ):
11
28
err = None
12
29
try :
13
- a1 = socket .getaddrinfo (* args , ** kwargs )
30
+ if _patch :
31
+ a1 = patched_getaddrinfo (* args , ** kwargs )
32
+ else :
33
+ a1 = socket .getaddrinfo (* args , ** kwargs )
14
34
except socket .gaierror as ex :
15
35
err = ex
16
36
@@ -100,20 +120,36 @@ def test_getaddrinfo_11(self):
100
120
self ._test_getaddrinfo (b'example.com' , '80' , type = socket .SOCK_STREAM )
101
121
102
122
def test_getaddrinfo_12 (self ):
123
+ # musl always returns ai_canonname but we don't
124
+ patch = self .implementation != 'asyncio'
125
+
103
126
self ._test_getaddrinfo ('127.0.0.1' , '80' )
104
- self ._test_getaddrinfo ('127.0.0.1' , '80' , type = socket .SOCK_STREAM )
127
+ self ._test_getaddrinfo ('127.0.0.1' , '80' , type = socket .SOCK_STREAM ,
128
+ _patch = patch )
105
129
106
130
def test_getaddrinfo_13 (self ):
131
+ # musl always returns ai_canonname but we don't
132
+ patch = self .implementation != 'asyncio'
133
+
107
134
self ._test_getaddrinfo (b'127.0.0.1' , b'80' )
108
- self ._test_getaddrinfo (b'127.0.0.1' , b'80' , type = socket .SOCK_STREAM )
135
+ self ._test_getaddrinfo (b'127.0.0.1' , b'80' , type = socket .SOCK_STREAM ,
136
+ _patch = patch )
109
137
110
138
def test_getaddrinfo_14 (self ):
139
+ # musl always returns ai_canonname but we don't
140
+ patch = self .implementation != 'asyncio'
141
+
111
142
self ._test_getaddrinfo (b'127.0.0.1' , b'http' )
112
- self ._test_getaddrinfo (b'127.0.0.1' , b'http' , type = socket .SOCK_STREAM )
143
+ self ._test_getaddrinfo (b'127.0.0.1' , b'http' , type = socket .SOCK_STREAM ,
144
+ _patch = patch )
113
145
114
146
def test_getaddrinfo_15 (self ):
147
+ # musl always returns ai_canonname but we don't
148
+ patch = self .implementation != 'asyncio'
149
+
115
150
self ._test_getaddrinfo ('127.0.0.1' , 'http' )
116
- self ._test_getaddrinfo ('127.0.0.1' , 'http' , type = socket .SOCK_STREAM )
151
+ self ._test_getaddrinfo ('127.0.0.1' , 'http' , type = socket .SOCK_STREAM ,
152
+ _patch = patch )
117
153
118
154
def test_getaddrinfo_16 (self ):
119
155
self ._test_getaddrinfo ('localhost' , 'http' )
@@ -128,12 +164,26 @@ def test_getaddrinfo_18(self):
128
164
self ._test_getaddrinfo ('localhost' , b'http' , type = socket .SOCK_STREAM )
129
165
130
166
def test_getaddrinfo_19 (self ):
167
+ # musl always returns ai_canonname while macOS never return for IPs,
168
+ # but we strictly follow the docs to use the AI_CANONNAME flag
169
+ patch = self .implementation != 'asyncio'
170
+
131
171
self ._test_getaddrinfo ('::1' , 80 )
132
- self ._test_getaddrinfo ('::1' , 80 , type = socket .SOCK_STREAM )
172
+ self ._test_getaddrinfo ('::1' , 80 , type = socket .SOCK_STREAM ,
173
+ _patch = patch )
174
+ self ._test_getaddrinfo ('::1' , 80 , type = socket .SOCK_STREAM ,
175
+ flags = socket .AI_CANONNAME , _patch = patch )
133
176
134
177
def test_getaddrinfo_20 (self ):
178
+ # musl always returns ai_canonname while macOS never return for IPs,
179
+ # but we strictly follow the docs to use the AI_CANONNAME flag
180
+ patch = self .implementation != 'asyncio'
181
+
135
182
self ._test_getaddrinfo ('127.0.0.1' , 80 )
136
- self ._test_getaddrinfo ('127.0.0.1' , 80 , type = socket .SOCK_STREAM )
183
+ self ._test_getaddrinfo ('127.0.0.1' , 80 , type = socket .SOCK_STREAM ,
184
+ _patch = patch )
185
+ self ._test_getaddrinfo ('127.0.0.1' , 80 , type = socket .SOCK_STREAM ,
186
+ flags = socket .AI_CANONNAME , _patch = patch )
137
187
138
188
######
139
189
0 commit comments