Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Jan 11, 2022
1 parent a673d03 commit 89b9c18
Show file tree
Hide file tree
Showing 18 changed files with 483 additions and 655 deletions.
34 changes: 15 additions & 19 deletions big_tests/tests/connect_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ suite() ->
init_per_suite(Config) ->
Config0 = escalus:init_per_suite([{escalus_user_db, {module, escalus_ejabberd, []}} | Config]),
C2SPort = ct:get_config({hosts, mim, c2s_port}),
[{_, ejabberd_c2s, _} = C2SListener] = mongoose_helper:get_listener_opts(mim(), C2SPort),
[C2SListener] = mongoose_helper:get_listeners(mim(), #{port => C2SPort, module => ejabberd_c2s}),
Config1 = [{c2s_listener, C2SListener} | Config0],
assert_cert_file_exists(),
escalus:create_users(Config1, escalus:get_users([?SECURE_USER, alice])).
Expand All @@ -148,32 +148,33 @@ end_per_suite(Config) ->
escalus:end_per_suite(Config).

init_per_group(c2s_noproc, Config) ->
config_ejabberd_node_c2s(Config, [starttls]),
configure_c2s_listener(Config, #{tls => [starttls | common_tls_opts(Config)]}),
Config;
init_per_group(session_replacement, Config) ->
config_ejabberd_node_c2s(Config, [starttls]),
configure_c2s_listener(Config, #{tls => [starttls | common_tls_opts(Config)]}),
logger_ct_backend:start(),
Config;
init_per_group(starttls, Config) ->
config_ejabberd_node_c2s(Config, [starttls_required]),
configure_c2s_listener(Config, #{tls => [starttls_required | common_tls_opts(Config)]}),
Config;
init_per_group(tls, Config) ->
config_ejabberd_node_c2s(Config, [tls]),
configure_c2s_listener(Config, #{tls => [tls | common_tls_opts(Config)]}),
Users = proplists:get_value(escalus_users, Config, []),
JoeSpec = lists:keydelete(starttls, 1, proplists:get_value(?SECURE_USER, Users)),
JoeSpec2 = {?SECURE_USER, lists:keystore(ssl, 1, JoeSpec, {ssl, true})},
NewUsers = lists:keystore(?SECURE_USER, 1, Users, JoeSpec2),
Config2 = lists:keystore(escalus_users, 1, Config, {escalus_users, NewUsers}),
[{c2s_port, ct:get_config({hosts, mim, c2s_port})} | Config2];
init_per_group(feature_order, Config) ->
config_ejabberd_node_c2s(Config, [starttls_required, {zlib, 10000}]),
configure_c2s_listener(Config, #{zlib => 100000,
tls => [starttls_required | common_tls_opts(Config)]}),
Config;
init_per_group(just_tls,Config)->
[{tls_module, just_tls} | Config];
init_per_group(fast_tls,Config)->
[{tls_module, fast_tls} | Config];
init_per_group(proxy_protocol, Config) ->
config_ejabberd_node_c2s(Config, [{proxy_protocol, true}]),
configure_c2s_listener(Config, #{proxy_protocol => true}),
Config;
init_per_group(_, Config) ->
Config.
Expand Down Expand Up @@ -751,28 +752,23 @@ openssl_client_can_use_cipher(Cipher, Port) ->
0 == string:str(Output, ":error:") andalso 0 == string:str(Output, "errno=0").

restore_c2s_listener(Config) ->
{_, _, Opts} = C2SListener = ?config(c2s_listener, Config),
mongoose_helper:restart_listener_with_opts(mim(), C2SListener, Opts).
C2SListener = ?config(c2s_listener, Config),
mongoose_helper:restart_listener(mim(), C2SListener).

assert_cert_file_exists() ->
ejabberd_node_utils:file_exists(?CERT_FILE) orelse
ct:fail("cert file ~s not exists", [?CERT_FILE]).

config_ejabberd_node_c2s(Config, ExtraC2SOpts) ->
Opts = ExtraC2SOpts ++ common_c2s_opts(Config),
configure_c2s_listener(Config, ExtraC2SOpts) ->
C2SListener = ?config(c2s_listener, Config),
mongoose_helper:restart_listener_with_opts(mim(), C2SListener, Opts).
NewC2SListener = maps:merge(C2SListener, ExtraC2SOpts),
mongoose_helper:restart_listener(mim(), NewC2SListener).

common_c2s_opts(Config) ->
common_tls_opts(Config) ->
TLSModule = ?config(tls_module, Config),
[{tls_module, TLSModule},
{certfile, ?CERT_FILE},
%starttls,
%{zlib, 10000},
{access, c2s},
{shaper, c2s_shaper},
{max_stanza_size, 65536},
{dhfile, "priv/ssl/fake_dh_server.pem"}].
{dhfile, ?DH_FILE}].

set_secure_connection_protocol(UserSpec, Version) ->
[{ssl_opts, [{versions, [Version]}]} | UserSpec].
Expand Down
33 changes: 16 additions & 17 deletions big_tests/tests/domain_rest_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
patch_custom/4]).

%% Handler
-export([start_listener/0,
start_listener/1,
stop_listener/0]).
-export([start_listener/1,
stop_listener/1]).

-import(distributed_helper, [mim/0, mim2/0, require_rpc_nodes/1, rpc/4]).

Expand Down Expand Up @@ -98,21 +97,21 @@ patch_custom(Config, Role, Path, Body) ->
body => Body }).

