Added tests to verify the password is obscured

The url test fails: see issue #528
This commit is contained in:
Daniele Varrazzo 2017-03-15 16:00:40 +00:00
parent 7187d6408a
commit cc047a445a

View File

@ -1479,6 +1479,46 @@ class AutocommitTests(ConnectingTestCase):
self.assertEqual(cur.fetchone()[0], 'on') self.assertEqual(cur.fetchone()[0], 'on')
class PasswordLeakTestCase(ConnectingTestCase):
def setUp(self):
super(PasswordLeakTestCase, self).setUp()
PasswordLeakTestCase.dsn = None
class GrassingConnection(ext.connection):
"""A connection snitching the dsn away.
This connection passes the dsn to the test case class even if init
fails (e.g. connection error). Test that we mangle the dsn ok anyway.
"""
def __init__(self, *args, **kwargs):
try:
super(PasswordLeakTestCase.GrassingConnection, self).__init__(
*args, **kwargs)
finally:
# The connection is not initialized entirely, however the C
# code should have set the dsn, and it should have scrubbed
# the password away
PasswordLeakTestCase.dsn = self.dsn
def test_leak(self):
self.assertRaises(psycopg2.DatabaseError,
self.GrassingConnection, "dbname=nosuch password=whateva")
self.assert_('nosuch' in self.dsn)
self.assert_('password' in self.dsn)
self.assert_('whateva' not in self.dsn)
def test_url_leak(self):
self.assertRaises(psycopg2.DatabaseError,
self.GrassingConnection,
"postgres://someone:whateva@localhost/nosuch")
self.assert_('nosuch' in self.dsn)
self.assert_('someone' in self.dsn)
self.assert_('whateva' not in self.dsn)
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)