1: -module(mod_version_SUITE).
    2: -compile([export_all, nowarn_export_all]).
    3: -include_lib("escalus/include/escalus.hrl").
    4: -include_lib("common_test/include/ct.hrl").
    5: -include_lib("eunit/include/eunit.hrl").
    6: -include_lib("escalus/include/escalus_xmlns.hrl").
    7: -include_lib("exml/include/exml.hrl").
    8: %%--------------------------------------------------------------------
    9: %% Suite configuration
   10: %%--------------------------------------------------------------------
   11: 
   12: all() ->
   13:     [{group, soft_version}, {group, soft_version_with_os}].
   14: 
   15: groups() ->
   16:     [{soft_version, [parallel], [version_service_discovery, ask_for_version]},
   17:      {soft_version_with_os, [parallel], [version_service_discovery, ask_for_version_with_os]}].
   18: 
   19: suite() ->
   20:     escalus:suite().
   21: 
   22: %%--------------------------------------------------------------------
   23: %% Init & teardown
   24: %%--------------------------------------------------------------------
   25: 
   26: init_per_suite(Config) ->
   27:     escalus:init_per_suite(Config).
   28: 
   29: end_per_suite(Config) ->
   30:     escalus_fresh:clean(),
   31:     escalus:end_per_suite(Config).
   32: 
   33: init_per_group(soft_version, Config) ->
   34:     dynamic_modules:start(domain_helper:host_type(), mod_version, []),
   35:     Config;
   36: init_per_group(soft_version_with_os, Config) ->
   37:     dynamic_modules:start(domain_helper:host_type(), mod_version, [{os_info, true}]),
   38:     Config.
   39: 
   40: end_per_group(_Group, Config) ->
   41:     dynamic_modules:stop(domain_helper:host_type(), mod_version),
   42:     Config.
   43: 
   44: init_per_testcase(CaseName, Config) ->
   45:     escalus:init_per_testcase(CaseName, Config).
   46: 
   47: end_per_testcase(CaseName, Config) ->
   48:     escalus:end_per_testcase(CaseName, Config).
   49: 
   50: %%--------------------------------------------------------------------
   51: %% Service discovery test
   52: %%--------------------------------------------------------------------
   53: 
   54: version_service_discovery(Config) ->
   55:     escalus:fresh_story(Config, [{bob, 1}],
   56:         fun(Bob) ->
   57:             ServJID = escalus_client:server(Bob),
   58:             Result = escalus:send_and_wait(Bob,
   59:                                            escalus_stanza:disco_info(ServJID)),
   60:             escalus:assert(is_iq_result, Result),
   61:             escalus:assert(has_feature, [?NS_SOFT_VERSION], Result)
   62:         end).
   63: 
   64: %%--------------------------------------------------------------------
   65: %% Software version response test
   66: %%--------------------------------------------------------------------
   67: 
   68: ask_for_version(Config) ->
   69:     escalus:fresh_story(Config, [{bob, 1}], fun(Bob) ->
   70:         Server = escalus_users:get_server(Config, bob),
   71:         ID = escalus_stanza:id(),
   72:         SoftStanza = soft_version_stanza(Server, ID),
   73:         escalus_client:send(Bob, SoftStanza),
   74:         Reply = escalus:wait_for_stanza(Bob, 5000),
   75:         escalus:assert(is_iq_result, Reply),
   76:         escalus:assert(fun check_namespace/1, Reply),
   77:         escalus:assert(fun check_name_and_version_presence/1, Reply)
   78:     end).
   79: 
   80: %%--------------------------------------------------------------------
   81: %% Software version with os info response test
   82: %%--------------------------------------------------------------------
   83: 
   84: ask_for_version_with_os(Config) ->
   85:     escalus:fresh_story(Config, [{bob, 1}], fun(Bob) ->
   86:         Server = escalus_users:get_server(Config, bob),
   87:         ID = escalus_stanza:id(),
   88:         SoftStanza = soft_version_stanza(Server, ID),
   89:         escalus_client:send(Bob, SoftStanza),
   90:         Reply = escalus:wait_for_stanza(Bob, 5000),
   91:         escalus:assert(is_iq_result, Reply),
   92:         escalus:assert(fun check_namespace/1, Reply),
   93:         escalus:assert(fun check_name_version_and_os_presence/1, Reply)
   94:     end).
   95: 
   96: %%--------------------------------------------------------------------
   97: %% Test helpers
   98: %%--------------------------------------------------------------------
   99: 
  100: soft_version_stanza(Server, ID) ->
  101:     #xmlel{name = <<"iq">>,
  102:            attrs = [{<<"type">>, <<"get">>},
  103:                     {<<"to">>, Server},
  104:                     {<<"id">>, ID}],
  105:            children = [#xmlel{name = <<"query">>,
  106:                               attrs = [{<<"xmlns">>, ?NS_SOFT_VERSION}]}]}.
  107: 
  108: check_namespace(#xmlel{name = <<"iq">>, attrs = _, children = [Child]}) ->
  109:     case Child of
  110:         #xmlel{name = <<"query">>,
  111:                attrs = [{<<"xmlns">>, ?NS_SOFT_VERSION}],
  112:                children = _} ->
  113:             true;
  114:         _ ->
  115:             false
  116:     end;
  117: 
  118: check_namespace(_) -> false.
  119: 
  120: check_name_and_version_presence(#xmlel{name = <<"iq">>, attrs = _, children = [Child]}) ->
  121:     case Child of
  122:         #xmlel{name = <<"query">>,
  123:                attrs = [{<<"xmlns">>, ?NS_SOFT_VERSION}],
  124:                children = Children} ->
  125:                    case Children of
  126:                        [#xmlel{name = <<"name">>, attrs = [], children = [#xmlcdata{content = _}]},
  127:                         #xmlel{name = <<"version">>, attrs = [], children = [#xmlcdata{content = _}]}] ->
  128:                             true;
  129:                         _ ->
  130:                             false
  131:                    end;
  132:         _ ->
  133:             false
  134:     end;
  135: 
  136: check_name_and_version_presence(_) -> false.
  137: 
  138: check_name_version_and_os_presence(#xmlel{name = <<"iq">>, attrs = _, children = [Child]}) ->
  139:     case Child of
  140:         #xmlel{name = <<"query">>,
  141:                attrs = [{<<"xmlns">>, ?NS_SOFT_VERSION}],
  142:                children = Children} ->
  143:                    case Children of
  144:                        [#xmlel{name = <<"name">>, attrs = [], children = [#xmlcdata{content = _}]},
  145:                         #xmlel{name = <<"version">>, attrs = [], children = [#xmlcdata{content = _}]},
  146:                         #xmlel{name = <<"os">>, attrs = [], children = [#xmlcdata{content = _}]}] ->
  147:                             true;
  148:                         _ ->
  149:                             false
  150:                    end;
  151:         _ ->
  152:             false
  153:     end;
  154: 
  155: check_name_version_and_os_presence(_) -> false.