1
0
Fork 0
mirror of https://github.com/processone/ejabberd synced 2025-10-03 09:49:18 +02:00

Refactorize each individual test case in individual functions

This commit is contained in:
Badlop 2025-06-06 17:38:13 +02:00 committed by Stefan Strigler
parent ee46333def
commit b607d95a93

View file

@ -20,6 +20,7 @@
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. %%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%% %%%
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
-module(antispam_tests). -module(antispam_tests).
-compile(export_all). -compile(export_all).
@ -39,59 +40,60 @@
%%%=================================================================== %%%===================================================================
%%% Single tests %%% Single tests
%%%=================================================================== %%%===================================================================
single_cases() -> single_cases() ->
{antispam_single, {antispam_single,
[sequence], [sequence],
[single_test(spam_files), [single_test(block_by_jid),
single_test(blocked_domains), single_test(block_by_url),
single_test(blocked_jid_is_cached),
single_test(uncache_blocked_jid),
single_test(check_blocked_domain),
single_test(unblock_domain),
single_test(empty_domain_list),
single_test(block_domain_globally),
single_test(check_domain_blocked_globally),
single_test(unblock_domain_in_vhost),
single_test(unblock_domain_globally),
single_test(block_domain_in_vhost),
single_test(unblock_domain_in_vhost2),
single_test(jid_cache), single_test(jid_cache),
single_test(rtbl_domains)]}. single_test(rtbl_domains)]}.
spam_files(Config) -> %%%===================================================================
Host = ?config(server, Config),
block_by_jid(Config) ->
is_spam(message_hello(<<"spammer_jid">>, <<"localhost">>, Config)).
block_by_url(Config) ->
From = jid:make(<<"spammer">>, <<"localhost">>, <<"spam_client">>),
To = my_jid(Config), To = my_jid(Config),
is_not_spam(message_hello(<<"spammer">>, <<"localhost">>, Config)),
is_spam(message(From, To, <<"hello world\nhttps://spam.domain.url">>)).
SpamJID = jid:make(<<"spammer_jid">>, <<"localhost">>, <<"spam_client">>), blocked_jid_is_cached(Config) ->
SpamJIDMsg = is_spam(message_hello(<<"spammer">>, <<"localhost">>, Config)).
#message{from = SpamJID,
to = To,
type = chat,
body = [#text{data = <<"hello world">>}]},
is_spam(SpamJIDMsg),
Spammer = jid:make(<<"spammer">>, <<"localhost">>, <<"spam_client">>), uncache_blocked_jid(Config) ->
NoSpamMsg = Host = ?config(server, Config),
#message{from = Spammer, Spammer = jid:make(<<"spammer">>, <<"localhost">>, <<"">>),
to = To,
type = chat,
body = [#text{data = <<"hello world">>}]},
is_not_spam(NoSpamMsg),
SpamMsg =
#message{from = Spammer,
to = To,
type = chat,
body = [#text{data = <<"hello world\nhttps://spam.domain.url">>}]},
is_spam(SpamMsg),
%% now check this mischief is in jid_cache
is_spam(NoSpamMsg),
mod_antispam:drop_from_spam_filter_cache(Host, jid:to_string(Spammer)), mod_antispam:drop_from_spam_filter_cache(Host, jid:to_string(Spammer)),
is_not_spam(NoSpamMsg), is_not_spam(message_hello(<<"spammer">>, <<"localhost">>, Config)).
check_blocked_domain(Config) ->
Host = ?config(server, Config),
?retry(100, 10, ?match(true, (has_spam_domain(<<"spam_domain.org">>))(Host))), ?retry(100, 10, ?match(true, (has_spam_domain(<<"spam_domain.org">>))(Host))),
is_spam(message_hello(<<"other_spammer">>, <<"spam_domain.org">>, Config)).
SpamDomain = jid:make(<<"spammer">>, <<"spam_domain.org">>, <<"spam_client">>), unblock_domain(Config) ->
SpamDomainMsg = Host = ?config(server, Config),
#message{from = SpamDomain,
to = To,
type = chat,
body = [#text{data = <<"hello world">>}]},
is_spam(SpamDomainMsg),
?match({ok, _}, mod_antispam:remove_blocked_domain(Host, <<"spam_domain.org">>)), ?match({ok, _}, mod_antispam:remove_blocked_domain(Host, <<"spam_domain.org">>)),
?match([], mod_antispam:get_blocked_domains(Host)), ?match([], mod_antispam:get_blocked_domains(Host)),
is_not_spam(SpamDomainMsg), is_not_spam(message_hello(<<"spammer">>, <<"spam_domain.org">>, Config)).
disconnect(Config).
blocked_domains(Config) -> %%%===================================================================
empty_domain_list(Config) ->
Host = ?config(server, Config), Host = ?config(server, Config),
?match([], mod_antispam:get_blocked_domains(Host)), ?match([], mod_antispam:get_blocked_domains(Host)),
SpamFrom = jid:make(<<"spammer">>, <<"spam.domain">>, <<"spam_client">>), SpamFrom = jid:make(<<"spammer">>, <<"spam.domain">>, <<"spam_client">>),
@ -100,40 +102,65 @@ blocked_domains(Config) ->
to = To, to = To,
type = chat, type = chat,
body = [#text{data = <<"hello world">>}]}, body = [#text{data = <<"hello world">>}]},
is_not_spam(Msg), is_not_spam(Msg).
block_domain_globally(Config) ->
?match({ok, _}, mod_antispam:add_blocked_domain(<<"global">>, <<"spam.domain">>)), ?match({ok, _}, mod_antispam:add_blocked_domain(<<"global">>, <<"spam.domain">>)),
is_spam(Msg), SpamFrom = jid:make(<<"spammer">>, <<"spam.domain">>, <<"spam_client">>),
To = my_jid(Config),
is_spam(message(SpamFrom, To, <<"hello world">>)).
check_domain_blocked_globally(_Config) ->
Vhosts = [H || H <- ejabberd_option:hosts(), gen_mod:is_loaded(H, mod_antispam)], Vhosts = [H || H <- ejabberd_option:hosts(), gen_mod:is_loaded(H, mod_antispam)],
NumVhosts = length(Vhosts), NumVhosts = length(Vhosts),
?match(NumVhosts, length(lists:filter(has_spam_domain(<<"spam.domain">>), Vhosts))), ?match(NumVhosts, length(lists:filter(has_spam_domain(<<"spam.domain">>), Vhosts))).
unblock_domain_in_vhost(Config) ->
Host = ?config(server, Config),
?match({ok, _}, mod_antispam:remove_blocked_domain(Host, <<"spam.domain">>)), ?match({ok, _}, mod_antispam:remove_blocked_domain(Host, <<"spam.domain">>)),
?match([], mod_antispam:get_blocked_domains(Host)), ?match([], mod_antispam:get_blocked_domains(Host)),
is_not_spam(Msg), SpamFrom = jid:make(<<"spammer">>, <<"spam.domain">>, <<"spam_client">>),
To = my_jid(Config),
is_not_spam(message(SpamFrom, To, <<"hello world">>)).
unblock_domain_globally(_Config) ->
Vhosts = [H || H <- ejabberd_option:hosts(), gen_mod:is_loaded(H, mod_antispam)],
NumVhosts = length(Vhosts),
?match(NumVhosts, length(lists:filter(has_spam_domain(<<"spam.domain">>), Vhosts)) + 1), ?match(NumVhosts, length(lists:filter(has_spam_domain(<<"spam.domain">>), Vhosts)) + 1),
?match({ok, _}, mod_antispam:remove_blocked_domain(<<"global">>, <<"spam.domain">>)), ?match({ok, _}, mod_antispam:remove_blocked_domain(<<"global">>, <<"spam.domain">>)),
?match([], lists:filter(has_spam_domain(<<"spam.domain">>), Vhosts)), ?match([], lists:filter(has_spam_domain(<<"spam.domain">>), Vhosts)).
block_domain_in_vhost(Config) ->
Host = ?config(server, Config),
Vhosts = [H || H <- ejabberd_option:hosts(), gen_mod:is_loaded(H, mod_antispam)],
?match({ok, _}, mod_antispam:add_blocked_domain(Host, <<"spam.domain">>)), ?match({ok, _}, mod_antispam:add_blocked_domain(Host, <<"spam.domain">>)),
?match([Host], lists:filter(has_spam_domain(<<"spam.domain">>), Vhosts)), ?match([Host], lists:filter(has_spam_domain(<<"spam.domain">>), Vhosts)),
is_spam(Msg), SpamFrom = jid:make(<<"spammer">>, <<"spam.domain">>, <<"spam_client">>),
To = my_jid(Config),
is_spam(message(SpamFrom, To, <<"hello world">>)).
unblock_domain_in_vhost2(Config) ->
Host = ?config(server, Config),
?match({ok, _}, mod_antispam:remove_blocked_domain(Host, <<"spam.domain">>)), ?match({ok, _}, mod_antispam:remove_blocked_domain(Host, <<"spam.domain">>)),
is_not_spam(Msg), SpamFrom = jid:make(<<"spammer">>, <<"spam.domain">>, <<"spam_client">>),
To = my_jid(Config),
is_not_spam(message(SpamFrom, To, <<"hello world">>)),
disconnect(Config). disconnect(Config).
%%%===================================================================
jid_cache(Config) -> jid_cache(Config) ->
Host = ?config(server, Config), Host = ?config(server, Config),
SpamFrom = jid:make(<<"spammer">>, Host, <<"spam_client">>), SpamFrom = jid:make(<<"spammer">>, Host, <<"spam_client">>),
To = my_jid(Config), is_not_spam(message_hello(<<"spammer">>, Host, Config)),
Msg = #message{from = SpamFrom,
to = To,
type = chat,
body = [#text{data = <<"hello world">>}]},
is_not_spam(Msg),
mod_antispam:add_to_spam_filter_cache(Host, jid:to_string(SpamFrom)), mod_antispam:add_to_spam_filter_cache(Host, jid:to_string(SpamFrom)),
is_spam(Msg), is_spam(message_hello(<<"spammer">>, Host, Config)),
mod_antispam:drop_from_spam_filter_cache(Host, jid:to_string(SpamFrom)), mod_antispam:drop_from_spam_filter_cache(Host, jid:to_string(SpamFrom)),
is_not_spam(Msg), is_not_spam(message_hello(<<"spammer">>, Host, Config)),
disconnect(Config). disconnect(Config).
%%%===================================================================
rtbl_domains(Config) -> rtbl_domains(Config) ->
Host = ?config(server, Config), Host = ?config(server, Config),
RTBLHost = RTBLHost =
@ -192,3 +219,14 @@ is_not_spam(Msg) ->
is_spam(Spam) -> is_spam(Spam) ->
?match({stop, {drop, undefined}}, mod_antispam:s2s_receive_packet({Spam, undefined})). ?match({stop, {drop, undefined}}, mod_antispam:s2s_receive_packet({Spam, undefined})).
message_hello(Username, Host, Config) ->
SpamFrom = jid:make(Username, Host, <<"spam_client">>),
To = my_jid(Config),
message(SpamFrom, To, <<"hello world">>).
message(From, To, BodyText) ->
#message{from = From,
to = To,
type = chat,
body = [#text{data = BodyText}]}.