1: -module(jlib_SUITE).
    2: -include_lib("exml/include/exml.hrl").
    3: -include_lib("proper/include/proper.hrl").
    4: -include_lib("eunit/include/eunit.hrl").
    5: -include("jlib.hrl").
    6: -include("mongoose.hrl").
    7: -include_lib("common_test/include/ct.hrl").
    8: -compile([export_all, nowarn_export_all]).
    9: 
   10: all() -> [
   11:           make_iq_reply_changes_type_to_result,
   12:           make_iq_reply_changes_to_to_from,
   13:           make_iq_reply_switches_from_to_to,
   14:           make_iq_reply_switches_to_and_from_attrs,
   15:           error_reply_check
   16:          ].
   17: 
   18: init_per_suite(C) ->
   19:     {ok, _} = application:ensure_all_started(jid),
   20:     C.
   21: 
   22: end_per_suite(C) ->
   23:     C.
   24: 
   25: 
   26: make_iq_reply_switches_to_and_from_attrs(_C) ->
   27:     ToJid = <<"test@esl.com/res">>,
   28:     FromJid = <<"test2@esl.com/res2">>,
   29:     #xmlel{attrs = Attrs} = BaseIQ = base_iq(),
   30: 
   31:     IQWithToAndFrom = BaseIQ#xmlel{attrs = [{<<"to">>, ToJid},
   32:                                             {<<"from">>, FromJid} | Attrs]},
   33: 
   34:     WithToFromReply = jlib:make_result_iq_reply(IQWithToAndFrom),
   35: 
   36:     <<"result">> = exml_query:attr(WithToFromReply, <<"type">>),
   37:     FromJid = exml_query:attr(WithToFromReply, <<"to">>),
   38:     ToJid = exml_query:attr(WithToFromReply, <<"from">>).
   39: 
   40: make_iq_reply_switches_from_to_to(_C) ->
   41:     FromJid = <<"test2@esl.com/res2">>,
   42:     #xmlel{attrs = Attrs} = BaseIQ = base_iq(),
   43:     IQWithFrom = BaseIQ#xmlel{attrs = [{<<"from">>, FromJid} | Attrs]},
   44: 
   45:     WithFromReply = jlib:make_result_iq_reply(IQWithFrom),
   46: 
   47:     <<"result">> = exml_query:attr(WithFromReply, <<"type">>),
   48:     FromJid = exml_query:attr(WithFromReply, <<"to">>).
   49: 
   50: make_iq_reply_changes_to_to_from(_C) ->
   51:     ToJid = <<"test@esl.com/res">>,
   52:     #xmlel{attrs = Attrs} = BaseIQ = base_iq(),
   53:     IQWithTo = BaseIQ#xmlel{attrs = [{<<"to">>, ToJid} | Attrs]},
   54: 
   55:     WithToReply = jlib:make_result_iq_reply(IQWithTo),
   56: 
   57:     <<"result">> = exml_query:attr(WithToReply, <<"type">>),
   58:     ToJid = exml_query:attr(WithToReply, <<"from">>).
   59: 
   60: make_iq_reply_changes_type_to_result(_) ->
   61:     BaseIQReply = jlib:make_result_iq_reply(base_iq()),
   62:     <<"result">> = exml_query:attr(BaseIQReply, <<"type">>).
   63: 
   64: error_reply_check(_) ->
   65:     BaseIQReply = jlib:make_result_iq_reply(base_iq()),
   66:     Acc = mongoose_acc:new(#{ location => ?LOCATION,
   67:                               lserver => <<"localhost">>,
   68:                               host_type => <<"localhost">>,
   69:                               element => BaseIQReply,
   70:                               from_jid => jid:make_noprep(<<"a">>, <<"localhost">>, <<>>),
   71:                               to_jid => jid:make_noprep(<<>>, <<"localhost">>, <<>>) }),
   72:     {Acc1, ErrorReply1} = jlib:make_error_reply(Acc, BaseIQReply, #xmlel{name = <<"testerror">>}),
   73:     ?assertMatch(#xmlel{}, ErrorReply1),
   74:     {_Acc2, ErrorReply2} = jlib:make_error_reply(Acc1, ErrorReply1, #xmlel{name = <<"testerror">>}),
   75:     ?assertMatch({error, {already_an_error, #xmlel{}, #xmlel{}}}, ErrorReply2),
   76:     ok.
   77: 
   78: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   79: 
   80: 
   81: base_iq() ->
   82:     #xmlel{name = <<"iq">>,
   83:            attrs = [{<<"id">>, base64:encode(crypto:strong_rand_bytes(4))},
   84:                     {<<"xmlns">>, <<"jabber:client">>},
   85:                     {<<"type">>, <<"set">>}],
   86:            children = [#xmlel{name = <<"session">>,
   87:                               attrs = [{<<"xmlns">>, <<"urn:ietf:params:xml:ns:xmpp-session">>}]}
   88:                       ]}.
   89: 
   90: element_length_is_too_big(Els) ->
   91:     lists:any(fun(El) -> size(El) >= 1024 end, Els).
   92: 
   93: run_property(Prop, NumTest, StartSize, StopSize) ->
   94:     ?assert(proper:quickcheck(Prop, [verbose, long_result,
   95:                                      {numtests, NumTest},
   96:                                      {start_size, StartSize},
   97:                                      {max_size, StopSize}])).
   98: