ssl_tests.erl 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. -define(certfile, "test/ssl/server-cert.pem").
  25. -define(keyfile, "test/ssl/server-key.pem").
  26. successful_ssl_connect_test() ->
  27. [ application:start(App) || App <- [crypto, asn1, public_key, ssl] ],
  28. common_basic_check([{ssl, [
  29. {server_name_indication, disable},
  30. {cacertfile, ?cacertfile}]},
  31. {certfile, ?certfile},
  32. {keyfile, ?keyfile},
  33. {user, ?ssl_user}, {password, ?ssl_password}]),
  34. common_conn_close(),
  35. ok.
  36. common_basic_check(ExtraOpts) ->
  37. Options = [{name, {local, tardis}},
  38. {queries, ["SET @foo = 'bar'", "SELECT 1",
  39. "SELECT 1; SELECT 2"]},
  40. {prepare, [{foo, "SELECT @foo"}]} | ExtraOpts],
  41. {ok, Pid} = mysql:start_link(Options),
  42. %% Check that queries and prepare has been done.
  43. ?assertEqual({ok, [<<"@foo">>], [[<<"bar">>]]},
  44. mysql:execute(Pid, foo, [])),
  45. Pid.
  46. common_conn_close() ->
  47. Pid = whereis(tardis),
  48. process_flag(trap_exit, true),
  49. exit(Pid, normal),
  50. receive
  51. {'EXIT', Pid, normal} -> ok
  52. after
  53. 5000 -> error({cant_stop_connection, Pid})
  54. end,
  55. process_flag(trap_exit, false).