4
%% Copyright Ericsson AB 2010-2011. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.2
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
22
-module(ssl_session_cache_SUITE).
24
%% Note: This directive should only be used in test suites.
27
-include_lib("common_test/include/ct.hrl").
30
-define(TIMEOUT, 60000).
31
-define(LONG_TIMEOUT, 600000).
32
-behaviour(ssl_session_cache_api).
34
%% For the session cache tests
35
-export([init/1, terminate/1, lookup/2, update/3,
36
delete/2, foldl/3, select_session/2]).
38
%% Test server callback functions
39
%%--------------------------------------------------------------------
40
%% Function: init_per_suite(Config) -> Config
42
%% A list of key/value pairs, holding the test case configuration.
43
%% Description: Initialization before the whole suite
45
%% Note: This function is free to add any key/value pairs to the Config
46
%% variable, but should NOT alter/remove any existing entries.
47
%%--------------------------------------------------------------------
48
init_per_suite(Config0) ->
49
Dog = ssl_test_lib:timetrap(?LONG_TIMEOUT *2),
52
application:start(public_key),
55
%% make rsa certs using oppenssl
57
(catch make_certs:all(?config(data_dir, Config0),
58
?config(priv_dir, Config0))),
59
test_server:format("Make certs ~p~n", [Result]),
61
Config1 = ssl_test_lib:make_dsa_cert(Config0),
62
Config = ssl_test_lib:cert_options(Config1),
63
[{watchdog, Dog} | Config]
65
{skip, "Crypto did not start"}
68
%%--------------------------------------------------------------------
69
%% Function: end_per_suite(Config) -> _
71
%% A list of key/value pairs, holding the test case configuration.
72
%% Description: Cleanup after the whole suite
73
%%--------------------------------------------------------------------
74
end_per_suite(_Config) ->
76
application:stop(crypto).
78
%%--------------------------------------------------------------------
79
%% Function: init_per_testcase(TestCase, Config) -> Config
81
%% Name of the test case that is about to be run.
83
%% A list of key/value pairs, holding the test case configuration.
85
%% Description: Initialization before each test case
87
%% Note: This function is free to add any key/value pairs to the Config
88
%% variable, but should NOT alter/remove any existing entries.
89
%% Description: Initialization before each test case
90
%%--------------------------------------------------------------------
91
init_per_testcase(session_cache_process_list, Config) ->
92
init_customized_session_cache(list, Config);
94
init_per_testcase(session_cache_process_mnesia, Config) ->
96
init_customized_session_cache(mnesia, Config);
98
init_per_testcase(_TestCase, Config0) ->
99
Config = lists:keydelete(watchdog, 1, Config0),
100
Dog = test_server:timetrap(?TIMEOUT),
101
[{watchdog, Dog} | Config].
103
init_customized_session_cache(Type, Config0) ->
104
Config = lists:keydelete(watchdog, 1, Config0),
105
Dog = test_server:timetrap(?TIMEOUT),
107
application:load(ssl),
108
application:set_env(ssl, session_cb, ?MODULE),
109
application:set_env(ssl, session_cb_init_args, [Type]),
111
[{watchdog, Dog} | Config].
113
%%--------------------------------------------------------------------
114
%% Function: end_per_testcase(TestCase, Config) -> _
116
%% Name of the test case that is about to be run.
117
%% Config - [tuple()]
118
%% A list of key/value pairs, holding the test case configuration.
119
%% Description: Cleanup after each test case
120
%%--------------------------------------------------------------------
121
end_per_testcase(session_cache_process_list, Config) ->
122
application:unset_env(ssl, session_cb),
123
end_per_testcase(default_action, Config);
124
end_per_testcase(session_cache_process_mnesia, Config) ->
125
application:unset_env(ssl, session_cb),
126
application:unset_env(ssl, session_cb_init_args),
130
end_per_testcase(default_action, Config);
131
end_per_testcase(_TestCase, Config) ->
132
Dog = ?config(watchdog, Config),
137
test_server:timetrap_cancel(Dog)
140
%%--------------------------------------------------------------------
141
%% Function: all(Clause) -> TestCases
142
%% Clause - atom() - suite | doc
143
%% TestCases - [Case]
145
%% Name of a test case.
146
%% Description: Returns a list of all test cases in this test suite
147
%%--------------------------------------------------------------------
148
suite() -> [{ct_hooks,[ts_install_cth]}].
151
[session_cache_process_list,
152
session_cache_process_mnesia].
157
init_per_group(_GroupName, Config) ->
160
end_per_group(_GroupName, Config) ->
163
session_cache_process_list(doc) ->
164
["Test reuse of sessions (short handshake)"];
166
session_cache_process_list(suite) ->
168
session_cache_process_list(Config) when is_list(Config) ->
169
session_cache_process(list,Config).
170
%%--------------------------------------------------------------------
171
session_cache_process_mnesia(doc) ->
172
["Test reuse of sessions (short handshake)"];
174
session_cache_process_mnesia(suite) ->
176
session_cache_process_mnesia(Config) when is_list(Config) ->
177
session_cache_process(mnesia,Config).
180
%%--------------------------------------------------------------------
181
%%% Session cache API callbacks
182
%%--------------------------------------------------------------------
185
ets:new(ssl_test, [named_table, public, set]),
186
ets:insert(ssl_test, {type, Type}),
189
spawn(fun() -> session_loop([]) end);
192
{atomic,ok} = mnesia:create_table(sess_cache, []),
197
[{type, Type}] = ets:lookup(ssl_test, type),
206
mnesia:delete_table(sess_cache)
209
lookup(Cache, Key) ->
212
Cache ! {self(), lookup, Key},
213
receive {Cache, Res} -> Res end;
215
case mnesia:transaction(fun() ->
216
mnesia:read(sess_cache,
219
{atomic, [{sess_cache, Key, Value}]} ->
226
update(Cache, Key, Value) ->
229
Cache ! {update, Key, Value};
232
mnesia:transaction(fun() ->
233
mnesia:write(sess_cache,
234
{sess_cache, Key, Value}, write)
238
delete(Cache, Key) ->
241
Cache ! {delete, Key};
244
mnesia:transaction(fun() ->
245
mnesia:delete(sess_cache, Key)
249
foldl(Fun, Acc, Cache) ->
252
Cache ! {self(),foldl,Fun,Acc},
253
receive {Cache, Res} -> Res end;
256
mnesia:foldl(Fun, Acc, sess_cache)
258
{atomic, Res} = mnesia:transaction(Foldl),
262
select_session(Cache, PartialKey) ->
265
Cache ! {self(),select_session, PartialKey},
273
[{{sess_cache,{PartialKey,'$1'}, '$2'},
276
{atomic, Res} = mnesia:transaction(Sel),
280
session_loop(Sess) ->
284
{Pid, lookup, Key} ->
285
case lists:keysearch(Key,1,Sess) of
286
{value, {Key,Value}} ->
287
Pid ! {self(), Value};
289
Pid ! {self(), undefined}
292
{update, Key, Value} ->
293
NewSess = [{Key,Value}| lists:keydelete(Key,1,Sess)],
294
session_loop(NewSess);
296
session_loop(lists:keydelete(Key,1,Sess));
297
{Pid,foldl,Fun,Acc} ->
298
Res = lists:foldl(Fun, Acc,Sess),
301
{Pid,select_session,PKey} ->
302
Sel = fun({{PKey0, Id},Session}, Acc) when PKey == PKey0 ->
307
Sessions = lists:foldl(Sel, [], Sess),
308
Pid ! {self(), Sessions},
312
%%--------------------------------------------------------------------
313
%%% Internal functions
314
%%--------------------------------------------------------------------
316
session_cache_process(_Type,Config) when is_list(Config) ->
317
ssl_basic_SUITE:reuse_session(Config).