diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/ejabberd_admin_test.exs | 41 | ||||
-rw-r--r-- | test/ejabberd_commands_mock_test.exs | 481 | ||||
-rw-r--r-- | test/ejabberd_commands_test.exs | 108 | ||||
-rw-r--r-- | test/ejabberd_cyrsasl_test.exs | 1 | ||||
-rw-r--r-- | test/elixir_SUITE.erl | 2 | ||||
-rw-r--r-- | test/ldap_srv.erl | 8 | ||||
-rw-r--r-- | test/mod_admin_extra_test.exs | 106 | ||||
-rw-r--r-- | test/mod_http_api_mock_test.exs | 31 | ||||
-rw-r--r-- | test/mod_http_api_test.exs | 10 | ||||
-rw-r--r-- | test/mod_roster_mock.exs | 13 |
10 files changed, 114 insertions, 687 deletions
diff --git a/test/ejabberd_admin_test.exs b/test/ejabberd_admin_test.exs index effcbc579..aa3c2ee41 100644 --- a/test/ejabberd_admin_test.exs +++ b/test/ejabberd_admin_test.exs @@ -25,12 +25,15 @@ defmodule EjabberdAdminTest do setup_all do :mnesia.start + :ejabberd_mnesia.start # For some myterious reason, :ejabberd_commands.init mays # sometimes fails if module is not loaded before {:module, :ejabberd_commands} = Code.ensure_loaded(:ejabberd_commands) + :ejabberd_hooks.start_link + {:ok, _} = :acl.start_link {:ok, _} = :ejabberd_access_permissions.start_link() - :ejabberd_commands.init - :ejabberd_admin.start + :ejabberd_commands.start_link + :ejabberd_admin.start_link :ok end @@ -41,40 +44,44 @@ defmodule EjabberdAdminTest do test "Logvel can be set and retrieved" do :ejabberd_logger.start() - assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [1]) + assert :lager == call_command(:set_loglevel, [1]) assert {1, :critical, 'Critical'} == - :ejabberd_commands.execute_command(:get_loglevel, []) + call_command(:get_loglevel, []) - assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [2]) + assert :lager == call_command(:set_loglevel, [2]) assert {2, :error, 'Error'} == - :ejabberd_commands.execute_command(:get_loglevel, []) + call_command(:get_loglevel, []) - assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [3]) + assert :lager == call_command(:set_loglevel, [3]) assert {3, :warning, 'Warning'} == - :ejabberd_commands.execute_command(:get_loglevel, []) + call_command(:get_loglevel, []) assert {:wrong_loglevel, 6} == - catch_throw :ejabberd_commands.execute_command(:set_loglevel, [6]) + catch_throw call_command(:set_loglevel, [6]) assert {3, :warning, 'Warning'} == - :ejabberd_commands.execute_command(:get_loglevel, []) + call_command(:get_loglevel, []) - assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [4]) + assert :lager == call_command(:set_loglevel, [4]) assert {4, :info, 'Info'} == - :ejabberd_commands.execute_command(:get_loglevel, []) + call_command(:get_loglevel, []) - assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [5]) + assert :lager == call_command(:set_loglevel, [5]) assert {5, :debug, 'Debug'} == - :ejabberd_commands.execute_command(:get_loglevel, []) + call_command(:get_loglevel, []) - assert :lager == :ejabberd_commands.execute_command(:set_loglevel, [0]) + assert :lager == call_command(:set_loglevel, [0]) assert {0, :no_log, 'No log'} == - :ejabberd_commands.execute_command(:get_loglevel, []) + call_command(:get_loglevel, []) end + defp call_command(name, args) do + :ejabberd_commands.execute_command2(name, args, %{:caller_module => :ejabberd_ctl}) + end + test "command status works with ejabberd stopped" do assert :ejabberd_not_running == - elem(:ejabberd_commands.execute_command(:status, []), 0) + elem(call_command(:status, []), 0) end end diff --git a/test/ejabberd_commands_mock_test.exs b/test/ejabberd_commands_mock_test.exs deleted file mode 100644 index d7ac7ae12..000000000 --- a/test/ejabberd_commands_mock_test.exs +++ /dev/null @@ -1,481 +0,0 @@ -# ---------------------------------------------------------------------- -# -# 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. -# -# ---------------------------------------------------------------------- - -## TODO Fix next test error: add admin user ACL - -defmodule EjabberdCommandsMockTest do - use ExUnit.Case, async: false - - require EjabberdOauthMock - - @author "jsautret@process-one.net" - - # mocked callback module - @module :test_module - # Admin user - @admin "admin" - @adminpass "adminpass" - # Non admin user - @user "user" - @userpass "userpass" - # XMPP domain - @domain "domain" - - require Record - Record.defrecord :ejabberd_commands, Record.extract(:ejabberd_commands, from_lib: "ejabberd/include/ejabberd_commands.hrl") - - setup_all do - :ok = :ejabberd.start_app(:lager) - try do - :stringprep.start - rescue - _ -> :ok - end - :mnesia.start - :ejabberd_mnesia.start - :jid.start - :ejabberd_hooks.start_link - :ok = :ejabberd_config.start(["domain1", "domain2"], []) - {:ok, _} = :ejabberd_access_permissions.start_link() - {:ok, _} = :acl.start_link - :ejabberd_oauth.start_link - :ejabberd_commands.start_link - EjabberdOauthMock.init - on_exit fn -> :meck.unload end - end - - setup do - :meck.unload - :meck.new(@module, [:non_strict]) - :mnesia.clear_table(:ejabberd_commands) - :ejabberd_commands.register_commands(:ejabberd_commands.get_commands_spec()) - :ok - end - - test "API command can be registered, listed and unregistered" do - command = ejabberd_commands name: :test, module: @module, - function: :test_command - - assert :ok == :ejabberd_commands.register_commands [command] - commands = :ejabberd_commands.list_commands - assert Enum.member? commands, {:test, [], ''} - - assert :ok == :ejabberd_commands.unregister_commands [command] - commands = :ejabberd_commands.list_commands - refute Enum.member? commands, {:test, [], ''} - end - - - test "API command with versions can be registered, listed and unregistered" do - command1 = ejabberd_commands name: :test, module: @module, - function: :test_command, version: 1, desc: 'version1' - command3 = ejabberd_commands name: :test, module: @module, - function: :test_command, version: 3, desc: 'version3' - assert :ejabberd_commands.register_commands [command1, command3] - - version1 = {:test, [], 'version1'} - version3 = {:test, [], 'version3'} - - # default version is latest one - commands = :ejabberd_commands.list_commands - refute Enum.member? commands, version1 - assert Enum.member? commands, version3 - - # no such command in APIv0 - commands = :ejabberd_commands.list_commands 0 - refute Enum.member? commands, version1 - refute Enum.member? commands, version3 - - commands = :ejabberd_commands.list_commands 1 - assert Enum.member? commands, version1 - refute Enum.member? commands, version3 - - commands = :ejabberd_commands.list_commands 2 - assert Enum.member? commands, version1 - refute Enum.member? commands, version3 - - commands = :ejabberd_commands.list_commands 3 - refute Enum.member? commands, version1 - assert Enum.member? commands, version3 - - commands = :ejabberd_commands.list_commands 4 - refute Enum.member? commands, version1 - assert Enum.member? commands, version3 - - assert :ok == :ejabberd_commands.unregister_commands [command1] - - commands = :ejabberd_commands.list_commands 1 - refute Enum.member? commands, version1 - refute Enum.member? commands, version3 - - commands = :ejabberd_commands.list_commands 3 - refute Enum.member? commands, version1 - assert Enum.member? commands, version3 - - assert :ok == :ejabberd_commands.unregister_commands [command3] - - commands = :ejabberd_commands.list_commands 1 - refute Enum.member? commands, version1 - refute Enum.member? commands, version3 - - commands = :ejabberd_commands.list_commands 3 - refute Enum.member? commands, version1 - refute Enum.member? commands, version3 - end - - - test "API command can be registered and executed" do - mock_commands_config - - # Create & register a mocked command test() -> :result - command_name = :test - function = :test_command - command = ejabberd_commands(name: command_name, - module: @module, - function: function) - :meck.expect @module, function, fn -> :result end - assert :ok == :ejabberd_commands.register_commands [command] - - assert :result == :ejabberd_commands.execute_command(command_name, []) - - assert :meck.validate @module - end - - test "API command with versions can be registered and executed" do - mock_commands_config - - command_name = :test - - function1 = :test_command1 - command1 = ejabberd_commands(name: command_name, - version: 1, - module: @module, - function: function1) - :meck.expect(@module, function1, fn -> :result1 end) - - function3 = :test_command3 - command3 = ejabberd_commands(name: command_name, - version: 3, - module: @module, - function: function3) - :meck.expect(@module, function3, fn -> :result3 end) - - assert :ok == :ejabberd_commands.register_commands [command1, command3] - - # default version is latest one - assert :result3 == :ejabberd_commands.execute_command(command_name, []) - # no such command in APIv0 - assert {:error, :unknown_command} == - catch_throw :ejabberd_commands.execute_command(command_name, [], 0) - assert :result1 == :ejabberd_commands.execute_command(command_name, [], 1) - assert :result1 == :ejabberd_commands.execute_command(command_name, [], 2) - assert :result3 == :ejabberd_commands.execute_command(command_name, [], 3) - assert :result3 == :ejabberd_commands.execute_command(command_name, [], 4) - - assert :meck.validate @module - end - - - - test "API command with user policy" do - mock_commands_config [:user, :admin] - - # Register a command test(user, domain) -> {:versionN, user, domain} - # with policy=user and versions 1 & 3 - command_name = :test - command1 = ejabberd_commands(name: command_name, - module: @module, - function: :test_command1, - policy: :user, version: 1) - command3 = ejabberd_commands(name: command_name, - module: @module, - function: :test_command3, - policy: :user, version: 3) - :meck.expect(@module, :test_command1, - fn(user, domain) when is_binary(user) and is_binary(domain) -> - {:version1, user, domain} - end) - :meck.expect(@module, :test_command3, - fn(user, domain) when is_binary(user) and is_binary(domain) -> - {:version3, user, domain} - end) - assert :ok == :ejabberd_commands.register_commands [command1, command3] - - # A normal user must not pass user info as parameter - assert {:version1, @user, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - @userpass, false}, - command_name, - [], 2) - assert {:version3, @user, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - @userpass, false}, - command_name, - [], 3) - token = EjabberdOauthMock.get_token @user, @domain, command_name - assert {:version3, @user, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - {:oauth, token}, false}, - command_name, - [], 4) - # Expired oauth token - token = EjabberdOauthMock.get_token @user, @domain, command_name, 1 - :timer.sleep 1500 - assert {:error, :invalid_account_data} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - {:oauth, token}, false}, - command_name, - [], 4) - # Wrong oauth scope - token = EjabberdOauthMock.get_token @user, @domain, :bad_command - assert {:error, :invalid_account_data} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - {:oauth, token}, false}, - command_name, - [], 4) - - - assert :function_clause == - catch_error :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - @userpass, false}, - command_name, - [@user, @domain], 2) - # @user is not admin - assert {:error, :account_unprivileged} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - @userpass, true}, - command_name, - [], 2) - assert {:error, :account_unprivileged} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - @userpass, true}, - command_name, - [@user, @domain], 2) - assert {:error, :account_unprivileged} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - {:oauth, token}, true}, - command_name, - [@user, @domain], 2) - - - # An admin must explicitely pass user info - assert {:version1, @user, @domain} == - :ejabberd_commands.execute_command(:undefined, :admin, - command_name, [@user, @domain], 2) - assert {:version3, @user, @domain} == - :ejabberd_commands.execute_command(:undefined, :admin, - command_name, [@user, @domain], 4) - assert {:version1, @user, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, @adminpass, true}, - command_name, [@user, @domain], 1) - token = EjabberdOauthMock.get_token @admin, @domain, command_name - assert {:version3, @user, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, {:oauth, token}, true}, - command_name, [@user, @domain], 3) - # Wrong @admin password - assert {:error, :account_unprivileged} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - @adminpass<>"bad", true}, - command_name, - [@user, @domain], 3) - # @admin calling as a normal user - assert {:version3, @admin, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - @adminpass, false}, - command_name, [], 5) - assert {:version3, @admin, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - {:oauth, token}, false}, - command_name, [], 6) - assert :function_clause == - catch_error :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - @adminpass, false}, - command_name, - [@user, @domain], 5) - assert :meck.validate @module - end - - - test "API command with admin policy" do - mock_commands_config [:admin] - - # Register a command test(user, domain) -> {user, domain} - # with policy=admin - command_name = :test - function = :test_command - command = ejabberd_commands(name: command_name, - args: [{:user, :binary}, {:host, :binary}], - module: @module, - function: function, - policy: :admin) - :meck.expect(@module, function, - fn(user, domain) when is_binary(user) and is_binary(domain) -> - {user, domain} - end) - assert :ok == :ejabberd_commands.register_commands [command] - - # A normal user cannot call the command - assert {:error, :account_unprivileged} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - @userpass, false}, - command_name, - [@user, @domain]) - - # An admin can call the command - assert {@user, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - @adminpass, true}, - command_name, - [@user, @domain]) - - # An admin can call the command with oauth token - token = EjabberdOauthMock.get_token @admin, @domain, command_name - assert {@user, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - {:oauth, token}, true}, - command_name, - [@user, @domain]) - - - # An admin with bad password cannot call the command - assert {:error, :account_unprivileged} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - "bad"<>@adminpass, false}, - command_name, - [@user, @domain]) - - # An admin cannot call the command with bad oauth token - assert {:error, :account_unprivileged} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - {:oauth, "bad"<>token}, true}, - command_name, - [@user, @domain]) - - # An admin as a normal user cannot call the command - assert {:error, :account_unprivileged} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - @adminpass, false}, - command_name, - [@user, @domain]) - - # An admin as a normal user cannot call the command with oauth token - assert {:error, :account_unprivileged} == - catch_throw :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - {:oauth, token}, false}, - command_name, - [@user, @domain]) - - assert :meck.validate @module - end - - test "Commands can perform extra check on access" do - mock_commands_config [:admin, :open] - - command_name = :test - function = :test_command - command = ejabberd_commands(name: command_name, - args: [{:user, :binary}, {:host, :binary}], - access: [:basic_rule_1], - module: @module, - function: function, - policy: :open) - :meck.expect(@module, function, - fn(user, domain) when is_binary(user) and is_binary(domain) -> - {user, domain} - end) - assert :ok == :ejabberd_commands.register_commands [command] - -# :acl.add(:global, :basic_acl_1, {:user, @user, @host}) -# :acl.add_access(:global, :basic_rule_1, [{:allow, [{:acl, :basic_acl_1}]}]) - - assert {@user, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@user, @domain, - @userpass, false}, - command_name, - [@user, @domain]) - assert {@user, @domain} == - :ejabberd_commands.execute_command(:undefined, - {@admin, @domain, - @adminpass, false}, - command_name, - [@user, @domain]) - - end - - ########################################################## - # Utils - - # Mock a config where only @admin user is allowed to call commands - # as admin - def mock_commands_config(commands \\ []) do - EjabberdAuthMock.init - EjabberdAuthMock.create_user @user, @domain, @userpass - EjabberdAuthMock.create_user @admin, @domain, @adminpass - - :meck.new :ejabberd_config - :meck.expect(:ejabberd_config, :get_option, - fn(:commands_admin_access, _) -> :commands_admin_access - (:oauth_access, _) -> :all - (:commands, _) -> [{:add_commands, commands}] - (_, default) -> default - end) - :meck.expect(:ejabberd_config, :get_myhosts, - fn() -> [@domain] end) - :meck.expect(:ejabberd_config, :default_db, - fn(_) -> :mnesia end) - :meck.expect(:ejabberd_config, :default_db, - fn(_, _) -> :mnesia end) - - :meck.new :acl - :meck.expect(:acl, :access_matches, - fn(:commands_admin_access, info, _scope) -> - case info do - %{usr: {@admin, @domain, _}} -> :allow - _ -> :deny - end; - (:all, _, _scope) -> - :allow - end) - end - -end diff --git a/test/ejabberd_commands_test.exs b/test/ejabberd_commands_test.exs deleted file mode 100644 index 40e860c27..000000000 --- a/test/ejabberd_commands_test.exs +++ /dev/null @@ -1,108 +0,0 @@ -# ---------------------------------------------------------------------- -# -# 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. -# -# ---------------------------------------------------------------------- - -defmodule EjabberdCommandsTest do - @author "mremond@process-one.net" - - use ExUnit.Case, async: true - - require Record - Record.defrecord :ejabberd_commands, Record.extract(:ejabberd_commands, from_lib: "ejabberd/include/ejabberd_commands.hrl") - - setup_all do - :mnesia.start - :ejabberd_mnesia.start - :stringprep.start - :ok = :ejabberd_config.start(["localhost"], []) - {:ok, _} = :ejabberd_access_permissions.start_link() - - :ejabberd_commands.start_link - :ok - end - - test "Check that we can register a command" do - :ok = :ejabberd_commands.register_commands([user_test_command]) - commands = :ejabberd_commands.list_commands - assert Enum.member?(commands, {:test_user, [], "Test user"}) - end - - test "get_exposed_commands/0 returns registered commands" do - commands = [open_test_command] - :ok = :ejabberd_commands.register_commands(commands) - :ok = :ejabberd_commands.expose_commands(commands) - exposed_commands = :ejabberd_commands.get_exposed_commands - assert Enum.member?(exposed_commands, :test_open) - end - - test "Check that admin commands are rejected with noauth credentials" do - :ok = :ejabberd_commands.register_commands([admin_test_command]) - - assert catch_throw(:ejabberd_commands.execute_command(:undefined, :noauth, :test_admin, [])) == {:error, :account_unprivileged} - - # Command executed from ejabberdctl passes anyway with access commands trick - # TODO: We should refactor to have explicit call when bypassing auth check for command-line - :ok = :ejabberd_commands.execute_command([], :noauth, :test_admin, []) - end - - # TODO Test that we can add command to list of expose commands - # This can be done with: - # ejabberd_config:add_local_option(commands, [[{add_commands, [open_cmd]}]]). - -# test "Check that a user can use a user command" do -# [Command] = ets:lookup(ejabberd_commands, test_user), -# AccessCommands = ejabberd_commands:get_access_commands(undefined), -# ejabberd_commands:check_access_commands(AccessCommands, {<<"test">>,<<"localhost">>, {oauth,<<"MyToken">>}, false}, test_user, Command, []). -# end - - defp user_test_command do - ejabberd_commands(name: :test_user, tags: [:roster], - desc: "Test user", - policy: :user, - module: __MODULE__, - function: :test_user, - args: [], - result: {:contacts, {:list, {:contact, {:tuple, [ - {:jid, :string}, - {:nick, :string} - ]}}}}) - end - - defp open_test_command do - ejabberd_commands(name: :test_open, tags: [:test], - desc: "Test open", - policy: :open, - module: __MODULE__, - function: :test_open, - args: [], - result: {:res, :rescode}) - end - - defp admin_test_command do - ejabberd_commands(name: :test_admin, tags: [:roster], - desc: "Test admin", - policy: :restricted, - module: __MODULE__, - function: :test_admin, - args: [], - result: {:res, :rescode}) - end - - def test_admin, do: :ok -end diff --git a/test/ejabberd_cyrsasl_test.exs b/test/ejabberd_cyrsasl_test.exs index f601c78ef..bdef92cd4 100644 --- a/test/ejabberd_cyrsasl_test.exs +++ b/test/ejabberd_cyrsasl_test.exs @@ -30,6 +30,7 @@ defmodule EjabberdCyrsaslTest do :ejabberd_mnesia.start :ok = start_module(:stringprep) start_module(:jid) + :ejabberd_hooks.start_link :ok = :ejabberd_config.start(["domain1"], []) {:ok, _} = :cyrsasl.start_link cyrstate = :cyrsasl.server_new("domain1", "domain1", "domain1", :ok, &get_password/1, diff --git a/test/elixir_SUITE.erl b/test/elixir_SUITE.erl index f4612fa6d..5cc0c95a4 100644 --- a/test/elixir_SUITE.erl +++ b/test/elixir_SUITE.erl @@ -99,7 +99,7 @@ run_elixir_test(Func) -> 'Elixir.Code':load_file(list_to_binary(filename:join(test_dir(), atom_to_list(Func)))), %% I did not use map syntax, so that this file can still be build under R16 - 'Elixir.ExUnit.Server':cases_loaded(), + catch 'Elixir.ExUnit.Server':cases_loaded(), ResultMap = 'Elixir.ExUnit':run(), case maps:find(failures, ResultMap) of {ok, 0} -> diff --git a/test/ldap_srv.erl b/test/ldap_srv.erl index d5dc8dbe2..f601827c4 100644 --- a/test/ldap_srv.erl +++ b/test/ldap_srv.erl @@ -45,7 +45,7 @@ -define(ERROR_MSG(Fmt, Args), error_logger:error_msg(Fmt, Args)). -define(TCP_SEND_TIMEOUT, 32000). --define(SERVER, ?MODULE). +-define(SERVER, ?MODULE). -record(state, {listener = make_ref() :: reference()}). @@ -122,7 +122,7 @@ accept(ListenSocket, Tree) -> process(Socket, Tree) -> case gen_tcp:recv(Socket, 0) of {ok, B} -> - case asn1rt:decode('ELDAPv3', 'LDAPMessage', B) of + case 'ELDAPv3':decode('LDAPMessage', B) of {ok, Msg} -> Replies = process_msg(Msg, Tree), Id = Msg#'LDAPMessage'.messageID, @@ -131,8 +131,8 @@ process(Socket, Tree) -> Reply = #'LDAPMessage'{messageID = Id, protocolOp = ReplyOp}, %%?DEBUG("sent:~n~p", [Reply]), - {ok, Bytes} = asn1rt:encode( - 'ELDAPv3', 'LDAPMessage', Reply), + {ok, Bytes} = 'ELDAPv3':encode( + 'LDAPMessage', Reply), gen_tcp:send(Socket, Bytes) end, Replies), process(Socket, Tree); diff --git a/test/mod_admin_extra_test.exs b/test/mod_admin_extra_test.exs index eefe2e9bd..23ed38c61 100644 --- a/test/mod_admin_extra_test.exs +++ b/test/mod_admin_extra_test.exs @@ -41,16 +41,20 @@ defmodule EjabberdModAdminExtraTest do :jid.start :stringprep.start :mnesia.start + :ejabberd_mnesia.start :p1_sha.load_nif + :ejabberd_hooks.start_link rescue _ -> :ok end - {:ok, _} = :ejabberd_access_permissions.start_link() - :ejabberd_commands.init - :ok = :ejabberd_config.start([@domain], []) + :acl.start_link + :ejabberd_access_permissions.start_link() + :ejabberd_commands.start_link + :ok = :ejabberd_config.start([@domain], []) + :gen_mod.start_link :mod_admin_extra.start(@domain, []) :sel_application.start_app(:moka) - {:ok, _pid} = :ejabberd_hooks.start_link + :ejabberd_hooks.start_link :ok end @@ -66,9 +70,9 @@ defmodule EjabberdModAdminExtraTest do test "check_account works" do EjabberdAuthMock.create_user @user, @domain, @password - assert :ejabberd_commands.execute_command(:check_account, [@user, @domain]) - refute :ejabberd_commands.execute_command(:check_account, [@user, "bad_domain"]) - refute :ejabberd_commands.execute_command(:check_account, ["bad_user", @domain]) + assert call_command(:check_account, [@user, @domain]) + refute call_command(:check_account, [@user, "bad_domain"]) + refute call_command(:check_account, ["bad_user", @domain]) assert :meck.validate :ejabberd_auth end @@ -77,13 +81,13 @@ defmodule EjabberdModAdminExtraTest do EjabberdAuthMock.create_user @user, @domain, @password - assert :ejabberd_commands.execute_command(:check_password, + assert call_command(:check_password, [@user, @domain, @password]) - refute :ejabberd_commands.execute_command(:check_password, + refute call_command(:check_password, [@user, @domain, "bad_password"]) - refute :ejabberd_commands.execute_command(:check_password, + refute call_command(:check_password, [@user, "bad_domain", @password]) - refute :ejabberd_commands.execute_command(:check_password, + refute call_command(:check_password, ["bad_user", @domain, @password]) assert :meck.validate :ejabberd_auth @@ -95,21 +99,21 @@ defmodule EjabberdModAdminExtraTest do EjabberdAuthMock.create_user @user, @domain, @password hash = "5F4DCC3B5AA765D61D8327DEB882CF99" # echo -n password|md5 - assert :ejabberd_commands.execute_command(:check_password_hash, + assert call_command(:check_password_hash, [@user, @domain, hash, "md5"]) - refute :ejabberd_commands.execute_command(:check_password_hash, + refute call_command(:check_password_hash, [@user, @domain, "bad_hash", "md5"]) - refute :ejabberd_commands.execute_command(:check_password_hash, + refute call_command(:check_password_hash, [@user, "bad_domain", hash, "md5"]) - refute :ejabberd_commands.execute_command(:check_password_hash, + refute call_command(:check_password_hash, ["bad_user", @domain, hash, "md5"]) hash = "5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8" # echo -n password|shasum - assert :ejabberd_commands.execute_command(:check_password_hash, + assert call_command(:check_password_hash, [@user, @domain, hash, "sha"]) assert :unkown_hash_method == - catch_throw :ejabberd_commands.execute_command(:check_password_hash, + catch_throw call_command(:check_password_hash, [@user, @domain, hash, "bad_method"]) assert :meck.validate :ejabberd_auth @@ -119,14 +123,14 @@ defmodule EjabberdModAdminExtraTest do test "set_password works" do EjabberdAuthMock.create_user @user, @domain, @password - assert :ejabberd_commands.execute_command(:change_password, + assert call_command(:change_password, [@user, @domain, "new_password"]) - refute :ejabberd_commands.execute_command(:check_password, + refute call_command(:check_password, [@user, @domain, @password]) - assert :ejabberd_commands.execute_command(:check_password, + assert call_command(:check_password, [@user, @domain, "new_password"]) assert {:not_found, 'unknown_user'} == - catch_throw :ejabberd_commands.execute_command(:change_password, + catch_throw call_command(:change_password, ["bad_user", @domain, @password]) assert :meck.validate :ejabberd_auth @@ -135,23 +139,23 @@ defmodule EjabberdModAdminExtraTest do ###################### Sessions test "num_resources works" do - assert 0 == :ejabberd_commands.execute_command(:num_resources, + assert 0 == call_command(:num_resources, [@user, @domain]) EjabberdSmMock.connect_resource @user, @domain, @resource - assert 1 == :ejabberd_commands.execute_command(:num_resources, + assert 1 == call_command(:num_resources, [@user, @domain]) EjabberdSmMock.connect_resource @user, @domain, @resource<>"2" - assert 2 == :ejabberd_commands.execute_command(:num_resources, + assert 2 == call_command(:num_resources, [@user, @domain]) EjabberdSmMock.connect_resource @user<>"1", @domain, @resource - assert 2 == :ejabberd_commands.execute_command(:num_resources, + assert 2 == call_command(:num_resources, [@user, @domain]) EjabberdSmMock.disconnect_resource @user, @domain, @resource - assert 1 == :ejabberd_commands.execute_command(:num_resources, + assert 1 == call_command(:num_resources, [@user, @domain]) assert :meck.validate :ejabberd_sm @@ -163,14 +167,14 @@ defmodule EjabberdModAdminExtraTest do EjabberdSmMock.connect_resource @user, @domain, @resource<>"1" assert :bad_argument == - elem(catch_throw(:ejabberd_commands.execute_command(:resource_num, + elem(catch_throw(call_command(:resource_num, [@user, @domain, 0])), 0) assert @resource<>"1" == - :ejabberd_commands.execute_command(:resource_num, [@user, @domain, 1]) + call_command(:resource_num, [@user, @domain, 1]) assert @resource<>"3" == - :ejabberd_commands.execute_command(:resource_num, [@user, @domain, 3]) + call_command(:resource_num, [@user, @domain, 3]) assert :bad_argument == - elem(catch_throw(:ejabberd_commands.execute_command(:resource_num, + elem(catch_throw(call_command(:resource_num, [@user, @domain, 4])), 0) assert :meck.validate :ejabberd_sm end @@ -184,7 +188,7 @@ defmodule EjabberdModAdminExtraTest do assert 1 == length EjabberdSmMock.get_session @user, @domain, @resource<>"2" assert :ok == - :ejabberd_commands.execute_command(:kick_session, + call_command(:kick_session, [@user, @domain, @resource<>"2", "kick"]) @@ -199,18 +203,18 @@ defmodule EjabberdModAdminExtraTest do test "get_last works" do assert {_, 'NOT FOUND'} = - :ejabberd_commands.execute_command(:get_last, [@user, @domain]) + call_command(:get_last, [@user, @domain]) EjabberdSmMock.connect_resource @user, @domain, @resource<>"1" EjabberdSmMock.connect_resource @user, @domain, @resource<>"2" assert {_, 'ONLINE'} = - :ejabberd_commands.execute_command(:get_last, [@user, @domain]) + call_command(:get_last, [@user, @domain]) EjabberdSmMock.disconnect_resource @user, @domain, @resource<>"1" assert {_, 'ONLINE'} = - :ejabberd_commands.execute_command(:get_last, [@user, @domain]) + call_command(:get_last, [@user, @domain]) now = {megasecs, secs, _microsecs} = :os.timestamp timestamp = megasecs * 1000000 + secs @@ -221,7 +225,7 @@ defmodule EjabberdModAdminExtraTest do "~w-~.2.0w-~.2.0wT~.2.0w:~.2.0w:~.2.0wZ", [year, month, day, hour, minute, second])) assert {result, ""} == - :ejabberd_commands.execute_command(:get_last, [@user, @domain]) + call_command(:get_last, [@user, @domain]) assert :meck.validate :mod_last end @@ -238,7 +242,7 @@ defmodule EjabberdModAdminExtraTest do assert [] == ModRosterMock.get_roster(@user, @domain) assert :ok == - :ejabberd_commands.execute_command(:add_rosteritem, [@user, @domain, + call_command(:add_rosteritem, [@user, @domain, @user<>"1", @domain, "nick1", "group1", @@ -261,7 +265,7 @@ defmodule EjabberdModAdminExtraTest do {:item, {@user<>"1", @domain, ""}, :both}]) assert :ok == - :ejabberd_commands.execute_command(:add_rosteritem, [@user, @domain, + call_command(:add_rosteritem, [@user, @domain, @user<>"2", @domain, "nick2", "group2", @@ -278,7 +282,7 @@ defmodule EjabberdModAdminExtraTest do {:item, {@user<>"2", @domain, ""}, :both}]) - :ejabberd_commands.execute_command(:delete_rosteritem, [@user, @domain, + call_command(:delete_rosteritem, [@user, @domain, @user<>"1", @domain]) result = ModRosterMock.get_roster(@user, @domain) assert 1 == length result @@ -296,7 +300,7 @@ defmodule EjabberdModAdminExtraTest do [jid, {:item, {@user<>"1", @domain, ""}, :none}]) - :ejabberd_commands.execute_command(:delete_rosteritem, [@user, @domain, + call_command(:delete_rosteritem, [@user, @domain, @user<>"2", @domain]) # Check that the item roster user2 was pushed with subscription @@ -321,39 +325,47 @@ defmodule EjabberdModAdminExtraTest do test "get_roster works" do assert [] == ModRosterMock.get_roster(@user, @domain) - assert [] == :ejabberd_commands.execute_command(:get_roster, [@user, @domain], + assert [] == call_command(:get_roster, [@user, @domain], :admin) assert :ok == - :ejabberd_commands.execute_command(:add_rosteritem, [@user, @domain, + call_command(:add_rosteritem, [@user, @domain, @user<>"1", @domain, "nick1", "group1", "both"]) assert [{@user<>"1@"<>@domain, "", 'both', 'none', "group1"}] == - :ejabberd_commands.execute_command(:get_roster, [@user, @domain], :admin) + call_command(:get_roster, [@user, @domain], :admin) assert :ok == - :ejabberd_commands.execute_command(:add_rosteritem, [@user, @domain, + call_command(:add_rosteritem, [@user, @domain, @user<>"2", @domain, "nick2", "group2", "none"]) - result = :ejabberd_commands.execute_command(:get_roster, [@user, @domain], :admin) + result = call_command(:get_roster, [@user, @domain], :admin) assert 2 == length result assert Enum.member?(result, {@user<>"1@"<>@domain, "", 'both', 'none', "group1"}) assert Enum.member?(result, {@user<>"2@"<>@domain, "", 'none', 'none', "group2"}) end + defp call_command(name, args) do + :ejabberd_commands.execute_command2(name, args, %{:caller_module => :ejabberd_ctl}) + end + + defp call_command(name, args, mode) do + call_command(name, args) + end + # kick_user command is defined in ejabberd_sm, move to extra? # test "kick_user works" do -# assert 0 == :ejabberd_commands.execute_command(:num_resources, +# assert 0 == call_command(:num_resources, # [@user, @domain]) # EjabberdSmMock.connect_resource(@user, @domain, @resource<>"1") # EjabberdSmMock.connect_resource(@user, @domain, @resource<>"2") # assert 2 == -# :ejabberd_commands.execute_command(:kick_user, [@user, @domain]) -# assert 0 == :ejabberd_commands.execute_command(:num_resources, +# call_command(:kick_user, [@user, @domain]) +# assert 0 == call_command(:num_resources, # [@user, @domain]) # assert :meck.validate :ejabberd_sm # end diff --git a/test/mod_http_api_mock_test.exs b/test/mod_http_api_mock_test.exs index 4dde78939..ceda2bb0f 100644 --- a/test/mod_http_api_mock_test.exs +++ b/test/mod_http_api_mock_test.exs @@ -45,10 +45,11 @@ defmodule ModHttpApiMockTest do :jid.start :mnesia.start :ejabberd_mnesia.start - :stringprep.start + :stringprep.start + :ejabberd_hooks.start_link :ejabberd_config.start([@domain], []) {:ok, _} = :ejabberd_access_permissions.start_link() - :ejabberd_commands.init + :ejabberd_commands.start_link rescue _ -> :ok end @@ -73,18 +74,12 @@ defmodule ModHttpApiMockTest do fn (@acommand, %{usr: {@user, @domain, _}}, @version) -> {[], {:res, :rescode}} end) - :meck.expect(:ejabberd_commands, :get_command_policy_and_scope, - fn (@acommand) -> {:ok, :user, [:erlang.atom_to_binary(@acommand,:utf8)]} end) - :meck.expect(:ejabberd_commands, :get_exposed_commands, + :meck.expect(:ejabberd_commands, :get_exposed_commands, fn () -> [@acommand] end) :meck.expect(:ejabberd_commands, :execute_command2, fn (@acommand, [], %{usr: {@user, @domain, _}}, @version) -> :ok end) - :meck.expect(:ejabberd_commands, :execute_command, - fn (:undefined, {@user, @domain, @userpass, false}, @acommand, [], @version, _) -> - :ok - end) :ejabberd_config.add_local_option(:commands, [[{:add_commands, [@acommand]}]]) @@ -130,9 +125,7 @@ defmodule ModHttpApiMockTest do fn (@acommand, %{usr: {@user, @domain, _}}, @version) -> {[], {:res, :rescode}} end) - :meck.expect(:ejabberd_commands, :get_command_policy_and_scope, - fn (@acommand) -> {:ok, :user, [:erlang.atom_to_binary(@acommand,:utf8), "ejabberd:user"]} end) - :meck.expect(:ejabberd_commands, :get_exposed_commands, + :meck.expect(:ejabberd_commands, :get_exposed_commands, fn () -> [@acommand] end) :meck.expect(:ejabberd_commands, :execute_command2, fn (@acommand, [], %{usr: {@user, @domain, _}, oauth_scope: ["ejabberd:user"]}, @version) -> @@ -142,11 +135,6 @@ defmodule ModHttpApiMockTest do (@acommand, [], %{usr: {@user, @domain, _}, oauth_scope: _}, @version) -> throw({:error, :access_rules_unauthorized}) end) - :meck.expect(:ejabberd_commands, :execute_command, - fn (:undefined, {@user, @domain, {:oauth, _token}, false}, - @acommand, [], @version, _) -> - :ok - end) # Correct OAuth call using specific scope @@ -231,15 +219,8 @@ defmodule ModHttpApiMockTest do fn (@acommand, {@user, @domain, {:oauth, _token}, false}, @version) -> {[], {:res, :rescode}} end) - :meck.expect(:ejabberd_commands, :get_command_policy_and_scope, - fn (@acommand) -> {:ok, :user, [:erlang.atom_to_binary(@acommand,:utf8), "ejabberd:user"]} end) - :meck.expect(:ejabberd_commands, :get_exposed_commands, + :meck.expect(:ejabberd_commands, :get_exposed_commands, fn () -> [@acommand] end) - :meck.expect(:ejabberd_commands, :execute_command, - fn (:undefined, {@user, @domain, {:oauth, _token}, false}, - @acommand, [], @version, _) -> - :ok - end) #Mock acl to allow oauth authorizations :meck.expect(:acl, :match_rule, fn(_Server, _Access, _Jid) -> :allow end) diff --git a/test/mod_http_api_test.exs b/test/mod_http_api_test.exs index c39af79dc..29405a3ec 100644 --- a/test/mod_http_api_test.exs +++ b/test/mod_http_api_test.exs @@ -31,9 +31,11 @@ defmodule ModHttpApiTest do :ok = :mnesia.start :ejabberd_mnesia.start :stringprep.start + :ejabberd_hooks.start_link :ok = :ejabberd_config.start(["localhost"], []) + :acl.start_link {:ok, _} = :ejabberd_access_permissions.start_link() - :ok = :ejabberd_commands.init + {:ok, _} = :ejabberd_commands.start_link :ok = :ejabberd_commands.register_commands(cmds) on_exit fn -> :meck.unload @@ -42,7 +44,7 @@ defmodule ModHttpApiTest do test "We can expose several commands to API at a time" do setup_mocks() - :ejabberd_commands.expose_commands([:open_cmd, :user_cmd]) + assert :ok == :ejabberd_commands.expose_commands([:open_cmd, :user_cmd]) commands = :ejabberd_commands.get_exposed_commands() assert Enum.member?(commands, :open_cmd) assert Enum.member?(commands, :user_cmd) @@ -58,14 +60,14 @@ defmodule ModHttpApiTest do # This related to the commands config file option test "Attempting to access a command that is not exposed as HTTP API returns 403" do setup_mocks() - :ejabberd_commands.expose_commands([]) + assert :ok == :ejabberd_commands.expose_commands([]) request = request(method: :POST, ip: {{127,0,0,1},50000}, data: "[]") {403, _, _} = :mod_http_api.process(["open_cmd"], request) end test "Call to user, admin or restricted commands without authentication are rejected" do setup_mocks() - :ejabberd_commands.expose_commands([:user_cmd, :admin_cmd, :restricted]) + assert :ok == :ejabberd_commands.expose_commands([:user_cmd, :admin_cmd, :restricted]) request = request(method: :POST, ip: {{127,0,0,1},50000}, data: "[]") {403, _, _} = :mod_http_api.process(["user_cmd"], request) {403, _, _} = :mod_http_api.process(["admin_cmd"], request) diff --git a/test/mod_roster_mock.exs b/test/mod_roster_mock.exs index 58e759729..02b62183e 100644 --- a/test/mod_roster_mock.exs +++ b/test/mod_roster_mock.exs @@ -23,6 +23,7 @@ defmodule ModRosterMock do require Record Record.defrecord :roster, Record.extract(:roster, from_lib: "ejabberd/include/mod_roster.hrl") + Record.defrecord :roster_version, Record.extract(:roster_version, from_lib: "ejabberd/include/mod_roster.hrl") @agent __MODULE__ @@ -37,6 +38,13 @@ defmodule ModRosterMock do mock_with_moka module + :ejabberd_mnesia.create(:mod_roster_mnesia, :roster, + [ram_copies: [node()], + attributes: Keyword.keys(roster(roster())), + index: [:us]]) + :ejabberd_mnesia.create(:mod_roster_mnesia, :roster_version, + [ram_copies: [node()], + attributes: Keyword.keys(roster_version(roster_version()))]) #:mod_roster.stop(domain) :mod_roster.start(domain, []) end @@ -92,6 +100,11 @@ defmodule ModRosterMock do :moka.load(roster_mock0) roster_mock = :moka.start(:mod_roster_mnesia) + :moka.replace(roster_mock, :init, + fn (_host, _opts) -> + :ok + end) + :moka.replace(roster_mock, :gen_mod, :db_type, fn (_host, _opts) -> {:none} |