mysql_change_user_tests.erl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. ?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. ?assertExit(noproc, mysql:stop(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, ?password1),
  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. mysql:stop(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, ?password1),
  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. mysql:stop(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, ?password1),
  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. mysql:stop(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, ?password1),
  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. mysql:stop(Pid),
  94. ok.
  95. with_db_test() ->
  96. Pid = connect_db(?user1, ?password1),
  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. mysql:stop(Pid),
  106. ok.
  107. execute_queries_test() ->
  108. Pid = connect_db(?user1, ?password1),
  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. mysql:stop(Pid),
  116. ok.
  117. execute_queries_failure_test() ->
  118. Pid = connect_db(?user1, ?password1),
  119. erlang:process_flag(trap_exit, true),
  120. ?assertError(_Reason, mysql:change_user(Pid, ?user2, ?password2, [{queries, [<<"foo">>]}])),
  121. receive
  122. {'EXIT', Pid, normal} -> ok
  123. after 1000 ->
  124. error(no_exit_message)
  125. end,
  126. erlang:process_flag(trap_exit, false).
  127. prepare_statements_test() ->
  128. Pid = connect_db(?user1, ?password1),
  129. ?assertEqual(ok, mysql:change_user(Pid, ?user2, ?password2, [{prepare, [{foo, <<"SELECT ? AS foo">>}]}])),
  130. ?assert(is_current_user(Pid, ?user2)),
  131. ?assertEqual({ok,
  132. [<<"foo">>],
  133. [[123]]},
  134. mysql:execute(Pid, foo, [123])),
  135. mysql:stop(Pid),
  136. ok.
  137. prepare_statements_failure_test() ->
  138. Pid = connect_db(?user1, ?password1),
  139. erlang:process_flag(trap_exit, true),
  140. ?assertError(_Reason, mysql:change_user(Pid, ?user2, ?password2, [{prepare, [{foo, <<"foo">>}]}])),
  141. receive
  142. {'EXIT', Pid, normal} -> ok
  143. after 1000 ->
  144. error(no_exit_message)
  145. end,
  146. erlang:process_flag(trap_exit, false).
  147. connect_db(User, Password) ->
  148. {ok, Pid} = mysql:start_link([{user, User}, {password, Password},
  149. {log_warnings, false}]),
  150. Pid.
  151. is_current_user(Pid, User) when is_binary(User) ->
  152. {ok, [<<"CURRENT_USER()">>], [[CurUser]]}=mysql:query(Pid, <<"SELECT CURRENT_USER()">>),
  153. <<User/binary, "@localhost">> =:= CurUser;
  154. is_current_user(Pid, User) ->
  155. is_current_user(Pid, iolist_to_binary(User)).