aboutsummaryrefslogtreecommitdiff
path: root/test/upload_tests.erl
diff options
context:
space:
mode:
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>2018-05-17 12:02:00 +0300
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>2018-05-17 12:02:00 +0300
commite4c106e0ddd8295a04a0a44cc33649c845a0d76c (patch)
treef23cb438051cd0f47603bbbd169b19e20b5506c9 /test/upload_tests.erl
parentFix typo file:read() -> file:open() (diff)
Add tests for mod_http_upload
Diffstat (limited to 'test/upload_tests.erl')
-rw-r--r--test/upload_tests.erl201
1 files changed, 201 insertions, 0 deletions
diff --git a/test/upload_tests.erl b/test/upload_tests.erl
new file mode 100644
index 000000000..aa847e4bd
--- /dev/null
+++ b/test/upload_tests.erl
@@ -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">>.