%% REST handler setup
start_listener() ->
start_listener(#{}).

stop_listener() ->
PortIpNet = {?TEST_PORT, {127,0,0,1}, tcp},
ListenerModule = ejabberd_cowboy,
rpc(mim(), ejabberd_listener, stop_listener, [PortIpNet, ListenerModule]).

start_listener(Params) ->
PortIpNet = {?TEST_PORT, {127,0,0,1}, tcp},
ListenerModule = ejabberd_cowboy,
ListenerOpts = [{modules, [domain_handler(Params)]},
{transport_options, transport_options()}],
rpc(mim(), ejabberd_listener, start_listener,
[PortIpNet, ListenerModule, ListenerOpts]).
rpc(mim(), ejabberd_listener, start_listener, [listener_opts(Params)]).

stop_listener(Params) ->
rpc(mim(), ejabberd_listener, stop_listener, [listener_opts(Params)]).

listener_opts(Params) ->
#{port => ?TEST_PORT,
ip_tuple => {127, 0, 0, 1},
ip_address => "127.0.0.1",
ip_version => 4,
proto => tcp,
module => ejabberd_cowboy,
modules => [domain_handler(Params)],
transport_options => transport_options()}.

transport_options() ->
[{max_connections, 1024}, {num_acceptors, 10}].
Expand Down
24 changes: 8 additions & 16 deletions big_tests/tests/login_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
-module(login_SUITE).
-compile([export_all, nowarn_export_all]).

-include_lib("escalus/include/escalus.hrl").
-include_lib("escalus/include/escalus_xmlns.hrl").

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

-include_lib("exml/include/exml.hrl").

