2015-11-21 00:08:48 +00:00
|
|
|
import socket
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from uvloop import _testbase as tb
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTestDNS:
|
|
|
|
|
|
|
|
def _test_getaddrinfo(self, *args, **kwargs):
|
|
|
|
err = None
|
|
|
|
try:
|
|
|
|
a1 = socket.getaddrinfo(*args, **kwargs)
|
|
|
|
except socket.gaierror as ex:
|
|
|
|
err = ex
|
|
|
|
|
|
|
|
try:
|
|
|
|
a2 = self.loop.run_until_complete(
|
|
|
|
self.loop.getaddrinfo(*args, **kwargs))
|
|
|
|
except socket.gaierror as ex:
|
|
|
|
if err is not None:
|
|
|
|
self.assertEqual(ex.args, err.args)
|
|
|
|
else:
|
2016-05-05 16:42:42 +00:00
|
|
|
ex.__context__ = err
|
|
|
|
raise ex
|
|
|
|
except OSError as ex:
|
|
|
|
ex.__context__ = err
|
|
|
|
raise ex
|
2015-11-21 00:08:48 +00:00
|
|
|
else:
|
2016-05-05 18:00:03 +00:00
|
|
|
if err is not None:
|
|
|
|
raise err
|
|
|
|
|
|
|
|
self.assertEqual(a1, a2)
|
2015-11-21 00:08:48 +00:00
|
|
|
|
2016-04-11 23:44:35 +00:00
|
|
|
def _test_getnameinfo(self, *args, **kwargs):
|
|
|
|
err = None
|
|
|
|
try:
|
|
|
|
a1 = socket.getnameinfo(*args, **kwargs)
|
|
|
|
except Exception as ex:
|
|
|
|
err = ex
|
|
|
|
|
|
|
|
try:
|
|
|
|
a2 = self.loop.run_until_complete(
|
|
|
|
self.loop.getnameinfo(*args, **kwargs))
|
|
|
|
except Exception as ex:
|
|
|
|
if err is not None:
|
2016-04-19 20:10:20 +00:00
|
|
|
if ex.__class__ is not err.__class__:
|
|
|
|
print(ex, err)
|
|
|
|
self.assertIs(ex.__class__, err.__class__)
|
2016-04-11 23:44:35 +00:00
|
|
|
self.assertEqual(ex.args, err.args)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if err is not None:
|
2016-05-05 16:42:42 +00:00
|
|
|
raise err
|
2016-04-11 23:44:35 +00:00
|
|
|
|
|
|
|
self.assertEqual(a1, a2)
|
|
|
|
|
2015-11-21 00:08:48 +00:00
|
|
|
def test_getaddrinfo_1(self):
|
2016-05-23 18:07:59 +00:00
|
|
|
self._test_getaddrinfo('example.com', 80)
|
2015-11-21 00:08:48 +00:00
|
|
|
|
|
|
|
def test_getaddrinfo_2(self):
|
2016-05-23 18:07:59 +00:00
|
|
|
self._test_getaddrinfo('example.com', 80, flags=socket.AI_CANONNAME)
|
2015-11-21 00:08:48 +00:00
|
|
|
|
|
|
|
def test_getaddrinfo_3(self):
|
2016-05-23 18:07:59 +00:00
|
|
|
self._test_getaddrinfo('a' + '1' * 50 + '.wat', 800)
|
2015-11-21 00:08:48 +00:00
|
|
|
|
|
|
|
def test_getaddrinfo_4(self):
|
2016-05-23 18:07:59 +00:00
|
|
|
self._test_getaddrinfo('example.com', 80, family=-1)
|
2015-11-21 00:08:48 +00:00
|
|
|
|
2016-04-19 20:10:20 +00:00
|
|
|
def test_getaddrinfo_5(self):
|
2016-05-23 18:07:59 +00:00
|
|
|
self._test_getaddrinfo('example.com', '80')
|
2016-04-19 20:10:20 +00:00
|
|
|
|
|
|
|
def test_getaddrinfo_6(self):
|
2016-05-23 18:07:59 +00:00
|
|
|
self._test_getaddrinfo(b'example.com', '80'.encode())
|
2016-04-19 20:10:20 +00:00
|
|
|
|
2016-05-05 16:42:42 +00:00
|
|
|
def test_getaddrinfo_7(self):
|
|
|
|
self._test_getaddrinfo(None, 0)
|
|
|
|
|
|
|
|
def test_getaddrinfo_8(self):
|
|
|
|
self._test_getaddrinfo('', 0)
|
|
|
|
|
|
|
|
def test_getaddrinfo_9(self):
|
|
|
|
self._test_getaddrinfo(b'', 0)
|
|
|
|
|
|
|
|
def test_getaddrinfo_10(self):
|
|
|
|
self._test_getaddrinfo(None, None)
|
|
|
|
|
|
|
|
def test_getaddrinfo_11(self):
|
2016-05-23 18:07:59 +00:00
|
|
|
self._test_getaddrinfo(b'example.com', '80')
|
2016-05-05 16:42:42 +00:00
|
|
|
|
|
|
|
def test_getaddrinfo_12(self):
|
2016-05-23 18:07:59 +00:00
|
|
|
self._test_getaddrinfo(b'example.com', b'80')
|
|
|
|
|
|
|
|
def test_getaddrinfo_13(self):
|
|
|
|
self._test_getaddrinfo('127.0.0.1', '80')
|
|
|
|
|
|
|
|
def test_getaddrinfo_14(self):
|
|
|
|
self._test_getaddrinfo(b'127.0.0.1', b'80')
|
|
|
|
|
|
|
|
def test_getaddrinfo_15(self):
|
|
|
|
self._test_getaddrinfo(b'127.0.0.1', b'http')
|
|
|
|
|
|
|
|
def test_getaddrinfo_16(self):
|
|
|
|
self._test_getaddrinfo('127.0.0.1', 'http', type=socket.SOCK_STREAM)
|
|
|
|
|
|
|
|
def test_getaddrinfo_17(self):
|
|
|
|
self._test_getaddrinfo('localhost', 'http')
|
|
|
|
|
|
|
|
def test_getaddrinfo_18(self):
|
|
|
|
self._test_getaddrinfo(b'localhost', 'http', type=socket.SOCK_STREAM)
|
2016-05-05 16:42:42 +00:00
|
|
|
|
2016-04-11 23:44:35 +00:00
|
|
|
######
|
|
|
|
|
|
|
|
def test_getnameinfo_1(self):
|
|
|
|
self._test_getnameinfo(('127.0.0.1', 80), 0)
|
|
|
|
|
|
|
|
def test_getnameinfo_2(self):
|
|
|
|
self._test_getnameinfo(('127.0.0.1', 80, 1231231231213), 0)
|
|
|
|
|
|
|
|
def test_getnameinfo_3(self):
|
|
|
|
self._test_getnameinfo(('127.0.0.1', 80, 0, 0), 0)
|
|
|
|
|
|
|
|
def test_getnameinfo_4(self):
|
|
|
|
self._test_getnameinfo(('::1', 80), 0)
|
|
|
|
|
|
|
|
def test_getnameinfo_5(self):
|
|
|
|
self._test_getnameinfo(('localhost', 8080), 0)
|
|
|
|
|
2015-11-21 00:08:48 +00:00
|
|
|
|
|
|
|
class Test_UV_DNS(BaseTestDNS, tb.UVTestCase):
|
|
|
|
|
|
|
|
def test_getaddrinfo_close_loop(self):
|
2016-05-23 18:07:59 +00:00
|
|
|
# Test that we can close the loop with a running
|
|
|
|
# DNS query.
|
|
|
|
|
2015-11-21 00:08:48 +00:00
|
|
|
try:
|
|
|
|
# Check that we have internet connection
|
2016-05-23 18:07:59 +00:00
|
|
|
socket.getaddrinfo('example.com', 80)
|
2015-11-21 00:08:48 +00:00
|
|
|
except socket.error:
|
|
|
|
raise unittest.SkipTest
|
|
|
|
|
|
|
|
async def run():
|
2016-05-23 18:07:59 +00:00
|
|
|
fut = self.loop.getaddrinfo('example.com', 80)
|
2015-11-21 00:08:48 +00:00
|
|
|
fut.cancel()
|
|
|
|
self.loop.stop()
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.loop.run_until_complete(run())
|
|
|
|
finally:
|
|
|
|
self.loop.close()
|
|
|
|
|
|
|
|
|
|
|
|
class Test_AIO_DNS(BaseTestDNS, tb.AIOTestCase):
|
2016-05-23 18:07:59 +00:00
|
|
|
pass
|