1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2017-2017. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%
-module(engine_SUITE).
-include_lib("common_test/include/ct.hrl").
%% Note: This directive should only be used in test suites.
-compile(export_all).
%%--------------------------------------------------------------------
%% Common Test interface functions -----------------------------------
%%--------------------------------------------------------------------
suite() ->
[{ct_hooks,[ts_install_cth]},
{timetrap,{seconds, 10}}
].
all() ->
[
get_all_possible_methods,
engine_load_all_methods,
engine_load_some_methods,
bad_arguments,
unknown_engine,
pre_command_fail_bad_value,
pre_command_fail_bad_key,
failed_engine_init
].
init_per_suite(Config) ->
try crypto:start() of
ok ->
Config;
{error,{already_started,crypto}} ->
Config
catch _:_ ->
{skip, "Crypto did not start"}
end.
end_per_suite(_Config) ->
ok.
%%--------------------------------------------------------------------
init_per_group(_Group, Config0) ->
Config0.
end_per_group(_, _) ->
ok.
%%--------------------------------------------------------------------
init_per_testcase(_Case, Config) ->
Config.
end_per_testcase(_Case, _Config) ->
ok.
%%-------------------------------------------------------------------------
%% Test cases starts here.
%%-------------------------------------------------------------------------
get_all_possible_methods() ->
[{doc, "Just fetch all possible engine methods supported."}].
get_all_possible_methods(Config) when is_list(Config) ->
try
List = crypto:engine_get_all_methods(),
ct:log("crypto:engine_get_all_methods() -> ~p\n", [List]),
ok
catch
error:notsup ->
{skip, "Engine not supported on this OpenSSL version"}
end.
engine_load_all_methods()->
[{doc, "Use a dummy md5 engine that does not implement md5"
"but rather returns a static binary to test that crypto:engine_load "
"functions works."}].
engine_load_all_methods(Config) when is_list(Config) ->
case crypto:get_test_engine() of
{error, notexist} ->
{skip, "OTP Test engine not found"};
{ok, Engine} ->
try
Md5Hash1 = <<106,30,3,246,166,222,229,158,244,217,241,179,50,232,107,109>>,
Md5Hash1 = crypto:hash(md5, "Don't panic"),
Md5Hash2 = <<0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15>>,
case crypto:engine_load(<<"dynamic">>,
[{<<"SO_PATH">>, Engine},
{<<"ID">>, <<"MD5">>},
<<"LOAD">>],
[]) of
{ok, E} ->
case crypto:hash(md5, "Don't panic") of
Md5Hash1 ->
ct:fail(fail_to_load_still_original_engine);
Md5Hash2 ->
ok;
_ ->
ct:fail(fail_to_load_engine)
end,
ok = crypto:engine_unload(E),
case crypto:hash(md5, "Don't panic") of
Md5Hash2 ->
ct:fail(fail_to_unload_still_test_engine);
Md5Hash1 ->
ok;
_ ->
ct:fail(fail_to_unload_engine)
end;
{error, bad_engine_id} ->
{skip, "Dynamic Engine not supported"}
end
catch
error:notsup ->
{skip, "Engine not supported on this OpenSSL version"}
end
end.
engine_load_some_methods()->
[{doc, "Use a dummy md5 engine that does not implement md5"
"but rather returns a static binary to test that crypto:engine_load "
"functions works."}].
engine_load_some_methods(Config) when is_list(Config) ->
case crypto:get_test_engine() of
{error, notexist} ->
{skip, "OTP Test engine not found"};
{ok, Engine} ->
try
Md5Hash1 = <<106,30,3,246,166,222,229,158,244,217,241,179,50,232,107,109>>,
Md5Hash1 = crypto:hash(md5, "Don't panic"),
Md5Hash2 = <<0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15>>,
EngineMethods = crypto:engine_get_all_methods() --
[engine_method_dh,engine_method_rand,
engine_method_ciphers, engine_method_store,
engine_method_pkey_meths, engine_method_pkey_asn1_meths],
case crypto:engine_load(<<"dynamic">>,
[{<<"SO_PATH">>, Engine},
{<<"ID">>, <<"MD5">>},
<<"LOAD">>],
[],
EngineMethods) of
{ok, E} ->
case crypto:hash(md5, "Don't panic") of
Md5Hash1 ->
ct:fail(fail_to_load_engine_still_original);
Md5Hash2 ->
ok;
_ ->
ct:fail(fail_to_load_engine)
end,
ok = crypto:engine_unload(E),
case crypto:hash(md5, "Don't panic") of
Md5Hash2 ->
ct:fail(fail_to_unload_still_test_engine);
Md5Hash1 ->
ok;
_ ->
ct:fail(fail_to_unload_engine)
end;
{error, bad_engine_id} ->
{skip, "Dynamic Engine not supported"}
end
catch
error:notsup ->
{skip, "Engine not supported on this OpenSSL version"}
end
end.
%%-------------------------------------------------------------------------
%% Error cases
bad_arguments()->
[{doc, "Test different arguments in bad format."}].
bad_arguments(Config) when is_list(Config) ->
case crypto:get_test_engine() of
{error, notexist} ->
{skip, "OTP Test engine not found"};
{ok, Engine} ->
try
try
crypto:engine_load(fail_engine, [], [])
catch
error:badarg ->
ok
end,
try
crypto:engine_load(<<"dynamic">>,
[{<<"SO_PATH">>, Engine},
1,
{<<"ID">>, <<"MD5">>},
<<"LOAD">>],
[])
catch
error:badarg ->
ok
end,
try
crypto:engine_load(<<"dynamic">>,
[{<<"SO_PATH">>, Engine},
{'ID', <<"MD5">>},
<<"LOAD">>],
[])
catch
error:badarg ->
ok
end
catch
error:notsup ->
{skip, "Engine not supported on this OpenSSL version"}
end
end.
unknown_engine() ->
[{doc, "Try to load a non existent engine."}].
unknown_engine(Config) when is_list(Config) ->
try
{error, bad_engine_id} = crypto:engine_load(<<"fail_engine">>, [], []),
ok
catch
error:notsup ->
{skip, "Engine not supported on this OpenSSL version"}
end.
pre_command_fail_bad_value() ->
[{doc, "Test pre command due to bad value"}].
pre_command_fail_bad_value(Config) when is_list(Config) ->
DataDir = unicode:characters_to_binary(code:priv_dir(crypto)),
try
case crypto:engine_load(<<"dynamic">>,
[{<<"SO_PATH">>,
<<DataDir/binary, <<"/libfail_engine.so">>/binary >>},
{<<"ID">>, <<"MD5">>},
<<"LOAD">>],
[]) of
{error, ctrl_cmd_failed} ->
ok;
{error, bad_engine_id} ->
{skip, "Dynamic Engine not supported"}
end
catch
error:notsup ->
{skip, "Engine not supported on this OpenSSL version"}
end.
pre_command_fail_bad_key() ->
[{doc, "Test pre command due to bad key"}].
pre_command_fail_bad_key(Config) when is_list(Config) ->
try
case crypto:get_test_engine() of
{error, notexist} ->
{skip, "OTP Test engine not found"};
{ok, Engine} ->
case crypto:engine_load(<<"dynamic">>,
[{<<"SO_WRONG_PATH">>, Engine},
{<<"ID">>, <<"MD5">>},
<<"LOAD">>],
[]) of
{error, ctrl_cmd_failed} ->
ok;
{error, bad_engine_id} ->
{skip, "Dynamic Engine not supported"}
end
end
catch
error:notsup ->
{skip, "Engine not supported on this OpenSSL version"}
end.
failed_engine_init()->
[{doc, "Test failing engine init due to missed pre command"}].
failed_engine_init(Config) when is_list(Config) ->
try
case crypto:get_test_engine() of
{error, notexist} ->
{skip, "OTP Test engine not found"};
{ok, Engine} ->
case crypto:engine_load(<<"dynamic">>,
[{<<"SO_PATH">>, Engine},
{<<"ID">>, <<"MD5">>}],
[]) of
{error, add_engine_failed} ->
ok;
{error, bad_engine_id} ->
{skip, "Dynamic Engine not supported"}
end
end
catch
error:notsup ->
{skip, "Engine not supported on this OpenSSL version"}
end.
|