9
10
ACCEPTED_GAIERROR_MISMATCH = {
10
"gaierror(-5, 'No address associated with hostname')": "DNSError(3, 'name does not exist')"
11
"gaierror(-5, 'No address associated with hostname')":
12
("DNSError(3, 'name does not exist')", "DNSError(66, 'unknown')"),
13
15
assert gaierror is real_socket.gaierror
14
16
assert error is real_socket.error
16
18
VERBOSE = '-v' in sys.argv
19
IGNORE_ERRORS = '--ignore' in sys.argv
20
DEBUG = '-d' in sys.argv
21
for arg in ['--ignore', '-d']:
19
28
class TestCase(greentest.TestCase):
32
def assertEqual(self, a, b):
34
print 'ERROR: %r != %r' % (a, b)
21
36
def _test(self, hostname, check_ip=None):
22
37
self._test_gethostbyname(hostname, check_ip=check_ip)
28
43
print 'real_socket.gethostbyname(%r)' % (hostname, )
29
44
real_ip = real_socket.gethostbyname(hostname)
30
45
except Exception, ex:
34
51
print 'gevent.socket.gethostbyname(%r)' % (hostname, )
35
52
ip = gethostbyname(hostname)
36
53
except Exception, ex:
38
57
if self.equal(real_ip, ip):
42
61
self.assertEqual(check_ip, ip)
64
PORTS = [80, 0, 53, 'http']
46
65
getaddrinfo_args = [(),
48
67
(AF_UNSPEC, SOCK_STREAM, 0, 0),
52
71
(AF_UNSPEC, SOCK_STREAM, 6),
53
72
(AF_INET, SOCK_DGRAM, 17)]
56
74
def _test_getaddrinfo(self, hostname):
57
75
for port in self.PORTS:
58
76
for args in self.getaddrinfo_args:
60
79
print 'real_socket.getaddrinfo(%r, %r, %r)' % (hostname, port, args)
62
81
real_ip = real_socket.getaddrinfo(hostname, port, *args)
83
print ' returned %r' % (real_ip, )
63
84
except Exception, ex:
66
89
print 'gevent.socket.getaddrinfo(%r, %r, %r)' % (hostname, port, args)
68
91
ip = getaddrinfo(hostname, port, *args)
93
print ' returned %r' % (ip, )
69
94
except Exception, ex:
71
98
if not self.equal(real_ip, ip):
72
99
args_str = ', '.join(repr(x) for x in (hostname, port) + args)
73
print 'WARNING: getaddrinfo(%s):\n %r\n != %r' % (args_str, real_ip, ip)
100
print 'WARNING: getaddrinfo(%s):\n %r (stdlib)\n != %r (gevent)' % (args_str, real_ip, ip)
74
101
# QQQ switch_expected becomes useless when a bunch of unrelated tests are merged
75
102
# into a single one like above. Generate individual test cases instead?
80
107
if isinstance(a, Exception) and isinstance(b, Exception):
81
108
if repr(a) == repr(b):
83
if ACCEPTED_GAIERROR_MISMATCH.get(repr(a), repr(b))==repr(b):
110
if repr(b) in ACCEPTED_GAIERROR_MISMATCH.get(repr(a), (repr(b), )):
86
113
def checkEqual(self, a, b):
152
179
self._test('sdfsdfgu5e66098032453245wfdggd.com')
182
class TestInternational(TestCase):
185
self._test(u'президент.рф')
188
class TestIPv6(TestCase):
191
#self.PORTS = ['http']
192
#self.getaddrinfo_args = [(), (AF_UNSPEC, ), (AF_INET, ), (AF_INET6, )]
193
self._test('aaaa.test-ipv6.com')
196
class TestBadPort(TestCase):
199
self.PORTS = ['xxxxxx']
200
self._test('www.google.com')
155
203
if __name__ == '__main__':