25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +01:00

Add tests for mod_http_upload

This commit is contained in:
Evgeniy Khramtsov 2018-05-17 12:02:00 +03:00
parent b64e1d95d2
commit e4c106e0dd
4 changed files with 221 additions and 8 deletions

View File

@ -388,7 +388,8 @@ no_db_tests() ->
muc_tests:master_slave_cases(),
proxy65_tests:single_cases(),
proxy65_tests:master_slave_cases(),
replaced_tests:master_slave_cases()].
replaced_tests:master_slave_cases(),
upload_tests:single_cases()].
db_tests(riak) ->
%% No support for mod_pubsub

View File

@ -475,6 +475,7 @@ listen:
captcha: true
request_handlers:
"/api": mod_http_api
"/upload": mod_http_upload
-
port: @@component_port@@
module: ejabberd_service
@ -505,6 +506,11 @@ Welcome to this XMPP server."
resume_timeout: 3
mod_time: []
mod_version: []
mod_http_upload:
docroot: "@@priv_dir@@"
put_url: "http://upload.@HOST@:@@web_port@@/upload"
get_url: "http://upload.@HOST@:@@web_port@@/upload"
max_size: 10000
registration_timeout: infinity
route_subdomains: s2s
domain_certfile: CERTFILE

View File

@ -68,7 +68,8 @@ init_config(Config) ->
{pgsql_port, 5432},
{pgsql_db, <<"ejabberd_test">>},
{pgsql_user, <<"ejabberd_test">>},
{pgsql_pass, <<"ejabberd_test">>}
{pgsql_pass, <<"ejabberd_test">>},
{priv_dir, PrivDir}
]),
HostTypes = re:split(CfgContent, "(\\s*- \"(.*)\\.localhost\")",
[group, {return, binary}]),
@ -191,12 +192,12 @@ 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
V when is_integer(V) ->
integer_to_binary(V);
V when is_atom(V) ->
atom_to_binary(V, latin1);
V ->
iolist_to_binary(V)
end,
NewContent = binary:replace(Content,
<<"@@",(atom_to_binary(Name,latin1))/binary, "@@">>,
@ -681,6 +682,10 @@ proxy_jid(Config) ->
Server = ?config(server, Config),
jid:make(<<>>, <<"proxy.", Server/binary>>, <<>>).
upload_jid(Config) ->
Server = ?config(server, Config),
jid:make(<<>>, <<"upload.", Server/binary>>, <<>>).
muc_jid(Config) ->
Server = ?config(server, Config),
jid:make(<<>>, <<"conference.", Server/binary>>, <<>>).

201
test/upload_tests.erl Normal file
View File

@ -0,0 +1,201 @@
%%%-------------------------------------------------------------------
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% Created : 17 May 2018 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2018 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.
%%%
%%%-------------------------------------------------------------------
-module(upload_tests).
%% API
-compile(export_all).
-import(suite, [disconnect/1, is_feature_advertised/3, upload_jid/1,
my_jid/1, wait_for_slave/1, wait_for_master/1,
send_recv/2, put_event/2, get_event/1]).
-include("suite.hrl").
-define(CONTENT_TYPE, "image/png").
%%%===================================================================
%%% API
%%%===================================================================
%%%===================================================================
%%% Single user tests
%%%===================================================================
single_cases() ->
{upload_single, [sequence],
[single_test(feature_enabled),
single_test(get_max_size),
single_test(slot_request),
single_test(put_get_request),
single_test(max_size_exceed)]}.
feature_enabled(Config) ->
lists:foreach(
fun(NS) ->
true = is_feature_advertised(Config, NS, upload_jid(Config))
end, namespaces()),
disconnect(Config).
get_max_size(Config) ->
Xs = get_disco_info_xdata(Config),
lists:foreach(
fun(NS) ->
get_max_size(Config, Xs, NS)
end, namespaces()),
disconnect(Config).
get_max_size(_, _, ?NS_HTTP_UPLOAD_OLD) ->
%% This old spec didn't specify 'max-file-size' attribute
ok;
get_max_size(Config, Xs, NS) ->
Xs = get_disco_info_xdata(Config),
get_size(NS, Config, Xs).
slot_request(Config) ->
lists:foreach(
fun(NS) ->
slot_request(Config, NS)
end, namespaces()),
disconnect(Config).
put_get_request(Config) ->
lists:foreach(
fun(NS) ->
{GetURL, PutURL, _Filename, Size} = slot_request(Config, NS),
Data = randoms:bytes(Size),
put_request(Config, PutURL, Data),
get_request(Config, GetURL, Data)
end, namespaces()),
disconnect(Config).
max_size_exceed(Config) ->
lists:foreach(
fun(NS) ->
max_size_exceed(Config, NS)
end, namespaces()),
disconnect(Config).
%%%===================================================================
%%% Internal functions
%%%===================================================================
single_test(T) ->
list_to_atom("upload_" ++ atom_to_list(T)).
get_disco_info_xdata(Config) ->
To = upload_jid(Config),
#iq{type = result, sub_els = [#disco_info{xdata = Xs}]} =
send_recv(Config,
#iq{type = get, sub_els = [#disco_info{}], to = To}),
Xs.
get_size(NS, Config, [X|Xs]) ->
case xmpp_util:get_xdata_values(<<"FORM_TYPE">>, X) of
[NS] ->
[Size] = xmpp_util:get_xdata_values(<<"max-file-size">>, X),
true = erlang:binary_to_integer(Size) > 0,
Size;
_ ->
get_size(NS, Config, Xs)
end;
get_size(NS, _Config, []) ->
ct:fail({disco_info_xdata_failed, NS}).
slot_request(Config, NS) ->
To = upload_jid(Config),
Filename = filename(),
Size = randoms:uniform(1, 1024),
case NS of
?NS_HTTP_UPLOAD_0 ->
#iq{type = result,
sub_els = [#upload_slot_0{get = GetURL,
put = PutURL,
xmlns = NS}]} =
send_recv(Config,
#iq{type = get, to = To,
sub_els = [#upload_request_0{
filename = Filename,
size = Size,
'content-type' = <<?CONTENT_TYPE>>,
xmlns = NS}]}),
{GetURL, PutURL, Filename, Size};
_ ->
#iq{type = result,
sub_els = [#upload_slot{get = GetURL,
put = PutURL,
xmlns = NS}]} =
send_recv(Config,
#iq{type = get, to = To,
sub_els = [#upload_request{
filename = Filename,
size = Size,
'content-type' = <<?CONTENT_TYPE>>,
xmlns = NS}]}),
{GetURL, PutURL, Filename, Size}
end.
put_request(Config, URL0, Data) ->
ct:comment("Putting ~B bytes to ~s", [size(Data), URL0]),
URL = binary_to_list(URL0),
{ok, {{"HTTP/1.1", 201, _}, _, _}} =
httpc:request(put, {URL, [], ?CONTENT_TYPE, Data}, [], []).
get_request(Config, URL0, Data) ->
ct:comment("Getting ~B bytes from ~s", [size(Data), URL0]),
URL = binary_to_list(URL0),
{ok, {{"HTTP/1.1", 200, _}, _, Body}} =
httpc:request(get, {URL, []}, [], [{body_format, binary}]),
ct:comment("Checking returned body"),
Body = Data.
max_size_exceed(Config, NS) ->
To = upload_jid(Config),
Filename = filename(),
Size = 1000000000,
IQErr =
case NS of
?NS_HTTP_UPLOAD_0 ->
#iq{type = error} =
send_recv(Config,
#iq{type = get, to = To,
sub_els = [#upload_request_0{
filename = Filename,
size = Size,
'content-type' = <<?CONTENT_TYPE>>,
xmlns = NS}]});
_ ->
#iq{type = error} =
send_recv(Config,
#iq{type = get, to = To,
sub_els = [#upload_request{
filename = Filename,
size = Size,
'content-type' = <<?CONTENT_TYPE>>,
xmlns = NS}]})
end,
check_size_error(IQErr).
check_size_error(IQErr) ->
Err = xmpp:get_error(IQErr),
#stanza_error{reason = 'not-acceptable'} = Err.
namespaces() ->
[?NS_HTTP_UPLOAD_0, ?NS_HTTP_UPLOAD, ?NS_HTTP_UPLOAD_OLD].
filename() ->
<<(randoms:get_string())/binary, ".png">>.