mysql_change_user_tests.erl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. %% MySQL/OTP – MySQL client library for Erlang/OTP
  2. %% Copyright (C) 2019 Jan Uhlig
  3. %%
  4. %% This file is part of MySQL/OTP.
  5. %%
  6. %% MySQL/OTP is free software: you can redistribute it and/or modify it under
  7. %% the terms of the GNU Lesser General Public License as published by the Free
  8. %% Software Foundation, either version 3 of the License, or (at your option)
  9. %% any later version.
  10. %%
  11. %% This program is distributed in the hope that it will be useful, but WITHOUT
  12. %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. %% more details.
  15. %%
  16. %% You should have received a copy of the GNU Lesser General Public License
  17. %% along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. %% @doc This module performs test to an actual database.
  19. -module(mysql_change_user_tests).
  20. -include_lib("eunit/include/eunit.hrl").
  21. -define(user1, "otptest").
  22. -define(password1, "otptest").
  23. -define(user2, "otptest2").
  24. -define(password2, "otptest2").
  25. %% Ensure that the current user can be changed to another user
  26. %% when given correct credentials.
  27. correct_credentials_test() ->
  28. Pid = connect_db(?user1),
  29. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2)),
  30. ?assert(is_current_user(Pid, ?user2)),
  31. close_conn(Pid),
  32. ok.
  33. %% Ensure that change user fails when given incorrect credentials,
  34. %% and that the current user still works.
  35. incorrect_credentials_fail_test() ->
  36. Pid = connect_db(?user1),
  37. TrapExit = erlang:process_flag(trap_exit, true),
  38. ?assertError({1045, <<"28000">>, <<"Access denied", _/binary>>},
  39. mysql:change_user(Pid, ?user2, ?password1)),
  40. ExitReason = receive {'EXIT', Pid, Reason} -> Reason after 1000 -> error(timeout) end,
  41. erlang:process_flag(trap_exit, TrapExit),
  42. ?assertEqual(change_user_failed, ExitReason),
  43. close_conn(Pid),
  44. ok.
  45. %% Ensure that user variables are reset after a successful change user
  46. %% operation.
  47. reset_variables_test() ->
  48. Pid = connect_db(?user1),
  49. ok = mysql:query(Pid, <<"SET @foo=123">>),
  50. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2)),
  51. ?assert(is_current_user(Pid, ?user2)),
  52. ?assertEqual({ok,
  53. [<<"@foo">>],
  54. [[null]]},
  55. mysql:query(Pid, <<"SELECT @foo">>)),
  56. close_conn(Pid),
  57. ok.
  58. %% Ensure that temporary tables are reset after a successful change user
  59. %% operation.
  60. reset_temptables_test() ->
  61. Pid = connect_db(?user1),
  62. ok = mysql:query(Pid, <<"CREATE DATABASE IF NOT EXISTS otptest">>),
  63. ok = mysql:query(Pid, <<"CREATE TEMPORARY TABLE otptest.foo (bar INT)">>),
  64. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2)),
  65. ?assert(is_current_user(Pid, ?user2)),
  66. ?assertMatch({error,
  67. {1146, <<"42S02">>, _}},
  68. mysql:query(Pid, <<"SELECT * FROM otptest.foo">>)),
  69. ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  70. close_conn(Pid),
  71. ok.
  72. %% Ensure that change user fails when inside an unmanaged transaction.
  73. fail_in_unmanaged_transaction_test() ->
  74. Pid = connect_db(?user1),
  75. ok = mysql:query(Pid, <<"BEGIN">>),
  76. ?assert(mysql:in_transaction(Pid)),
  77. ?assertError(change_user_in_transaction,
  78. mysql:change_user(Pid, ?user2, ?password2)),
  79. ?assert(is_current_user(Pid, ?user1)),
  80. ?assert(mysql:in_transaction(Pid)),
  81. close_conn(Pid),
  82. ok.
  83. %% Ensure that change user fails when inside a managed transaction.
  84. fail_in_managed_transaction_test() ->
  85. Pid = connect_db(?user1),
  86. ?assertError(change_user_in_transaction,
  87. mysql:transaction(Pid,
  88. fun () -> mysql:change_user(Pid,
  89. ?user2,
  90. ?password2)
  91. end)),
  92. ?assert(is_current_user(Pid, ?user1)),
  93. close_conn(Pid),
  94. ok.
  95. with_db_test() ->
  96. Pid = connect_db(?user1),
  97. ok = mysql:query(Pid, <<"CREATE DATABASE IF NOT EXISTS otptest">>),
  98. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2, [{database, <<"otptest">>}])),
  99. ?assert(is_current_user(Pid, ?user2)),
  100. ?assertEqual({ok,
  101. [<<"DATABASE()">>],
  102. [[<<"otptest">>]]},
  103. mysql:query(Pid, <<"SELECT DATABASE()">>)),
  104. ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  105. close_conn(Pid),
  106. ok.
  107. execute_queries_test() ->
  108. Pid = connect_db(?user1),
  109. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2, [{queries, [<<"SET @foo=123">>]}])),
  110. ?assert(is_current_user(Pid, ?user2)),
  111. ?assertEqual({ok,
  112. [<<"@foo">>],
  113. [[123]]},
  114. mysql:query(Pid, <<"SELECT @foo">>)),
  115. close_conn(Pid),
  116. ok.
  117. prepare_statements_test() ->
  118. Pid = connect_db(?user1),
  119. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2, [{prepare, [{foo, <<"SELECT ? AS foo">>}]}])),
  120. ?assert(is_current_user(Pid, ?user2)),
  121. ?assertEqual({ok,
  122. [<<"foo">>],
  123. [[123]]},
  124. mysql:execute(Pid, foo, [123])),
  125. close_conn(Pid),
  126. ok.
  127. connect_db(User) ->
  128. {ok, Pid} = mysql:start_link([{user, User}, {password, ?password1},
  129. {log_warnings, false}]),
  130. Pid.
  131. close_conn(Pid) ->
  132. exit(Pid, normal).
  133. is_current_user(Pid, User) when is_binary(User) ->
  134. {ok, [<<"CURRENT_USER()">>], [[CurUser]]}=mysql:query(Pid, <<"SELECT CURRENT_USER()">>),
  135. <<User/binary, "@localhost">> =:= CurUser;
  136. is_current_user(Pid, User) ->
  137. is_current_user(Pid, iolist_to_binary(User)).