2020-11-03 18:38:40 +08:00
|
|
|
import os
|
2017-04-02 09:01:08 +08:00
|
|
|
import signal
|
2020-10-05 06:27:20 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
2020-10-05 06:25:29 +08:00
|
|
|
from unittest import mock, skipUnless
|
2015-06-17 21:37:09 +08:00
|
|
|
|
2020-10-05 06:25:29 +08:00
|
|
|
from django.db import connection
|
2015-08-05 22:08:56 +08:00
|
|
|
from django.db.backends.postgresql.client import DatabaseClient
|
2017-01-20 01:16:04 +08:00
|
|
|
from django.test import SimpleTestCase
|
2015-06-17 21:37:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
|
2020-10-05 06:25:29 +08:00
|
|
|
def settings_to_cmd_args_env(self, settings_dict, parameters=None):
|
2020-04-14 15:56:40 +08:00
|
|
|
if parameters is None:
|
|
|
|
parameters = []
|
2020-10-05 06:25:29 +08:00
|
|
|
return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters)
|
2015-06-17 21:37:09 +08:00
|
|
|
|
|
|
|
def test_basic(self):
|
|
|
|
self.assertEqual(
|
2020-10-05 06:25:29 +08:00
|
|
|
self.settings_to_cmd_args_env({
|
|
|
|
'NAME': 'dbname',
|
|
|
|
'USER': 'someuser',
|
|
|
|
'PASSWORD': 'somepassword',
|
|
|
|
'HOST': 'somehost',
|
|
|
|
'PORT': '444',
|
2015-06-17 21:37:09 +08:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'],
|
2019-04-16 17:40:22 +08:00
|
|
|
{'PGPASSWORD': 'somepassword'},
|
2015-06-17 21:37:09 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_nopass(self):
|
|
|
|
self.assertEqual(
|
2020-10-05 06:25:29 +08:00
|
|
|
self.settings_to_cmd_args_env({
|
|
|
|
'NAME': 'dbname',
|
|
|
|
'USER': 'someuser',
|
|
|
|
'HOST': 'somehost',
|
|
|
|
'PORT': '444',
|
2015-06-17 21:37:09 +08:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'],
|
2019-04-16 17:40:22 +08:00
|
|
|
{},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ssl_certificate(self):
|
|
|
|
self.assertEqual(
|
2020-10-05 06:25:29 +08:00
|
|
|
self.settings_to_cmd_args_env({
|
|
|
|
'NAME': 'dbname',
|
|
|
|
'USER': 'someuser',
|
|
|
|
'HOST': 'somehost',
|
|
|
|
'PORT': '444',
|
|
|
|
'OPTIONS': {
|
|
|
|
'sslmode': 'verify-ca',
|
|
|
|
'sslrootcert': 'root.crt',
|
|
|
|
'sslcert': 'client.crt',
|
|
|
|
'sslkey': 'client.key',
|
|
|
|
},
|
2019-04-16 17:40:22 +08:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'],
|
|
|
|
{
|
|
|
|
'PGSSLCERT': 'client.crt',
|
|
|
|
'PGSSLKEY': 'client.key',
|
|
|
|
'PGSSLMODE': 'verify-ca',
|
|
|
|
'PGSSLROOTCERT': 'root.crt',
|
|
|
|
},
|
2015-06-17 21:37:09 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-12-24 06:39:43 +08:00
|
|
|
def test_service(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self.settings_to_cmd_args_env({'OPTIONS': {'service': 'django_test'}}),
|
2021-02-27 03:53:01 +08:00
|
|
|
(['psql'], {'PGSERVICE': 'django_test'}),
|
2020-12-24 06:39:43 +08:00
|
|
|
)
|
|
|
|
|
2015-06-17 21:37:09 +08:00
|
|
|
def test_column(self):
|
|
|
|
self.assertEqual(
|
2020-10-05 06:25:29 +08:00
|
|
|
self.settings_to_cmd_args_env({
|
|
|
|
'NAME': 'dbname',
|
|
|
|
'USER': 'some:user',
|
|
|
|
'PASSWORD': 'some:password',
|
|
|
|
'HOST': '::1',
|
|
|
|
'PORT': '444',
|
2015-06-17 21:37:09 +08:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'some:user', '-h', '::1', '-p', '444', 'dbname'],
|
2019-04-16 17:40:22 +08:00
|
|
|
{'PGPASSWORD': 'some:password'},
|
2015-06-17 21:37:09 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_accent(self):
|
|
|
|
username = 'rôle'
|
|
|
|
password = 'sésame'
|
|
|
|
self.assertEqual(
|
2020-10-05 06:25:29 +08:00
|
|
|
self.settings_to_cmd_args_env({
|
|
|
|
'NAME': 'dbname',
|
|
|
|
'USER': username,
|
|
|
|
'PASSWORD': password,
|
|
|
|
'HOST': 'somehost',
|
|
|
|
'PORT': '444',
|
2015-06-17 21:37:09 +08:00
|
|
|
}), (
|
2017-01-12 06:17:25 +08:00
|
|
|
['psql', '-U', username, '-h', 'somehost', '-p', '444', 'dbname'],
|
2019-04-16 17:40:22 +08:00
|
|
|
{'PGPASSWORD': password},
|
2015-06-17 21:37:09 +08:00
|
|
|
)
|
|
|
|
)
|
2017-04-02 09:01:08 +08:00
|
|
|
|
2020-04-14 15:56:40 +08:00
|
|
|
def test_parameters(self):
|
|
|
|
self.assertEqual(
|
2020-10-05 06:25:29 +08:00
|
|
|
self.settings_to_cmd_args_env({'NAME': 'dbname'}, ['--help']),
|
2020-04-14 15:56:40 +08:00
|
|
|
(['psql', 'dbname', '--help'], {}),
|
|
|
|
)
|
|
|
|
|
2020-10-05 06:25:29 +08:00
|
|
|
@skipUnless(connection.vendor == 'postgresql', 'Requires a PostgreSQL connection')
|
2017-04-02 09:01:08 +08:00
|
|
|
def test_sigint_handler(self):
|
2019-07-19 22:45:13 +08:00
|
|
|
"""SIGINT is ignored in Python and passed to psql to abort queries."""
|
2019-02-11 09:17:24 +08:00
|
|
|
def _mock_subprocess_run(*args, **kwargs):
|
2017-04-02 09:01:08 +08:00
|
|
|
handler = signal.getsignal(signal.SIGINT)
|
|
|
|
self.assertEqual(handler, signal.SIG_IGN)
|
|
|
|
|
|
|
|
sigint_handler = signal.getsignal(signal.SIGINT)
|
|
|
|
# The default handler isn't SIG_IGN.
|
|
|
|
self.assertNotEqual(sigint_handler, signal.SIG_IGN)
|
2019-02-11 09:17:24 +08:00
|
|
|
with mock.patch('subprocess.run', new=_mock_subprocess_run):
|
2020-10-05 06:25:29 +08:00
|
|
|
connection.client.runshell([])
|
2018-08-02 00:55:53 +08:00
|
|
|
# dbshell restores the original handler.
|
2017-04-02 09:01:08 +08:00
|
|
|
self.assertEqual(sigint_handler, signal.getsignal(signal.SIGINT))
|
2020-10-05 06:27:20 +08:00
|
|
|
|
|
|
|
def test_crash_password_does_not_leak(self):
|
|
|
|
# The password doesn't leak in an exception that results from a client
|
|
|
|
# crash.
|
|
|
|
args, env = self.settings_to_cmd_args_env({'PASSWORD': 'somepassword'}, [])
|
2020-11-03 18:38:40 +08:00
|
|
|
if env:
|
|
|
|
env = {**os.environ, **env}
|
2020-10-05 06:27:20 +08:00
|
|
|
fake_client = Path(__file__).with_name('fake_client.py')
|
|
|
|
args[0:1] = [sys.executable, str(fake_client)]
|
|
|
|
with self.assertRaises(subprocess.CalledProcessError) as ctx:
|
|
|
|
subprocess.run(args, check=True, env=env)
|
|
|
|
self.assertNotIn('somepassword', str(ctx.exception))
|