ssl_tests.erl 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. %% MySQL/OTP – MySQL client library for Erlang/OTP
  2. %% Copyright (C) 2017 Piotr Nosek
  3. %% Copyright (C) 2017-2018 Viktor Söderqvist
  4. %%
  5. %% This file is part of MySQL/OTP.
  6. %%
  7. %% MySQL/OTP is free software: you can redistribute it and/or modify it under
  8. %% the terms of the GNU Lesser General Public License as published by the Free
  9. %% Software Foundation, either version 3 of the License, or (at your option)
  10. %% any later version.
  11. %%
  12. %% This program is distributed in the hope that it will be useful, but WITHOUT
  13. %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. %% more details.
  16. %%
  17. %% You should have received a copy of the GNU Lesser General Public License
  18. %% along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. %% @doc This module tests to connect to a database over SSL.
  20. -module(ssl_tests).
  21. -include_lib("eunit/include/eunit.hrl").
  22. -define(ssl_user, "otptestssl").
  23. -define(ssl_password, "otptestssl").
  24. -define(cacertfile, "test/ssl/ca.pem").
  25. successful_ssl_connect_test() ->
  26. [ application:start(App) || App <- [crypto, asn1, public_key, ssl] ],
  27. common_basic_check([{ssl, [{server_name_indication, disable},
  28. {cacertfile, ?cacertfile}]},
  29. {user, ?ssl_user}, {password, ?ssl_password}]),
  30. common_conn_close(),
  31. ok.
  32. common_basic_check(ExtraOpts) ->
  33. Options = [{name, {local, tardis}},
  34. {queries, ["SET @foo = 'bar'", "SELECT 1",
  35. "SELECT 1; SELECT 2"]},
  36. {prepare, [{foo, "SELECT @foo"}]} | ExtraOpts],
  37. {ok, Pid} = mysql:start_link(Options),
  38. %% Check that queries and prepare has been done.
  39. ?assertEqual({ok, [<<"@foo">>], [[<<"bar">>]]},
  40. mysql:execute(Pid, foo, [])),
  41. Pid.
  42. common_conn_close() ->
  43. Pid = whereis(tardis),
  44. process_flag(trap_exit, true),
  45. exit(Pid, normal),
  46. receive
  47. {'EXIT', Pid, normal} -> ok
  48. after
  49. 5000 -> error({cant_stop_connection, Pid})
  50. end,
  51. process_flag(trap_exit, false).