summaryrefslogtreecommitdiffstats
path: root/src/ct_helper.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/ct_helper.erl')
-rw-r--r--src/ct_helper.erl78
1 files changed, 77 insertions, 1 deletions
diff --git a/src/ct_helper.erl b/src/ct_helper.erl
index 475f088..7516e9b 100644
--- a/src/ct_helper.erl
+++ b/src/ct_helper.erl
@@ -12,18 +12,47 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Helper functions for common_test suites.
-module(ct_helper).
+-export([all/1]).
+-export([config/2]).
-export([create_static_dir/1]).
-export([delete_static_dir/1]).
+-export([doc/1]).
+-export([get_certs_from_ets/0]).
-export([get_loopback_mtu/0]).
+-export([ignore/3]).
-export([make_certs/0]).
+-export([make_certs_in_ets/0]).
+-export([start/1]).
-type der_encoded() :: binary().
-type key() :: {'RSAPrivateKey' | 'DSAPrivateKey' | 'PrivateKeyInfo',
der_encoded()}.
+%% @doc List all test cases in the suite.
+%%
+%% Functions test and do_* are considered internal and are ignored.
+
+all(Suite) ->
+ lists:usort([F || {F, 1} <- Suite:module_info(exports),
+ F =/= module_info,
+ F =/= test, %% This is leftover from the eunit parse_transform...
+ F =/= all,
+ F =/= groups,
+ string:substr(atom_to_list(F), 1, 5) =/= "init_",
+ string:substr(atom_to_list(F), 1, 4) =/= "end_",
+ string:substr(atom_to_list(F), 1, 3) =/= "do_"
+ ]).
+
+%% @doc Quick configuration value retrieval.
+
+config(Key, Config) ->
+ {_, Value} = lists:keyfind(Key, 1, Config),
+ Value.
+
+%% @doc Create a directory with various useful files for testing.
+
create_static_dir(Path) ->
ok = file:make_dir(Path),
ok = file:make_dir(Path ++ "/directory"),
@@ -35,6 +64,8 @@ create_static_dir(Path) ->
ok = file:change_mode(Path ++ "/unreadable", 8#0333),
ok.
+%% @doc Delete the directory created with create_static_dir/1
+
delete_static_dir(Path) ->
ok = file:delete(Path ++ "/unreadable"),
ok = file:delete(Path ++ "/index.html"),
@@ -44,6 +75,19 @@ delete_static_dir(Path) ->
ok = file:del_dir(Path),
ok.
+%% @doc Test case description.
+
+doc(String) ->
+ ct:comment(String),
+ ct:log(String).
+
+%% @doc Retrieve previously created certificates from the ets table.
+
+get_certs_from_ets() ->
+ ets:lookup_element(?MODULE, cert_opts, 2).
+
+%% @doc Return the MTU for the loopback interface.
+
get_loopback_mtu() ->
{ok, Interfaces} = inet:getiflist(),
[LocalInterface | _ ] = lists:filter(fun(Interface) ->
@@ -53,10 +97,42 @@ get_loopback_mtu() ->
{ok, [{mtu, MTU}]} = inet:ifget(LocalInterface, [mtu]),
MTU.
+%% @doc Ignore crashes from Pid occuring in M:F/A.
+
+ignore(M, F, A) ->
+ ct_helper_error_h:ignore(M, F, A).
+
%% @doc Create a set of certificates.
+
-spec make_certs()
-> {CaCert::der_encoded(), Cert::der_encoded(), Key::key()}.
make_certs() ->
CaInfo = {CaCert, _} = erl_make_certs:make_cert([{key, dsa}]),
{Cert, {Asn1Type, Der, _}} = erl_make_certs:make_cert([{key, dsa}, {issuer, CaInfo}]),
{CaCert, Cert, {Asn1Type, Der}}.
+
+%% @doc Create a set of certificates and store them in an ets table.
+
+make_certs_in_ets() ->
+ {_, Cert, Key} = ct_helper:make_certs(),
+ CertOpts = [{cert, Cert}, {key, Key}],
+ Pid = spawn(fun() -> receive after infinity -> ok end end),
+ ?MODULE = ets:new(?MODULE, [ordered_set, public, named_table,
+ {heir, Pid, undefined}]),
+ ets:insert(?MODULE, {cert_opts, CertOpts}),
+ ok.
+
+%% @doc Start and stop applications and their dependencies.
+
+start(Apps) ->
+ _ = [do_start(App) || App <- Apps],
+ ok.
+
+do_start(App) ->
+ case application:start(App) of
+ ok ->
+ ok;
+ {error, {not_started, Dep}} ->
+ do_start(Dep),
+ do_start(App)
+ end.