Author: Brian Kearns <bdkea...@gmail.com> Branch: Changeset: r62617:1247527d5553 Date: 2013-03-21 17:39 -0400 http://bitbucket.org/pypy/pypy/changeset/1247527d5553/
Log: further improve socket.gethostby{name,addr} tests diff --git a/pypy/module/_socket/test/test_sock_app.py b/pypy/module/_socket/test/test_sock_app.py --- a/pypy/module/_socket/test/test_sock_app.py +++ b/pypy/module/_socket/test/test_sock_app.py @@ -20,29 +20,22 @@ assert space.unwrap(host) == socket.gethostname() def test_gethostbyname(): - host = "localhost" - ip = space.appexec([w_socket, space.wrap(host)], - "(_socket, host): return _socket.gethostbyname(host)") - assert space.unwrap(ip) == socket.gethostbyname(host) + for host in ["localhost", "127.0.0.1"]: + ip = space.appexec([w_socket, space.wrap(host)], + "(_socket, host): return _socket.gethostbyname(host)") + assert space.unwrap(ip) == socket.gethostbyname(host) def test_gethostbyname_ex(): - host = "localhost" - ip = space.appexec([w_socket, space.wrap(host)], - "(_socket, host): return _socket.gethostbyname_ex(host)") - assert isinstance(space.unwrap(ip), tuple) - assert space.unwrap(ip) == socket.gethostbyname_ex(host) + for host in ["localhost", "127.0.0.1"]: + ip = space.appexec([w_socket, space.wrap(host)], + "(_socket, host): return _socket.gethostbyname_ex(host)") + assert space.unwrap(ip) == socket.gethostbyname_ex(host) def test_gethostbyaddr(): - host = "localhost" - expected = socket.gethostbyaddr(host) - ip = space.appexec([w_socket, space.wrap(host)], - "(_socket, host): return _socket.gethostbyaddr(host)") - assert space.unwrap(ip) == socket.gethostbyaddr(host) - - host = "127.0.0.1" - ip = space.appexec([w_socket, space.wrap(host)], - "(_socket, host): return _socket.gethostbyaddr(host)") - assert space.unwrap(ip) == socket.gethostbyaddr(host) + for host in ["localhost", "127.0.0.1"]: + ip = space.appexec([w_socket, space.wrap(host)], + "(_socket, host): return _socket.gethostbyaddr(host)") + assert space.unwrap(ip) == socket.gethostbyaddr(host) def test_getservbyname(): name = "smtp" diff --git a/rpython/rlib/test/test_rsocket.py b/rpython/rlib/test/test_rsocket.py --- a/rpython/rlib/test/test_rsocket.py +++ b/rpython/rlib/test/test_rsocket.py @@ -40,55 +40,49 @@ a = NETLINKAddress(pid, group_mask) assert a.get_pid() == pid assert a.get_groups() == group_mask - + def test_gethostname(): s = gethostname() assert isinstance(s, str) def test_gethostbyname(): - a = gethostbyname('localhost') - assert isinstance(a, INETAddress) - assert a.get_host() == "127.0.0.1" + for host in ["localhost", "127.0.0.1"]: + a = gethostbyname(host) + assert isinstance(a, INETAddress) + assert a.get_host() == "127.0.0.1" def test_gethostbyname_ex(): - name, aliases, address_list = gethostbyname_ex('localhost') - allnames = [name] + aliases - for n in allnames: - assert isinstance(n, str) - if sys.platform != 'win32': - assert 'localhost' in allnames - for a in address_list: - if isinstance(a, INETAddress) and a.get_host() == "127.0.0.1": - break # ok - else: - py.test.fail("could not find the 127.0.0.1 IPv4 address in %r" - % (address_list,)) + for host in ["localhost", "127.0.0.1"]: + name, aliases, address_list = gethostbyname_ex(host) + allnames = [name] + aliases + for n in allnames: + assert isinstance(n, str) + if sys.platform != 'win32': + assert host in allnames + for a in address_list: + if isinstance(a, INETAddress) and a.get_host() == "127.0.0.1": + break # ok + # no IPV6, should always return IPV4 + else: + py.test.fail("could not find the localhost address in %r" + % (address_list,)) def test_gethostbyaddr(): - name, aliases, address_list = gethostbyaddr('127.0.0.1') - allnames = [name] + aliases - for n in allnames: - assert isinstance(n, str) - if sys.platform != 'win32': - assert 'localhost' in allnames - for a in address_list: - if isinstance(a, INETAddress) and a.get_host() == "127.0.0.1": - break # ok - else: - py.test.fail("could not find the 127.0.0.1 IPv4 address in %r" - % (address_list,)) - - name, aliases, address_list = gethostbyaddr('localhost') + for host in ["localhost", "127.0.0.1"]: + name, aliases, address_list = gethostbyaddr(host) allnames = [name] + aliases for n in allnames: assert isinstance(n, str) if sys.platform != 'win32': assert 'localhost' in allnames for a in address_list: - if isinstance(a, INET6Address) and a.get_host() == "::1": + if isinstance(a, INETAddress) and a.get_host() == "127.0.0.1": break # ok + if host == 'localhost': # name lookup might return IPV6 + if isinstance(a, INET6Address) and a.get_host() == "::1": + break # ok else: - py.test.fail("could not find the ::1 IPv6 address in %r" + py.test.fail("could not find the localhost address in %r" % (address_list,)) def test_getservbyname(): @@ -124,7 +118,7 @@ def as_str(self): return self.x - + if sys.platform == "win32": py.test.skip('No socketpair on Windows') s1, s2 = socketpair() _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit