1: %%==============================================================================
    2: %% Copyright 2013 Erlang Solutions Ltd.
    3: %%
    4: %% Licensed under the Apache License, Version 2.0 (the "License");
    5: %% you may not use this file except in compliance with the License.
    6: %% You may obtain a copy of the License at
    7: %%
    8: %% http://www.apache.org/licenses/LICENSE-2.0
    9: %%
   10: %% Unless required by applicable law or agreed to in writing, software
   11: %% distributed under the License is distributed on an "AS IS" BASIS,
   12: %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13: %% See the License for the specific language governing permissions and
   14: %% limitations under the License.
   15: %%==============================================================================
   16: 
   17: -module(metrics_c2s_SUITE).
   18: -compile([export_all, nowarn_export_all]).
   19: 
   20: -include_lib("escalus/include/escalus.hrl").
   21: -include_lib("common_test/include/ct.hrl").
   22: 
   23: -define(WAIT_TIME, 100).
   24: 
   25: -import(metrics_helper, [assert_counter/2,
   26:                          get_counter_value/1,
   27:                          wait_for_counter/2]).
   28: 
   29: %%--------------------------------------------------------------------
   30: %% Suite configuration
   31: %%--------------------------------------------------------------------
   32: 
   33: all() ->
   34:     [{group, single},
   35:      {group, multiple},
   36:      {group, drop},
   37:      {group, errors},
   38:      {group, count}].
   39: 
   40: groups() ->
   41:     [{single, [sequence], [message_one,
   42:                            stanza_one,
   43:                            presence_one,
   44:                            presence_direct_one,
   45:                            iq_one]},
   46:      {multiple, [sequence], [messages]},
   47:      {drop, [sequence], [bounced
   48:                         ]},
   49:      {errors, [sequence], [error_total,
   50:                            error_mesg,
   51:                            error_iq,
   52:                            error_presence]},
   53:      {count, [sequence], [stanza_count]}].
   54: 
   55: suite() ->
   56:     [{require, ejabberd_node} | escalus:suite()].
   57: 
   58: %%--------------------------------------------------------------------
   59: %% Init & teardown
   60: %%--------------------------------------------------------------------
   61: 
   62: init_per_suite(Config) ->
   63:     HostType = domain_helper:host_type(),
   64:     Config1 = dynamic_modules:save_modules(HostType, Config),
   65:     dynamic_modules:ensure_stopped(HostType, [mod_offline]),
   66:     escalus:init_per_suite(Config1).
   67: 
   68: end_per_suite(Config) ->
   69:     dynamic_modules:restore_modules(Config),
   70:     escalus:end_per_suite(Config).
   71: 
   72: init_per_group(_GroupName, Config) ->
   73:     escalus:create_users(Config, escalus:get_users([alice, bob])).
   74: 
   75: end_per_group(_GroupName, Config) ->
   76:     escalus:delete_users(Config, escalus:get_users([alice, bob])).
   77: 
   78: init_per_testcase(CaseName, Config) ->
   79:     escalus:init_per_testcase(CaseName, Config).
   80: 
   81: end_per_testcase(CaseName, Config) ->
   82:     escalus:end_per_testcase(CaseName, Config).
   83: 
   84: %%--------------------------------------------------------------------
   85: %% Message tests
   86: %%--------------------------------------------------------------------
   87: 
   88: 
   89: message_one(Config) ->
   90:     {value, MesgSent} = get_counter_value(xmppMessageSent),
   91:     {value, MesgReceived} = get_counter_value(xmppMessageReceived),
   92:     escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
   93: 
   94:         escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi!">>)),
   95:         escalus_client:wait_for_stanza(Bob),
   96: 
   97:         assert_counter(MesgSent + 1, xmppMessageSent),
   98:         assert_counter(MesgReceived + 1, xmppMessageReceived)
   99: 
  100:         end).
  101: 
  102: stanza_one(Config) ->
  103:     escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
  104:         {value, StanzaSent} = get_counter_value(xmppStanzaSent),
  105:         {value, StanzaReceived} = get_counter_value(xmppStanzaReceived),
  106: 
  107:         escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi!">>)),
  108:         escalus_client:wait_for_stanza(Bob),
  109: 
  110:         assert_counter(StanzaSent + 1, xmppStanzaSent),
  111:         assert_counter(StanzaReceived + 1, xmppStanzaReceived)
  112: 
  113:         end).
  114: 
  115: presence_one(Config) ->
  116:     escalus:story(Config, [{alice, 1}], fun(Alice) ->
  117:         {value, PresenceSent} = get_counter_value(xmppPresenceSent),
  118:         {value, PresenceReceived} = get_counter_value(xmppPresenceReceived),
  119:         {value, StanzaSent} = get_counter_value(xmppStanzaSent),
  120:         {value, StanzaReceived} = get_counter_value(xmppStanzaReceived),
  121: 
  122:         escalus:send(Alice, escalus_stanza:presence(<<"available">>)),
  123:         escalus:wait_for_stanza(Alice),
  124: 
  125:         assert_counter(PresenceSent + 1, xmppPresenceSent),
  126:         assert_counter(PresenceReceived + 1, xmppPresenceReceived),
  127:         assert_counter(StanzaSent + 1, xmppStanzaSent),
  128:         assert_counter(StanzaReceived + 1, xmppStanzaReceived)
  129: 
  130:         end).
  131: 
  132: presence_direct_one(Config) ->
  133:     escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
  134:         {value, PresenceSent} = get_counter_value(xmppPresenceSent),
  135:         {value, PresenceReceived} = get_counter_value(xmppPresenceReceived),
  136:         {value, StanzaSent} = get_counter_value(xmppStanzaSent),
  137:         {value, StanzaReceived} = get_counter_value(xmppStanzaReceived),
  138: 
  139:         Presence = escalus_stanza:presence_direct(escalus_client:short_jid(Bob), <<"available">>),
  140:         escalus:send(Alice, Presence),
  141:         escalus:wait_for_stanza(Bob),
  142: 
  143:         assert_counter(PresenceSent + 1, xmppPresenceSent),
  144:         assert_counter(PresenceReceived + 1, xmppPresenceReceived),
  145:         assert_counter(StanzaSent + 1, xmppStanzaSent),
  146:         assert_counter(StanzaReceived + 1, xmppStanzaReceived)
  147: 
  148:         end).
  149: 
  150: iq_one(Config) ->
  151:     escalus:story(Config, [{alice, 1}], fun(Alice) ->
  152:         {value, IqSent} = get_counter_value(xmppIqSent),
  153:         {value, IqReceived} = get_counter_value(xmppIqReceived),
  154:         {value, StanzaSent} = get_counter_value(xmppStanzaSent),
  155:         {value, StanzaReceived} = get_counter_value(xmppStanzaReceived),
  156: 
  157:         escalus_client:send(Alice,
  158:                             escalus_stanza:roster_get()),
  159:         escalus_client:wait_for_stanza(Alice),
  160: 
  161:         assert_counter(IqSent + 1, xmppIqSent),
  162:         assert_counter(StanzaSent + 1, xmppStanzaSent),
  163:         assert_counter(StanzaReceived + 1, xmppStanzaReceived),
  164:         assert_counter(IqReceived + 1, xmppIqReceived)
  165: 
  166:         end).
  167: 
  168: messages(Config) ->
  169:     {value, MesgSent} = get_counter_value(xmppMessageSent),
  170:     {value, MesgReceived} = get_counter_value(xmppMessageReceived),
  171:     escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
  172: 
  173:         escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi!">>)),
  174:         escalus_client:wait_for_stanza(Bob),
  175:         escalus_client:send(Bob, escalus_stanza:chat_to(Alice, <<"Hi!">>)),
  176:         escalus_client:wait_for_stanza(Alice),
  177:         escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi!">>)),
  178:         escalus_client:wait_for_stanza(Bob),
  179:         escalus_client:send(Bob, escalus_stanza:chat_to(Alice, <<"Hi!">>)),
  180:         escalus_client:wait_for_stanza(Alice),
  181: 
  182:         assert_counter(MesgSent + 4, xmppMessageSent),
  183:         assert_counter(MesgReceived + 4, xmppMessageReceived)
  184: 
  185:         end).
  186: 
  187: bounced(Config) ->
  188:     {value, MesgBounced} = get_counter_value(xmppMessageBounced),
  189:     escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
  190: 
  191:         escalus_client:stop(Config, Bob),
  192:         timer:sleep(?WAIT_TIME),
  193: 
  194:         escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi!">>)),
  195:         wait_for_counter(MesgBounced + 1, xmppMessageBounced)
  196: 
  197:         end).
  198: 
  199: stanza_count(Config) ->
  200:     escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
  201:         {value, OldStanzaCount} = get_counter_value(xmppStanzaCount),
  202: 
  203:         escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi!">>)),
  204:         escalus_client:wait_for_stanza(Bob),
  205:         escalus_client:send(Bob, escalus_stanza:chat_to(Alice, <<"Hi!">>)),
  206:         escalus_client:wait_for_stanza(Alice),
  207:         escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi!">>)),
  208:         escalus_client:wait_for_stanza(Bob),
  209:         escalus_client:send(Bob, escalus_stanza:chat_to(Alice, <<"Hi!">>)),
  210:         escalus_client:wait_for_stanza(Alice),
  211: 
  212:         {value, StanzaCount} = get_counter_value(xmppStanzaCount),
  213:         true = StanzaCount >= OldStanzaCount + 4
  214: 
  215:         end).
  216: 
  217: 
  218: %%-----------------------------------------------------
  219: %% Error tests
  220: %%-----------------------------------------------------
  221: 
  222: error_total(Config) ->
  223:     {value, Errors} = get_counter_value(xmppErrorTotal),
  224:     escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
  225: 
  226:         escalus_client:stop(Config, Bob),
  227:         timer:sleep(?WAIT_TIME),
  228: 
  229:         escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi!">>)),
  230:         wait_for_counter(Errors + 1, xmppErrorTotal)
  231: 
  232:         end).
  233: 
  234: error_mesg(Config) ->
  235:     {value, Errors} = get_counter_value(xmppErrorMessage),
  236:     escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
  237: 
  238:         escalus_client:stop(Config, Bob),
  239:         timer:sleep(?WAIT_TIME),
  240: 
  241:         escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi!">>)),
  242:         wait_for_counter(Errors + 1, xmppErrorMessage)
  243: 
  244:         end).
  245: 
  246: error_presence(Config) ->
  247:     {value, Errors} = get_counter_value(xmppErrorPresence),
  248:     escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
  249: 
  250:         escalus:send(Alice, escalus_stanza:presence_direct(
  251:                               escalus_client:short_jid(Bob), <<"available">>)),
  252:         escalus:wait_for_stanza(Bob),
  253: 
  254:         ErrorElt = escalus_stanza:error_element(<<"cancel">>, <<"gone">>),
  255:         Presence = escalus_stanza:presence_direct(escalus_client:short_jid(Alice),
  256:                                                   <<"error">>, [ErrorElt]),
  257:         escalus:send(Bob, Presence),
  258:         escalus:wait_for_stanza(Alice),
  259: 
  260:         wait_for_counter(Errors + 1, xmppErrorPresence)
  261: 
  262:         end).
  263: 
  264: error_iq(Config) ->
  265:     {value, Errors} = get_counter_value(xmppErrorIq),
  266: 
  267:     Users = escalus_config:get_config(escalus_users, Config),
  268:     Alice = escalus_users:get_user_by_name(alice, Users),
  269:     escalus_users:create_user(Config, Alice),
  270:     wait_for_counter(Errors + 1, xmppErrorIq).