Skip to content

Commit 641964e

Browse files
committed
BUG28443941: differences on pure and c-ext in cmd_change_user()
The invocation of the method cmd_change_user() using the c extension does not return any status information while the pure python implementation it does. This patch adds the missing status results for the cmd_change_user(), in addition this patchs renames the server_status atribute name to status_flag from the status information returned from fetch_eof_status() to be consistent with the name.
1 parent 9dd3f20 commit 641964e

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

lib/mysql/connector/connection_cext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def fetch_eof_status(self):
355355
'field_count': self._cmysql.st_field_count(),
356356
'insert_id': self._cmysql.insert_id(),
357357
'affected_rows': self._cmysql.affected_rows(),
358-
'server_status': self._server_status,
358+
'status_flag': self._server_status,
359359
}
360360

361361
return None
@@ -534,6 +534,7 @@ def cmd_change_user(self, username='', password='', database='',
534534

535535
self._charset_id = charset
536536
self._post_connection()
537+
return self.fetch_eof_status()
537538

538539
def cmd_refresh(self, options):
539540
"""Send the Refresh command to the MySQL server"""

tests/cext/test_cext_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,6 @@ def test_cmd_query(self):
118118
info = self.cnx.cmd_query("SET @a = 1")
119119
exp = {
120120
'warning_count': 0, 'insert_id': 0, 'affected_rows': 0,
121-
'server_status': 0, 'field_count': 0
121+
'status_flag': 0, 'field_count': 0
122122
}
123123
self.assertEqual(exp, info)

tests/test_bugs.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4861,6 +4861,137 @@ def test_retrieve_mysql_json_time_types(self):
48614861
mysql_type, expected_type)
48624862

48634863

4864+
unittest.skipIf(tests.MYSQL_VERSION < (5, 6, 7),
4865+
"BugOra16217765 not tested with MySQL version < 5.6.7")
4866+
@unittest.skipIf(not CMySQLConnection, ERR_NO_CEXT)
4867+
class Bug28443941(tests.MySQLConnectorTests):
4868+
"""BUG#28443941: DIFFERENCE PURE AND C-EXT ON CMD_CHANGE_USER()
4869+
"""
4870+
4871+
users = {
4872+
'sha256user': {
4873+
'username': 'sha256user',
4874+
'password': 'sha256P@ss',
4875+
'auth_plugin': 'sha256_password',
4876+
},
4877+
'nativeuser': {
4878+
'username': 'nativeuser',
4879+
'password': 'nativeP@ss',
4880+
'auth_plugin': 'mysql_native_password',
4881+
}
4882+
}
4883+
4884+
def _create_user(self, cnx, user, password, host, database,
4885+
plugin):
4886+
4887+
self._drop_user(user, host)
4888+
create_user = ("CREATE USER '{user}'@'{host}' "
4889+
"IDENTIFIED WITH {plugin}")
4890+
cnx.cmd_query(create_user.format(user=user, host=host, plugin=plugin))
4891+
4892+
if tests.MYSQL_VERSION[0:3] < (8, 0, 5):
4893+
if plugin == 'sha256_password':
4894+
cnx.cmd_query("SET old_passwords = 2")
4895+
else:
4896+
cnx.cmd_query("SET old_passwords = 0")
4897+
4898+
if tests.MYSQL_VERSION < (5, 7, 5):
4899+
passwd = ("SET PASSWORD FOR '{user}'@'{host}' = "
4900+
"PASSWORD('{password}')").format(user=user, host=host,
4901+
password=password)
4902+
else:
4903+
passwd = ("ALTER USER '{user}'@'{host}' IDENTIFIED BY "
4904+
"'{password}'").format(user=user, host=host,
4905+
password=password)
4906+
cnx.cmd_query(passwd)
4907+
4908+
grant = "GRANT ALL ON {database}.* TO '{user}'@'{host}'"
4909+
cnx.cmd_query(grant.format(database=database, user=user, host=host))
4910+
4911+
def _drop_user(self, user, host):
4912+
try:
4913+
self.admin_cnx.cmd_query("DROP USER '{user}'@'{host}'".format(
4914+
host=host,
4915+
user=user))
4916+
except errors.DatabaseError:
4917+
# It's OK when drop fails
4918+
pass
4919+
4920+
def setUp(self):
4921+
config = tests.get_mysql_config()
4922+
self.admin_cnx = connection.MySQLConnection(**config)
4923+
for _, user in self.users.items():
4924+
self._create_user(self.admin_cnx, user['username'],
4925+
user['password'],
4926+
config['host'],
4927+
config['database'],
4928+
plugin=user['auth_plugin'])
4929+
4930+
def tearDown(self):
4931+
config = tests.get_mysql_config()
4932+
for _, user in self.users.items():
4933+
self._drop_user(user['username'], config['host'])
4934+
4935+
def test_cmd_change_user(self):
4936+
config = tests.get_mysql_config()
4937+
config['unix_socket'] = None
4938+
4939+
user = self.users['sha256user']
4940+
config['user'] = user['username']
4941+
config['password'] = user['password']
4942+
config['client_flags'] = [constants.ClientFlag.PLUGIN_AUTH]
4943+
config['auth_plugin'] = user['auth_plugin']
4944+
4945+
try:
4946+
cnx = connection.MySQLConnection(**config)
4947+
except Exception as exc:
4948+
import traceback
4949+
traceback.print_exc()
4950+
self.fail(self.errmsg.format(config['auth_plugin'], exc))
4951+
4952+
user2 = self.users['nativeuser']
4953+
config2 = {'user': user2['username'],
4954+
'password': user2['password'],
4955+
'client_flags': [constants.ClientFlag.PLUGIN_AUTH],
4956+
'auth_plugin': user2['auth_plugin']}
4957+
try:
4958+
status_p = cnx.cmd_change_user(config2['user'],
4959+
config2['password'])
4960+
except:
4961+
self.fail("Changing user failed with pure Python connector")
4962+
4963+
try:
4964+
cnx = CMySQLConnection(**config)
4965+
except Exception as exc:
4966+
import traceback
4967+
traceback.print_exc()
4968+
self.fail(self.errmsg.format(config['auth_plugin'], exc))
4969+
4970+
try:
4971+
status_c = cnx.cmd_change_user(config2['user'],
4972+
config2['password'])
4973+
except:
4974+
self.fail("cmd_change_user did not return any result.")
4975+
4976+
if status_c is None:
4977+
self.fail("Changing user failed with c-extension")
4978+
4979+
# Server status can be different, therefore we only check that exists.
4980+
for key in status_p.keys():
4981+
try:
4982+
value = status_c.pop(key)
4983+
if key is not 'status_flag':
4984+
self.assertEqual(status_p[key], value, "status {} not "
4985+
"equal: {} differs from {}"
4986+
"".format(key, value, status_p[key]))
4987+
except KeyError as err:
4988+
self.fail("The cmd_change_user from c-ext is missing an"
4989+
"element: {}".format(err))
4990+
if status_c:
4991+
self.fail("The cmd_change_user from c-ext has additional elements:"
4992+
" {}".format(status_c))
4993+
4994+
48644995
class BugOra27364914(tests.MySQLConnectorTests):
48654996
"""BUG#27364914: CURSOR PREPARED STATEMENTS DO NOT CONVERT STRINGS
48664997
"""

0 commit comments

Comments
 (0)