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