10
10
import uuid
11
11
12
12
from gc import collect , get_referrers
13
- from errno import ECONNREFUSED , EINPROGRESS , EWOULDBLOCK , EPIPE , ESHUTDOWN
13
+ from errno import (
14
+ EAFNOSUPPORT , ECONNREFUSED , EINPROGRESS , EWOULDBLOCK , EPIPE , ESHUTDOWN )
14
15
from sys import platform , getfilesystemencoding
15
- from socket import MSG_PEEK , SHUT_RDWR , error , socket
16
+ from socket import AF_INET , AF_INET6 , MSG_PEEK , SHUT_RDWR , error , socket
16
17
from os import makedirs
17
18
from os .path import join
18
19
from weakref import ref
101
102
skip_if_py3 = pytest .mark .skipif (PY3 , reason = "Python 2 only" )
102
103
103
104
105
+ def socket_any_family ():
106
+ try :
107
+ return socket (AF_INET )
108
+ except error as e :
109
+ if e .errno == EAFNOSUPPORT :
110
+ return socket (AF_INET6 )
111
+ raise
112
+
113
+
114
+ def loopback_address (socket ):
115
+ if socket .family == AF_INET :
116
+ return "127.0.0.1"
117
+ else :
118
+ assert socket .family == AF_INET6
119
+ return "::1"
120
+
121
+
104
122
def join_bytes_or_unicode (prefix , suffix ):
105
123
"""
106
124
Join two path components of either ``bytes`` or ``unicode``.
@@ -127,12 +145,12 @@ def socket_pair():
127
145
Establish and return a pair of network sockets connected to each other.
128
146
"""
129
147
# Connect a pair of sockets
130
- port = socket ()
148
+ port = socket_any_family ()
131
149
port .bind (('' , 0 ))
132
150
port .listen (1 )
133
- client = socket ()
151
+ client = socket (port . family )
134
152
client .setblocking (False )
135
- client .connect_ex (("127.0.0.1" , port .getsockname ()[1 ]))
153
+ client .connect_ex ((loopback_address ( port ) , port .getsockname ()[1 ]))
136
154
client .setblocking (True )
137
155
server = port .accept ()[0 ]
138
156
@@ -1209,7 +1227,7 @@ def test_set_default_verify_paths(self):
1209
1227
VERIFY_PEER ,
1210
1228
lambda conn , cert , errno , depth , preverify_ok : preverify_ok )
1211
1229
1212
- client = socket ()
1230
+ client = socket_any_family ()
1213
1231
client .connect (("encrypted.google.com" , 443 ))
1214
1232
clientSSL = Connection (context , client )
1215
1233
clientSSL .set_connect_state ()
@@ -2237,7 +2255,7 @@ def test_connect_wrong_args(self):
2237
2255
`Connection.connect` raises `TypeError` if called with a non-address
2238
2256
argument.
2239
2257
"""
2240
- connection = Connection (Context (TLSv1_METHOD ), socket ())
2258
+ connection = Connection (Context (TLSv1_METHOD ), socket_any_family ())
2241
2259
with pytest .raises (TypeError ):
2242
2260
connection .connect (None )
2243
2261
@@ -2246,13 +2264,13 @@ def test_connect_refused(self):
2246
2264
`Connection.connect` raises `socket.error` if the underlying socket
2247
2265
connect method raises it.
2248
2266
"""
2249
- client = socket ()
2267
+ client = socket_any_family ()
2250
2268
context = Context (TLSv1_METHOD )
2251
2269
clientSSL = Connection (context , client )
2252
2270
# pytest.raises here doesn't work because of a bug in py.test on Python
2253
2271
# 2.6: https://github.com/pytest-dev/pytest/issues/988
2254
2272
try :
2255
- clientSSL .connect (("127.0.0.1" , 1 ))
2273
+ clientSSL .connect ((loopback_address ( client ) , 1 ))
2256
2274
except error as e :
2257
2275
exc = e
2258
2276
assert exc .args [0 ] == ECONNREFUSED
@@ -2261,12 +2279,12 @@ def test_connect(self):
2261
2279
"""
2262
2280
`Connection.connect` establishes a connection to the specified address.
2263
2281
"""
2264
- port = socket ()
2282
+ port = socket_any_family ()
2265
2283
port .bind (('' , 0 ))
2266
2284
port .listen (3 )
2267
2285
2268
- clientSSL = Connection (Context (TLSv1_METHOD ), socket ())
2269
- clientSSL .connect (('127.0.0.1' , port .getsockname ()[1 ]))
2286
+ clientSSL = Connection (Context (TLSv1_METHOD ), socket (port . family ))
2287
+ clientSSL .connect ((loopback_address ( port ) , port .getsockname ()[1 ]))
2270
2288
# XXX An assertion? Or something?
2271
2289
2272
2290
@pytest .mark .skipif (
@@ -2278,11 +2296,11 @@ def test_connect_ex(self):
2278
2296
If there is a connection error, `Connection.connect_ex` returns the
2279
2297
errno instead of raising an exception.
2280
2298
"""
2281
- port = socket ()
2299
+ port = socket_any_family ()
2282
2300
port .bind (('' , 0 ))
2283
2301
port .listen (3 )
2284
2302
2285
- clientSSL = Connection (Context (TLSv1_METHOD ), socket ())
2303
+ clientSSL = Connection (Context (TLSv1_METHOD ), socket (port . family ))
2286
2304
clientSSL .setblocking (False )
2287
2305
result = clientSSL .connect_ex (port .getsockname ())
2288
2306
expected = (EINPROGRESS , EWOULDBLOCK )
@@ -2297,16 +2315,16 @@ def test_accept(self):
2297
2315
ctx = Context (TLSv1_METHOD )
2298
2316
ctx .use_privatekey (load_privatekey (FILETYPE_PEM , server_key_pem ))
2299
2317
ctx .use_certificate (load_certificate (FILETYPE_PEM , server_cert_pem ))
2300
- port = socket ()
2318
+ port = socket_any_family ()
2301
2319
portSSL = Connection (ctx , port )
2302
2320
portSSL .bind (('' , 0 ))
2303
2321
portSSL .listen (3 )
2304
2322
2305
- clientSSL = Connection (Context (TLSv1_METHOD ), socket ())
2323
+ clientSSL = Connection (Context (TLSv1_METHOD ), socket (port . family ))
2306
2324
2307
2325
# Calling portSSL.getsockname() here to get the server IP address
2308
2326
# sounds great, but frequently fails on Windows.
2309
- clientSSL .connect (('127.0.0.1' , portSSL .getsockname ()[1 ]))
2327
+ clientSSL .connect ((loopback_address ( port ) , portSSL .getsockname ()[1 ]))
2310
2328
2311
2329
serverSSL , address = portSSL .accept ()
2312
2330
@@ -2379,7 +2397,7 @@ def test_set_shutdown(self):
2379
2397
`Connection.set_shutdown` sets the state of the SSL connection
2380
2398
shutdown process.
2381
2399
"""
2382
- connection = Connection (Context (TLSv1_METHOD ), socket ())
2400
+ connection = Connection (Context (TLSv1_METHOD ), socket_any_family ())
2383
2401
connection .set_shutdown (RECEIVED_SHUTDOWN )
2384
2402
assert connection .get_shutdown () == RECEIVED_SHUTDOWN
2385
2403
@@ -2389,7 +2407,7 @@ def test_set_shutdown_long(self):
2389
2407
On Python 2 `Connection.set_shutdown` accepts an argument
2390
2408
of type `long` as well as `int`.
2391
2409
"""
2392
- connection = Connection (Context (TLSv1_METHOD ), socket ())
2410
+ connection = Connection (Context (TLSv1_METHOD ), socket_any_family ())
2393
2411
connection .set_shutdown (long (RECEIVED_SHUTDOWN ))
2394
2412
assert connection .get_shutdown () == RECEIVED_SHUTDOWN
2395
2413
@@ -3503,7 +3521,7 @@ def test_socket_overrides_memory(self):
3503
3521
work on `OpenSSL.SSL.Connection`() that use sockets.
3504
3522
"""
3505
3523
context = Context (TLSv1_METHOD )
3506
- client = socket ()
3524
+ client = socket_any_family ()
3507
3525
clientSSL = Connection (context , client )
3508
3526
with pytest .raises (TypeError ):
3509
3527
clientSSL .bio_read (100 )
0 commit comments