Skip to content

Commit ab14b59

Browse files
committed
fix and debug
1 parent cfab9f8 commit ab14b59

File tree

1 file changed

+21
-45
lines changed

1 file changed

+21
-45
lines changed

tests/test_dns.py

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
import asyncio
22
import socket
3+
import sys
34
import unittest
45

56
from uvloop import _testbase as tb
67

78

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-
first = True
14-
for af, sk, proto, canon_name, addr in result:
15-
if kwargs.get('flags', 0) & socket.AI_CANONNAME:
16-
if not canon_name and first:
17-
first = False
18-
canon_name = args[0]
19-
if not isinstance(canon_name, str):
20-
canon_name = canon_name.decode('ascii')
21-
elif canon_name:
22-
canon_name = ''
23-
rv.append((af, sk, proto, canon_name, addr))
24-
return rv
25-
26-
279
class BaseTestDNS:
2810

29-
def _test_getaddrinfo(self, *args, _patch=False, _sorted=False, **kwargs):
11+
def _test_getaddrinfo(self, *args, _sorted=False, **kwargs):
3012
err = None
3113
try:
32-
if _patch:
33-
a1 = patched_getaddrinfo(*args, **kwargs)
34-
else:
35-
a1 = socket.getaddrinfo(*args, **kwargs)
14+
a1 = socket.getaddrinfo(*args, **kwargs)
3615
except (socket.gaierror, UnicodeError) as ex:
3716
err = ex
3817

@@ -54,20 +33,23 @@ def _test_getaddrinfo(self, *args, _patch=False, _sorted=False, **kwargs):
5433

5534
if _sorted:
5635
if kwargs.get('flags', 0) & socket.AI_CANONNAME and a1 and a2:
57-
af, sk, proto, canon_name1, addr = a1[0]
58-
a1[0] = (af, sk, proto, '', addr)
59-
af, sk, proto, canon_name2, addr = a2[0]
60-
a2[0] = (af, sk, proto, '', addr)
61-
self.assertEqual(canon_name1, canon_name2)
36+
self.assertEqual(a1[0][3], a2[0][3])
37+
a1 = [(af, sk, pr, addr) for af, sk, pr, _, addr in a1]
38+
a2 = [(af, sk, pr, addr) for af, sk, pr, _, addr in a2]
6239

6340
try:
6441
self.assertEqual(sorted(a1), sorted(a2))
6542
except AssertionError:
6643
for x, y in zip(sorted(a1), sorted(a2)):
67-
print(x, '\t', y)
44+
print(x, '\t', y, file=sys.stderr)
6845
raise
6946
else:
70-
self.assertEqual(a1, a2)
47+
try:
48+
self.assertEqual(a1, a2)
49+
except AssertionError:
50+
for x, y in zip(a1, a2):
51+
print(x, '\t', y, file=sys.stderr)
52+
raise
7153

7254
def _test_getnameinfo(self, *args, **kwargs):
7355
err = None
@@ -146,32 +128,28 @@ def test_getaddrinfo_12(self):
146128
patch = self.implementation != 'asyncio'
147129

148130
self._test_getaddrinfo('127.0.0.1', '80')
149-
self._test_getaddrinfo('127.0.0.1', '80', type=socket.SOCK_STREAM,
150-
_patch=patch)
131+
self._test_getaddrinfo('127.0.0.1', '80', type=socket.SOCK_STREAM)
151132

152133
def test_getaddrinfo_13(self):
153134
# musl always returns ai_canonname but we don't
154135
patch = self.implementation != 'asyncio'
155136

156137
self._test_getaddrinfo(b'127.0.0.1', b'80')
157-
self._test_getaddrinfo(b'127.0.0.1', b'80', type=socket.SOCK_STREAM,
158-
_patch=patch)
138+
self._test_getaddrinfo(b'127.0.0.1', b'80', type=socket.SOCK_STREAM)
159139

160140
def test_getaddrinfo_14(self):
161141
# musl always returns ai_canonname but we don't
162142
patch = self.implementation != 'asyncio'
163143

164144
self._test_getaddrinfo(b'127.0.0.1', b'http')
165-
self._test_getaddrinfo(b'127.0.0.1', b'http', type=socket.SOCK_STREAM,
166-
_patch=patch)
145+
self._test_getaddrinfo(b'127.0.0.1', b'http', type=socket.SOCK_STREAM)
167146

168147
def test_getaddrinfo_15(self):
169148
# musl always returns ai_canonname but we don't
170149
patch = self.implementation != 'asyncio'
171150

172151
self._test_getaddrinfo('127.0.0.1', 'http')
173-
self._test_getaddrinfo('127.0.0.1', 'http', type=socket.SOCK_STREAM,
174-
_patch=patch)
152+
self._test_getaddrinfo('127.0.0.1', 'http', type=socket.SOCK_STREAM)
175153

176154
def test_getaddrinfo_16(self):
177155
self._test_getaddrinfo('localhost', 'http')
@@ -192,10 +170,9 @@ def test_getaddrinfo_19(self):
192170
patch = self.implementation != 'asyncio'
193171

194172
self._test_getaddrinfo('::1', 80)
173+
self._test_getaddrinfo('::1', 80, type=socket.SOCK_STREAM)
195174
self._test_getaddrinfo('::1', 80, type=socket.SOCK_STREAM,
196-
_patch=patch)
197-
self._test_getaddrinfo('::1', 80, type=socket.SOCK_STREAM,
198-
flags=socket.AI_CANONNAME, _patch=patch)
175+
flags=socket.AI_CANONNAME)
199176

200177
def test_getaddrinfo_20(self):
201178
# musl always returns ai_canonname while macOS never return for IPs,
@@ -204,10 +181,9 @@ def test_getaddrinfo_20(self):
204181
patch = self.implementation != 'asyncio'
205182

206183
self._test_getaddrinfo('127.0.0.1', 80)
184+
self._test_getaddrinfo('127.0.0.1', 80, type=socket.SOCK_STREAM)
207185
self._test_getaddrinfo('127.0.0.1', 80, type=socket.SOCK_STREAM,
208-
_patch=patch)
209-
self._test_getaddrinfo('127.0.0.1', 80, type=socket.SOCK_STREAM,
210-
flags=socket.AI_CANONNAME, _patch=patch)
186+
flags=socket.AI_CANONNAME)
211187

212188
# https://github.com/libuv/libuv/security/advisories/GHSA-f74f-cvh7-c6q6
213189
# See also: https://github.com/MagicStack/uvloop/pull/600

0 commit comments

Comments
 (0)