MCPcopy Index your code
hub / github.com/MagicStack/asyncpg / test_tls_version

Method test_tls_version

tests/test_connect.py:2005–2054  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2003 await con.close()
2004
2005 async def test_tls_version(self):
2006 if self.cluster.get_pg_version() < (12, 0):
2007 self.skipTest("PostgreSQL < 12 cannot set ssl protocol version")
2008
2009 # XXX: uvloop artifact
2010 old_handler = self.loop.get_exception_handler()
2011
2012 with warnings.catch_warnings():
2013 warnings.filterwarnings(
2014 "ignore",
2015 message="ssl.TLSVersion.TLSv1_1 is deprecated",
2016 category=DeprecationWarning
2017 )
2018 try:
2019 self.loop.set_exception_handler(lambda *args: None)
2020 with self.assertRaisesRegex(
2021 ssl.SSLError,
2022 '(protocol version)|(handshake failure)',
2023 ):
2024 await self.connect(
2025 dsn='postgresql://ssl_user@localhost/postgres'
2026 '?sslmode=require&ssl_min_protocol_version=TLSv1.3'
2027 )
2028 with self.assertRaises((ssl.SSLError, ConnectionResetError)):
2029 await self.connect(
2030 dsn='postgresql://ssl_user@localhost/postgres'
2031 '?sslmode=require'
2032 '&ssl_min_protocol_version=TLSv1.1'
2033 '&ssl_max_protocol_version=TLSv1.1'
2034 )
2035 if not ssl.OPENSSL_VERSION.startswith('LibreSSL'):
2036 with self.assertRaisesRegex(ssl.SSLError, 'no protocols'):
2037 await self.connect(
2038 dsn='postgresql://ssl_user@localhost/postgres'
2039 '?sslmode=require'
2040 '&ssl_min_protocol_version=TLSv1.2'
2041 '&ssl_max_protocol_version=TLSv1.1'
2042 )
2043 con = await self.connect(
2044 dsn='postgresql://ssl_user@localhost/postgres'
2045 '?sslmode=require'
2046 '&ssl_min_protocol_version=TLSv1.2'
2047 '&ssl_max_protocol_version=TLSv1.2'
2048 )
2049 try:
2050 self.assertEqual(await con.fetchval('SELECT 42'), 42)
2051 finally:
2052 await con.close()
2053 finally:
2054 self.loop.set_exception_handler(old_handler)
2055
2056
2057@unittest.skipIf(os.environ.get('PGHOST'), 'unmanaged cluster')

Callers

nothing calls this directly

Calls 4

get_pg_versionMethod · 0.80
connectMethod · 0.45
fetchvalMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected