55from  uvloop  import  _testbase  as  tb 
66
77
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+ 
825class  BaseTestDNS :
926
10-  def  _test_getaddrinfo (self , * args , ** kwargs ):
27+  def  _test_getaddrinfo (self , * args , _patch = False ,  ** kwargs ):
1128 err  =  None 
1229 try :
13-  a1  =  socket .getaddrinfo (* args , ** kwargs )
30+  if  _patch :
31+  a1  =  patched_getaddrinfo (* args , ** kwargs )
32+  else :
33+  a1  =  socket .getaddrinfo (* args , ** kwargs )
1434 except  socket .gaierror  as  ex :
1535 err  =  ex 
1636
@@ -100,20 +120,36 @@ def test_getaddrinfo_11(self):
100120 self ._test_getaddrinfo (b'example.com' , '80' , type = socket .SOCK_STREAM )
101121
102122 def  test_getaddrinfo_12 (self ):
123+  # musl always returns ai_canonname but we don't 
124+  patch  =  self .implementation  !=  'asyncio' 
125+ 
103126 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 )
105129
106130 def  test_getaddrinfo_13 (self ):
131+  # musl always returns ai_canonname but we don't 
132+  patch  =  self .implementation  !=  'asyncio' 
133+ 
107134 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 )
109137
110138 def  test_getaddrinfo_14 (self ):
139+  # musl always returns ai_canonname but we don't 
140+  patch  =  self .implementation  !=  'asyncio' 
141+ 
111142 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 )
113145
114146 def  test_getaddrinfo_15 (self ):
147+  # musl always returns ai_canonname but we don't 
148+  patch  =  self .implementation  !=  'asyncio' 
149+ 
115150 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 )
117153
118154 def  test_getaddrinfo_16 (self ):
119155 self ._test_getaddrinfo ('localhost' , 'http' )
@@ -128,12 +164,26 @@ def test_getaddrinfo_18(self):
128164 self ._test_getaddrinfo ('localhost' , b'http' , type = socket .SOCK_STREAM )
129165
130166 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+ 
131171 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 )
133176
134177 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+ 
135182 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 )
137187
138188 ###### 
139189
0 commit comments