1: -module(migration_scripts_SUITE).
    2: 
    3: % CT callbacks
    4: -export([all/0, groups/0, init_per_suite/1, end_per_suite/1]).
    5: % sender-jid-from-mam-message.escript test cases
    6: -export([
    7:          sender_jid_from_mam_muc_eterm_stream/1,
    8:          sender_jid_from_mam_muc_xml_stream/1,
    9:          sender_jid_from_mam_muc_doesnt_crash_on_unsupported_eterm_input/1,
   10:          sender_jid_from_mam_muc_doesnt_crash_on_unsupported_xml_input/1,
   11:          sender_jid_from_mam_muc_doesnt_crash_on_malformed_eterm_input/1,
   12:          sender_jid_from_mam_muc_doesnt_crash_on_malformed_xml_input/1
   13:         ]).
   14: 
   15: %% ----------------------------------------------------------
   16: %% CT callbacks
   17: %% ----------------------------------------------------------
   18: 
   19: all() ->
   20:     [
   21:      {group, sender_jid_from_mam_message}
   22:     ].
   23: 
   24: groups() ->
   25:     [
   26:      {sender_jid_from_mam_message, [parallel], [
   27:                                                 sender_jid_from_mam_muc_eterm_stream,
   28:                                                 sender_jid_from_mam_muc_xml_stream,
   29:                                                 sender_jid_from_mam_muc_doesnt_crash_on_unsupported_eterm_input,
   30:                                                 sender_jid_from_mam_muc_doesnt_crash_on_unsupported_xml_input,
   31:                                                 sender_jid_from_mam_muc_doesnt_crash_on_malformed_eterm_input,
   32:                                                 sender_jid_from_mam_muc_doesnt_crash_on_malformed_xml_input
   33:                                                ]}
   34:     ].
   35: 
   36: init_per_suite(Config) ->
   37:     file:delete("/tmp/script-debug"),
   38:     {ok, _} = application:ensure_all_started(jid),
   39:     Config.
   40: 
   41: end_per_suite(Config) ->
   42:     {ok, DebugData} = file:read_file("/tmp/script-debug"),
   43:     ct:pal("~p", [DebugData]),
   44:     Config.
   45: 
   46: %% ----------------------------------------------------------
   47: %% Test cases
   48: %% ----------------------------------------------------------
   49: 
   50: %% ----------------- sender-jid-from-mam-message.escript ----------------------
   51: 
   52: sender_jid_from_mam_muc_eterm_stream(_Config) ->
   53:     Port = script_helper:start("tools/migration/sender-jid-from-mam-message.escript", ["eterm"]),
   54:     sender_jid_from_mam_muc_data_stream(Port, fun binary_string_to_eterm/1).
   55: 
   56: sender_jid_from_mam_muc_xml_stream(_Config) ->
   57:     Port = script_helper:start("tools/migration/sender-jid-from-mam-message.escript", ["xml"]),
   58:     sender_jid_from_mam_muc_data_stream(Port, fun(B) -> B end).
   59: 
   60: sender_jid_from_mam_muc_data_stream(Port, PayloadConverterFun) ->
   61:     lists:foreach(fun(JID) ->
   62:                           MsgBin = sample_archived_muc_message(JID),
   63:                           script_helper:write(Port, PayloadConverterFun(MsgBin)),
   64:                           BareJID = mod_mam_utils:bare_jid(jid:from_binary(JID)),
   65:                           BareJID = script_helper:read(Port)
   66:                   end, [<<"alice@localhost">>, <<"zAżółćgęśLąjaźń@localhost2/res3"/utf8>>,
   67:                         <<"kate@kędZierzyn.koźle.pl"/utf8>>]).
   68: 
   69: sender_jid_from_mam_muc_doesnt_crash_on_unsupported_eterm_input(_Config) ->
   70:     Port = script_helper:start("tools/migration/sender-jid-from-mam-message.escript", ["eterm"]),
   71:     sender_jid_from_mam_muc_doesnt_crash_on_unsupported_input(Port, fun binary_string_to_eterm/1).
   72: 
   73: sender_jid_from_mam_muc_doesnt_crash_on_unsupported_xml_input(_Config) ->
   74:     Port = script_helper:start("tools/migration/sender-jid-from-mam-message.escript", ["xml"]),
   75:     sender_jid_from_mam_muc_doesnt_crash_on_unsupported_input(Port, fun(B) -> B end).
   76: 
   77: 
   78: sender_jid_from_mam_muc_doesnt_crash_on_malformed_eterm_input(_Config) ->
   79:     Port = script_helper:start("tools/migration/sender-jid-from-mam-message.escript", ["eterm"]),
   80:     sender_jid_from_mam_muc_doesnt_crash_on_malformed_input(Port, fun binary_string_to_eterm/1).
   81: 
   82: sender_jid_from_mam_muc_doesnt_crash_on_malformed_xml_input(_Config) ->
   83:     Port = script_helper:start("tools/migration/sender-jid-from-mam-message.escript", ["xml"]),
   84:     sender_jid_from_mam_muc_doesnt_crash_on_malformed_input(Port, fun(B) -> B end).
   85: 
   86: sender_jid_from_mam_muc_doesnt_crash_on_unsupported_input(Port, PayloadConverterFun) ->
   87:     %% First we expect that the script replies with -2 length (non MUC message)....
   88:     InvalidPayload = PayloadConverterFun(sample_archived_1_to_1_message()),
   89:     script_helper:write(Port, InvalidPayload),
   90:     {error, -2} = script_helper:read(Port),
   91: 
   92:     %% Then we confirm with valid payload that the script actually still works
   93:     sender_jid_from_mam_muc_data_stream(Port, PayloadConverterFun).
   94: 
   95: sender_jid_from_mam_muc_doesnt_crash_on_malformed_input(Port, PayloadConverterFun) ->
   96:     %% First we expect that the script replies with -1 length (malformed message)....
   97:     InvalidPayload = PayloadConverterFun(sample_malformed_muc_message()),
   98:     script_helper:write(Port, InvalidPayload),
   99:     {error, -1} = script_helper:read(Port),
  100: 
  101:     %% Then we confirm with valid payload that the script actually still works
  102:     sender_jid_from_mam_muc_data_stream(Port, PayloadConverterFun).
  103: 
  104: %% ----------------------------------------------------------
  105: %% Helpers
  106: %% ----------------------------------------------------------
  107: 
  108: sample_archived_muc_message(JID) ->
  109:   <<"<message xmlns='jabber:client'
  110:         from='coven@chat.shakespeare.lit/firstwitch'
  111:         id='162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2'
  112:         type='groupchat'>
  113:         <body>Zażółć gęślą jaźń</body>
  114:         <x xmlns='http://jabber.org/protocol/muc#user'>
  115:           <item affiliation='none'
  116:                 jid='"/utf8, JID/binary, "'
  117:                 role='participant' />
  118:         </x>
  119:       </message>"/utf8>>.
  120: 
  121: sample_archived_1_to_1_message() ->
  122:     <<"<message from='a@localhost' to='b@localhost' type='chat'><body>"
  123:       "Zażółć gęślą jaźń</body></message>"/utf8>>.
  124: 
  125: sample_malformed_muc_message() ->
  126:     <<"<message xmlns='jabber:client'
  127:         from='coven@chat.shakespeare.lit/firstwitch'
  128:         id='162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2'
  129:         type='groupchat'>
  130:         <body>Zażółć gęślą jaźń</body>
  131:         <x xmlns='http://jabber.org/protocol/muc#user'>
  132:           <item_malformed affiliation='none'
  133:                 jid='a@localhost'
  134:                 role='participant' />
  135:         </x>
  136:       </message>"/utf8>>.
  137: 
  138: 
  139: binary_string_to_eterm(Bin) ->
  140:     {ok, XmlEl} = exml:parse(Bin),
  141:     term_to_binary(XmlEl).
  142: