summaryrefslogtreecommitdiff
path: root/lib/inets/test/inets_socketwrap_SUITE.erl
blob: b88cff4e908e9e9a1dff564eac5195668c3626dd (plain)
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2018. 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(inets_socketwrap_SUITE).

-include_lib("common_test/include/ct.hrl").
-include("inets_test_lib.hrl").

%% Note: This directive should only be used in test suites.
-compile(export_all).

suite() -> 
    [{ct_hooks,[ts_install_cth]}].

all() -> 
    [start_httpd_fd].

init_per_suite(Config) ->
    case os:type() of
	{unix, linux} ->
	    Config;
	_ ->
	    {skip, linux_feature}
    end.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.

init_per_testcase(Case, Config) ->
    end_per_testcase(Case, Config),
    Config.

end_per_testcase(_, Config) ->
    inets:stop(),
    Config.

%%-------------------------------------------------------------------------
start_httpd_fd() ->
    [{doc, "Start/stop of httpd service with socket wrapper"}].
start_httpd_fd(Config) when is_list(Config) ->
    PrivDir = proplists:get_value(priv_dir, Config),
    DataDir = proplists:get_value(data_dir, Config),
    HttpdConf = [{port, 80}, {ipfamily, inet},
		 {server_name, "httpd_fd_test"}, {server_root, PrivDir},
		 {document_root, PrivDir}, {bind_address, any}],
    case setup_node_info(node()) of
	{skip, _}  = Skip ->
	    Skip;
	{Node, NodeArg} ->
	    InetPort = inets_test_lib:inet_port(node()),
	    ct:pal("Node: ~p  Port ~p~n", [Node, InetPort]),
      	    Wrapper = filename:join(DataDir, "setuid_socket_wrap"),
	    Cmd = Wrapper ++  
		" -s -httpd_80,0:" ++ integer_to_list(InetPort) 
		++ " -p " ++ os:find_executable("erl") ++
		" -- " ++ NodeArg, 
	    ct:pal("cmd: ~p~n", [Cmd]),
	    case open_port({spawn, Cmd}, [stderr_to_stdout]) of 
	    	Port when is_port(Port) ->
		    wait_node_up(Node, 10),
		    ct:pal("~p", [rpc:call(Node, init, get_argument, [httpd_80])]),
		    ok  = rpc:call(Node, inets, start, []),
		    {ok, Pid} = rpc:call(Node, inets, start, [httpd, HttpdConf]),
		    [{port, InetPort}] = rpc:call(Node, httpd, info, [Pid, [port]]),
		    rpc:call(Node, erlang, halt, []);
		_  ->
		    ct:fail(open_port_failed)
	    end
    end.

%%-------------------------------------------------------------------------
%% Internal functions
%%-------------------------------------------------------------------------
setup_node_info(nonode@nohost) ->
    {skip, needs_distributed_node};
setup_node_info(Node) ->
    Static = "-detached -noinput",
    Name = "inets_fd_test",
    NameSw = case net_kernel:longnames() of
		 false -> "-sname ";
		 _ -> "-name "
		     end,
    StrNode = 
	Static ++ " "
	++ NameSw ++ " " ++ Name ++ " "
	++ "-setcookie " ++ atom_to_list(erlang:get_cookie()),
	    [_, Location] = string:tokens(atom_to_list(Node), "$@"),
    TestNode =  Name ++ "@" ++ Location, 
    {list_to_atom(TestNode), StrNode}.

wait_node_up(Node, 0) ->
    ct:fail({failed_to_start_node, Node});
wait_node_up(Node, N) ->
    ct:pal("(Node ~p: net_adm:ping(~p)~n", [node(), Node]),
    case net_adm:ping(Node) of
	pong ->
	    ok;
	pang ->
	    ct:sleep(5000),
	    wait_node_up(Node, N-1)
    end.