Skip to content

Commit b384e6c

Browse files
committed
Merged revisions 84024 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r84024 | giampaolo.rodola | 2010-08-14 18:45:41 +0200 (sab, 14 ago 2010) | 1 line fix issue #8857: provide a test case for socket.getaddrinfo ........
1 parent d6dac2e commit b384e6c

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lib/test/test_socket.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,21 @@
1717
from weakref import proxy
1818
import signal
1919

20+
def try_address(host, port=0, family=socket.AF_INET):
21+
"""Try to bind a socket on the given host:port and return True
22+
if that has been possible."""
23+
try:
24+
sock = socket.socket(family, socket.SOCK_STREAM)
25+
sock.bind((host, port))
26+
except (socket.error, socket.gaierror):
27+
return False
28+
else:
29+
sock.close()
30+
return True
31+
2032
HOST = support.HOST
2133
MSG = b'Michael Gilfix was here\n'
34+
SUPPORTS_IPV6 = socket.has_ipv6 and try_address('::1', family=socket.AF_INET6)
2235

2336
class SocketTCPTest(unittest.TestCase):
2437

@@ -554,6 +567,44 @@ def test_sock_ioctl(self):
554567
self.assertTrue(hasattr(socket, 'RCVALL_ON'))
555568
self.assertTrue(hasattr(socket, 'RCVALL_OFF'))
556569

570+
def testGetaddrinfo(self):
571+
try:
572+
socket.getaddrinfo('localhost', 80)
573+
except socket.gaierror as err:
574+
if err.errno == socket.EAI_SERVICE:
575+
# see http://bugs.python.org/issue1282647
576+
self.skipTest("buggy libc version")
577+
raise
578+
# len of every sequence is supposed to be == 5
579+
for info in socket.getaddrinfo(HOST, None):
580+
self.assertEqual(len(info), 5)
581+
# host can be a domain name, a string representation of an
582+
# IPv4/v6 address or None
583+
socket.getaddrinfo('localhost', 80)
584+
socket.getaddrinfo('127.0.0.1', 80)
585+
socket.getaddrinfo(None, 80)
586+
if SUPPORTS_IPV6:
587+
socket.getaddrinfo('::1', 80)
588+
# port can be a string service name such as "http", a numeric
589+
# port number or None
590+
socket.getaddrinfo(HOST, "http")
591+
socket.getaddrinfo(HOST, 80)
592+
socket.getaddrinfo(HOST, None)
593+
# test family and socktype filters
594+
infos = socket.getaddrinfo(HOST, None, socket.AF_INET)
595+
for family, _, _, _, _ in infos:
596+
self.assertEqual(family, socket.AF_INET)
597+
infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
598+
for _, socktype, _, _, _ in infos:
599+
self.assertEqual(socktype, socket.SOCK_STREAM)
600+
# test proto and flags arguments
601+
socket.getaddrinfo(HOST, None, 0, 0, socket.AI_CANONNAME)
602+
socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE)
603+
# a server willing to support both IPv4 and IPv6 will
604+
# usually do this
605+
socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
606+
socket.AI_PASSIVE)
607+
557608

558609
class BasicTCPTest(SocketConnectedTest):
559610

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ Build
479479
Tests
480480
-----
481481

482+
- Issue #8857: Provide a test case for socket.getaddrinfo.
483+
482484
- Issue #8433: Fix test_curses failure with newer versions of ncurses.
483485

484486
- Issue #9496: Provide a test suite for the rlcompleter module. Patch by

0 commit comments

Comments
 (0)