4
%% Copyright Ericsson AB 2001-2010. 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/.
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
20
-module(inets_test_lib).
22
-include("inets_test_lib.hrl").
23
-include_lib("inets/src/http_lib/http_internal.hrl").
25
%% Various small utility functions
26
-export([start_http_server/1, start_http_server/2]).
27
-export([start_http_server_ssl/1, start_http_server_ssl/2]).
28
-export([hostname/0]).
29
-export([connect_bin/3, connect_byte/3, send/3, close/2]).
30
-export([copy_file/3, copy_files/2, copy_dirs/2, del_dirs/1]).
31
-export([info/4, log/4, debug/4, print/4]).
32
-export([check_body/1]).
33
-export([millis/0, millis_diff/2, hours/1, minutes/1, seconds/1, sleep/1]).
35
-export([non_pc_tc_maybe_skip/4, os_based_skip/1]).
37
-export([start_node/1, stop_node/1]).
39
%% -- Misc os command and stuff
42
string:strip(os:cmd(Cmd), right, $\n).
44
%% -- Misc node operation wrapper functions --
47
Pa = filename:dirname(code:which(?MODULE)),
48
Args = case init:get_argument('CC_TEST') of
50
" -pa /clearcase/otp/libraries/snmp/ebin ";
56
A = Args ++ " -pa " ++ Pa,
57
Opts = [{cleanup,false}, {args, A}],
58
case (catch test_server:start_node(Name, slave, Opts)) of
62
exit({failed_starting_node, Name, Else})
66
rpc:cast(Node, erlang, halt, []),
67
await_stopped(Node, 5).
69
await_stopped(_, 0) ->
71
await_stopped(Node, N) ->
72
Nodes = erlang:nodes(),
73
case lists:member(Node, Nodes) of
76
await_stopped(Node, N-1);
82
%% ----------------------------------------------------------------
83
%% HTTPD starter functions
86
start_http_server(Conf) ->
87
start_http_server(Conf, ?HTTP_DEFAULT_SSL_KIND).
89
start_http_server(Conf, essl = _SslTag) ->
90
application:start(crypto),
91
do_start_http_server(Conf);
92
start_http_server(Conf, _SslTag) ->
93
do_start_http_server(Conf).
95
do_start_http_server(Conf) ->
96
tsp("start http server with "
99
application:load(inets),
100
case application:set_env(inets, services, [{httpd, Conf}]) of
102
case application:start(inets) of
106
test_server:format("<ERROR> Failed starting application: "
112
test_server:format("<ERROR> Failed set application env: "
118
start_http_server_ssl(FileName) ->
119
start_http_server_ssl(FileName, ?HTTP_DEFAULT_SSL_KIND).
121
start_http_server_ssl(FileName, essl = _SslTag) ->
122
application:start(crypto),
123
do_start_http_server_ssl(FileName);
124
start_http_server_ssl(FileName, _SslTag) ->
125
do_start_http_server_ssl(FileName).
127
do_start_http_server_ssl(FileName) ->
128
tsp("start (ssl) http server with "
131
application:start(ssl),
132
catch do_start_http_server(FileName).
135
%% ----------------------------------------------------------------------
139
info(F, A, Mod, Line) ->
140
print("INF ", F, A, Mod, Line).
142
log(F, A, Mod, Line) ->
143
print("LOG ", F, A, Mod, Line).
145
debug(F, A, Mod, Line) ->
146
print("DBG ", F, A, Mod, Line).
148
print(P, F, A, Mod, Line) ->
149
io:format("~s[~p:~p:~p] : " ++ F ++ "~n", [P, self(), Mod, Line| A]).
151
print(F, A, Mod, Line) ->
152
print("", F, A, Mod, Line).
155
from($@, atom_to_list(node())).
156
from(H, [H | T]) -> T;
157
from(H, [_ | T]) -> from(H, T);
161
copy_file(File, From, To) ->
162
file:copy(filename:join(From, File), filename:join(To, File)).
164
copy_files(FromDir, ToDir) ->
165
{ok, Files} = file:list_dir(FromDir),
166
lists:foreach(fun(File) ->
167
FullPath = filename:join(FromDir, File),
168
case filelib:is_file(FullPath) of
171
filename:join(ToDir, File));
178
copy_dirs(FromDirRoot, ToDirRoot) ->
179
{ok, Files} = file:list_dir(FromDirRoot),
182
%% Check if it's a directory or a file
183
case filelib:is_dir(filename:join(FromDirRoot, FileOrDir)) of
185
FromDir = filename:join([FromDirRoot, FileOrDir]),
186
ToDir = filename:join([ToDirRoot, FileOrDir]),
187
ok = file:make_dir(ToDir),
188
copy_dirs(FromDir, ToDir);
190
copy_file(FileOrDir, FromDirRoot, ToDirRoot)
195
case file:list_dir(Dir) of
199
lists:foreach(fun(File) ->
200
FullPath = filename:join(Dir,File),
201
case filelib:is_dir(FullPath) of
204
file:del_dir(FullPath);
206
file:delete(FullPath)
214
case string:rstr(Body, "</html>") of
216
case string:rstr(Body, "</HTML>") of
218
tsp("Body ~p", [Body]),
219
tsf(did_not_receive_whole_body);
227
%% ----------------------------------------------------------------
228
%% Conditional skip of testcases
231
non_pc_tc_maybe_skip(Config, Condition, File, Line)
232
when is_list(Config) andalso is_function(Condition) ->
233
%% Check if we shall skip the skip
234
case os:getenv("TS_OS_BASED_SKIP") of
238
case lists:keysearch(ts, 1, Config) of
239
{value, {ts, inets}} ->
240
%% Always run the testcase if we are using our own
244
case (catch Condition()) of
246
skip(non_pc_testcase, File, Line);
254
os_based_skip(any) ->
256
os_based_skip(Skippable) when is_list(Skippable) ->
259
{_Fam, _Name} = FamAndName ->
264
case lists:member(OsFam, Skippable) of
268
case lists:keysearch(OsFam, 1, Skippable) of
269
{value, {OsFam, OsName}} ->
271
{value, {OsFam, OsNames}} when is_list(OsNames) ->
272
lists:member(OsName, OsNames);
281
%% ----------------------------------------------------------------------
283
%% open(SocketType, Host, Port) -> {ok, Socket} | {error, Reason}
284
%% SocketType -> ssl | ip_comm
285
%% Host -> atom() | string() | {A, B, C, D}
288
connect_bin(ssl, Host, Port) ->
289
connect(ssl, Host, Port, [binary, {packet,0}]);
290
connect_bin(ossl, Host, Port) ->
291
connect(ssl, Host, Port, [{ssl_imp, old}, binary, {packet,0}]);
292
connect_bin(essl, Host, Port) ->
293
connect(ssl, Host, Port, [{ssl_imp, new}, binary, {packet,0}, {reuseaddr, true}]);
294
connect_bin(ip_comm, Host, Port) ->
295
Opts = [inet6, binary, {packet,0}],
296
connect(ip_comm, Host, Port, Opts).
299
connect_byte(ssl, Host, Port) ->
300
connect(ssl, Host, Port, [{packet,0}]);
301
connect_byte(ossl, Host, Port) ->
302
connect(ssl, Host, Port, [{ssl_imp, old}, {packet,0}]);
303
connect_byte(essl, Host, Port) ->
304
connect(ssl, Host, Port, [{ssl_imp, new}, {packet,0}]);
305
connect_byte(ip_comm, Host, Port) ->
306
Opts = [inet6, {packet,0}],
307
connect(ip_comm, Host, Port, Opts).
310
connect(ssl, Host, Port, Opts) ->
312
%% Does not support ipv6 in old ssl
313
case ssl:connect(Host, Port, Opts) of
321
connect(ip_comm, Host, Port, Opts) ->
322
case gen_tcp:connect(Host,Port, Opts) of
324
%% tsp("connect success"),
327
tsp("nxdomain opts: ~p", [Opts]),
328
connect(ip_comm, Host, Port, lists:delete(inet6, Opts));
329
{error, eafnosupport} ->
330
tsp("eafnosupport opts: ~p", [Opts]),
331
connect(ip_comm, Host, Port, lists:delete(inet6, Opts));
332
{error, enetunreach} ->
333
tsp("eafnosupport opts: ~p", [Opts]),
334
connect(ip_comm, Host, Port, lists:delete(inet6, Opts));
335
{error, {enfile,_}} ->
339
tsp("Unexpected error: "
345
"~n", [Error, Host, Port, Opts]),
350
send(ssl, Socket, Data) ->
351
ssl:send(Socket, Data);
352
send(ossl, Socket, Data) ->
353
ssl:send(Socket, Data);
354
send(essl, Socket, Data) ->
355
ssl:send(Socket, Data);
356
send(ip_comm,Socket,Data) ->
357
gen_tcp:send(Socket,Data).
361
catch ssl:close(Socket);
362
close(ossl,Socket) ->
363
catch ssl:close(Socket);
364
close(essl,Socket) ->
365
catch ssl:close(Socket);
366
close(ip_comm,Socket) ->
367
catch gen_tcp:close(Socket).
373
T1 = (element(1,A)*1000000) + element(2,A) + (element(3,A)/1000000),
374
T2 = (element(1,B)*1000000) + element(2,B) + (element(3,B)/1000000),
377
hours(N) -> trunc(N * 1000 * 60 * 60).
378
minutes(N) -> trunc(N * 1000 * 60).
379
seconds(N) -> trunc(N * 1000).
389
after trunc(MSecs) ->
395
skip(Reason, File, Line) ->
396
exit({skipped, {Reason, File, Line}}).
410
test_server:format("~p ~p:" ++ F ++ "~n", [self(), ?MODULE | A]).
413
test_server:fail(Reason).