summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2017-03-15 16:00:40 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2017-03-15 16:00:40 +0000
commitcc047a445a9b74e38f1f3128545302f26d83ff81 (patch)
tree57a41e4f8e3e40b0a59a332c9cbcf0ae51526a33
parent7187d6408a416fcb09aaa8b3039aba424320e9bd (diff)
downloadpsycopg2-cc047a445a9b74e38f1f3128545302f26d83ff81.tar.gz
Added tests to verify the password is obscured
The url test fails: see issue #528
-rwxr-xr-xtests/test_connection.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/test_connection.py b/tests/test_connection.py
index 385d979..dcf3a4a 100755
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -1479,6 +1479,46 @@ class AutocommitTests(ConnectingTestCase):
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():
return unittest.TestLoader().loadTestsFromName(__name__)