-import(distributed_helper, [mim/0,
Expand All @@ -35,9 +29,6 @@
%% Suite configuration
%%--------------------------------------------------------------------

-define(REGISTRATION_TIMEOUT, 2). %% seconds
-define(CERT_FILE, "priv/ssl/fake_server.pem").

all() ->
[
{group, login},
Expand Down Expand Up @@ -147,7 +138,7 @@ init_per_group(login_scram_tls = GroupName, ConfigIn) ->
mongoose_helper:restore_config(Config),
{skip, "scram password type not supported"};
true ->
Config1 = config_ejabberd_node_tls(Config),
Config1 = configure_c2s_listener(Config),
Config2 = create_tls_users(Config1),
assert_password_format(scram, Config2)
end;
Expand Down Expand Up @@ -401,12 +392,13 @@ message_zlib_limit(Config) ->
%% Helpers
%%--------------------------------------------------------------------

config_ejabberd_node_tls(Config) ->
configure_c2s_listener(Config) ->
C2SPort = ct:get_config({hosts, mim, c2s_port}),
[{_, ejabberd_c2s, Opts} = C2SListener] = mongoose_helper:get_listener_opts(mim(), C2SPort),
[C2SListener = #{tls := TLSOpts}] =
mongoose_helper:get_listeners(mim(), #{port => C2SPort, module => ejabberd_c2s}),
%% replace starttls with tls
NewOpts = [tls | Opts -- [starttls]],
mongoose_helper:restart_listener_with_opts(mim(), C2SListener, NewOpts),
NewTLSOpts = [tls | TLSOpts -- [starttls]],
mongoose_helper:restart_listener(mim(), C2SListener#{tls := NewTLSOpts}),
[{c2s_listener, C2SListener} | Config].

create_tls_users(Config) ->
Expand Down Expand Up @@ -500,5 +492,5 @@ are_sasl_scram_modules_supported() ->
[true, true, true, true, true] == IsSupported.

restore_c2s(Config) ->
{_, _, Opts} = C2SListener = proplists:get_value(c2s_listener, Config),
mongoose_helper:restart_listener_with_opts(mim(), C2SListener, Opts).
C2SListener = proplists:get_value(c2s_listener, Config),
mongoose_helper:restart_listener(mim(), C2SListener).
17 changes: 9 additions & 8 deletions big_tests/tests/mongoose_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
-export([wait_for_pid_to_die/1]).
-export([supports_sasl_module/1]).
-export([auth_opts_with_password_format/1]).
-export([get_listener_opts/2]).
-export([restart_listener_with_opts/3]).
-export([get_listeners/2]).
-export([restart_listener/2]).
-export([should_minio_be_running/1]).
-export([new_mongoose_acc/1]).
-export([print_debug_info_for_module/1]).
Expand Down Expand Up @@ -492,14 +492,15 @@ build_new_password_opts(scram, PassOpts) ->
build_new_password_opts(Type, PassOpts) ->
PassOpts#{format => Type}.

get_listener_opts(#{} = Spec, Port) ->
get_listeners(#{} = Spec, Pattern) ->
Keys = maps:keys(Pattern),
Listeners = rpc(Spec, mongoose_config, get_opt, [listen]),
[Item || {{ListenerPort, _, _}, _, _} = Item <- Listeners, ListenerPort =:= Port].
lists:filter(fun(Listener) -> maps:with(Keys, Listener) =:= Pattern end, Listeners).

restart_listener_with_opts(Spec, Listener, NewOpts) ->
{PortIPProto, Module, _Opts} = Listener,
rpc(Spec, ejabberd_listener, stop_listener, [PortIPProto, Module]),
rpc(Spec, ejabberd_listener, start_listener, [PortIPProto, Module, NewOpts]).
%% 'port', 'ip_tuple' and 'proto' options need to stay unchanged for a successful restart
restart_listener(Spec, Listener) ->
rpc(Spec, ejabberd_listener, stop_listener, [Listener]),
rpc(Spec, ejabberd_listener, start_listener, [Listener]).

should_minio_be_running(Config) ->
case proplists:get_value(preset, Config, undefined) of
Expand Down
40 changes: 17 additions & 23 deletions big_tests/tests/rest_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,14 @@ get_port(_Role, _Node, #{port := Port}) ->
Port;
get_port(Role, Node, _Params) ->
Listeners = rpc(Node, mongoose_config, get_opt, [listen]),
[{PortIpNet, ejabberd_cowboy, _Opts}] =
lists:filter(fun(Config) -> is_roles_config(Config, Role) end, Listeners),
case PortIpNet of
{Port, _Host, _Net} -> Port;
{Port, _Host} -> Port;
Port -> Port
end.
[#{port := Port}] = lists:filter(fun(Config) -> is_roles_config(Config, Role) end, Listeners),
Port.

-spec get_ssl_status(Role :: role(), Server :: distributed_helper:rpc_spec()) -> boolean().
get_ssl_status(Role, Node) ->
Listeners = rpc(Node, mongoose_config, get_opt, [listen]),
[{_PortIpNet, _Module, Opts}] =
lists:filter(fun (Opts) -> is_roles_config(Opts, Role) end, Listeners),
lists:keymember(ssl, 1, Opts).
[Opts] = lists:filter(fun (Opts) -> is_roles_config(Opts, Role) end, Listeners),
maps:is_key(ssl, Opts).

% @doc Changes the control credentials for admin by restarting the listener
% with new options.
Expand All @@ -274,22 +268,22 @@ change_admin_creds(Creds) ->
-spec stop_admin_listener() -> 'ok' | {'error', 'not_found' | 'restarting' | 'running' | 'simple_one_for_one'}.
stop_admin_listener() ->
Listeners = rpc(mim(), mongoose_config, get_opt, [listen]),
[{PortIpNet, Module, _Opts}] = lists:filter(fun (Opts) -> is_roles_config(Opts, admin) end, Listeners),
rpc(mim(), ejabberd_listener, stop_listener, [PortIpNet, Module]).
[Opts] = lists:filter(fun (Opts) -> is_roles_config(Opts, admin) end, Listeners),
rpc(mim(), ejabberd_listener, stop_listener, [Opts]).

-spec start_admin_listener(Creds :: {binary(), binary()}) -> {'error', pid()} | {'ok', _}.
start_admin_listener(Creds) ->
Listeners = rpc(mim(), mongoose_config, get_opt, [listen]),
[{PortIpNet, Module, Opts}] = lists:filter(fun (Opts) -> is_roles_config(Opts, admin) end, Listeners),
[Opts] = lists:filter(fun (Opts) -> is_roles_config(Opts, admin) end, Listeners),
NewOpts = insert_creds(Opts, Creds),
rpc(mim(), ejabberd_listener, start_listener, [PortIpNet, Module, NewOpts]).
rpc(mim(), ejabberd_listener, start_listener, [NewOpts]).

insert_creds(Opts, Creds) ->
Modules = proplists:get_value(modules, Opts),
insert_creds(Opts = #{modules := Modules}, Creds) ->
{Host, Path, mongoose_api_admin, PathOpts} = lists:keyfind(mongoose_api_admin, 3, Modules),
NewPathOpts = inject_creds_to_opts(PathOpts, Creds),
NewModules = lists:keyreplace(mongoose_api_admin, 3, Modules, {Host, Path, mongoose_api_admin, NewPathOpts}),
lists:keyreplace(modules, 1, Opts, {modules, NewModules}).
NewModules = lists:keyreplace(mongoose_api_admin, 3, Modules,
{Host, Path, mongoose_api_admin, NewPathOpts}),
Opts#{modules := NewModules}.

inject_creds_to_opts(PathOpts, any) ->
lists:keydelete(auth, 1, PathOpts);
Expand All @@ -305,12 +299,12 @@ inject_creds_to_opts(PathOpts, Creds) ->
% This is determined based on modules used. If there is any mongoose_api_admin module used,
% it is admin config. If not and there is at least one mongoose_api_client* module used,
% it's clients.
is_roles_config({_PortIpNet, ejabberd_cowboy, Opts}, admin) ->
{value, {modules, Modules}} = lists:keysearch(modules, 1, Opts),
is_roles_config(#{module := ejabberd_cowboy, modules := Modules}, admin) ->
lists:any(fun({_, _Path, Mod, _Args}) -> Mod == mongoose_api_admin; (_) -> false end, Modules);
is_roles_config({_PortIpNet, ejabberd_cowboy, Opts}, client) ->
{value, {modules, ModulesConfs}} = lists:keysearch(modules, 1, Opts),
ModulesTokens = lists:map(fun({_, _Path, Mod, _}) -> string:tokens(atom_to_list(Mod), "_"); (_) -> [] end, ModulesConfs),
is_roles_config(#{module := ejabberd_cowboy, modules := Modules}, client) ->
ModulesTokens = lists:map(fun({_, _Path, Mod, _}) -> string:tokens(atom_to_list(Mod), "_");
(_) -> []
end, Modules),
lists:any(fun(["mongoose", "client", "api" | _T]) -> true; (_) -> false end, ModulesTokens);
is_roles_config(_, _) -> false.

Expand Down
11 changes: 5 additions & 6 deletions big_tests/tests/s2s_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
require_rpc_nodes/1,
rpc/4]).

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

-record(s2s_opts, {
Expand All @@ -28,12 +27,14 @@ init_s2s(Config) ->
Node1S2SCertfile = rpc(mim(), mongoose_config, get_opt, [s2s_certfile, undefined]),
Node1S2SUseStartTLS = rpc(mim(), mongoose_config, get_opt, [s2s_use_starttls, undefined]),
Node1S2SPort = ct:get_config({hosts, mim, incoming_s2s_port}),
[Node1S2SListener] = mongoose_helper:get_listener_opts(mim(), Node1S2SPort),
[Node1S2SListener] = mongoose_helper:get_listeners(mim(), #{port => Node1S2SPort,
module => ejabberd_s2s_in}),

Node2S2SCertfile = rpc(fed(), mongoose_config, get_opt, [s2s_certfile, undefined]),
Node2S2SUseStartTLS = rpc(fed(), mongoose_config, get_opt, [s2s_use_starttls, undefined]),
Node2S2SPort = ct:get_config({hosts, fed, incoming_s2s_port}),
[Node2S2SListener] = mongoose_helper:get_listener_opts(fed(), Node2S2SPort),
[Node2S2SListener] = mongoose_helper:get_listeners(fed(), #{port => Node2S2SPort,
module => ejabberd_s2s_in}),
S2S = #s2s_opts{node1_s2s_certfile = Node1S2SCertfile,
node1_s2s_use_starttls = Node1S2SUseStartTLS,
node1_s2s_listener = Node1S2SListener,
Expand Down Expand Up @@ -137,6 +138,4 @@ restart_s2s(#{} = Spec, S2SListener) ->
[rpc(Spec, erlang, exit, [Pid, kill]) ||
{_, Pid, _, _} <- ChildrenIn],

{_PortIPProto, ejabberd_s2s_in, Opts} = S2SListener,
mongoose_helper:restart_listener_with_opts(Spec, S2SListener, Opts).

mongoose_helper:restart_listener(Spec, S2SListener).
11 changes: 5 additions & 6 deletions big_tests/tests/service_domain_db_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
patch_custom/4]).

-import(domain_rest_helper,
[start_listener/0,
start_listener/1,
stop_listener/0]).
[start_listener/1,
stop_listener/1]).

-import(domain_helper, [domain/0]).

Expand Down Expand Up @@ -218,7 +217,7 @@ init_per_group(db, Config) ->
false -> {skip, require_rdbms}
end;
init_per_group(rest_with_auth, Config) ->
start_listener(),
start_listener(#{}),
[{auth_creds, valid}|Config];
init_per_group(rest_without_auth, Config) ->
start_listener(#{skip_auth => true}),
Expand All @@ -232,9 +231,9 @@ init_per_group(GroupName, Config) ->
Config1.

end_per_group(rest_with_auth, _Config) ->
stop_listener();
stop_listener(#{});
end_per_group(rest_without_auth, _Config) ->
stop_listener();
stop_listener(#{skip_auth => true});
end_per_group(_GroupName, Config) ->
case ?config(service_setup, Config) of
per_group -> teardown_service();
Expand Down
4 changes: 2 additions & 2 deletions src/config/mongoose_config_parser_toml.erl
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ handle_default(Path, Value, Spec) ->

-spec handle(path(), toml_value(), mongoose_config_spec:config_node(), [step()]) -> [config_part()].
handle(Path, Value, Spec, Steps) ->
lists:foldl(fun(_, [#{what := _, class := error}] = Error) ->
Error;
lists:foldl(fun(_, [#{what := _, class := error}|_] = Errors) ->
Errors;
(Step, Acc) ->
try_step(Step, Path, Value, Acc, Spec)
end, Value, Steps).
Expand Down
Loading

0 comments on commit 89b9c18

Please sign in to comment.