1: %% Stream Management tests 2: -module(sm_SUITE). 3: 4: -compile([export_all, nowarn_export_all]). 5: -include_lib("exml/include/exml.hrl"). 6: -include_lib("eunit/include/eunit.hrl"). 7: -include_lib("escalus/include/escalus.hrl"). 8: -include_lib("common_test/include/ct.hrl"). 9: 10: %% Injected code callbacks 11: -export([rpc_start_hook_handler/3, 12: rpc_stop_hook_handler/2, 13: hook_handler_fn/3, 14: regression_handler/5]). 15: 16: -export([rpc_start_filter_hook_handler/3, 17: rpc_stop_filter_hook_handler/2, 18: filter_hook_handler_fn/3]). 19: 20: -import(distributed_helper, [mim/0, 21: require_rpc_nodes/1, 22: rpc/4]). 23: 24: -import(domain_helper, [host_type/0]). 25: 26: -import(sm_helper, [connect_fresh/3, 27: connect_fresh/4, 28: connect_spec/2, 29: connect_spec/3, 30: connect_same/2, 31: connect_same/3, 32: connect_resume/2, 33: process_initial_stanza/1, 34: send_initial_presence/1, 35: get_ack/1, 36: ack_initial_presence/1]). 37: 38: -define(MOD_SM, mod_stream_management). 39: -define(CONSTRAINT_CHECK_TIMEOUT, 5000). 40: -define(LONG_TIMEOUT, 3600). 41: -define(SHORT_TIMEOUT, 1). 42: -define(SMALL_SM_BUFFER, 3). 43: -define(PING_REQUEST_TIMEOUT, 1). 44: -define(PING_INTERVAL, 3). 45: 46: %%-------------------------------------------------------------------- 47: %% Suite configuration 48: %%-------------------------------------------------------------------- 49: 50: suite() -> 51: require_rpc_nodes([mim]) ++ escalus:suite(). 52: 53: all() -> 54: ct_helper:groups_to_all(groups()) ++ [ping_timeout]. 55: 56: groups() -> 57: [ 58: {parallel, [parallel], parallel_cases()}, 59: {parallel_manual_ack_freq_1, [parallel], parallel_manual_ack_freq_1_cases()}, 60: {manual_ack_freq_2, [], manual_ack_freq_2_cases()}, 61: {stale_h, [], stale_h_cases()}, 62: {parallel_unacknowledged_message_hook, [parallel], parallel_unacknowledged_message_hook_cases()} 63: ]. 64: 65: parallel_cases() -> 66: [server_announces_sm, 67: server_enables_sm_before_session, 68: server_enables_sm_after_session, 69: server_returns_failed_after_start, 70: server_returns_failed_after_auth, 71: server_enables_resumption, 72: client_enables_sm_twice_fails_with_correct_error_stanza, 73: session_resumed_then_old_session_is_closed_gracefully_with_correct_error_stanza, 74: session_resumed_and_old_session_dead_doesnt_route_error_to_new_session, 75: basic_ack, 76: h_ok_before_session, 77: h_ok_after_session_enabled_before_session, 78: h_ok_after_session_enabled_after_session, 79: h_ok_after_a_chat, 80: h_non_given_closes_stream_gracefully, 81: resend_unacked_on_reconnection, 82: session_established, 83: wait_for_resumption, 84: resume_session, 85: resume_session_with_wrong_h_does_not_leak_sessions, 86: resume_session_with_wrong_sid_returns_item_not_found, 87: resume_session_with_wrong_namespace_is_a_noop, 88: resume_dead_session_results_in_item_not_found, 89: resume_session_kills_old_C2S_gracefully, 90: carboncopy_works, 91: carboncopy_works_after_resume, 92: aggressively_pipelined_resume, 93: replies_are_processed_by_resumed_session, 94: subscription_requests_are_buffered_properly, 95: messages_are_properly_flushed_during_resumption]. 96: 97: parallel_manual_ack_freq_1_cases() -> 98: [client_acks_more_than_sent, 99: too_many_unacked_stanzas, 100: resend_unacked_after_resume_timeout, 101: resume_session_state_send_message_with_ack, 102: resume_session_state_send_message_without_ack, 103: resume_session_state_stop_c2s, 104: server_requests_ack_after_session, 105: resend_more_offline_messages_than_buffer_size, 106: server_requests_ack]. 107: 108: manual_ack_freq_2_cases() -> 109: [server_requests_ack_freq_2]. 110: 111: stale_h_cases() -> 112: [resume_expired_session_returns_correct_h, 113: gc_repeat_after_never_means_no_cleaning, 114: gc_repeat_after_timeout_does_clean]. 115: 116: stream_mgmt_disabled_cases() -> 117: [no_crash_if_stream_mgmt_disabled_but_client_requests_stream_mgmt, 118: no_crash_if_stream_mgmt_disabled_but_client_requests_stream_mgmt_with_resumption]. 119: 120: manual_ack_freq_long_session_timeout_cases() -> 121: [preserve_order]. 122: 123: parallel_unacknowledged_message_hook_cases() -> 124: [unacknowledged_message_hook_bounce, 125: unacknowledged_message_hook_offline, 126: unacknowledged_message_hook_resume, 127: unacknowledged_message_hook_filter]. 128: 129: %%-------------------------------------------------------------------- 130: %% Init & teardown 131: %%-------------------------------------------------------------------- 132: 133: init_per_suite(Config) -> 134: NewConfig = dynamic_modules:save_modules(host_type(), Config), 135: NewConfigWithSM = escalus_users:update_userspec(NewConfig, alice, stream_management, true), 136: mongoose_helper:inject_module(?MODULE), 137: escalus:init_per_suite(NewConfigWithSM). 138: 139: end_per_suite(Config) -> 140: escalus_fresh:clean(), 141: dynamic_modules:restore_modules(Config), 142: escalus:end_per_suite(Config). 143: 144: init_per_group(Group, Config) when Group =:= parallel_unacknowledged_message_hook; 145: Group =:= manual_ack_freq_long_session_timeout; 146: Group =:= parallel_manual_ack_freq_1; 147: Group =:= manual_ack_freq_2 -> 148: dynamic_modules:ensure_modules(host_type(), required_modules(group, Group)), 149: Config; 150: init_per_group(stale_h, Config) -> 151: Config; 152: init_per_group(stream_mgmt_disabled, Config) -> 153: dynamic_modules:stop(host_type(), ?MOD_SM), 154: rpc(mim(), mnesia, delete_table, [sm_session]), 155: Config; 156: init_per_group(Group, Config) -> 157: dynamic_modules:ensure_modules(host_type(), required_modules(group, Group)), 158: Config. 159: 160: end_per_group(_Group, _Config) -> 161: ok. 162: 163: init_per_testcase(resume_expired_session_returns_correct_h = CN, Config) -> 164: dynamic_modules:ensure_modules(host_type(), required_modules(testcase, CN)), 165: escalus:init_per_testcase(CN, Config); 166: init_per_testcase(CN, Config) when CN =:= gc_repeat_after_never_means_no_cleaning; 167: CN =:= gc_repeat_after_timeout_does_clean -> 168: dynamic_modules:ensure_modules(host_type(), required_modules(testcase, CN)), 169: Config2 = register_some_smid_h(Config), 170: escalus:init_per_testcase(CN, Config2); 171: init_per_testcase(ping_timeout = CN, Config) -> 172: ok = rpc(mim(), meck, new, [mod_ping, [passthrough, no_link]]), 173: dynamic_modules:ensure_modules(host_type(), required_modules(testcase, CN)), 174: escalus:init_per_testcase(CN, Config); 175: init_per_testcase(server_requests_ack_freq_2 = CN, Config) -> 176: escalus:init_per_testcase(CN, Config); 177: init_per_testcase(replies_are_processed_by_resumed_session = CN, Config) -> 178: register_handler(), 179: escalus:init_per_testcase(CN, Config); 180: init_per_testcase(CaseName, Config) -> 181: escalus:init_per_testcase(CaseName, Config). 182: 183: end_per_testcase(CN, Config) when CN =:= resume_expired_session_returns_correct_h; 184: CN =:= gc_repeat_after_never_means_no_cleaning; 185: CN =:= gc_repeat_after_timeout_does_clean -> 186: Name = rpc(mim(), gen_mod, get_module_proc, [host_type(), stream_management_stale_h]), 187: rpc(mim(), ejabberd_sup, stop_child, [Name]), 188: escalus:end_per_testcase(CN, Config); 189: end_per_testcase(replies_are_processed_by_resumed_session = CN, Config) -> 190: unregister_handler(), 191: escalus:end_per_testcase(CN, Config); 192: end_per_testcase(ping_timeout = CN, Config) -> 193: rpc(mim(), meck, unload, [mod_ping]), 194: escalus:end_per_testcase(CN, Config); 195: end_per_testcase(CaseName, Config) -> 196: escalus:end_per_testcase(CaseName, Config). 197: 198: %% Module configuration per group (in case of stale_h group it is per testcase) 199: 200: required_modules(Scope, Name) -> 201: SMConfig = case required_sm_opts(Scope, Name) of 202: stopped -> stopped; 203: ExtraOpts -> maps:merge(common_sm_opts(), ExtraOpts) 204: end, 205: Backend = mongoose_helper:mnesia_or_rdbms_backend(), 206: BaseModules = [ 207: {mod_stream_management, config_parser_helper:mod_config(mod_stream_management, SMConfig)}, 208: {mod_offline, config_parser_helper:mod_config(mod_offline, #{backend => Backend})} 209: ], 210: case Name of 211: ping_timeout -> 212: BaseModules ++ [{mod_ping, config_parser_helper:mod_config(mod_ping, mod_ping_opts())}]; 213: _ -> 214: BaseModules 215: end. 216: 217: required_sm_opts(group, parallel) -> 218: #{ack_freq => never}; 219: required_sm_opts(group, parallel_manual_ack_freq_1) -> 220: #{ack_freq => 1, 221: resume_timeout => ?LONG_TIMEOUT}; 222: required_sm_opts(group, manual_ack_freq_2) -> 223: #{ack_freq => 2}; 224: required_sm_opts(group, stream_mgmt_disabled) -> 225: stopped; 226: required_sm_opts(group, parallel_unacknowledged_message_hook) -> 227: #{ack_freq => 1}; 228: required_sm_opts(group, manual_ack_freq_long_session_timeout) -> 229: #{ack_freq => 1, buffer_max => 1000}; 230: required_sm_opts(testcase, resume_expired_session_returns_correct_h) -> 231: #{ack_freq => 1, 232: resume_timeout => ?SHORT_TIMEOUT, 233: stale_h => stale_h(?LONG_TIMEOUT, ?LONG_TIMEOUT)}; 234: required_sm_opts(testcase, gc_repeat_after_never_means_no_cleaning) -> 235: #{stale_h => stale_h(?LONG_TIMEOUT, ?SHORT_TIMEOUT)}; 236: required_sm_opts(testcase, gc_repeat_after_timeout_does_clean) -> 237: #{stale_h => stale_h(?SHORT_TIMEOUT, ?SHORT_TIMEOUT)}; 238: required_sm_opts(testcase, ping_timeout) -> 239: #{ack_freq => 1, 240: resume_timeout => ?SHORT_TIMEOUT}. 241: 242: common_sm_opts() -> 243: Backend = ct_helper:get_internal_database(), 244: #{buffer_max => ?SMALL_SM_BUFFER, backend => Backend}. 245: 246: stale_h(RepeatAfter, Geriatric) -> 247: #{enabled => true, 248: repeat_after => RepeatAfter, 249: geriatric => Geriatric}. 250: 251: make_smid() -> 252: base64:encode(crypto:strong_rand_bytes(21)). 253: 254: register_smid(IntSmidId) -> 255: S = {SMID = make_smid(), IntSmidId}, 256: ok = rpc(mim(), ?MOD_SM, register_stale_smid_h, [host_type(), SMID, IntSmidId]), 257: S. 258: 259: register_some_smid_h(Config) -> 260: TestSmids = lists:map(fun register_smid/1, lists:seq(1, 3)), 261: [{smid_test, TestSmids} | Config]. 262: 263: mod_ping_opts() -> 264: #{send_pings => true, 265: ping_interval => ?PING_INTERVAL, 266: ping_req_timeout => ?PING_REQUEST_TIMEOUT, 267: timeout_action => kill}. 268: 269: %%-------------------------------------------------------------------- 270: %% Tests 271: %%-------------------------------------------------------------------- 272: 273: server_announces_sm(Config) -> 274: AliceSpec = escalus_fresh:freshen_spec(Config, alice), 275: {ok, #client{props = Props}, Features} = escalus_connection:start(AliceSpec, 276: [start_stream]), 277: true = escalus_session:can_use_stream_management(Props, Features). 278: 279: 280: server_enables_sm_before_session(Config) -> 281: connect_fresh(Config, alice, sm_after_bind). 282: 283: server_enables_sm_after_session(Config) -> 284: connect_fresh(Config, alice, sm_after_session). 285: 286: server_returns_failed_after_start(Config) -> 287: Alice = connect_fresh(Config, alice, before_auth), 288: server_returns_failed(Alice). 289: 290: server_returns_failed_after_auth(Config) -> 291: Alice = connect_fresh(Config, alice, auth), 292: server_returns_failed(Alice). 293: 294: server_enables_resumption(Config) -> 295: Alice = connect_fresh(Config, alice, sr_presence), 296: escalus_connection:stop(Alice). 297: 298: server_returns_failed(Alice) -> 299: escalus_connection:send(Alice, escalus_stanza:enable_sm()), 300: escalus:assert(is_sm_failed, [<<"unexpected-request">>], 301: escalus_connection:get_stanza(Alice, enable_sm_failed)). 302: 303: client_enables_sm_twice_fails_with_correct_error_stanza(Config) -> 304: Alice = connect_fresh(Config, alice, sm_before_session), 305: escalus_connection:send(Alice, escalus_stanza:enable_sm()), 306: escalus:assert(is_sm_failed, [<<"unexpected-request">>], 307: escalus_connection:get_stanza(Alice, enable_sm_failed)), 308: escalus:assert(is_stream_end, 309: escalus_connection:get_stanza(Alice, enable_sm_failed)), 310: true = escalus_connection:wait_for_close(Alice, timer:seconds(5)). 311: 312: session_resumed_then_old_session_is_closed_gracefully_with_correct_error_stanza(Config) -> 313: %% GIVEN USER WITH STREAM RESUMPTION ENABLED 314: Alice = connect_fresh(Config, alice, sr_presence), 315: SMH = escalus_connection:get_sm_h(Alice), 316: %% WHEN USER RESUMES SESSION FROM NEW CLIENT 317: Alice2 = connect_resume(Alice, SMH), 318: process_initial_stanza(Alice2), 319: %% THEN: Old session is gracefully closed with the correct error stanza 320: escalus:assert(is_stream_error, [<<"conflict">>, <<>>], 321: escalus_connection:get_stanza(Alice, close_old_stream)), 322: escalus:assert(is_stream_end, 323: escalus_connection:get_stanza(Alice, close_old_stream)), 324: true = escalus_connection:wait_for_close(Alice, timer:seconds(5)), 325: true = escalus_connection:is_connected(Alice2), 326: escalus_connection:stop(Alice2). 327: 328: session_resumed_and_old_session_dead_doesnt_route_error_to_new_session(Config) -> 329: %% GIVEN USER WITH STREAM RESUMPTION ENABLED 330: Alice = connect_fresh(Config, alice, sr_presence), 331: %% WHEN FIRST SESSION DIES AND USER RESUMES FROM NEW CLIENT 332: Alice2 = sm_helper:kill_and_connect_resume(Alice), 333: process_initial_stanza(Alice2), 334: %% THEN new session does not have any message rerouted 335: false = escalus_client:has_stanzas(Alice2), 336: true = escalus_connection:is_connected(Alice2), 337: escalus_connection:stop(Alice2). 338: 339: basic_ack(Config) -> 340: Alice = connect_fresh(Config, alice, sm_after_session), 341: escalus_connection:send(Alice, escalus_stanza:roster_get()), 342: escalus:assert(is_roster_result, 343: escalus_connection:get_stanza(Alice, roster_result)), 344: escalus_connection:send(Alice, escalus_stanza:sm_request()), 345: escalus:assert(is_sm_ack, 346: escalus_connection:get_stanza(Alice, stream_mgmt_ack)). 347: 348: %% Test that "h" value is valid when: 349: %% - SM is enabled *before* the session is established 350: %% - <r/> is sent *before* the session is established 351: h_ok_before_session(Config) -> 352: Alice = connect_fresh(Config, alice, sm_after_bind), 353: escalus_connection:send(Alice, escalus_stanza:sm_request()), 354: escalus:assert(is_sm_ack, [0], 355: escalus_connection:get_stanza(Alice, stream_mgmt_ack)). 356: 357: %% Test that "h" value is valid when: 358: %% - SM is enabled *before* the session is established 359: %% - <r/> is sent *after* the session is established 360: h_ok_after_session_enabled_before_session(Config) -> 361: Alice = connect_fresh(Config, alice, sm_before_session), 362: escalus_connection:send(Alice, escalus_stanza:sm_request()), 363: escalus:assert(is_sm_ack, [1], 364: escalus_connection:get_stanza(Alice, stream_mgmt_ack)). 365: 366: %% Test that "h" value is valid when: 367: %% - SM is enabled *after* the session is established 368: %% - <r/> is sent *after* the session is established 369: h_ok_after_session_enabled_after_session(Config) -> 370: Alice = connect_fresh(Config, alice, sm_after_session), 371: escalus_connection:send(Alice, escalus_stanza:roster_get()), 372: escalus:assert(is_roster_result, 373: escalus_connection:get_stanza(Alice, roster_result)), 374: escalus_connection:send(Alice, escalus_stanza:sm_request()), 375: escalus:assert(is_sm_ack, [1], 376: escalus_connection:get_stanza(Alice, stream_mgmt_ack)). 377: 378: %% Test that "h" value is valid after exchanging a few messages. 379: h_ok_after_a_chat(ConfigIn) -> 380: Config = escalus_users:update_userspec(ConfigIn, alice, 381: stream_management, true), 382: escalus:fresh_story(Config, [{alice,1}, {bob,1}], fun(Alice, Bob) -> 383: escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi, Bob!">>)), 384: escalus:assert(is_chat_message, [<<"Hi, Bob!">>], 385: escalus:wait_for_stanza(Bob)), 386: escalus:send(Bob, escalus_stanza:chat_to(Alice, <<"Hi, Alice!">>)), 387: escalus:assert(is_chat_message, [<<"Hi, Alice!">>], 388: escalus:wait_for_stanza(Alice)), 389: escalus:send(Bob, escalus_stanza:chat_to(Alice, <<"How's life?">>)), 390: escalus:assert(is_chat_message, [<<"How's life?">>], 391: escalus:wait_for_stanza(Alice)), 392: escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"Pretty !@#$%^$">>)), 393: escalus:assert(is_chat_message, [<<"Pretty !@#$%^$">>], 394: escalus:wait_for_stanza(Bob)), 395: escalus:send(Alice, escalus_stanza:sm_request()), 396: escalus:assert(is_sm_ack, [3], escalus:wait_for_stanza(Alice)), 397: %% Ack, so that unacked messages don't go into offline store. 398: escalus:send(Alice, escalus_stanza:sm_ack(3)) 399: end). 400: 401: h_non_given_closes_stream_gracefully(ConfigIn) -> 402: AStanza = #xmlel{name = <<"a">>, 403: attrs = [{<<"xmlns">>, <<"urn:xmpp:sm:3">>}]}, 404: Config = escalus_users:update_userspec(ConfigIn, alice, 405: stream_management, true), 406: escalus:fresh_story(Config, [{alice,1}], fun(Alice) -> 407: C2SPid = mongoose_helper:get_session_pid(Alice), 408: escalus:send(Alice, AStanza), 409: escalus:assert(is_stream_error, 410: [<<"policy-violation">>, <<>>], 411: escalus:wait_for_stanza(Alice)), 412: mongoose_helper:wait_for_pid_to_die(C2SPid), 413: escalus:assert(is_stream_end, escalus_connection:get_stanza(Alice, stream_end)), 414: true = escalus_connection:wait_for_close(Alice, timer:seconds(5)) 415: end). 416: 417: client_acks_more_than_sent(Config) -> 418: Alice = connect_fresh(Config, alice, sm_after_session), 419: escalus:send(Alice, escalus_stanza:sm_ack(5)), 420: StreamErrorStanza = escalus:wait_for_stanza(Alice), 421: %% Assert "undefined-condition" children 422: escalus:assert(is_stream_error, [<<"undefined-condition">>, <<>>], StreamErrorStanza), 423: %% Assert "handled-count-too-high" children with correct attributes 424: HandledCountSubElement = exml_query:path(StreamErrorStanza, 425: [{element_with_ns, 426: <<"handled-count-too-high">>, 427: <<"urn:xmpp:sm:3">>}]), 428: <<"5">> = exml_query:attr(HandledCountSubElement, <<"h">>), 429: <<"0">> = exml_query:attr(HandledCountSubElement, <<"send-count">>), 430: %% Assert graceful stream end 431: escalus:assert(is_stream_end, escalus_connection:get_stanza(Alice, stream_end)), 432: true = escalus_connection:wait_for_close(Alice, timer:seconds(5)). 433: 434: too_many_unacked_stanzas(Config) -> 435: Bob = connect_fresh(Config, bob, presence), 436: Alice = connect_fresh(Config, alice, sm_presence, manual), 437: get_ack(Alice), 438: [escalus:send(Bob, escalus_stanza:chat_to(Alice, 439: <<(integer_to_binary(N))/binary, ": Hi, Alice!">>)) 440: || N <- lists:seq(1,?SMALL_SM_BUFFER)], 441: escalus:wait_for_stanzas(Alice, ?SMALL_SM_BUFFER * 2), % messages and ack requests 442: escalus:assert(is_stream_error, [<<"resource-constraint">>, 443: <<"too many unacked stanzas">>], 444: %% wait for deferred buffer check 445: escalus:wait_for_stanza(Alice, ?CONSTRAINT_CHECK_TIMEOUT + 1000)). 446: 447: server_requests_ack(Config) -> 448: server_requests_ack(Config, 1). 449: 450: server_requests_ack_freq_2(Config) -> 451: server_requests_ack(Config, 2). 452: 453: server_requests_ack(Config, N) -> 454: Bob = connect_fresh(Config, bob, presence), 455: Alice = connect_fresh(Config, alice, sm_presence, manual), 456: %% ack request after initial presence 457: maybe_assert_ack_request(1, N, Alice), 458: escalus:send(Bob, escalus_stanza:chat_to(Alice, <<"Hi, Alice!">>)), 459: escalus:assert(is_chat_message, [<<"Hi, Alice!">>], 460: escalus:wait_for_stanza(Alice)), 461: maybe_assert_ack_request(2, N, Alice). 462: 463: maybe_assert_ack_request(StanzasRec, AckRequests, Alice) -> 464: ct:log("StanzasRec: ~p, AckRequests: ~p", [StanzasRec, AckRequests]), 465: case StanzasRec rem AckRequests of 466: 0 -> 467: escalus:assert(is_sm_ack_request, escalus:wait_for_stanza(Alice)); 468: _ -> 469: ok 470: end, 471: StanzasRec. 472: 473: server_requests_ack_after_session(Config) -> 474: Alice = connect_fresh(Config, alice, sm_before_session, manual), 475: escalus:assert(is_sm_ack_request, escalus_connection:get_stanza(Alice, stream_mgmt_req)). 476: 477: resend_more_offline_messages_than_buffer_size(Config) -> 478: %% connect bob and alice 479: Bob = connect_fresh(Config, bob, presence), 480: AliceSpec = escalus_fresh:create_fresh_user(Config, alice), 481: 482: % sent some messages - more than unacked buffer size 483: MessagesToSend = ?SMALL_SM_BUFFER + 1, 484: AliceJid = common_helper:get_bjid(AliceSpec), 485: [escalus_connection:send(Bob, escalus_stanza:chat_to(AliceJid, integer_to_binary(I))) 486: || I <- lists:seq(1, MessagesToSend)], 487: mongoose_helper:wait_for_n_offline_messages(AliceJid, MessagesToSend), 488: 489: % connect alice who wants to receive all messages from offline storage 490: Alice = connect_spec(AliceSpec, sm_after_session, manual), 491: send_initial_presence(Alice), 492: escalus:wait_for_stanzas(Alice, MessagesToSend * 2), %messages and ack requests 493: 494: escalus_connection:get_stanza(Alice, presence), 495: get_ack(Alice), % ack request 496: 497: % confirm messages + presence 498: escalus_connection:send(Alice, escalus_stanza:sm_ack(4)), 499: % wait for check constraint message on server side 500: 501: ct:sleep(?CONSTRAINT_CHECK_TIMEOUT + 1000), 502: false = escalus_client:has_stanzas(Alice), 503: % should not receive anything especially any stream errors 504: 505: escalus_connection:stop(Alice), 506: escalus_connection:stop(Bob). 507: 508: resend_unacked_on_reconnection(Config) -> 509: Texts = three_texts(), 510: Bob = connect_fresh(Config, bob, presence), 511: Alice = connect_fresh(Config, alice, sm_presence), 512: AliceSpec = sm_helper:client_to_spec0(Alice), 513: %% Bob sends some messages to Alice. 514: sm_helper:send_messages(Bob, Alice, Texts), 515: %% Alice receives the messages. 516: sm_helper:wait_for_messages(Alice, Texts), 517: %% Alice disconnects without acking the messages. 518: sm_helper:stop_client_and_wait_for_termination(Alice), 519: %% Messages go to the offline store. 520: %% Alice receives the messages from the offline store. 521: NewAlice = connect_spec(AliceSpec, session, manual), 522: send_initial_presence(NewAlice), 523: sm_helper:wait_for_messages(NewAlice, Texts), 524: %% Alice acks the delayed messages so they won't go again 525: %% to the offline store. 526: escalus_connection:send(NewAlice, escalus_stanza:sm_ack(3)). 527: 528: %% Remove wait_for_n_offline_messages and you will get anything, but preserve_order 529: %% TODO Test without wait_for_n_offline_messages. It would require changes in SM 530: %% and more strict tests, reproducing delays in SM and in mod_offline. 531: preserve_order(Config) -> 532: %% connect bob and alice 533: Bob = connect_fresh(Config, bob, presence), 534: Alice = connect_fresh(Config, alice, sr_presence, manual), 535: AliceSpec = sm_helper:client_to_spec(Alice), 536: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"1">>)), 537: 538: %% kill alice connection 539: escalus_connection:kill(Alice), 540: C2SPid = mongoose_helper:get_session_pid(Alice), 541: sm_helper:wait_until_resume_session(C2SPid), 542: 543: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"2">>)), 544: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"3">>)), 545: 546: NewAlice = connect_spec(AliceSpec, session, manual), 547: escalus_connection:send(NewAlice, escalus_stanza:enable_sm([resume])), 548: 549: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"4">>)), 550: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"5">>)), 551: 552: %% Without this check we will get stuff out of order 553: mongoose_helper:wait_for_n_offline_messages(NewAlice, 5), 554: 555: send_initial_presence(NewAlice), 556: %% Without this check we can get "6, 1, 2, 3, 4, 5" messages in the next receive_all_ordered 557: mongoose_helper:wait_for_n_offline_messages(NewAlice, 0), 558: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"6">>)), 559: 560: %% "2, 3, 4, 5, 6, 1" is possible (where only 1 is from offline storage, the rest is from sm) 561: receive_all_ordered(NewAlice, 6), 562: 563: % replace connection 564: NewAlice2 = connect_spec(AliceSpec, session, manual), 565: % allow messages to go to the offline storage 566: mongoose_helper:wait_for_n_offline_messages(NewAlice, 6), 567: 568: send_initial_presence(NewAlice2), 569: 570: % receves messages in correct order 571: receive_all_ordered(NewAlice2, 6), 572: 573: escalus_connection:stop(Bob), 574: escalus_connection:stop(NewAlice2). 575: 576: receive_all_ordered(Conn, Last) -> 577: receive_all_ordered(Conn, 1, Last, []). 578: 579: %% Receive messages from N to Last. 580: %% Ignores acks and presences. 581: %% Handles case when out of order and when not enough stanzas. 582: receive_all_ordered(_Conn, N, Last, Acc) when N > Last -> 583: Texts = lists:map(fun integer_to_binary/1, lists:seq(1, Last)), 584: sm_helper:assert_messages(Acc, Texts); 585: receive_all_ordered(Conn, N, Last, Acc) -> 586: Stanzas = escalus:wait_for_stanzas(Conn, 1), 587: case Stanzas of 588: [#xmlel{name = <<"message">>}] -> 589: receive_all_ordered(Conn, N + 1, Last, Acc ++ Stanzas); 590: [_] -> %% Ack or presence 591: receive_all_ordered(Conn, N, Last, Acc); 592: [] -> 593: ct:fail({timeout_waiting, N, Acc}) 594: end. 595: 596: resend_unacked_after_resume_timeout(Config) -> 597: %% connect bob and alice 598: Bob = connect_fresh(Config, bob, presence), 599: Alice = connect_fresh(Config, alice, sr_presence), 600: AliceSpec = sm_helper:client_to_spec(Alice), 601: 602: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"msg-1">>)), 603: %% kill alice connection 604: escalus_connection:kill(Alice), 605: 606: %% ensure there is no session 607: C2SPid = mongoose_helper:get_session_pid(Alice), 608: sm_helper:wait_until_resume_session(C2SPid), 609: 610: %% alice come back and receives unacked message 611: NewAlice = connect_spec(AliceSpec, session), 612: send_initial_presence(NewAlice), 613: 614: escalus_new_assert:mix_match([is_presence, is_chat(<<"msg-1">>)], 615: escalus:wait_for_stanzas(NewAlice, 2)), 616: 617: escalus_connection:stop(Bob), 618: escalus_connection:stop(NewAlice). 619: 620: ping_timeout(Config) -> 621: %% make sure there are no leftover stanzas in the history 622: ?assertEqual([], get_stanzas_filtered_by_mod_ping()), 623: 624: %% connect Alice and wait for the session to close 625: Alice = connect_fresh(Config, alice, sr_presence), 626: 627: escalus_client:wait_for_stanza(Alice), 628: ct:sleep(?PING_REQUEST_TIMEOUT + ?PING_INTERVAL + timer:seconds(1)), 629: 630: %% attempt to resume the session after the connection drop 631: NewAlice = sm_helper:kill_and_connect_with_resume_session_without_waiting_for_result(Alice), 632: 633: %% after resume_timeout, we expect the session to be closed 634: escalus_connection:get_stanza(NewAlice, failed_resumption), 635: 636: %% bind a new session and expect unacknowledged messages to be resent 637: escalus_session:session(escalus_session:bind(NewAlice)), 638: send_initial_presence(NewAlice), 639: 640: %% check if the error stanza was handled by mod_ping 641: [Stanza] = get_stanzas_filtered_by_mod_ping(), 642: escalus:assert(is_iq_error, Stanza), 643: ?assertNotEqual(undefined, 644: exml_query:subelement_with_name_and_ns(Stanza, <<"ping">>, <<"urn:xmpp:ping">>)), 645: 646: %% stop the connection 647: escalus_connection:stop(NewAlice). 648: 649: resume_expired_session_returns_correct_h(Config) -> 650: %% connect bob and alice 651: Bob = connect_fresh(Config, bob, sr_presence), 652: Alice = connect_fresh(Config, alice, sr_presence, manual), 653: get_ack(Alice), 654: 655: %% Bob sends a message to Alice, and Alice receives it but doesn't acknowledge 656: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"msg-1">>)), 657: escalus:assert(is_chat_message, [<<"msg-1">>], escalus:wait_for_stanza(Alice)), 658: %% alice comes back, but too late, so resumption doesn't work, 659: %% but she receives the previous h = 1 anyway 660: %% NewAlice is also manual ack 661: NewAlice = sm_helper:kill_and_connect_with_resume_session_without_waiting_for_result(Alice), 662: FailedResumption = escalus_connection:get_stanza(NewAlice, failed_resumption), 663: <<"1">> = exml_query:attr(FailedResumption, <<"h">>), 664: %% And we can continue with bind and session 665: escalus_session:session(escalus_session:bind(NewAlice)), 666: send_initial_presence(NewAlice), 667: Stanzas = [escalus_connection:get_stanza(NewAlice, {msg, 1}), 668: escalus_connection:get_stanza(NewAlice, {msg, 2})], 669: escalus_new_assert:mix_match([is_presence, is_chat(<<"msg-1">>)], Stanzas), 670: escalus_connection:stop(Bob), 671: escalus_connection:stop(NewAlice). 672: 673: gc_repeat_after_never_means_no_cleaning(Config) -> 674: [{SMID1, _}, {SMID2, _}, {SMID3, _}] = ?config(smid_test, Config), 675: {stale_h, 1} = rpc(mim(), ?MOD_SM, get_session_from_smid, [host_type(), SMID1]), 676: {stale_h, 2} = rpc(mim(), ?MOD_SM, get_session_from_smid, [host_type(), SMID2]), 677: {stale_h, 3} = rpc(mim(), ?MOD_SM, get_session_from_smid, [host_type(), SMID3]). 678: 679: gc_repeat_after_timeout_does_clean(Config) -> 680: [{SMID1, _} | _ ] = ?config(smid_test, Config), 681: mongoose_helper:wait_until(fun() -> 682: rpc(mim(), ?MOD_SM, get_stale_h, [host_type(), SMID1]) 683: end, 684: {error, smid_not_found}, 685: #{name => smid_garbage_collected}). 686: 687: resume_session_state_send_message_without_ack(Config) -> 688: resume_session_state_send_message_generic(Config, no_ack). 689: 690: resume_session_state_send_message_with_ack(Config) -> 691: resume_session_state_send_message_generic(Config, ack). 692: 693: resume_session_state_send_message_generic(Config, AckInitialPresence) -> 694: %% connect bob and alice 695: Bob = connect_fresh(Config, bob, presence), 696: Alice = connect_fresh(Config, alice, sr_presence, manual), 697: maybe_ack_initial_presence(Alice, AckInitialPresence), 698: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"msg-1">>)), 699: %% kill alice connection 700: C2SPid = mongoose_helper:get_session_pid(Alice), 701: escalus_connection:kill(Alice), 702: sm_helper:wait_until_resume_session(C2SPid), 703: sm_helper:assert_alive_resources(Alice, 1), 704: 705: %% send some messages and check if c2s can handle it 706: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"msg-2">>)), 707: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"msg-3">>)), 708: %% suspend the process to ensure that Alice has enough time to reconnect, 709: %% before resumption timeout occurs. 710: ok = rpc(mim(), sys, suspend, [C2SPid]), 711: 712: %% alice comes back and receives unacked message 713: NewAlice = connect_same(Alice, presence), 714: %% now we can resume c2s process of the old connection 715: %% and let it process session resumption timeout 716: ok = rpc(mim(), sys, resume, [C2SPid]), 717: Stanzas = escalus:wait_for_stanzas(NewAlice, 3), 718: 719: % what about order ? 720: % alice receive presence from herself and 3 unacked messages from bob 721: escalus_new_assert:mix_match([is_chat(<<"msg-1">>), 722: is_chat(<<"msg-2">>), 723: is_chat(<<"msg-3">>)], 724: Stanzas), 725: escalus_connection:stop(Bob), 726: escalus_connection:stop(NewAlice). 727: 728: %%for instance it can be done by mod ping 729: resume_session_state_stop_c2s(Config) -> 730: Bob = connect_fresh(Config, bob, presence), 731: Alice = connect_fresh(Config, alice, sr_presence, manual), 732: 733: get_ack(Alice), 734: ack_initial_presence(Alice), 735: 736: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"msg-1">>)), 737: escalus:assert(is_chat_message, [<<"msg-1">>], escalus_connection:get_stanza(Alice, msg)), 738: 739: %% get pid of c2s 740: C2SPid = mongoose_helper:get_session_pid(Alice), 741: %% Wait c2s process to process our presence ack. 742: %% Otherwise, we can receive two initial presences sometimes. 743: sm_helper:wait_for_c2s_unacked_count(C2SPid, 1), 744: 745: % kill alice connection 746: escalus_connection:kill(Alice), 747: % session should be alive 748: sm_helper:assert_alive_resources(Alice, 1), 749: rpc(mim(), mongoose_c2s, stop, [C2SPid, normal]), 750: sm_helper:wait_until_resume_session(C2SPid), 751: %% suspend the process to ensure that Alice has enough time to reconnect, 752: %% before resumption timeout occurs. 753: ok = rpc(mim(), sys, suspend, [C2SPid]), 754: 755: %% alice comes back and receives unacked message 756: NewAlice = connect_same(Alice, presence, manual), 757: %% now we can resume c2s process of the old connection 758: %% and let it process session resumption timeout 759: ok = rpc(mim(), sys, resume, [C2SPid]), 760: 761: escalus:assert(is_chat_message, [<<"msg-1">>], escalus_connection:get_stanza(NewAlice, msg)), 762: escalus_connection:stop(Bob), 763: escalus_connection:stop(NewAlice). 764: 765: %% This test only verifies the validity of helpers (get_session_pid, 766: %% get_c2s_state_name) written for wait_for_resumption 767: %% testcase. 768: session_established(Config) -> 769: Alice = connect_fresh(Config, alice, presence), 770: C2SPid = mongoose_helper:get_session_pid(Alice), 771: session_established = mongoose_helper:get_c2s_state_name(C2SPid), 772: escalus_connection:stop(Alice). 773: 774: %% Ensure that after a violent disconnection, 775: %% the c2s waits for resumption (but don't resume yet). 776: wait_for_resumption(Config) -> 777: AliceSpec = escalus_fresh:create_fresh_user(Config, alice), 778: Bob = connect_fresh(Config, bob, session), 779: Texts = three_texts(), 780: {C2SPid, _} = buffer_unacked_messages_and_die(Config, AliceSpec, Bob, Texts), 781: sm_helper:wait_until_resume_session(C2SPid). 782: 783: unacknowledged_message_hook_filter(Config) -> 784: FilterText = <<"filter">>, 785: Bob = connect_fresh(Config, bob, presence), 786: AliceSpec = escalus_fresh:create_fresh_user(Config, alice), 787: Resource = proplists:get_value(username, AliceSpec), 788: HookHandlerExtra = start_filter_hook_listener(FilterText, Resource), 789: Alice = connect_spec([{resource, Resource} | AliceSpec], sr_presence, manual), 790: %% Ack the presence stanza 791: get_ack(Alice), 792: ack_initial_presence(Alice), 793: Messages = [<<"msg-1">>, <<"msg-2">>, <<"msg-3">>, <<"msg-4">>], 794: All = [<<"msg-1">>, FilterText, <<"msg-2">>, FilterText, <<"msg-3">>, <<"msg-4">>], 795: [ escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, Body)) || Body <- All ], 796: %% kill alice connection 797: C2SPid = mongoose_helper:get_session_pid(Alice), 798: escalus_connection:kill(Alice), 799: sm_helper:wait_until_resume_session(C2SPid), 800: sm_helper:assert_alive_resources(Alice, 1), 801: %% ensure second C2S is registered so all the messages are bounced properly 802: NewAlice = connect_spec([{resource, <<"2">>}| AliceSpec], sr_presence, manual), 803: send_initial_presence(NewAlice), 804: sm_helper:wait_for_resource_count(NewAlice, 2), 805: ok = rpc(mim(), sys, terminate, [C2SPid, normal]), 806: %% verify that the filtered message is never received 807: verify_no_receive_filtertext(NewAlice, FilterText, Messages), 808: stop_hook_listener(HookHandlerExtra), 809: escalus_connection:stop(Bob). 810: 811: verify_no_receive_filtertext(_, _, []) -> 812: ok; 813: verify_no_receive_filtertext(Client, FilterText, Messages) -> 814: case escalus:wait_for_stanzas(Client, 1, 500) of 815: [] -> ct:fail("Messages pending: ~p~n", [Messages]); 816: [St] -> 817: case exml_query:path(St, [{element, <<"body">>}, cdata]) of 818: undefined -> 819: verify_no_receive_filtertext(Client, FilterText, Messages); 820: FilterText -> 821: ct:fail("Composing forwarded from a different c2s process"); 822: Message -> 823: Pred = fun(Msg) -> Msg =/= Message end, 824: {Pending, _} = lists:partition(Pred, Messages), 825: verify_no_receive_filtertext(Client, FilterText, Pending) 826: end 827: end. 828: 829: unacknowledged_message_hook_resume(Config) -> 830: unacknowledged_message_hook_common(fun unacknowledged_message_hook_resume/4, Config). 831: 832: unacknowledged_message_hook_resume(AliceSpec, Resource, SMID, _C2SPid) -> 833: NewAlice = connect_spec(AliceSpec, {resume, SMID, 1}, manual), 834: send_initial_presence(NewAlice), 835: {Resource, NewAlice}. 836: 837: unacknowledged_message_hook_bounce(Config) -> 838: unacknowledged_message_hook_common(fun unacknowledged_message_hook_bounce/4, Config). 839: 840: unacknowledged_message_hook_bounce(AliceSpec, Resource, _SMID, C2SPid) -> 841: NewResource = <<"new_", Resource/binary>>, 842: NewSpec = lists:keystore(resource, 1, AliceSpec, {resource, NewResource}), 843: NewAlice = connect_spec(NewSpec, sr_session, manual), 844: send_initial_presence(NewAlice), 845: %% ensure second C2S is registered so all the messages are bounced properly 846: sm_helper:wait_for_resource_count(NewAlice, 2), 847: ok = rpc(mim(), sys, terminate, [C2SPid, normal]), 848: {NewResource, NewAlice}. 849: 850: unacknowledged_message_hook_offline(Config) -> 851: unacknowledged_message_hook_common(fun unacknowledged_message_hook_offline/4, Config). 852: 853: unacknowledged_message_hook_offline(AliceSpec, Resource, _SMID, C2SPid) -> 854: C2SRef = erlang:monitor(process, C2SPid), 855: sm_helper:wait_for_process_termination(C2SRef), 856: %% reset the session, so old C2S process is stopped 857: NewAlice = connect_spec(AliceSpec, sr_session, manual), 858: %% wait for old C2S termination before send presence. other way 859: %% some of the latest unacknowledged messages can be bounced to 860: %% the new C2S process instead of going to the mod_offline storage. 861: %% looks like all the unacknowledged messages arrive to the new 862: %% C2S, but the message sequence is broken (the bounced messages 863: %% delivered before the messages from the mod_offline storage) 864: send_initial_presence(NewAlice), 865: {Resource, NewAlice}. 866: 867: unacknowledged_message_hook_common(RestartConnectionFN, Config) -> 868: %% connect bob and alice 869: Bob = connect_fresh(Config, bob, presence), 870: 871: AliceSpec0 = escalus_fresh:create_fresh_user(Config, alice), 872: Resource = proplists:get_value(username, AliceSpec0), 873: AliceSpec = [{resource, Resource} | AliceSpec0], 874: HookHandlerExtra = start_hook_listener(Resource), 875: Alice = connect_spec(AliceSpec, sr_presence, manual), 876: %% Ack the presence stanza 877: get_ack(Alice), 878: ack_initial_presence(Alice), 879: 880: SMID = sm_helper:client_to_smid(Alice), 881: 882: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"msg-1">>)), 883: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"msg-2">>)), 884: %% kill alice connection 885: C2SPid = mongoose_helper:get_session_pid(Alice), 886: escalus_connection:kill(Alice), 887: sm_helper:wait_until_resume_session(C2SPid), 888: sm_helper:assert_alive_resources(Alice, 1), 889: 890: escalus:assert(is_chat_message, [<<"msg-1">>], wait_for_unacked_msg_hook(0, Resource, 100)), 891: escalus:assert(is_chat_message, [<<"msg-2">>], wait_for_unacked_msg_hook(0, Resource, 100)), 892: ?assertEqual(timeout, wait_for_unacked_msg_hook(0, Resource, 100)), 893: 894: %% send some messages and check if c2s can handle it 895: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"msg-3">>)), 896: escalus_connection:send(Bob, escalus_stanza:chat_to_short_jid(Alice, <<"msg-4">>)), 897: escalus:assert(is_chat_message, [<<"msg-3">>], wait_for_unacked_msg_hook(0, Resource, 100)), 898: escalus:assert(is_chat_message, [<<"msg-4">>], wait_for_unacked_msg_hook(0, Resource, 100)), 899: ?assertEqual(timeout, wait_for_unacked_msg_hook(0, Resource, 100)), 900: 901: %% alice comes back and receives unacked message 902: {NewResource, NewAlice} = RestartConnectionFN(AliceSpec, Resource, SMID, C2SPid), 903: 904: mongoose_helper:wait_until( 905: fun() -> 906: Stanza = escalus_connection:get_stanza(NewAlice, msg), 907: escalus:assert(is_chat_message, [<<"msg-4">>], Stanza), 908: ok 909: end, ok), 910: 911: NewC2SPid = mongoose_helper:get_session_pid(NewAlice), 912: escalus_connection:kill(NewAlice), 913: sm_helper:wait_until_resume_session(NewC2SPid), 914: 915: escalus:assert(is_chat_message, [<<"msg-1">>], wait_for_unacked_msg_hook(1, NewResource, 100)), 916: escalus:assert(is_chat_message, [<<"msg-2">>], wait_for_unacked_msg_hook(1, NewResource, 100)), 917: escalus:assert(is_chat_message, [<<"msg-3">>], wait_for_unacked_msg_hook(1, NewResource, 100)), 918: escalus:assert(is_chat_message, [<<"msg-4">>], wait_for_unacked_msg_hook(1, NewResource, 100)), 919: ?assertEqual(timeout, wait_for_unacked_msg_hook(0, Resource, 100)), 920: stop_hook_listener(HookHandlerExtra), 921: escalus_connection:stop(Bob). 922: 923: resume_session(Config) -> 924: AliceSpec = escalus_fresh:create_fresh_user(Config, alice), 925: Texts = three_texts(), 926: escalus:fresh_story(Config, [{bob, 1}], fun(Bob) -> 927: {_, SMID} = buffer_unacked_messages_and_die(Config, AliceSpec, Bob, Texts), 928: %% Resume the session. 929: Alice = connect_spec(AliceSpec, {resume, SMID, 1}, manual), 930: %% Alice receives the unacked messages from the previous 931: %% interrupted session. 932: sm_helper:wait_for_messages(Alice, Texts), 933: %% Alice acks the received messages. 934: escalus_connection:send(Alice, escalus_stanza:sm_ack(5)), 935: escalus_connection:stop(Alice) 936: end). 937: 938: resume_session_with_wrong_h_does_not_leak_sessions(Config) -> 939: AliceSpec = escalus_fresh:create_fresh_user(Config, alice), 940: Messages = three_texts(), 941: HostType = host_type(), 942: escalus:fresh_story(Config, [{bob, 1}], fun(Bob) -> 943: {_, SMID} = buffer_unacked_messages_and_die(Config, AliceSpec, Bob, Messages), 944: %% Resume the session. 945: Alice = connect_spec(AliceSpec, auth, manual), 946: Resumed = sm_helper:try_to_resume_stream(Alice, SMID, 30), 947: escalus:assert(is_stream_error, [<<"undefined-condition">>, <<>>], Resumed), 948: escalus_connection:wait_for_close(Alice, timer:seconds(5)), 949: Fun = fun() -> 950: [] = sm_helper:get_user_present_resources(Alice), 951: sm_helper:get_sid_by_stream_id(HostType, SMID) 952: end, 953: mongoose_helper:wait_until(Fun, {error, smid_not_found}, #{name => smid_is_cleaned}) 954: end). 955: 956: resume_session_with_wrong_sid_returns_item_not_found(Config) -> 957: session_resumption_expects_item_not_found(Config, <<"wrong-sid">>). 958: 959: resume_session_with_wrong_namespace_is_a_noop(Config) -> 960: Alice = connect_fresh(Config, alice, auth), 961: #xmlel{attrs = Attrs} = Resume = escalus_stanza:resume(<<"doesnt_matter">>, 4), 962: Attrs2 = lists:keyreplace(<<"xmlns">>, 1, Attrs, {<<"xmlns">>, <<"not-stream-mgnt">>}), 963: escalus_connection:send(Alice, Resume#xmlel{attrs = Attrs2}), 964: escalus_assert:has_no_stanzas(Alice), 965: [] = sm_helper:get_user_present_resources(Alice), 966: true = escalus_connection:is_connected(Alice), 967: escalus_connection:stop(Alice). 968: 969: resume_dead_session_results_in_item_not_found(Config) -> 970: SMID = base64:encode(crypto:strong_rand_bytes(21)), 971: SID = {os:timestamp(), undefined}, 972: HostType = host_type(), 973: rpc(mim(), ?MOD_SM, register_smid, [HostType, SMID, SID]), 974: session_resumption_expects_item_not_found(Config, SMID). 975: 976: session_resumption_expects_item_not_found(Config, SMID) -> 977: Alice = connect_fresh(Config, alice, auth), 978: Resumed = sm_helper:try_to_resume_stream(Alice, SMID, 2), 979: escalus:assert(is_sm_failed, [<<"item-not-found">>], Resumed), 980: [] = sm_helper:get_user_present_resources(Alice), 981: true = escalus_connection:is_connected(Alice), 982: escalus_connection:stop(Alice). 983: 984: resume_session_kills_old_C2S_gracefully(Config) -> 985: Alice = connect_fresh(Config, alice, sr_presence, manual), 986: C2SPid = mongoose_helper:get_session_pid(Alice), 987: 988: %% Monitor the C2S process and disconnect Alice. 989: MonitorRef = sm_helper:monitor_session(Alice), 990: escalus_client:kill_connection(Config, Alice), 991: 992: %% Ensure the c2s process is waiting for resumption. 993: sm_helper:wait_until_resume_session(C2SPid), 994: 995: %% Resume the session. 996: NewAlice = connect_resume(Alice, 1), 997: 998: %% C2S process should die gracefully with Reason=normal. 999: sm_helper:wait_for_process_termination(MonitorRef), 1000: escalus_connection:stop(NewAlice). 1001: 1002: carboncopy_works(Config) -> 1003: escalus:fresh_story(Config, [{alice, 2}, {bob, 1}], fun(Alice1, Alice, Bob) -> 1004: mongoose_helper:enable_carbons([Alice1, Alice]), 1005: escalus_connection:send(Bob, escalus_stanza:chat_to(Alice1, <<"msg-4">>)), 1006: sm_helper:wait_for_messages(Alice1, [<<"msg-4">>]), 1007: carboncopy_helper:wait_for_carbon_chat_with_body(Alice, <<"msg-4">>, #{from => Bob, to => Alice1}) 1008: end). 1009: 1010: carboncopy_works_after_resume(Config) -> 1011: Texts = three_texts(), 1012: escalus:fresh_story(Config, [{alice, 1}, {bob, 1}], fun(Alice1, Bob) -> 1013: AliceSpec = [{resource, <<"res2">>} | sm_helper:client_to_spec(Alice1)], 1014: F = fun(Alice2) -> 1015: [escalus:assert(is_presence_with_type, [<<"available">>], escalus:wait_for_stanza(A)) || A <- [Alice1, Alice2]], 1016: mongoose_helper:enable_carbons([Alice1, Alice2]) 1017: end, 1018: {_, SMID} = buffer_unacked_messages_and_die(Config, AliceSpec, Bob, Texts, F), 1019: %% Resume the session. 1020: Alice = connect_spec(AliceSpec, {resume, SMID, 1}, manual), 1021: wait_for_carbon_with_bodies(Alice1, Texts, #{from => Bob, to => Alice}), 1022: %% Get a presence from Alice1 again 1023: escalus:assert(is_presence_with_type, [<<"available">>], escalus:wait_for_stanza(Alice)), 1024: %% Alice receives an IQ result from the carbon copy enable request 1025: escalus:assert(is_iq_result, [], escalus:wait_for_stanza(Alice)), 1026: %% Alice receives the unacked messages from the previous 1027: %% interrupted session. 1028: sm_helper:wait_for_messages(Alice, Texts), 1029: %% Alice acks the received messages. 1030: escalus_connection:send(Alice, escalus_stanza:sm_ack(5)), 1031: %% Direct send 1032: escalus_connection:send(Bob, escalus_stanza:chat_to(Alice1, <<"msg-4">>)), 1033: sm_helper:wait_for_messages(Alice1, [<<"msg-4">>]), 1034: carboncopy_helper:wait_for_carbon_chat_with_body(Alice, <<"msg-4">>, #{from => Bob, to => Alice1}), 1035: escalus_connection:stop(Alice) 1036: end). 1037: 1038: wait_for_carbon_with_bodies(Client, Texts, Params) -> 1039: [carboncopy_helper:wait_for_carbon_chat_with_body(Client, Text, Params) || Text <- Texts]. 1040: 1041: buffer_unacked_messages_and_die(Config, AliceSpec, Bob, Texts) -> 1042: F = fun(_Client) -> ok end, 1043: buffer_unacked_messages_and_die(Config, AliceSpec, Bob, Texts, F). 1044: 1045: buffer_unacked_messages_and_die(Config, AliceSpec, Bob, Texts, F) -> 1046: Alice = connect_spec(AliceSpec, sr_presence, manual), 1047: F(Alice), 1048: C2SPid = mongoose_helper:get_session_pid(Alice), 1049: %% Bobs sends some messages to Alice. 1050: sm_helper:send_messages(Bob, Alice, Texts), 1051: %% Alice receives them, but doesn't ack. 1052: sm_helper:wait_for_messages(Alice, Texts), 1053: %% Alice's connection is violently terminated. 1054: escalus_client:kill_connection(Config, Alice), 1055: sm_helper:wait_until_resume_session(C2SPid), 1056: SMID = sm_helper:client_to_smid(Alice), 1057: {C2SPid, SMID}. 1058: 1059: aggressively_pipelined_resume(Config) -> 1060: AliceSpec = [{manual_ack, true}, {parser_opts, [{start_tag, <<"stream:stream">>}]} 1061: | escalus_fresh:create_fresh_user(Config, alice)], 1062: UnackedMessages = three_texts(), 1063: escalus:fresh_story(Config, [{bob, 1}], fun(Bob) -> 1064: {_, SMID} = buffer_unacked_messages_and_die(Config, AliceSpec, Bob, UnackedMessages), 1065: %% Resume the session. 1066: Alice = escalus_connection:connect(AliceSpec), 1067: 1068: Username = proplists:get_value(username, AliceSpec), 1069: Password = proplists:get_value(password, AliceSpec), 1070: Payload = <<0:8,Username/binary,0:8,Password/binary>>, 1071: Server = proplists:get_value(server, AliceSpec), 1072: 1073: Stream = escalus_stanza:stream_start(Server, <<"jabber:client">>), 1074: Auth = escalus_stanza:auth(<<"PLAIN">>, [#xmlcdata{content = base64:encode(Payload)}]), 1075: AuthStream = escalus_stanza:stream_start(Server, <<"jabber:client">>), 1076: Resume = escalus_stanza:resume(SMID, 2), 1077: 1078: escalus_client:send(Alice, [Stream, Auth, AuthStream, Resume]), 1079: Messages = [escalus_connection:get_stanza(Alice, {get_resumed, I}) || I <- lists:seq(1, 6)], 1080: escalus:assert(is_sm_resumed, [SMID], lists:last(Messages)), 1081: 1082: escalus_connection:stop(Alice) 1083: end). 1084: 1085: %% This is a regression test for a case when a session processes a request, which will 1086: %% receive a response from the server, i.e. will have the same origin SID in mongoose_acc. 1087: %% Without proper handling, the reply would be rejected because the resumed session 1088: %% has new SID. 1089: replies_are_processed_by_resumed_session(Config) -> 1090: %% GIVEN a session and registered special IQ handler (added in init_per_testcase), 1091: %% that waits for old session process to terminate (at this point new process 1092: %% has fully taken over) and then actually sends the reply. 1093: Alice = connect_fresh(Config, alice, sr_presence), 1094: 1095: %% WHEN a client sends IQ request to the special handler... 1096: IQReq = escalus_stanza:iq_get(regression_ns(), []), 1097: escalus:send(Alice, IQReq), 1098: 1099: %% ... goes down and session is resumed. 1100: Alice2 = sm_helper:kill_and_connect_resume(Alice), 1101: 1102: %% THEN the client receives the reply properly. 1103: IQReply = escalus:wait_for_stanza(Alice2), 1104: escalus:assert(is_iq_result, [IQReq], IQReply), 1105: escalus_connection:stop(Alice2). 1106: 1107: %% This is a regression test for a bug, which manifested in following scenario 1108: %% (due to improper presence sub requests buffering): 1109: %% 1. Bob is online, Alice is offline 1110: %% 2. Bob subscribes to Alice's presence; 1111: %% 3. Alice becomes online 1112: %% 4. Bob sends a message to Alice 1113: %% 5. Alice doesn't SM-ack the request or message, terminates the connection 1114: %% 6. Alice reconnects but with session *replace*, not resume 1115: %% 7. Packet rerouting crashes on the buffered sub request, preventing resending whole buffer 1116: %% 8. Alice doesn't receive the buffered message 1117: subscription_requests_are_buffered_properly(Config) -> 1118: AliceSpec = escalus_fresh:create_fresh_user(Config, alice), 1119: MsgBody = <<"buffered">>, 1120: escalus:fresh_story(Config, [{bob, 1}], fun(Bob) -> 1121: % GIVEN Bob's pending subscription to Alice's presence 1122: AliceJid = common_helper:get_bjid(AliceSpec), 1123: escalus:send(Bob, escalus_stanza:presence_direct(AliceJid, <<"subscribe">>)), 1124: _RosterPushReq = escalus:wait_for_stanza(Bob), 1125: 1126: % WHEN Alice becomes online... 1127: Alice = connect_spec(AliceSpec, sr_session, manual), 1128: send_initial_presence(Alice), 1129: %% subscribe could come before the initial presence 1130: escalus:assert_many([is_presence(<<"available">>), is_presence(<<"subscribe">>)], 1131: escalus:wait_for_stanzas(Alice, 2)), 1132: 1133: % ...and Bob sends a message to Alice... 1134: escalus:send(Bob, escalus_stanza:chat_to(Alice, MsgBody)), 1135: MsgStanza = escalus:wait_for_stanza(Alice), 1136: escalus:assert(is_chat_message, [MsgBody], MsgStanza), 1137: 1138: % ...and Alice terminates connection without acking anything... 1139: escalus_client:kill_connection(Config, Alice), 1140: 1141: % ...and reconnects with session replacement. 1142: Alice2 = connect_spec(AliceSpec, session, manual), 1143: 1144: % THEN Alice receives (without sending initial presence): 1145: % * buffered Bob's message (like above) 1146: % Alice DOESN'T receive: 1147: % * buffered subscription request because it is dropped by ejabberd_sm 1148: % because it's treated like repeated sub request to bare JID, so it's not 1149: % processed by any sub req handler (like mod_roster) 1150: % * buffered available presence from Alice - because it is addressed to another SID 1151: % and Alice2 is a brand new session 1152: escalus:assert(is_chat_message, [MsgBody], escalus:wait_for_stanza(Alice2)), 1153: sm_helper:send_and_receive(Bob, Alice2, <<"flush1">>), 1154: escalus_assert:has_no_stanzas(Alice2), 1155: 1156: %% Only once an initial presence is sent, a subscription request is sent 1157: send_initial_presence(Alice2), 1158: escalus:assert_many([is_presence(<<"available">>), is_presence(<<"subscribe">>)], 1159: escalus:wait_for_stanzas(Alice2, 2)), 1160: 1161: sm_helper:send_and_receive(Bob, Alice2, <<"flush2">>), 1162: escalus_assert:has_no_stanzas(Alice2), 1163: 1164: escalus_connection:stop(Alice2) 1165: end). 1166: 1167: %% This is a regression test for a bug, due to which messages sent to old session 1168: %% in a middle of state handover were not appended properly to SM buffer. 1169: %% Scenario to reproduce: 1170: %% 1. Online Bob and Alice 1171: %% 2. Alice kills the connection 1172: %% 3. Alice's session is suspended 1173: %% 4. Alice resumes session with new connection. At this moment new session is still not 1174: %% present in session table. `resume` request is stuck in old proc mailbox. 1175: %% 5. Bob sends a message to Alice. Only old proc is present in session table so now 1176: %% old session has two messages in mailbox: `resume` and XML from Bob 1177: %% 6. We resume old process and it begins session handover 1178: %% 7. Bob's message is appended to SM buffer in "flush" step 1179: %% 8. With bug fixed, the message is retransmitted properly 1180: messages_are_properly_flushed_during_resumption(Config) -> 1181: escalus:fresh_story(Config, [{bob, 1}], fun(Bob) -> 1182: % GIVEN (online Bob) and (Alice in resume state); Alice's session is suspended 1183: Alice = connect_fresh(Config, alice, sr_presence), 1184: SMH = escalus_connection:get_sm_h(Alice), 1185: escalus_client:kill_connection(Config, Alice), 1186: %% The receiver process would stop now 1187: C2SPid = mongoose_helper:get_session_pid(Alice), 1188: sm_helper:wait_until_resume_session(C2SPid), 1189: 1190: sm_helper:wait_for_queue_length(C2SPid, 0), 1191: ok = rpc(mim(), sys, suspend, [C2SPid]), 1192: 1193: % WHEN new session requests resumption 1194: % we wait until that old session has resumption request enqueued; 1195: % we need it to ensure the order of messages: resume first, Bob's chat second. 1196: % Actual wait and message sent by Bob is done in separate process 1197: % because new client start will block until old process is resumed 1198: 1199: MsgBody = <<"flush-regression">>, 1200: spawn_link(fun() -> 1201: sm_helper:wait_for_queue_length(C2SPid, 1), 1202: 1203: % Bob sends a message... 1204: escalus:send(Bob, escalus_stanza:chat_to(Alice, MsgBody)), 1205: 1206: % ...we ensure that a message is enqueued in Alice's session... 1207: % (2 messages = resume request + Bob's message) 1208: sm_helper:wait_for_queue_length(C2SPid, 2), 1209: 1210: % ...and old process is resumed. 1211: ok = rpc(mim(), sys, resume, [C2SPid]) 1212: end), 1213: Alice2 = connect_resume(Alice, SMH), 1214: % THEN Alice's new session receives Bob's message 1215: RecvMsg = escalus:wait_for_stanza(Alice2), 1216: escalus:assert(is_chat_message, [MsgBody], RecvMsg) 1217: end). 1218: 1219: no_crash_if_stream_mgmt_disabled_but_client_requests_stream_mgmt(Config) -> 1220: Alice = connect_fresh(Config, alice, session, manual), 1221: %% Should not crash anything! 1222: escalus_connection:send(Alice, escalus_stanza:enable_sm()), 1223: Response = escalus_connection:get_stanza(Alice, service_unavailable), 1224: escalus:assert(is_sm_failed, [<<"feature-not-implemented">>], Response), 1225: escalus_connection:stop(Alice). 1226: 1227: no_crash_if_stream_mgmt_disabled_but_client_requests_stream_mgmt_with_resumption(Config) -> 1228: Alice = connect_fresh(Config, alice, session, manual), 1229: %% Should not crash anything! 1230: escalus_connection:send(Alice, escalus_stanza:enable_sm([resume])), 1231: Response = escalus_connection:get_stanza(Alice, service_unavailable), 1232: escalus:assert(is_sm_failed, [<<"feature-not-implemented">>], Response), 1233: escalus_connection:stop(Alice). 1234: 1235: %%-------------------------------------------------------------------- 1236: %% Helpers 1237: %%-------------------------------------------------------------------- 1238: start_hook_listener(Resource) -> 1239: TestCasePid = self(), 1240: rpc(mim(), ?MODULE, rpc_start_hook_handler, [TestCasePid, Resource, host_type()]). 1241: 1242: stop_hook_listener(HookExtra) -> 1243: rpc(mim(), ?MODULE, rpc_stop_hook_handler, [HookExtra, host_type()]). 1244: 1245: rpc_start_hook_handler(TestCasePid, User, HostType) -> 1246: LUser = jid:nodeprep(User), 1247: Extra = #{luser => LUser, pid => TestCasePid}, 1248: gen_hook:add_handler(unacknowledged_message, HostType, 1249: fun ?MODULE:hook_handler_fn/3, 1250: Extra, 50), 1251: Extra. 1252: 1253: rpc_stop_hook_handler(HookExtra, HostType) -> 1254: gen_hook:delete_handler(unacknowledged_message, HostType, 1255: fun ?MODULE:hook_handler_fn/3, 1256: HookExtra, 50). 1257: 1258: hook_handler_fn(Acc, 1259: #{jid := Jid} = _Params, 1260: #{luser := LUser, pid := TestCasePid} = _Extra) -> 1261: {U, _S, R} = jid:to_lower(Jid), 1262: case U of 1263: LUser -> 1264: Counter = mongoose_acc:get(sm_test, counter, 0, Acc), 1265: El = mongoose_acc:element(Acc), 1266: TestCasePid ! {sm_test, Counter, R, El}, 1267: {ok, mongoose_acc:set_permanent(sm_test, counter, Counter + 1, Acc)}; 1268: _ -> {ok, Acc} 1269: end. 1270: 1271: wait_for_unacked_msg_hook(Counter, Res, Timeout) -> 1272: receive 1273: {sm_test, AccCounter, Resource, Stanza} = Msg -> 1274: ?assertEqual(Counter, AccCounter, Msg), 1275: ?assertEqual(Res, Resource, Msg), 1276: Stanza 1277: after Timeout -> 1278: timeout 1279: end. 1280: 1281: start_filter_hook_listener(FilterText, Resource) -> 1282: rpc(mim(), ?MODULE, rpc_start_filter_hook_handler, [FilterText, Resource, host_type()]). 1283: 1284: stop_filter_hook_listener(HookExtra) -> 1285: rpc(mim(), ?MODULE, rpc_stop_filter_hook_handler, [HookExtra, host_type()]). 1286: 1287: rpc_start_filter_hook_handler(FilterText, User, HostType) -> 1288: LUser = jid:nodeprep(User), 1289: Extra = #{luser => LUser, filter_text => FilterText}, 1290: gen_hook:add_handler(filter_unacknowledged_messages, HostType, 1291: fun ?MODULE:filter_hook_handler_fn/3, 1292: Extra, 50), 1293: Extra. 1294: 1295: rpc_stop_filter_hook_handler(HookExtra, HostType) -> 1296: gen_hook:delete_handler(filter_unacknowledged_messages, HostType, 1297: fun ?MODULE:filter_hook_handler_fn/3, 1298: HookExtra, 50). 1299: 1300: filter_hook_handler_fn(Buffer, 1301: #{jid := Jid} = _Params, 1302: #{luser := LUser, filter_text := FilterText} = _Extra) -> 1303: {U, _} = jid:to_lus(Jid), 1304: case U of 1305: LUser -> 1306: F = fun(Acc) -> filter_text(Acc, FilterText) end, 1307: NewBuffer = lists:filter(F, Buffer), 1308: {ok, NewBuffer}; 1309: _ -> {ok, Buffer} 1310: end. 1311: 1312: filter_text(Acc, FilterText) -> 1313: case mongoose_acc:stanza_name(Acc) of 1314: <<"message">> -> 1315: El = mongoose_acc:element(Acc), 1316: FilterText =/= exml_query:path(El, [{element, <<"body">>}, cdata]); 1317: _ -> 1318: true 1319: end. 1320: 1321: is_chat(Content) -> 1322: fun(Stanza) -> escalus_pred:is_chat_message(Content, Stanza) end. 1323: 1324: is_presence(Type) -> 1325: fun(Stanza) -> escalus_pred:is_presence_with_type(Type, Stanza) end. 1326: 1327: three_texts() -> 1328: [<<"msg-1">>, <<"msg-2">>, <<"msg-3">>]. 1329: 1330: get_stanzas_filtered_by_mod_ping() -> 1331: History = rpc(mim(), meck, history, [mod_ping]), 1332: [Stanza || 1333: {_Pid, 1334: {_Mod, 1335: filter_local_packet = _Func, 1336: [{_, _, _, Stanza} = _Acc, _Params, _Extra] = _Args 1337: }, 1338: {stop, drop} = _Result 1339: } <- History 1340: ]. 1341: %%-------------------------------------------------------------------- 1342: %% IQ handler necessary for reproducing "replies_are_processed_by_resumed_session" 1343: %%-------------------------------------------------------------------- 1344: 1345: regression_ns() -> 1346: <<"regression">>. 1347: 1348: register_handler() -> 1349: HostType = host_type(), 1350: rpc(mim(), gen_iq_handler, add_iq_handler_for_domain, 1351: [HostType, regression_ns(), ejabberd_sm, 1352: fun ?MODULE:regression_handler/5, #{}, one_queue]). 1353: 1354: unregister_handler() -> 1355: HostType = host_type(), 1356: rpc(mim(), gen_iq_handler, remove_iq_handler_for_domain, 1357: [HostType, regression_ns(), ejabberd_sm]). 1358: 1359: regression_handler(Acc, _From, _To, IQ, _Extra) -> 1360: %% A bit of a hack - will no longer work when the SID format changes 1361: {_, Pid} = mongoose_acc:get(c2s, origin_sid, undefined, Acc), 1362: erlang:monitor(process, Pid), 1363: receive 1364: {'DOWN', _, _, _, _} -> 1365: ok 1366: after 1367: 10000 -> 1368: error({c2s_not_stopped_after_timeout, Pid}) 1369: end, 1370: %% We avoid another race condition - there is a short window where user session 1371: %% is not registered in ejabberd_sm: between old process termination and the moment 1372: %% when the new process stores new session in memory. It should be fixed separately. 1373: wait_for_session(mongoose_acc:get(c2s, origin_jid, undefined, Acc), 50, 100), 1374: {Acc, jlib:make_result_iq_reply(IQ)}. 1375: 1376: wait_for_session(JID, Retries, SleepTime) -> 1377: case ejabberd_sm:get_session(JID) of 1378: offline -> 1379: timer:sleep(SleepTime), 1380: wait_for_session(JID, Retries - 1, SleepTime); 1381: _ -> 1382: ok 1383: end. 1384: 1385: maybe_ack_initial_presence(Alice, ack) -> 1386: ack_initial_presence(Alice); 1387: maybe_ack_initial_presence(_Alice, no_ack) -> 1388: ok.