ssl_tests.erl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. %% MySQL/OTP – MySQL client library for Erlang/OTP
  2. %% Copyright (C) 2017 Piotr Nosek, Viktor Söderqvist
  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 tests to connect to a database over SSL.
  19. -module(ssl_tests).
  20. -include_lib("eunit/include/eunit.hrl").
  21. -define(ssl_user, "otptestssl").
  22. -define(ssl_password, "otptestssl").
  23. -define(cacertfile, "test/ssl/ca.pem").
  24. successful_ssl_connect_test() ->
  25. [ application:start(App) || App <- [crypto, asn1, public_key, ssl] ],
  26. common_basic_check([{ssl, [{cacertfile, ?cacertfile}]},
  27. {user, ?ssl_user}, {password, ?ssl_password}]),
  28. common_conn_close(),
  29. ok.
  30. common_basic_check(ExtraOpts) ->
  31. Options = [{name, {local, tardis}},
  32. {queries, ["SET @foo = 'bar'", "SELECT 1",
  33. "SELECT 1; SELECT 2"]},
  34. {prepare, [{foo, "SELECT @foo"}]} | ExtraOpts],
  35. {ok, Pid} = mysql:start_link(Options),
  36. %% Check that queries and prepare has been done.
  37. ?assertEqual({ok, [<<"@foo">>], [[<<"bar">>]]},
  38. mysql:execute(Pid, foo, [])),
  39. Pid.
  40. common_conn_close() ->
  41. Pid = whereis(tardis),
  42. process_flag(trap_exit, true),
  43. exit(Pid, normal),
  44. receive
  45. {'EXIT', Pid, normal} -> ok
  46. after
  47. 5000 -> error({cant_stop_connection, Pid})
  48. end,
  49. process_flag(trap_exit, false).