#
# MDEV-38550: COM_CHANGE_USER with Long Password Corrupts Database Name
#
# When password > 255 bytes with COM_CHANGE_USER, the password length was
# truncated to a single byte, causing the server to read garbage as the
# database name.
#
# Fix: Add LENENC support for COM_CHANGE_USER password field in both
# client (libmariadb) and server (sql_acl.cc).
#

--source include/not_embedded.inc
--source include/have_plugin_auth.inc

--echo #
--echo # Setup
--echo #
CREATE DATABASE mdev38550_db;

--echo #
--echo # Test 1: COM_CHANGE_USER with short password
--echo # Verify mysql_change_user() works correctly with database parameter
--echo #
CREATE USER changeusertest IDENTIFIED VIA cleartext_plugin_server USING 'changepwd';
GRANT ALL ON *.* TO changeusertest;

# Use change_user command to switch user with password and database
change_user changeusertest,changepwd,mdev38550_db;
SELECT DATABASE() AS db;
change_user root;

--echo #
--echo # Test 2: COM_CHANGE_USER with long password (260 bytes)
--echo # This works because auth plugin switching sends password in a SECOND packet
--echo # (via ma_net_write), bypassing the 255-byte limit in send_change_user_packet()
--echo #
--echo # Note: If connection used cleartext directly (no auth switch), it would fail
--echo # due to libmariadb's 255-byte limit in send_change_user_packet()
--echo #
--let $change_pwd=`SELECT REPEAT('c', 260)`
eval CREATE USER changeuserlongpwd IDENTIFIED VIA cleartext_plugin_server USING '$change_pwd';
GRANT ALL ON *.* TO changeuserlongpwd;

change_user changeuserlongpwd,$change_pwd,mdev38550_db;
SELECT DATABASE() AS db;
change_user root;

--echo #
--echo # Test 3: COM_CHANGE_USER with long password - no auth switch
--echo # Connection uses cleartext directly, password goes in first packet
--echo # Requires LENENC support in both client and server for COM_CHANGE_USER
--echo #
--connect (cleartext_con, 127.0.0.1, changeuserlongpwd, $change_pwd, mdev38550_db, $MASTER_MYPORT, , , mysql_clear_password)
SELECT DATABASE() AS db, CURRENT_USER() AS user;
change_user changeuserlongpwd,$change_pwd,mdev38550_db;
SELECT DATABASE() AS db, CURRENT_USER() AS user;
--connection default
--disconnect cleartext_con

--echo #
--echo # Cleanup
--echo #
DROP USER changeusertest, changeuserlongpwd;
DROP DATABASE mdev38550_db;
