mysql_change_user_tests.erl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, ?password1),
  29. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2)),
  30. ?assert(is_current_user(Pid, ?user2)),
  31. mysql:stop(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, ?password1),
  37. TrapExit = erlang:process_flag(trap_exit, true),
  38. {ok, {Ret, ExitReason}, Logged} = error_logger_acc:capture(fun () ->
  39. ChangeUserReturn = mysql:change_user(Pid, ?user2, ?password1),
  40. receive {'EXIT', Pid, Reason} -> {ChangeUserReturn, Reason}
  41. after 1000 -> error(no_exit_message)
  42. end
  43. end),
  44. erlang:process_flag(trap_exit, TrapExit),
  45. ?assertMatch([{error, "Connection Id " ++ _}, % closing with reason: cha...
  46. {error, "** Generic server" ++ _},
  47. {error_report, {crash_report, _}}], Logged),
  48. ?assertMatch({error, {1045, <<"28000">>, <<"Access denied", _/binary>>}},
  49. Ret),
  50. ?assertEqual(change_user_failed, ExitReason),
  51. ?assertExit(noproc, mysql:stop(Pid)),
  52. ok.
  53. %% Ensure that user variables are reset after a successful change user
  54. %% operation.
  55. reset_variables_test() ->
  56. Pid = connect_db(?user1, ?password1),
  57. ok = mysql:query(Pid, <<"SET @foo=123">>),
  58. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2)),
  59. ?assert(is_current_user(Pid, ?user2)),
  60. ?assertEqual({ok,
  61. [<<"@foo">>],
  62. [[null]]},
  63. mysql:query(Pid, <<"SELECT @foo">>)),
  64. mysql:stop(Pid),
  65. ok.
  66. %% Ensure that temporary tables are reset after a successful change user
  67. %% operation.
  68. reset_temptables_test() ->
  69. Pid = connect_db(?user1, ?password1),
  70. ok = mysql:query(Pid, <<"CREATE DATABASE IF NOT EXISTS otptest">>),
  71. ok = mysql:query(Pid, <<"CREATE TEMPORARY TABLE otptest.foo (bar INT)">>),
  72. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2)),
  73. ?assert(is_current_user(Pid, ?user2)),
  74. ?assertMatch({error,
  75. {1146, <<"42S02">>, _}},
  76. mysql:query(Pid, <<"SELECT * FROM otptest.foo">>)),
  77. ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  78. mysql:stop(Pid),
  79. ok.
  80. %% Ensure that change user fails when inside an unmanaged transaction.
  81. fail_in_unmanaged_transaction_test() ->
  82. Pid = connect_db(?user1, ?password1),
  83. ok = mysql:query(Pid, <<"BEGIN">>),
  84. ?assert(mysql:in_transaction(Pid)),
  85. ?assertError(change_user_in_transaction,
  86. mysql:change_user(Pid, ?user2, ?password2)),
  87. ?assert(is_current_user(Pid, ?user1)),
  88. ?assert(mysql:in_transaction(Pid)),
  89. mysql:stop(Pid),
  90. ok.
  91. %% Ensure that change user fails when inside a managed transaction.
  92. fail_in_managed_transaction_test() ->
  93. Pid = connect_db(?user1, ?password1),
  94. ?assertError(change_user_in_transaction,
  95. mysql:transaction(Pid,
  96. fun () -> mysql:change_user(Pid,
  97. ?user2,
  98. ?password2)
  99. end)),
  100. ?assert(is_current_user(Pid, ?user1)),
  101. mysql:stop(Pid),
  102. ok.
  103. with_db_test() ->
  104. Pid = connect_db(?user1, ?password1),
  105. ok = mysql:query(Pid, <<"CREATE DATABASE IF NOT EXISTS otptest">>),
  106. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2, [{database, <<"otptest">>}])),
  107. ?assert(is_current_user(Pid, ?user2)),
  108. ?assertEqual({ok,
  109. [<<"DATABASE()">>],
  110. [[<<"otptest">>]]},
  111. mysql:query(Pid, <<"SELECT DATABASE()">>)),
  112. ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  113. mysql:stop(Pid),
  114. ok.
  115. execute_queries_test() ->
  116. Pid = connect_db(?user1, ?password1),
  117. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2, [{queries, [<<"SET @foo=123">>]}])),
  118. ?assert(is_current_user(Pid, ?user2)),
  119. ?assertEqual({ok,
  120. [<<"@foo">>],
  121. [[123]]},
  122. mysql:query(Pid, <<"SELECT @foo">>)),
  123. mysql:stop(Pid),
  124. ok.
  125. execute_queries_failure_test() ->
  126. Pid = connect_db(?user1, ?password1),
  127. erlang:process_flag(trap_exit, true),
  128. {ok, Ret, Logged} = error_logger_acc:capture(fun () ->
  129. Ret1 = mysql:change_user(Pid, ?user2, ?password2, [{queries, [<<"foo">>]}]),
  130. receive {'EXIT', Pid, _Reason} -> Ret1
  131. after 1000 -> error(no_exit_message)
  132. end
  133. end),
  134. ?assertMatch([{error, "Connection Id " ++ _}, % closing with reason: {1064,
  135. {error, "** Generic server" ++ _},
  136. {error_report, {crash_report, _}}], Logged),
  137. {error, Reason} = Ret,
  138. ?assertMatch({1064, <<"42000">>, <<"You have an erro", _/binary>>}, Reason),
  139. erlang:process_flag(trap_exit, false).
  140. prepare_statements_test() ->
  141. Pid = connect_db(?user1, ?password1),
  142. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2,
  143. [{prepare, [{foo, <<"SELECT ? AS foo">>}]}])),
  144. ?assert(is_current_user(Pid, ?user2)),
  145. ?assertEqual({ok,
  146. [<<"foo">>],
  147. [[123]]},
  148. mysql:execute(Pid, foo, [123])),
  149. mysql:stop(Pid),
  150. ok.
  151. prepare_statements_failure_test() ->
  152. Pid = connect_db(?user1, ?password1),
  153. erlang:process_flag(trap_exit, true),
  154. {ok, Ret, Logged} = error_logger_acc:capture(fun () ->
  155. Ret1 = mysql:change_user(Pid, ?user2, ?password2,
  156. [{prepare, [{foo, <<"foo">>}]}]),
  157. receive {'EXIT', Pid, _Reason} -> Ret1
  158. after 1000 -> error(no_exit_message)
  159. end
  160. end),
  161. ?assertMatch([{error, "Connection Id " ++ _}, % closing with reason: {1064,
  162. {error, "** Generic server" ++ _},
  163. {error_report, {crash_report, _}}], Logged),
  164. {error, Reason} = Ret,
  165. ?assertMatch({1064, <<"42000">>, <<"You have an erro", _/binary>>}, Reason),
  166. erlang:process_flag(trap_exit, false).
  167. connect_db(User, Password) ->
  168. {ok, Pid} = mysql:start_link([{user, User}, {password, Password},
  169. {log_warnings, false}]),
  170. Pid.
  171. is_current_user(Pid, User) when is_binary(User) ->
  172. {ok, [<<"CURRENT_USER()">>], [[CurUser]]}=mysql:query(Pid, <<"SELECT CURRENT_USER()">>),
  173. <<User/binary, "@localhost">> =:= CurUser;
  174. is_current_user(Pid, User) ->
  175. is_current_user(Pid, iolist_to_binary(User)).