2013-06-26 19:55:29 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-01-03 15:58:52 +01:00
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Created : 27 Jun 2013 by Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
2013-06-26 19:55:29 +02:00
|
|
|
%%%
|
2017-01-03 15:58:52 +01:00
|
|
|
%%%
|
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2013-06-26 19:55:29 +02:00
|
|
|
-module(suite).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-compile(export_all).
|
|
|
|
|
|
|
|
-include("suite.hrl").
|
2016-05-30 23:05:45 +02:00
|
|
|
-include_lib("kernel/include/file.hrl").
|
2016-10-22 12:01:45 +02:00
|
|
|
-include("mod_roster.hrl").
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init_config(Config) ->
|
|
|
|
DataDir = proplists:get_value(data_dir, Config),
|
|
|
|
PrivDir = proplists:get_value(priv_dir, Config),
|
|
|
|
[_, _|Tail] = lists:reverse(filename:split(DataDir)),
|
|
|
|
BaseDir = filename:join(lists:reverse(Tail)),
|
2015-04-09 17:14:30 +02:00
|
|
|
ConfigPathTpl = filename:join([DataDir, "ejabberd.yml"]),
|
2013-06-26 19:55:29 +02:00
|
|
|
LogPath = filename:join([PrivDir, "ejabberd.log"]),
|
|
|
|
SASLPath = filename:join([PrivDir, "sasl.log"]),
|
|
|
|
MnesiaDir = filename:join([PrivDir, "mnesia"]),
|
|
|
|
CertFile = filename:join([DataDir, "cert.pem"]),
|
2016-09-23 11:30:33 +02:00
|
|
|
SelfSignedCertFile = filename:join([DataDir, "self-signed-cert.pem"]),
|
|
|
|
CAFile = filename:join([DataDir, "ca.pem"]),
|
2013-06-26 19:55:29 +02:00
|
|
|
{ok, CWD} = file:get_cwd(),
|
|
|
|
{ok, _} = file:copy(CertFile, filename:join([CWD, "cert.pem"])),
|
2016-09-23 11:30:33 +02:00
|
|
|
{ok, _} = file:copy(SelfSignedCertFile,
|
|
|
|
filename:join([CWD, "self-signed-cert.pem"])),
|
|
|
|
{ok, _} = file:copy(CAFile, filename:join([CWD, "ca.pem"])),
|
2015-04-09 17:14:30 +02:00
|
|
|
{ok, CfgContentTpl} = file:read_file(ConfigPathTpl),
|
2016-09-21 09:45:11 +02:00
|
|
|
Password = <<"password!@#$%^&*()'\"`~<>+-/;:_=[]{}|\\">>,
|
2015-04-09 17:14:30 +02:00
|
|
|
CfgContent = process_config_tpl(CfgContentTpl, [
|
|
|
|
{c2s_port, 5222},
|
2016-09-23 11:30:33 +02:00
|
|
|
{loglevel, 4},
|
2015-04-20 16:16:30 +02:00
|
|
|
{s2s_port, 5269},
|
2016-09-21 09:45:11 +02:00
|
|
|
{component_port, 5270},
|
2015-04-09 17:14:30 +02:00
|
|
|
{web_port, 5280},
|
2016-09-21 09:45:11 +02:00
|
|
|
{password, Password},
|
2015-04-09 17:14:30 +02:00
|
|
|
{mysql_server, <<"localhost">>},
|
|
|
|
{mysql_port, 3306},
|
|
|
|
{mysql_db, <<"ejabberd_test">>},
|
|
|
|
{mysql_user, <<"ejabberd_test">>},
|
|
|
|
{mysql_pass, <<"ejabberd_test">>},
|
|
|
|
{pgsql_server, <<"localhost">>},
|
|
|
|
{pgsql_port, 5432},
|
|
|
|
{pgsql_db, <<"ejabberd_test">>},
|
|
|
|
{pgsql_user, <<"ejabberd_test">>},
|
|
|
|
{pgsql_pass, <<"ejabberd_test">>}
|
|
|
|
]),
|
|
|
|
ConfigPath = filename:join([CWD, "ejabberd.yml"]),
|
|
|
|
ok = file:write_file(ConfigPath, CfgContent),
|
2016-05-30 23:05:45 +02:00
|
|
|
setup_ejabberd_lib_path(Config),
|
2013-06-27 08:38:36 +02:00
|
|
|
ok = application:load(sasl),
|
|
|
|
ok = application:load(mnesia),
|
|
|
|
ok = application:load(ejabberd),
|
2013-06-26 19:55:29 +02:00
|
|
|
application:set_env(ejabberd, config, ConfigPath),
|
|
|
|
application:set_env(ejabberd, log_path, LogPath),
|
|
|
|
application:set_env(sasl, sasl_error_logger, {file, SASLPath}),
|
|
|
|
application:set_env(mnesia, dir, MnesiaDir),
|
2015-04-09 17:14:30 +02:00
|
|
|
[{server_port, ct:get_config(c2s_port, 5222)},
|
2013-06-26 19:55:29 +02:00
|
|
|
{server_host, "localhost"},
|
2016-09-21 09:45:11 +02:00
|
|
|
{component_port, ct:get_config(component_port, 5270)},
|
2016-09-23 11:30:33 +02:00
|
|
|
{s2s_port, ct:get_config(s2s_port, 5269)},
|
2013-06-26 19:55:29 +02:00
|
|
|
{server, ?COMMON_VHOST},
|
2016-05-24 17:44:07 +02:00
|
|
|
{user, <<"test_single!#$%^*()`~+-;_=[]{}|\\">>},
|
2016-10-17 12:37:23 +02:00
|
|
|
{nick, <<"nick!@#$%^&*()'\"`~<>+-/;:_=[]{}|\\">>},
|
2016-05-24 17:44:07 +02:00
|
|
|
{master_nick, <<"master_nick!@#$%^&*()'\"`~<>+-/;:_=[]{}|\\">>},
|
|
|
|
{slave_nick, <<"slave_nick!@#$%^&*()'\"`~<>+-/;:_=[]{}|\\">>},
|
|
|
|
{room_subject, <<"hello, world!@#$%^&*()'\"`~<>+-/;:_=[]{}|\\">>},
|
2013-06-26 19:55:29 +02:00
|
|
|
{certfile, CertFile},
|
2016-10-17 12:37:23 +02:00
|
|
|
{persistent_room, true},
|
2016-09-25 08:57:56 +02:00
|
|
|
{anonymous, false},
|
2016-09-21 09:45:11 +02:00
|
|
|
{type, client},
|
|
|
|
{xmlns, ?NS_CLIENT},
|
2016-09-20 13:04:07 +02:00
|
|
|
{ns_stream, ?NS_STREAM},
|
2016-09-23 11:30:33 +02:00
|
|
|
{stream_version, {1, 0}},
|
2016-09-20 13:04:07 +02:00
|
|
|
{stream_id, <<"">>},
|
2016-09-23 11:30:33 +02:00
|
|
|
{stream_from, <<"">>},
|
|
|
|
{db_xmlns, <<"">>},
|
2016-09-20 13:04:07 +02:00
|
|
|
{mechs, []},
|
2016-11-07 08:10:57 +01:00
|
|
|
{rosterver, false},
|
2016-09-20 13:04:07 +02:00
|
|
|
{lang, <<"en">>},
|
2013-06-26 19:55:29 +02:00
|
|
|
{base_dir, BaseDir},
|
2016-09-23 11:30:33 +02:00
|
|
|
{socket, undefined},
|
2016-10-07 09:31:03 +02:00
|
|
|
{pubsub_node, <<"node!@#$%^&*()'\"`~<>+-/;:_=[]{}|\\">>},
|
|
|
|
{pubsub_node_title, <<"title!@#$%^&*()'\"`~<>+-/;:_=[]{}|\\">>},
|
2016-05-24 17:44:07 +02:00
|
|
|
{resource, <<"resource!@#$%^&*()'\"`~<>+-/;:_=[]{}|\\">>},
|
|
|
|
{master_resource, <<"master_resource!@#$%^&*()'\"`~<>+-/;:_=[]{}|\\">>},
|
|
|
|
{slave_resource, <<"slave_resource!@#$%^&*()'\"`~<>+-/;:_=[]{}|\\">>},
|
2016-09-21 09:45:11 +02:00
|
|
|
{password, Password},
|
2016-03-24 10:02:13 +01:00
|
|
|
{backends, get_config_backends()}
|
2013-06-26 19:55:29 +02:00
|
|
|
|Config].
|
|
|
|
|
2016-05-30 23:05:45 +02:00
|
|
|
find_top_dir(Dir) ->
|
|
|
|
case file:read_file_info(filename:join([Dir, ebin])) of
|
|
|
|
{ok, #file_info{type = directory}} ->
|
|
|
|
Dir;
|
|
|
|
_ ->
|
|
|
|
find_top_dir(filename:dirname(Dir))
|
|
|
|
end.
|
|
|
|
|
|
|
|
setup_ejabberd_lib_path(Config) ->
|
|
|
|
case code:lib_dir(ejabberd) of
|
|
|
|
{error, _} ->
|
|
|
|
DataDir = proplists:get_value(data_dir, Config),
|
|
|
|
{ok, CWD} = file:get_cwd(),
|
|
|
|
NewEjPath = filename:join([CWD, "ejabberd-0.0.1"]),
|
|
|
|
TopDir = find_top_dir(DataDir),
|
|
|
|
ok = file:make_symlink(TopDir, NewEjPath),
|
|
|
|
code:replace_path(ejabberd, NewEjPath);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2016-03-24 10:02:13 +01:00
|
|
|
%% Read environment variable CT_DB=riak,mysql to limit the backends to test.
|
|
|
|
%% You can thus limit the backend you want to test with:
|
|
|
|
%% CT_BACKENDS=riak,mysql rebar ct suites=ejabberd
|
|
|
|
get_config_backends() ->
|
|
|
|
case os:getenv("CT_BACKENDS") of
|
|
|
|
false -> all;
|
|
|
|
String ->
|
|
|
|
Backends0 = string:tokens(String, ","),
|
|
|
|
lists:map(fun(Backend) -> string:strip(Backend, both, $ ) end, Backends0)
|
|
|
|
end.
|
|
|
|
|
2015-04-09 17:14:30 +02:00
|
|
|
process_config_tpl(Content, []) ->
|
|
|
|
Content;
|
|
|
|
process_config_tpl(Content, [{Name, DefaultValue} | Rest]) ->
|
|
|
|
Val = case ct:get_config(Name, DefaultValue) of
|
|
|
|
V1 when is_integer(V1) ->
|
|
|
|
integer_to_binary(V1);
|
|
|
|
V2 when is_atom(V2) ->
|
|
|
|
atom_to_binary(V2, latin1);
|
|
|
|
V3 ->
|
|
|
|
V3
|
|
|
|
end,
|
2016-11-21 12:06:45 +01:00
|
|
|
NewContent = binary:replace(Content,
|
|
|
|
<<"@@",(atom_to_binary(Name,latin1))/binary, "@@">>,
|
|
|
|
Val, [global]),
|
2015-04-09 17:14:30 +02:00
|
|
|
process_config_tpl(NewContent, Rest).
|
|
|
|
|
2016-09-20 13:04:07 +02:00
|
|
|
stream_header(Config) ->
|
|
|
|
To = case ?config(server, Config) of
|
2016-09-23 11:30:33 +02:00
|
|
|
<<"">> -> undefined;
|
|
|
|
Server -> jid:make(Server)
|
2016-09-20 13:04:07 +02:00
|
|
|
end,
|
2016-09-23 11:30:33 +02:00
|
|
|
From = case ?config(stream_from, Config) of
|
|
|
|
<<"">> -> undefined;
|
|
|
|
Frm -> jid:make(Frm)
|
|
|
|
end,
|
|
|
|
#stream_start{to = To,
|
|
|
|
from = From,
|
|
|
|
lang = ?config(lang, Config),
|
|
|
|
version = ?config(stream_version, Config),
|
|
|
|
xmlns = ?config(xmlns, Config),
|
|
|
|
db_xmlns = ?config(db_xmlns, Config),
|
|
|
|
stream_xmlns = ?config(ns_stream, Config)}.
|
2015-04-09 17:14:30 +02:00
|
|
|
|
2013-06-26 19:55:29 +02:00
|
|
|
connect(Config) ->
|
2016-09-21 09:45:11 +02:00
|
|
|
NewConfig = init_stream(Config),
|
|
|
|
case ?config(type, NewConfig) of
|
|
|
|
client -> process_stream_features(NewConfig);
|
2016-09-23 11:30:33 +02:00
|
|
|
server -> process_stream_features(NewConfig);
|
2016-09-21 09:45:11 +02:00
|
|
|
component -> NewConfig
|
|
|
|
end.
|
2016-09-20 13:04:07 +02:00
|
|
|
|
2016-09-23 11:30:33 +02:00
|
|
|
tcp_connect(Config) ->
|
|
|
|
case ?config(socket, Config) of
|
|
|
|
undefined ->
|
2016-10-22 12:01:45 +02:00
|
|
|
Owner = self(),
|
|
|
|
NS = case ?config(type, Config) of
|
|
|
|
client -> ?NS_CLIENT;
|
|
|
|
server -> ?NS_SERVER;
|
|
|
|
component -> ?NS_COMPONENT
|
|
|
|
end,
|
|
|
|
ReceiverPid = spawn(fun() -> receiver(NS, Owner) end),
|
2016-09-23 11:30:33 +02:00
|
|
|
{ok, Sock} = ejabberd_socket:connect(
|
|
|
|
?config(server_host, Config),
|
|
|
|
?config(server_port, Config),
|
2016-10-22 12:01:45 +02:00
|
|
|
[binary, {packet, 0}, {active, false}],
|
|
|
|
infinity, ReceiverPid),
|
2016-09-23 11:30:33 +02:00
|
|
|
set_opt(socket, Sock, Config);
|
|
|
|
_ ->
|
|
|
|
Config
|
|
|
|
end.
|
|
|
|
|
2016-09-20 13:04:07 +02:00
|
|
|
init_stream(Config) ->
|
|
|
|
Version = ?config(stream_version, Config),
|
2016-09-23 11:30:33 +02:00
|
|
|
NewConfig = tcp_connect(Config),
|
|
|
|
send(NewConfig, stream_header(NewConfig)),
|
2016-09-21 09:45:11 +02:00
|
|
|
XMLNS = case ?config(type, Config) of
|
|
|
|
client -> ?NS_CLIENT;
|
2016-09-23 11:30:33 +02:00
|
|
|
component -> ?NS_COMPONENT;
|
|
|
|
server -> ?NS_SERVER
|
2016-09-21 09:45:11 +02:00
|
|
|
end,
|
2016-11-08 13:15:19 +01:00
|
|
|
receive
|
|
|
|
#stream_start{id = ID, xmlns = XMLNS, version = Version} ->
|
|
|
|
set_opt(stream_id, ID, NewConfig)
|
|
|
|
end.
|
2013-06-26 19:55:29 +02:00
|
|
|
|
2016-09-20 13:04:07 +02:00
|
|
|
process_stream_features(Config) ->
|
2016-11-08 13:15:19 +01:00
|
|
|
receive
|
|
|
|
#stream_features{sub_els = Fs} ->
|
|
|
|
Mechs = lists:flatmap(
|
|
|
|
fun(#sasl_mechanisms{list = Ms}) ->
|
|
|
|
Ms;
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Fs),
|
|
|
|
lists:foldl(
|
|
|
|
fun(#feature_register{}, Acc) ->
|
|
|
|
set_opt(register, true, Acc);
|
|
|
|
(#starttls{}, Acc) ->
|
|
|
|
set_opt(starttls, true, Acc);
|
2017-01-23 11:51:05 +01:00
|
|
|
(#legacy_auth_feature{}, Acc) ->
|
|
|
|
set_opt(legacy_auth, true, Acc);
|
2016-11-08 13:15:19 +01:00
|
|
|
(#compression{methods = Ms}, Acc) ->
|
|
|
|
set_opt(compression, Ms, Acc);
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, set_opt(mechs, Mechs, Config), Fs)
|
|
|
|
end.
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
disconnect(Config) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
ct:comment("Disconnecting"),
|
2013-06-26 19:55:29 +02:00
|
|
|
Socket = ?config(socket, Config),
|
2016-09-21 09:45:11 +02:00
|
|
|
try
|
2017-01-23 11:51:05 +01:00
|
|
|
send_text(Config, ?STREAM_TRAILER)
|
2016-09-21 09:45:11 +02:00
|
|
|
catch exit:normal ->
|
|
|
|
ok
|
|
|
|
end,
|
2016-10-22 12:01:45 +02:00
|
|
|
receive {xmlstreamend, <<"stream:stream">>} -> ok end,
|
|
|
|
flush(Config),
|
2013-06-26 19:55:29 +02:00
|
|
|
ejabberd_socket:close(Socket),
|
2016-10-22 12:01:45 +02:00
|
|
|
ct:comment("Disconnected"),
|
|
|
|
set_opt(socket, undefined, Config).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
2014-07-22 11:59:31 +02:00
|
|
|
close_socket(Config) ->
|
|
|
|
Socket = ?config(socket, Config),
|
|
|
|
ejabberd_socket:close(Socket),
|
|
|
|
Config.
|
|
|
|
|
2013-06-26 19:55:29 +02:00
|
|
|
starttls(Config) ->
|
2016-09-23 11:30:33 +02:00
|
|
|
starttls(Config, false).
|
|
|
|
|
|
|
|
starttls(Config, ShouldFail) ->
|
2013-06-26 19:55:29 +02:00
|
|
|
send(Config, #starttls{}),
|
2016-11-08 13:15:19 +01:00
|
|
|
receive
|
2016-09-23 11:30:33 +02:00
|
|
|
#starttls_proceed{} when ShouldFail ->
|
|
|
|
ct:fail(starttls_should_have_failed);
|
|
|
|
#starttls_failure{} when ShouldFail ->
|
|
|
|
Config;
|
|
|
|
#starttls_failure{} ->
|
|
|
|
ct:fail(starttls_failed);
|
|
|
|
#starttls_proceed{} ->
|
2017-01-23 11:51:05 +01:00
|
|
|
{ok, TLSSocket} = ejabberd_socket:starttls(
|
|
|
|
?config(socket, Config),
|
|
|
|
[{certfile, ?config(certfile, Config)},
|
|
|
|
connect]),
|
2016-09-23 11:30:33 +02:00
|
|
|
set_opt(socket, TLSSocket, Config)
|
|
|
|
end.
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
zlib(Config) ->
|
|
|
|
send(Config, #compress{methods = [<<"zlib">>]}),
|
2016-11-08 13:15:19 +01:00
|
|
|
receive #compressed{} -> ok end,
|
2017-01-23 11:51:05 +01:00
|
|
|
{ok, ZlibSocket} = ejabberd_socket:compress(?config(socket, Config)),
|
2016-09-20 13:04:07 +02:00
|
|
|
process_stream_features(init_stream(set_opt(socket, ZlibSocket, Config))).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
auth(Config) ->
|
2016-09-20 13:04:07 +02:00
|
|
|
auth(Config, false).
|
|
|
|
|
|
|
|
auth(Config, ShouldFail) ->
|
2016-09-21 09:45:11 +02:00
|
|
|
Type = ?config(type, Config),
|
2016-09-25 08:57:56 +02:00
|
|
|
IsAnonymous = ?config(anonymous, Config),
|
2013-06-26 19:55:29 +02:00
|
|
|
Mechs = ?config(mechs, Config),
|
|
|
|
HaveMD5 = lists:member(<<"DIGEST-MD5">>, Mechs),
|
|
|
|
HavePLAIN = lists:member(<<"PLAIN">>, Mechs),
|
2016-09-23 11:30:33 +02:00
|
|
|
HaveExternal = lists:member(<<"EXTERNAL">>, Mechs),
|
2016-09-25 08:57:56 +02:00
|
|
|
HaveAnonymous = lists:member(<<"ANONYMOUS">>, Mechs),
|
|
|
|
if HaveAnonymous and IsAnonymous ->
|
|
|
|
auth_SASL(<<"ANONYMOUS">>, Config, ShouldFail);
|
|
|
|
HavePLAIN ->
|
2016-09-20 13:04:07 +02:00
|
|
|
auth_SASL(<<"PLAIN">>, Config, ShouldFail);
|
2013-06-26 19:55:29 +02:00
|
|
|
HaveMD5 ->
|
2016-09-20 13:04:07 +02:00
|
|
|
auth_SASL(<<"DIGEST-MD5">>, Config, ShouldFail);
|
2017-03-07 12:20:50 +01:00
|
|
|
HaveExternal ->
|
2016-09-23 11:30:33 +02:00
|
|
|
auth_SASL(<<"EXTERNAL">>, Config, ShouldFail);
|
2016-09-21 09:45:11 +02:00
|
|
|
Type == client ->
|
|
|
|
auth_legacy(Config, false, ShouldFail);
|
|
|
|
Type == component ->
|
2016-09-23 11:30:33 +02:00
|
|
|
auth_component(Config, ShouldFail);
|
|
|
|
true ->
|
|
|
|
ct:fail(no_known_sasl_mechanism_available)
|
2013-06-26 19:55:29 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
bind(Config) ->
|
2016-09-25 08:57:56 +02:00
|
|
|
U = ?config(user, Config),
|
|
|
|
S = ?config(server, Config),
|
|
|
|
R = ?config(resource, Config),
|
2016-09-21 09:45:11 +02:00
|
|
|
case ?config(type, Config) of
|
|
|
|
client ->
|
2016-09-25 08:57:56 +02:00
|
|
|
#iq{type = result, sub_els = [#bind{jid = JID}]} =
|
2016-09-21 09:45:11 +02:00
|
|
|
send_recv(
|
2016-09-25 08:57:56 +02:00
|
|
|
Config, #iq{type = set, sub_els = [#bind{resource = R}]}),
|
|
|
|
case ?config(anonymous, Config) of
|
|
|
|
false ->
|
|
|
|
{U, S, R} = jid:tolower(JID),
|
|
|
|
Config;
|
|
|
|
true ->
|
|
|
|
{User, S, Resource} = jid:tolower(JID),
|
|
|
|
set_opt(user, User, set_opt(resource, Resource, Config))
|
|
|
|
end;
|
2016-09-21 09:45:11 +02:00
|
|
|
component ->
|
2016-09-25 08:57:56 +02:00
|
|
|
Config
|
|
|
|
end.
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
open_session(Config) ->
|
2016-09-20 13:04:07 +02:00
|
|
|
open_session(Config, false).
|
|
|
|
|
|
|
|
open_session(Config, Force) ->
|
|
|
|
if Force ->
|
|
|
|
#iq{type = result, sub_els = []} =
|
|
|
|
send_recv(Config, #iq{type = set, sub_els = [#xmpp_session{}]});
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2013-06-26 19:55:29 +02:00
|
|
|
Config.
|
|
|
|
|
2016-09-20 13:04:07 +02:00
|
|
|
auth_legacy(Config, IsDigest) ->
|
|
|
|
auth_legacy(Config, IsDigest, false).
|
|
|
|
|
|
|
|
auth_legacy(Config, IsDigest, ShouldFail) ->
|
|
|
|
ServerJID = server_jid(Config),
|
|
|
|
U = ?config(user, Config),
|
|
|
|
R = ?config(resource, Config),
|
|
|
|
P = ?config(password, Config),
|
|
|
|
#iq{type = result,
|
|
|
|
from = ServerJID,
|
|
|
|
sub_els = [#legacy_auth{username = <<"">>,
|
|
|
|
password = <<"">>,
|
|
|
|
resource = <<"">>} = Auth]} =
|
|
|
|
send_recv(Config,
|
|
|
|
#iq{to = ServerJID, type = get,
|
|
|
|
sub_els = [#legacy_auth{}]}),
|
|
|
|
Res = case Auth#legacy_auth.digest of
|
|
|
|
<<"">> when IsDigest ->
|
|
|
|
StreamID = ?config(stream_id, Config),
|
|
|
|
D = p1_sha:sha(<<StreamID/binary, P/binary>>),
|
|
|
|
send_recv(Config, #iq{to = ServerJID, type = set,
|
|
|
|
sub_els = [#legacy_auth{username = U,
|
|
|
|
resource = R,
|
|
|
|
digest = D}]});
|
|
|
|
_ when not IsDigest ->
|
|
|
|
send_recv(Config, #iq{to = ServerJID, type = set,
|
|
|
|
sub_els = [#legacy_auth{username = U,
|
|
|
|
resource = R,
|
|
|
|
password = P}]})
|
|
|
|
end,
|
|
|
|
case Res of
|
|
|
|
#iq{from = ServerJID, type = result, sub_els = []} ->
|
|
|
|
if ShouldFail ->
|
|
|
|
ct:fail(legacy_auth_should_have_failed);
|
|
|
|
true ->
|
|
|
|
Config
|
|
|
|
end;
|
|
|
|
#iq{from = ServerJID, type = error} ->
|
|
|
|
if ShouldFail ->
|
|
|
|
Config;
|
|
|
|
true ->
|
|
|
|
ct:fail(legacy_auth_failed)
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2016-09-21 09:45:11 +02:00
|
|
|
auth_component(Config, ShouldFail) ->
|
|
|
|
StreamID = ?config(stream_id, Config),
|
|
|
|
Password = ?config(password, Config),
|
|
|
|
Digest = p1_sha:sha(<<StreamID/binary, Password/binary>>),
|
|
|
|
send(Config, #handshake{data = Digest}),
|
2016-11-08 13:15:19 +01:00
|
|
|
receive
|
2016-09-21 09:45:11 +02:00
|
|
|
#handshake{} when ShouldFail ->
|
|
|
|
ct:fail(component_auth_should_have_failed);
|
|
|
|
#handshake{} ->
|
|
|
|
Config;
|
|
|
|
#stream_error{reason = 'not-authorized'} when ShouldFail ->
|
|
|
|
Config;
|
|
|
|
#stream_error{reason = 'not-authorized'} ->
|
|
|
|
ct:fail(component_auth_failed)
|
|
|
|
end.
|
|
|
|
|
2013-06-26 19:55:29 +02:00
|
|
|
auth_SASL(Mech, Config) ->
|
2016-09-20 13:04:07 +02:00
|
|
|
auth_SASL(Mech, Config, false).
|
|
|
|
|
|
|
|
auth_SASL(Mech, Config, ShouldFail) ->
|
2017-03-07 12:20:50 +01:00
|
|
|
Creds = {?config(user, Config),
|
|
|
|
?config(server, Config),
|
|
|
|
?config(password, Config)},
|
|
|
|
auth_SASL(Mech, Config, ShouldFail, Creds).
|
|
|
|
|
|
|
|
auth_SASL(Mech, Config, ShouldFail, Creds) ->
|
|
|
|
{Response, SASL} = sasl_new(Mech, Creds),
|
2013-06-26 19:55:29 +02:00
|
|
|
send(Config, #sasl_auth{mechanism = Mech, text = Response}),
|
2016-09-20 13:04:07 +02:00
|
|
|
wait_auth_SASL_result(set_opt(sasl, SASL, Config), ShouldFail).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
2016-09-20 13:04:07 +02:00
|
|
|
wait_auth_SASL_result(Config, ShouldFail) ->
|
2016-11-08 13:15:19 +01:00
|
|
|
receive
|
2016-09-20 13:04:07 +02:00
|
|
|
#sasl_success{} when ShouldFail ->
|
|
|
|
ct:fail(sasl_auth_should_have_failed);
|
2013-06-26 19:55:29 +02:00
|
|
|
#sasl_success{} ->
|
|
|
|
ejabberd_socket:reset_stream(?config(socket, Config)),
|
2016-09-23 11:30:33 +02:00
|
|
|
send(Config, stream_header(Config)),
|
|
|
|
Type = ?config(type, Config),
|
|
|
|
NS = if Type == client -> ?NS_CLIENT;
|
|
|
|
Type == server -> ?NS_SERVER
|
|
|
|
end,
|
2016-11-08 13:15:19 +01:00
|
|
|
receive #stream_start{xmlns = NS, version = {1,0}} -> ok end,
|
|
|
|
receive #stream_features{sub_els = Fs} ->
|
|
|
|
if Type == client ->
|
|
|
|
#xmpp_session{optional = true} =
|
|
|
|
lists:keyfind(xmpp_session, 1, Fs);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
lists:foldl(
|
|
|
|
fun(#feature_sm{}, ConfigAcc) ->
|
|
|
|
set_opt(sm, true, ConfigAcc);
|
|
|
|
(#feature_csi{}, ConfigAcc) ->
|
|
|
|
set_opt(csi, true, ConfigAcc);
|
|
|
|
(#rosterver_feature{}, ConfigAcc) ->
|
|
|
|
set_opt(rosterver, true, ConfigAcc);
|
|
|
|
(_, ConfigAcc) ->
|
|
|
|
ConfigAcc
|
|
|
|
end, Config, Fs)
|
|
|
|
end;
|
2013-06-26 19:55:29 +02:00
|
|
|
#sasl_challenge{text = ClientIn} ->
|
|
|
|
{Response, SASL} = (?config(sasl, Config))(ClientIn),
|
|
|
|
send(Config, #sasl_response{text = Response}),
|
2016-09-20 13:04:07 +02:00
|
|
|
wait_auth_SASL_result(set_opt(sasl, SASL, Config), ShouldFail);
|
|
|
|
#sasl_failure{} when ShouldFail ->
|
|
|
|
Config;
|
2013-06-26 19:55:29 +02:00
|
|
|
#sasl_failure{} ->
|
|
|
|
ct:fail(sasl_auth_failed)
|
|
|
|
end.
|
|
|
|
|
|
|
|
re_register(Config) ->
|
|
|
|
User = ?config(user, Config),
|
|
|
|
Server = ?config(server, Config),
|
|
|
|
Pass = ?config(password, Config),
|
|
|
|
{atomic, ok} = ejabberd_auth:try_register(User, Server, Pass),
|
|
|
|
ok.
|
|
|
|
|
2015-04-09 16:39:52 +02:00
|
|
|
match_failure(Received, [Match]) when is_list(Match)->
|
|
|
|
ct:fail("Received input:~n~n~p~n~ndon't match expected patterns:~n~n~s", [Received, Match]);
|
2015-02-04 14:42:03 +01:00
|
|
|
match_failure(Received, Matches) ->
|
|
|
|
ct:fail("Received input:~n~n~p~n~ndon't match expected patterns:~n~n~p", [Received, Matches]).
|
|
|
|
|
2016-10-22 12:01:45 +02:00
|
|
|
recv(_Config) ->
|
2013-06-26 19:55:29 +02:00
|
|
|
receive
|
2016-10-22 12:01:45 +02:00
|
|
|
{fail, El, Why} ->
|
|
|
|
ct:fail("recv failed: ~p->~n~s",
|
|
|
|
[El, xmpp:format_error(Why)]);
|
|
|
|
Event ->
|
|
|
|
Event
|
2013-06-26 19:55:29 +02:00
|
|
|
end.
|
|
|
|
|
2016-10-22 12:01:45 +02:00
|
|
|
recv_iq(_Config) ->
|
|
|
|
receive #iq{} = IQ -> IQ end.
|
2016-10-17 12:37:23 +02:00
|
|
|
|
2016-10-22 12:01:45 +02:00
|
|
|
recv_presence(_Config) ->
|
|
|
|
receive #presence{} = Pres -> Pres end.
|
2016-10-17 12:37:23 +02:00
|
|
|
|
2016-10-22 12:01:45 +02:00
|
|
|
recv_message(_Config) ->
|
|
|
|
receive #message{} = Msg -> Msg end.
|
2016-10-17 12:37:23 +02:00
|
|
|
|
2016-10-22 12:01:45 +02:00
|
|
|
decode_stream_element(NS, El) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
decode(El, NS, []).
|
|
|
|
|
|
|
|
format_element(El) ->
|
|
|
|
case erlang:function_exported(ct, log, 5) of
|
|
|
|
true -> ejabberd_web_admin:pretty_print_xml(El);
|
2016-11-21 20:04:25 +01:00
|
|
|
false -> io_lib:format("~p~n", [El])
|
2016-10-17 12:37:23 +02:00
|
|
|
end.
|
|
|
|
|
2016-09-24 13:17:21 +02:00
|
|
|
decode(El, NS, Opts) ->
|
2016-09-20 13:04:07 +02:00
|
|
|
try
|
2016-11-18 11:51:57 +01:00
|
|
|
Pkt = xmpp:decode(El, NS, Opts),
|
2016-10-17 12:37:23 +02:00
|
|
|
ct:pal("RECV:~n~s~n~s",
|
|
|
|
[format_element(El), xmpp:pp(Pkt)]),
|
2016-09-20 13:04:07 +02:00
|
|
|
Pkt
|
|
|
|
catch _:{xmpp_codec, Why} ->
|
2016-11-18 11:38:08 +01:00
|
|
|
ct:pal("recv failed: ~p->~n~s",
|
|
|
|
[El, xmpp:format_error(Why)]),
|
|
|
|
erlang:error({xmpp_codec, Why})
|
2016-09-20 13:04:07 +02:00
|
|
|
end.
|
|
|
|
|
2013-06-26 19:55:29 +02:00
|
|
|
send_text(Config, Text) ->
|
|
|
|
ejabberd_socket:send(?config(socket, Config), Text).
|
|
|
|
|
|
|
|
send(State, Pkt) ->
|
|
|
|
{NewID, NewPkt} = case Pkt of
|
|
|
|
#message{id = I} ->
|
|
|
|
ID = id(I),
|
|
|
|
{ID, Pkt#message{id = ID}};
|
|
|
|
#presence{id = I} ->
|
|
|
|
ID = id(I),
|
|
|
|
{ID, Pkt#presence{id = ID}};
|
|
|
|
#iq{id = I} ->
|
|
|
|
ID = id(I),
|
|
|
|
{ID, Pkt#iq{id = ID}};
|
|
|
|
_ ->
|
|
|
|
{undefined, Pkt}
|
|
|
|
end,
|
2016-09-24 13:17:21 +02:00
|
|
|
El = xmpp:encode(NewPkt),
|
2016-10-17 12:37:23 +02:00
|
|
|
ct:pal("SENT:~n~s~n~s",
|
|
|
|
[format_element(El), xmpp:pp(NewPkt)]),
|
2016-09-23 11:30:33 +02:00
|
|
|
Data = case NewPkt of
|
|
|
|
#stream_start{} -> fxml:element_to_header(El);
|
|
|
|
_ -> fxml:element_to_binary(El)
|
|
|
|
end,
|
|
|
|
ok = send_text(State, Data),
|
2013-06-26 19:55:29 +02:00
|
|
|
NewID.
|
|
|
|
|
2016-10-17 12:37:23 +02:00
|
|
|
send_recv(State, #message{} = Msg) ->
|
|
|
|
ID = send(State, Msg),
|
2016-10-22 12:01:45 +02:00
|
|
|
receive #message{id = ID} = Result -> Result end;
|
2016-10-17 12:37:23 +02:00
|
|
|
send_recv(State, #presence{} = Pres) ->
|
|
|
|
ID = send(State, Pres),
|
2016-10-22 12:01:45 +02:00
|
|
|
receive #presence{id = ID} = Result -> Result end;
|
2016-10-17 12:37:23 +02:00
|
|
|
send_recv(State, #iq{} = IQ) ->
|
2013-06-26 19:55:29 +02:00
|
|
|
ID = send(State, IQ),
|
2016-10-22 12:01:45 +02:00
|
|
|
receive #iq{id = ID} = Result -> Result end.
|
2013-06-26 19:55:29 +02:00
|
|
|
|
2017-03-07 12:20:50 +01:00
|
|
|
sasl_new(<<"PLAIN">>, {User, Server, Password}) ->
|
2013-06-26 19:55:29 +02:00
|
|
|
{<<User/binary, $@, Server/binary, 0, User/binary, 0, Password/binary>>,
|
|
|
|
fun (_) -> {error, <<"Invalid SASL challenge">>} end};
|
2017-03-07 12:20:50 +01:00
|
|
|
sasl_new(<<"EXTERNAL">>, {User, Server, _Password}) ->
|
|
|
|
{jid:encode(jid:make(User, Server)),
|
2016-09-23 11:30:33 +02:00
|
|
|
fun(_) -> ct:fail(sasl_challenge_is_not_expected) end};
|
2017-03-07 12:20:50 +01:00
|
|
|
sasl_new(<<"ANONYMOUS">>, _) ->
|
2016-09-25 08:57:56 +02:00
|
|
|
{<<"">>,
|
|
|
|
fun(_) -> ct:fail(sasl_challenge_is_not_expected) end};
|
2017-03-07 12:20:50 +01:00
|
|
|
sasl_new(<<"DIGEST-MD5">>, {User, Server, Password}) ->
|
2013-06-26 19:55:29 +02:00
|
|
|
{<<"">>,
|
|
|
|
fun (ServerIn) ->
|
|
|
|
case cyrsasl_digest:parse(ServerIn) of
|
|
|
|
bad -> {error, <<"Invalid SASL challenge">>};
|
|
|
|
KeyVals ->
|
2016-02-03 19:22:59 +01:00
|
|
|
Nonce = fxml:get_attr_s(<<"nonce">>, KeyVals),
|
2013-06-26 19:55:29 +02:00
|
|
|
CNonce = id(),
|
|
|
|
Realm = proplists:get_value(<<"realm">>, KeyVals, Server),
|
|
|
|
DigestURI = <<"xmpp/", Realm/binary>>,
|
|
|
|
NC = <<"00000001">>,
|
|
|
|
QOP = <<"auth">>,
|
|
|
|
AuthzId = <<"">>,
|
|
|
|
MyResponse = response(User, Password, Nonce, AuthzId,
|
|
|
|
Realm, CNonce, DigestURI, NC, QOP,
|
|
|
|
<<"AUTHENTICATE">>),
|
2016-05-24 17:44:07 +02:00
|
|
|
SUser = << <<(case Char of
|
|
|
|
$" -> <<"\\\"">>;
|
|
|
|
$\\ -> <<"\\\\">>;
|
|
|
|
_ -> <<Char>>
|
|
|
|
end)/binary>> || <<Char>> <= User >>,
|
|
|
|
Resp = <<"username=\"", SUser/binary, "\",realm=\"",
|
2013-06-26 19:55:29 +02:00
|
|
|
Realm/binary, "\",nonce=\"", Nonce/binary,
|
|
|
|
"\",cnonce=\"", CNonce/binary, "\",nc=", NC/binary,
|
|
|
|
",qop=", QOP/binary, ",digest-uri=\"",
|
|
|
|
DigestURI/binary, "\",response=\"",
|
|
|
|
MyResponse/binary, "\"">>,
|
|
|
|
{Resp,
|
|
|
|
fun (ServerIn2) ->
|
|
|
|
case cyrsasl_digest:parse(ServerIn2) of
|
|
|
|
bad -> {error, <<"Invalid SASL challenge">>};
|
2013-06-26 20:22:24 +02:00
|
|
|
_KeyVals2 ->
|
|
|
|
{<<"">>,
|
|
|
|
fun (_) ->
|
|
|
|
{error,
|
|
|
|
<<"Invalid SASL challenge">>}
|
|
|
|
end}
|
2013-06-26 19:55:29 +02:00
|
|
|
end
|
|
|
|
end}
|
|
|
|
end
|
|
|
|
end}.
|
|
|
|
|
|
|
|
hex(S) ->
|
|
|
|
p1_sha:to_hexlist(S).
|
|
|
|
|
|
|
|
response(User, Passwd, Nonce, AuthzId, Realm, CNonce,
|
|
|
|
DigestURI, NC, QOP, A2Prefix) ->
|
|
|
|
A1 = case AuthzId of
|
|
|
|
<<"">> ->
|
2013-11-05 11:05:12 +01:00
|
|
|
<<((erlang:md5(<<User/binary, ":", Realm/binary, ":",
|
2013-06-26 19:55:29 +02:00
|
|
|
Passwd/binary>>)))/binary,
|
|
|
|
":", Nonce/binary, ":", CNonce/binary>>;
|
|
|
|
_ ->
|
2013-11-05 11:05:12 +01:00
|
|
|
<<((erlang:md5(<<User/binary, ":", Realm/binary, ":",
|
2013-06-26 19:55:29 +02:00
|
|
|
Passwd/binary>>)))/binary,
|
|
|
|
":", Nonce/binary, ":", CNonce/binary, ":",
|
|
|
|
AuthzId/binary>>
|
|
|
|
end,
|
|
|
|
A2 = case QOP of
|
|
|
|
<<"auth">> ->
|
|
|
|
<<A2Prefix/binary, ":", DigestURI/binary>>;
|
|
|
|
_ ->
|
|
|
|
<<A2Prefix/binary, ":", DigestURI/binary,
|
|
|
|
":00000000000000000000000000000000">>
|
|
|
|
end,
|
2013-11-05 11:05:12 +01:00
|
|
|
T = <<(hex((erlang:md5(A1))))/binary, ":", Nonce/binary,
|
2013-06-26 19:55:29 +02:00
|
|
|
":", NC/binary, ":", CNonce/binary, ":", QOP/binary,
|
2013-11-05 11:05:12 +01:00
|
|
|
":", (hex((erlang:md5(A2))))/binary>>,
|
|
|
|
hex((erlang:md5(T))).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
my_jid(Config) ->
|
2016-02-11 10:19:23 +01:00
|
|
|
jid:make(?config(user, Config),
|
|
|
|
?config(server, Config),
|
|
|
|
?config(resource, Config)).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
server_jid(Config) ->
|
2016-02-11 10:19:23 +01:00
|
|
|
jid:make(<<>>, ?config(server, Config), <<>>).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
pubsub_jid(Config) ->
|
|
|
|
Server = ?config(server, Config),
|
2016-02-11 10:19:23 +01:00
|
|
|
jid:make(<<>>, <<"pubsub.", Server/binary>>, <<>>).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
proxy_jid(Config) ->
|
|
|
|
Server = ?config(server, Config),
|
2016-02-11 10:19:23 +01:00
|
|
|
jid:make(<<>>, <<"proxy.", Server/binary>>, <<>>).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
muc_jid(Config) ->
|
|
|
|
Server = ?config(server, Config),
|
2016-02-11 10:19:23 +01:00
|
|
|
jid:make(<<>>, <<"conference.", Server/binary>>, <<>>).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
muc_room_jid(Config) ->
|
|
|
|
Server = ?config(server, Config),
|
2016-02-11 10:19:23 +01:00
|
|
|
jid:make(<<"test">>, <<"conference.", Server/binary>>, <<>>).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
2016-10-17 12:37:23 +02:00
|
|
|
my_muc_jid(Config) ->
|
|
|
|
Nick = ?config(nick, Config),
|
|
|
|
RoomJID = muc_room_jid(Config),
|
|
|
|
jid:replace_resource(RoomJID, Nick).
|
|
|
|
|
|
|
|
peer_muc_jid(Config) ->
|
|
|
|
PeerNick = ?config(peer_nick, Config),
|
|
|
|
RoomJID = muc_room_jid(Config),
|
|
|
|
jid:replace_resource(RoomJID, PeerNick).
|
|
|
|
|
|
|
|
alt_room_jid(Config) ->
|
|
|
|
Server = ?config(server, Config),
|
|
|
|
jid:make(<<"alt">>, <<"conference.", Server/binary>>, <<>>).
|
|
|
|
|
2016-03-10 15:42:37 +01:00
|
|
|
mix_jid(Config) ->
|
|
|
|
Server = ?config(server, Config),
|
|
|
|
jid:make(<<>>, <<"mix.", Server/binary>>, <<>>).
|
|
|
|
|
|
|
|
mix_room_jid(Config) ->
|
|
|
|
Server = ?config(server, Config),
|
|
|
|
jid:make(<<"test">>, <<"mix.", Server/binary>>, <<>>).
|
|
|
|
|
2013-06-26 19:55:29 +02:00
|
|
|
id() ->
|
2016-09-13 11:30:05 +02:00
|
|
|
id(<<>>).
|
2013-06-26 19:55:29 +02:00
|
|
|
|
2016-09-13 11:30:05 +02:00
|
|
|
id(<<>>) ->
|
2013-06-26 19:55:29 +02:00
|
|
|
randoms:get_string();
|
|
|
|
id(ID) ->
|
|
|
|
ID.
|
|
|
|
|
|
|
|
get_features(Config) ->
|
|
|
|
get_features(Config, server_jid(Config)).
|
|
|
|
|
|
|
|
get_features(Config, To) ->
|
2017-03-07 16:11:28 +01:00
|
|
|
ct:comment("Getting features of ~s", [jid:encode(To)]),
|
2013-06-26 19:55:29 +02:00
|
|
|
#iq{type = result, sub_els = [#disco_info{features = Features}]} =
|
|
|
|
send_recv(Config, #iq{type = get, sub_els = [#disco_info{}], to = To}),
|
|
|
|
Features.
|
|
|
|
|
|
|
|
is_feature_advertised(Config, Feature) ->
|
|
|
|
is_feature_advertised(Config, Feature, server_jid(Config)).
|
|
|
|
|
|
|
|
is_feature_advertised(Config, Feature, To) ->
|
|
|
|
Features = get_features(Config, To),
|
|
|
|
lists:member(Feature, Features).
|
|
|
|
|
|
|
|
set_opt(Opt, Val, Config) ->
|
|
|
|
[{Opt, Val}|lists:keydelete(Opt, 1, Config)].
|
|
|
|
|
|
|
|
wait_for_master(Config) ->
|
2016-11-07 08:10:57 +01:00
|
|
|
put_event(Config, peer_ready),
|
2016-09-23 11:30:33 +02:00
|
|
|
case get_event(Config) of
|
2016-11-07 08:10:57 +01:00
|
|
|
peer_ready ->
|
2016-09-23 11:30:33 +02:00
|
|
|
ok;
|
|
|
|
Other ->
|
2016-11-07 08:10:57 +01:00
|
|
|
suite:match_failure(Other, peer_ready)
|
2016-09-23 11:30:33 +02:00
|
|
|
end.
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
wait_for_slave(Config) ->
|
2016-11-07 08:10:57 +01:00
|
|
|
put_event(Config, peer_ready),
|
2016-09-23 11:30:33 +02:00
|
|
|
case get_event(Config) of
|
2016-11-07 08:10:57 +01:00
|
|
|
peer_ready ->
|
2016-09-23 11:30:33 +02:00
|
|
|
ok;
|
|
|
|
Other ->
|
2016-11-07 08:10:57 +01:00
|
|
|
suite:match_failure(Other, peer_ready)
|
2016-09-23 11:30:33 +02:00
|
|
|
end.
|
2013-06-26 19:55:29 +02:00
|
|
|
|
|
|
|
make_iq_result(#iq{from = From} = IQ) ->
|
|
|
|
IQ#iq{type = result, to = From, from = undefined, sub_els = []}.
|
|
|
|
|
2016-11-07 08:10:57 +01:00
|
|
|
self_presence(Config, Type) ->
|
|
|
|
MyJID = my_jid(Config),
|
|
|
|
ct:comment("Sending self-presence"),
|
|
|
|
#presence{type = Type, from = MyJID} =
|
|
|
|
send_recv(Config, #presence{type = Type}).
|
|
|
|
|
2016-10-22 12:01:45 +02:00
|
|
|
set_roster(Config, Subscription, Groups) ->
|
|
|
|
MyJID = my_jid(Config),
|
|
|
|
{U, S, _} = jid:tolower(MyJID),
|
|
|
|
PeerJID = ?config(peer, Config),
|
|
|
|
PeerBareJID = jid:remove_resource(PeerJID),
|
|
|
|
PeerLJID = jid:tolower(PeerBareJID),
|
|
|
|
ct:comment("Adding ~s to roster with subscription '~s' in groups ~p",
|
2017-03-07 16:11:28 +01:00
|
|
|
[jid:encode(PeerBareJID), Subscription, Groups]),
|
2016-10-22 12:01:45 +02:00
|
|
|
{atomic, _} = mod_roster:set_roster(#roster{usj = {U, S, PeerLJID},
|
|
|
|
us = {U, S},
|
|
|
|
jid = PeerLJID,
|
|
|
|
subscription = Subscription,
|
|
|
|
groups = Groups}),
|
|
|
|
Config.
|
|
|
|
|
|
|
|
del_roster(Config) ->
|
2016-11-07 08:10:57 +01:00
|
|
|
del_roster(Config, ?config(peer, Config)).
|
|
|
|
|
|
|
|
del_roster(Config, PeerJID) ->
|
2016-10-22 12:01:45 +02:00
|
|
|
MyJID = my_jid(Config),
|
|
|
|
{U, S, _} = jid:tolower(MyJID),
|
|
|
|
PeerBareJID = jid:remove_resource(PeerJID),
|
|
|
|
PeerLJID = jid:tolower(PeerBareJID),
|
2017-03-07 16:11:28 +01:00
|
|
|
ct:comment("Removing ~s from roster", [jid:encode(PeerBareJID)]),
|
2016-10-22 12:01:45 +02:00
|
|
|
{atomic, _} = mod_roster:del_roster(U, S, PeerLJID),
|
|
|
|
Config.
|
|
|
|
|
2016-11-07 08:10:57 +01:00
|
|
|
get_roster(Config) ->
|
|
|
|
{LUser, LServer, _} = jid:tolower(my_jid(Config)),
|
|
|
|
mod_roster:get_roster(LUser, LServer).
|
|
|
|
|
2016-10-22 12:01:45 +02:00
|
|
|
receiver(NS, Owner) ->
|
|
|
|
MRef = erlang:monitor(process, Owner),
|
|
|
|
receiver(NS, Owner, MRef).
|
|
|
|
|
|
|
|
receiver(NS, Owner, MRef) ->
|
|
|
|
receive
|
|
|
|
{'$gen_event', {xmlstreamelement, El}} ->
|
|
|
|
Owner ! decode_stream_element(NS, El),
|
|
|
|
receiver(NS, Owner, MRef);
|
|
|
|
{'$gen_event', {xmlstreamstart, Name, Attrs}} ->
|
|
|
|
Owner ! decode(#xmlel{name = Name, attrs = Attrs}, <<>>, []),
|
|
|
|
receiver(NS, Owner, MRef);
|
|
|
|
{'$gen_event', Event} ->
|
|
|
|
Owner ! Event,
|
|
|
|
receiver(NS, Owner, MRef);
|
|
|
|
{'DOWN', MRef, process, Owner, _} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2013-06-26 19:55:29 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Clients puts and gets events via this relay.
|
|
|
|
%%%===================================================================
|
|
|
|
start_event_relay() ->
|
|
|
|
spawn(fun event_relay/0).
|
|
|
|
|
|
|
|
stop_event_relay(Config) ->
|
|
|
|
Pid = ?config(event_relay, Config),
|
|
|
|
exit(Pid, normal).
|
|
|
|
|
|
|
|
event_relay() ->
|
|
|
|
event_relay([], []).
|
|
|
|
|
|
|
|
event_relay(Events, Subscribers) ->
|
|
|
|
receive
|
|
|
|
{subscribe, From} ->
|
2016-09-23 11:30:33 +02:00
|
|
|
erlang:monitor(process, From),
|
2013-06-26 19:55:29 +02:00
|
|
|
From ! {ok, self()},
|
|
|
|
lists:foreach(
|
|
|
|
fun(Event) -> From ! {event, Event, self()}
|
|
|
|
end, Events),
|
|
|
|
event_relay(Events, [From|Subscribers]);
|
|
|
|
{put, Event, From} ->
|
|
|
|
From ! {ok, self()},
|
|
|
|
lists:foreach(
|
|
|
|
fun(Pid) when Pid /= From ->
|
|
|
|
Pid ! {event, Event, self()};
|
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end, Subscribers),
|
2016-09-23 11:30:33 +02:00
|
|
|
event_relay([Event|Events], Subscribers);
|
|
|
|
{'DOWN', _MRef, process, Pid, _Info} ->
|
2016-10-22 12:01:45 +02:00
|
|
|
case lists:member(Pid, Subscribers) of
|
|
|
|
true ->
|
|
|
|
NewSubscribers = lists:delete(Pid, Subscribers),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Subscriber) ->
|
|
|
|
Subscriber ! {event, peer_down, self()}
|
|
|
|
end, NewSubscribers),
|
|
|
|
event_relay(Events, NewSubscribers);
|
|
|
|
false ->
|
|
|
|
event_relay(Events, Subscribers)
|
|
|
|
end
|
2013-06-26 19:55:29 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
subscribe_to_events(Config) ->
|
|
|
|
Relay = ?config(event_relay, Config),
|
|
|
|
Relay ! {subscribe, self()},
|
|
|
|
receive
|
|
|
|
{ok, Relay} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
put_event(Config, Event) ->
|
|
|
|
Relay = ?config(event_relay, Config),
|
|
|
|
Relay ! {put, Event, self()},
|
|
|
|
receive
|
|
|
|
{ok, Relay} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_event(Config) ->
|
|
|
|
Relay = ?config(event_relay, Config),
|
|
|
|
receive
|
|
|
|
{event, Event, Relay} ->
|
|
|
|
Event
|
|
|
|
end.
|
2016-10-17 12:37:23 +02:00
|
|
|
|
|
|
|
flush(Config) ->
|
2016-10-22 12:01:45 +02:00
|
|
|
receive
|
|
|
|
{event, peer_down, _} -> flush(Config);
|
|
|
|
closed -> flush(Config);
|
|
|
|
Msg -> ct:fail({unexpected_msg, Msg})
|
|
|
|
after 0 ->
|
|
|
|
ok
|
|
|
|
end.
|