20
20
import unittest
21
21
import unittest .mock
22
22
import urllib .parse
23
+ import warnings
23
24
import weakref
24
25
25
26
import asyncpg
@@ -1144,7 +1145,7 @@ def check():
1144
1145
1145
1146
@unittest .skipIf (os .environ .get ('PGHOST' ), 'unmanaged cluster' )
1146
1147
async def test_connection_ssl_to_no_ssl_server (self ):
1147
- ssl_context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
1148
+ ssl_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
1148
1149
ssl_context .load_verify_locations (SSL_CA_CERT_FILE )
1149
1150
1150
1151
with self .assertRaisesRegex (ConnectionError , 'rejected SSL' ):
@@ -1268,7 +1269,7 @@ def _add_hba_entry(self):
1268
1269
auth_method = 'trust' )
1269
1270
1270
1271
async def test_ssl_connection_custom_context (self ):
1271
- ssl_context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
1272
+ ssl_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
1272
1273
ssl_context .load_verify_locations (SSL_CA_CERT_FILE )
1273
1274
1274
1275
con = await self .connect (
@@ -1360,7 +1361,7 @@ async def test_ssl_connection_default_context(self):
1360
1361
self .loop .set_exception_handler (old_handler )
1361
1362
1362
1363
async def test_ssl_connection_pool (self ):
1363
- ssl_context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
1364
+ ssl_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
1364
1365
ssl_context .load_verify_locations (SSL_CA_CERT_FILE )
1365
1366
1366
1367
pool = await self .create_pool (
@@ -1385,7 +1386,7 @@ async def worker():
1385
1386
await pool .close ()
1386
1387
1387
1388
async def test_executemany_uvloop_ssl_issue_700 (self ):
1388
- ssl_context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
1389
+ ssl_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
1389
1390
ssl_context .load_verify_locations (SSL_CA_CERT_FILE )
1390
1391
1391
1392
con = await self .connect (
@@ -1417,38 +1418,46 @@ async def test_tls_version(self):
1417
1418
1418
1419
# XXX: uvloop artifact
1419
1420
old_handler = self .loop .get_exception_handler ()
1420
- try :
1421
- self .loop .set_exception_handler (lambda * args : None )
1422
- with self .assertRaisesRegex (ssl .SSLError , 'protocol version' ):
1423
- await self .connect (
1424
- dsn = 'postgresql://ssl_user@localhost/postgres'
1425
- '?sslmode=require&ssl_min_protocol_version=TLSv1.3'
1426
- )
1427
- with self .assertRaises (ssl .SSLError ):
1428
- await self .connect (
1429
- dsn = 'postgresql://ssl_user@localhost/postgres'
1430
- '?sslmode=require'
1431
- '&ssl_min_protocol_version=TLSv1.1'
1432
- '&ssl_max_protocol_version=TLSv1.1'
1433
- )
1434
- with self .assertRaisesRegex (ssl .SSLError , 'no protocols' ):
1435
- await self .connect (
1421
+
1422
+ with warnings .catch_warnings ():
1423
+ warnings .filterwarnings (
1424
+ "ignore" ,
1425
+ message = "ssl.TLSVersion.TLSv1_1 is deprecated" ,
1426
+ category = DeprecationWarning
1427
+ )
1428
+ try :
1429
+ self .loop .set_exception_handler (lambda * args : None )
1430
+ with self .assertRaisesRegex (ssl .SSLError , 'protocol version' ):
1431
+ await self .connect (
1432
+ dsn = 'postgresql://ssl_user@localhost/postgres'
1433
+ '?sslmode=require&ssl_min_protocol_version=TLSv1.3'
1434
+ )
1435
+ with self .assertRaises (ssl .SSLError ):
1436
+ await self .connect (
1437
+ dsn = 'postgresql://ssl_user@localhost/postgres'
1438
+ '?sslmode=require'
1439
+ '&ssl_min_protocol_version=TLSv1.1'
1440
+ '&ssl_max_protocol_version=TLSv1.1'
1441
+ )
1442
+ with self .assertRaisesRegex (ssl .SSLError , 'no protocols' ):
1443
+ await self .connect (
1444
+ dsn = 'postgresql://ssl_user@localhost/postgres'
1445
+ '?sslmode=require'
1446
+ '&ssl_min_protocol_version=TLSv1.2'
1447
+ '&ssl_max_protocol_version=TLSv1.1'
1448
+ )
1449
+ con = await self .connect (
1436
1450
dsn = 'postgresql://ssl_user@localhost/postgres'
1437
1451
'?sslmode=require'
1438
1452
'&ssl_min_protocol_version=TLSv1.2'
1439
- '&ssl_max_protocol_version=TLSv1.1 '
1453
+ '&ssl_max_protocol_version=TLSv1.2 '
1440
1454
)
1441
- con = await self .connect (
1442
- dsn = 'postgresql://ssl_user@localhost/postgres?sslmode=require'
1443
- '&ssl_min_protocol_version=TLSv1.2'
1444
- '&ssl_max_protocol_version=TLSv1.2'
1445
- )
1446
- try :
1447
- self .assertEqual (await con .fetchval ('SELECT 42' ), 42 )
1455
+ try :
1456
+ self .assertEqual (await con .fetchval ('SELECT 42' ), 42 )
1457
+ finally :
1458
+ await con .close ()
1448
1459
finally :
1449
- await con .close ()
1450
- finally :
1451
- self .loop .set_exception_handler (old_handler )
1460
+ self .loop .set_exception_handler (old_handler )
1452
1461
1453
1462
1454
1463
@unittest .skipIf (os .environ .get ('PGHOST' ), 'unmanaged cluster' )
0 commit comments