Browse Source

Move SSL tests to separate suite

Viktor Söderqvist 7 years ago
parent
commit
e8296e3ff9
3 changed files with 59 additions and 10 deletions
  1. 0 9
      test/mysql_tests.erl
  2. 3 1
      test/ssl/Makefile
  3. 56 0
      test/ssl_tests.erl

+ 0 - 9
test/mysql_tests.erl

@@ -67,15 +67,6 @@ successful_connect_test() ->
     ?assertMatch({error, _}, mysql:code_change("2.0.0", unknown_state, [])),
     common_conn_close().
 
-successful_ssl_connect_test() ->
-    %% The same test as successful_connect_test(), minus gen_server checks,
-    %% plus SSL
-    [ application:start(App) || App <- [crypto, asn1, public_key, ssl] ],
-    common_basic_check([{ssl, [{cacertfile, "test/ssl/ca.pem"}]},
-                        {user, ?ssl_user}, {password, ?ssl_password}]),
-    common_conn_close(),
-    ok.
-
 common_basic_check(ExtraOpts) ->
     Options = [{name, {local, tardis}},
                {queries, ["SET @foo = 'bar'", "SELECT 1",

+ 3 - 1
test/ssl/Makefile

@@ -10,7 +10,9 @@ SERVERCERT = server-cert.pem
 CASTRING = "/C=PL/L=Krakow/CN=MYSQL CA"
 SERVERSTRING = "/C=PL/L=Krakow/CN=localhost"
 
-all:
+all: my-ssl.cnf
+
+my-ssl.cnf:
 	openssl genrsa -out $(CAKEY) 2048
 	openssl req -x509 -new -nodes -key $(CAKEY) -sha256 -days 1024 -out $(CACERT) -subj $(CASTRING)
 	openssl genrsa -out $(SERVERKEY) 2048

+ 56 - 0
test/ssl_tests.erl

@@ -0,0 +1,56 @@
+%% MySQL/OTP – MySQL client library for Erlang/OTP
+%% Copyright (C) 2017 Piotr Nosek, Viktor Söderqvist
+%%
+%% This file is part of MySQL/OTP.
+%%
+%% MySQL/OTP is free software: you can redistribute it and/or modify it under
+%% the terms of the GNU Lesser General Public License as published by the Free
+%% Software Foundation, either version 3 of the License, or (at your option)
+%% any later version.
+%%
+%% This program is distributed in the hope that it will be useful, but WITHOUT
+%% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+%% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+%% more details.
+%%
+%% You should have received a copy of the GNU Lesser General Public License
+%% along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+%% @doc This module tests to connect to a database over SSL.
+-module(ssl_tests).
+
+-include_lib("eunit/include/eunit.hrl").
+
+-define(ssl_user,     "otptestssl").
+-define(ssl_password, "otptestssl").
+-define(cacertfile,   "test/ssl/ca.pem").
+
+successful_ssl_connect_test() ->
+    [ application:start(App) || App <- [crypto, asn1, public_key, ssl] ],
+    common_basic_check([{ssl, [{cacertfile, ?cacertfile}]},
+                        {user, ?ssl_user}, {password, ?ssl_password}]),
+    common_conn_close(),
+    ok.
+
+common_basic_check(ExtraOpts) ->
+    Options = [{name, {local, tardis}},
+               {queries, ["SET @foo = 'bar'", "SELECT 1",
+                          "SELECT 1; SELECT 2"]},
+               {prepare, [{foo, "SELECT @foo"}]} | ExtraOpts],
+    {ok, Pid} = mysql:start_link(Options),
+    %% Check that queries and prepare has been done.
+    ?assertEqual({ok, [<<"@foo">>], [[<<"bar">>]]},
+                 mysql:execute(Pid, foo, [])),
+    Pid.
+
+common_conn_close() ->
+    Pid = whereis(tardis),
+    process_flag(trap_exit, true),
+    exit(Pid, normal),
+    receive
+        {'EXIT', Pid, normal} -> ok
+    after
+        5000 -> error({cant_stop_connection, Pid})
+    end,
+    process_flag(trap_exit, false).
+