Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix announcing session establishment feature #3970

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 48 additions & 15 deletions big_tests/tests/mim_c2s_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

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

-import(distributed_helper, [mim/0]).

Expand All @@ -13,15 +16,21 @@

all() ->
[
{group, basic}
{group, basic},
{group, backwards_compatible_session}
].

groups() ->
[
{basic, [parallel],
[
two_users_can_log_and_chat,
too_big_stanza_rejected
too_big_stanza_rejected,
verify_session_establishment_is_not_announced
]},
{backwards_compatible_session, [parallel],
[
verify_session_establishment_is_announced
]}
].

Expand All @@ -30,34 +39,39 @@ groups() ->
%%--------------------------------------------------------------------
init_per_suite(Config) ->
HostType = domain_helper:host_type(),
Steps = [start_stream, stream_features, maybe_use_ssl, authenticate, bind],
Config1 = dynamic_modules:save_modules(HostType, Config),
dynamic_modules:ensure_stopped(HostType, [mod_presence]),
EscalusOverrides = [{initial_activity, fun(_) -> ok end},
{start_ready_clients, fun ?MODULE:escalus_start/2}],
Config1 = save_c2s_listener(Config),
Config2 = dynamic_modules:save_modules(HostType, Config1),
dynamic_modules:ensure_stopped(HostType, [mod_presence]),
Config3 = escalus_users:update_userspec(Config2, alice, connection_steps, Steps),
Config4 = escalus_users:update_userspec(Config3, bob, connection_steps, Steps),
configure_c2s_listener(Config4, #{backwards_compatible_session => false, max_stanza_size => 1024}),
escalus:init_per_suite([{escalus_overrides, EscalusOverrides} | Config4 ]).
escalus:init_per_suite([{escalus_overrides, EscalusOverrides} | Config1 ]).

end_per_suite(Config) ->
restore_c2s_listener(Config),
escalus_fresh:clean(),
dynamic_modules:restore_modules(Config),
mongoose_helper:restore_config(Config),
escalus:end_per_suite(Config).

init_per_group(_, Config) ->
init_per_group(basic, Config) ->
Steps = [start_stream, stream_features, maybe_use_ssl, authenticate, bind],
Config1 = save_c2s_listener(Config),
Config2 = escalus_users:update_userspec(Config1, alice, connection_steps, Steps),
Config3 = escalus_users:update_userspec(Config2, bob, connection_steps, Steps),
configure_c2s_listener(Config3, #{backwards_compatible_session => false, max_stanza_size => 1024}),
Config3;
init_per_group(backwards_compatible_session, Config) ->
Config.

end_per_group(_GroupName, Config) ->
end_per_group(basic, Config) ->
escalus_fresh:clean(),
restore_c2s_listener(Config),
Config;
end_per_group(backwards_compatible_session, Config) ->
escalus_fresh:clean(),
Config.

init_per_testcase(Name, Config) ->
escalus:init_per_testcase(Name, Config).

end_per_testcase(Name, Config) ->
mongoose_helper:restore_config(Config),
escalus:end_per_testcase(Name, Config).

%%--------------------------------------------------------------------
Expand All @@ -80,6 +94,25 @@ too_big_stanza_rejected(Config) ->
escalus:assert(is_stream_end, escalus_client:wait_for_stanza(Alice)),
true = escalus_connection:wait_for_close(Alice, timer:seconds(1)).

verify_session_establishment_is_not_announced(Config) ->
MaybeSessionFeature = start_connection_maybe_get_session_feature(Config),
?assertEqual(undefined, MaybeSessionFeature).

verify_session_establishment_is_announced(Config) ->
MaybeSessionFeature = start_connection_maybe_get_session_feature(Config),
?assertNotEqual(undefined, MaybeSessionFeature).

start_connection_maybe_get_session_feature(Config) ->
Steps = [start_stream, stream_features],
AliceSpec = escalus_fresh:create_fresh_user(Config, alice),
{ok, Client = #client{props = Props}, _} = escalus_connection:start(AliceSpec, Steps),
ok = escalus_auth:auth_plain(Client, Props),
escalus_connection:reset_parser(Client),
Client1 = escalus_session:start_stream(Client),
Features = escalus_connection:get_stanza(Client1, wait_for_features),
escalus_connection:stop(Client1),
exml_query:path(Features, [{element_with_ns, <<"session">>, ?NS_SESSION}]).

%%--------------------------------------------------------------------
%% helpers
%%--------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/c2s/mongoose_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ stream_start_features_before_auth(#c2s_data{host_type = HostType, lserver = LSer
stream_start_features_after_auth(#c2s_data{host_type = HostType, lserver = LServer,
lang = Lang, listener_opts = LOpts} = S) ->
send_header(S, LServer, <<"1.0">>, Lang),
StreamFeatures = mongoose_c2s_stanzas:stream_features_after_auth(HostType, LServer),
StreamFeatures = mongoose_c2s_stanzas:stream_features_after_auth(HostType, LServer, LOpts),
send_element_from_server_jid(S, StreamFeatures),
{next_state, {wait_for_feature_after_auth, ?BIND_RETRIES}, S, state_timeout(LOpts)}.

Expand Down
14 changes: 11 additions & 3 deletions src/c2s/mongoose_c2s_stanzas.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
stream_header/4,
stream_features_before_auth/4,
tls_proceed/0,
stream_features_after_auth/2,
stream_features_after_auth/3,
sasl_success_stanza/1,
sasl_failure_stanza/1,
sasl_challenge_stanza/1,
Expand Down Expand Up @@ -87,11 +87,19 @@ tls_proceed() ->
#xmlel{name = <<"proceed">>,
attrs = [{<<"xmlns">>, ?NS_TLS}]}.

-spec stream_features_after_auth(mongooseim:host_type(), jid:lserver()) -> exml:element().
stream_features_after_auth(HostType, LServer) ->
-spec stream_features_after_auth(mongooseim:host_type(), jid:lserver(), mongoose_listener:options()) ->
exml:element().
stream_features_after_auth(HostType, LServer, #{backwards_compatible_session := false}) ->
Features = [#xmlel{name = <<"bind">>,
attrs = [{<<"xmlns">>, ?NS_BIND}]}
| hook_enabled_features(HostType, LServer)],
stream_features(Features);
stream_features_after_auth(HostType, LServer, #{backwards_compatible_session := true}) ->
Features = [#xmlel{name = <<"session">>,
attrs = [{<<"xmlns">>, ?NS_SESSION}]},
#xmlel{name = <<"bind">>,
attrs = [{<<"xmlns">>, ?NS_BIND}]}
| hook_enabled_features(HostType, LServer)],
stream_features(Features).

hook_enabled_features(HostType, LServer) ->
Expand Down