mysql_change_user_tests.erl 7.5 KB

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