Skip to content

Commit ab728dc

Browse files
committed
test: Check for host availability of IPv6 support properly
Check if host supports IPv6 protocol, and if not, skip the IPv6-related bits. Also, make sure that server listens on IPv6 loopback properly: 'localhost' may not always resolve to ::1. In fact, a properly configured system would most likely have 'localhost6' instead. In any case, it's better to use the IP addresses directly.
1 parent 237d11a commit ab728dc

1 file changed

Lines changed: 36 additions & 10 deletions

File tree

postgresql/test/test_connect.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import os
66
import unittest
77
import atexit
8+
import socket
9+
import errno
810

911
from ..python.socket import find_available_port
1012

@@ -16,8 +18,31 @@
1618
from .. import driver as pg_driver
1719
from .. import open as pg_open
1820

21+
22+
def check_for_ipv6():
23+
result = False
24+
if socket.has_ipv6:
25+
try:
26+
socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
27+
result = True
28+
except socket.error as e:
29+
errs = [errno.EAFNOSUPPORT]
30+
WSAEAFNOSUPPORT = getattr(errno, 'WSAEAFNOSUPPORT', None)
31+
if WSAEAFNOSUPPORT is not None:
32+
errs.append(WSAEAFNOSUPPORT)
33+
if e.errno not in errs:
34+
raise
35+
return result
36+
37+
1938
msw = sys.platform in ('win32', 'win64')
2039

40+
# win32 binaries don't appear to be built with ipv6
41+
has_ipv6 = check_for_ipv6() and not msw
42+
43+
has_unix_sock = not msw
44+
45+
2146
class TestCaseWithCluster(unittest.TestCase):
2247
"""
2348
postgresql.driver *interface* tests.
@@ -50,11 +75,15 @@ def configure_cluster(self):
5075
'failed to find a port for the test cluster on localhost',
5176
creator = self.cluster
5277
).raise_exception()
78+
79+
listen_addresses = '127.0.0.1'
80+
if has_ipv6:
81+
listen_addresses += ',::1'
5382
self.cluster.settings.update(dict(
5483
port = str(self.cluster_port),
5584
max_connections = '6',
5685
shared_buffers = '24',
57-
listen_addresses = 'localhost',
86+
listen_addresses = listen_addresses,
5887
log_destination = 'stderr',
5988
log_min_messages = 'FATAL',
6089
silent_mode = 'off',
@@ -123,7 +152,6 @@ def __init__(self, *args, **kw):
123152
super().__init__(*args,**kw)
124153
# 8.4 nixed this.
125154
self.do_crypt = self.cluster.installation.version_info < (8,4)
126-
self.do_unix = sys.platform != msw
127155

128156
def configure_cluster(self):
129157
super().configure_cluster()
@@ -135,7 +163,7 @@ def configure_cluster(self):
135163
# Configure the hba file with the supported methods.
136164
with open(self.cluster.hba_file, 'w') as hba:
137165
hosts = ['0.0.0.0/0',]
138-
if not msw:
166+
if has_ipv6:
139167
hosts.append('0::0/0')
140168
methods = ['md5', 'password'] + (['crypt'] if self.do_crypt else [])
141169
for h in hosts:
@@ -148,11 +176,11 @@ def configure_cluster(self):
148176
# trusted
149177
hba.writelines(["local all all trust\n"])
150178
hba.writelines(["host test trusted 0.0.0.0/0 trust\n"])
151-
if not msw:
179+
if has_ipv6:
152180
hba.writelines(["host test trusted 0::0/0 trust\n"])
153181
# admin lines
154182
hba.writelines(["host all test 0.0.0.0/0 trust\n"])
155-
if not msw:
183+
if has_ipv6:
156184
hba.writelines(["host all test 0::0/0 trust\n"])
157185

158186
def initialize_database(self):
@@ -379,9 +407,7 @@ def test_IP4_connect(self):
379407
with C() as c:
380408
self.assertEqual(c.prepare('select 1').first(), 1)
381409

382-
if not msw:
383-
# win32 binaries don't appear to be built with ipv6
384-
# so filter this test on windows.
410+
if has_ipv6:
385411
def test_IP6_connect(self):
386412
C = pg_driver.default.ip6(
387413
user = 'test',
@@ -445,7 +471,7 @@ def test_trusted_connect(self):
445471
self.assertEqual(c.prepare('select current_user').first(), 'trusted')
446472

447473
def test_Unix_connect(self):
448-
if msw:
474+
if not has_unix_sock:
449475
return
450476
unix_domain_socket = os.path.join(
451477
self.cluster.data_directory,
@@ -460,7 +486,7 @@ def test_Unix_connect(self):
460486
self.assertEqual(c.client_address, None)
461487

462488
def test_pg_open_unix(self):
463-
if msw:
489+
if not has_unix_sock:
464490
return
465491
unix_domain_socket = os.path.join(
466492
self.cluster.data_directory,

0 commit comments

Comments
 (0)