Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mimemail: test binary attachents with proper #272

Merged
merged 1 commit into from
Aug 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions test/prop_mimemail.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @doc property-based tests for `mimemail' module
%%
%% We only have 2 types of tests:
%% * Testing that we can encode valid `mimetuple()' without crashes
%% * Testing that we can encode and then decode `mimetuple()' without loosing information
%% Following limitations of mimemail are discovered and modelled in this suite:
%% * We may truncate leading and trailing whitespaces " " from header values
%% * We may truncate trailing tabs and whitespaces from payload when Content-Transfer-Encoding is not base64
%% * For binary payload it's highly recommended to set `#{transfer_encoding => <<"base64">>}' explicitly
-module(prop_mimemail).

-export([
Expand Down Expand Up @@ -89,8 +90,8 @@ match({TypeA, SubTypeA, HeadersA, _ParamsA, BodyA},
end, HeadersA),
case is_binary(BodyA) of
true ->
?assertEqual(string:trim(BodyA, trailing, "\t "),
string:trim(BodyB, trailing, "\t ")),
?assertEqual(trim_trail(BodyA, "\t "),
trim_trail(BodyB, "\t ")),
true;
false ->
Bodies = lists:zip(BodyA, BodyB),
Expand All @@ -106,7 +107,7 @@ prop_quoted_printable(doc) ->
"* decode(encode(data)) returns the same result as original input".

prop_quoted_printable() ->
Trim = fun(B) -> binstr:reverse(trim(binstr:reverse(trim(B)))) end,
Trim = fun(B) -> trim_both(B, "\t ") end,
?FORALL(
Body,
proper_types:oneof([?SIZED(Size, printable_ascii(Size * 50)),
Expand All @@ -122,10 +123,22 @@ prop_quoted_printable() ->
true
end).

trim(<<C, Tail/binary>>) when C == $\s; C == $\t ->
trim(Tail);
trim(Tail) ->
Tail.
trim(B) ->
trim(B, "\t ").

trim_both(B, Chars) ->
trim_trail(trim(B, Chars), Chars).

trim_trail(B, Chars) ->
binstr:reverse(trim(binstr:reverse(B), Chars)).

trim(<<C, Tail/binary>> = B, Chars) ->
case lists:member(C, Chars) of
true -> trim(Tail);
false -> B
end;
trim(<<>>, _) ->
<<>>.

prop_smtp_compatible(doc) ->
"Makes sure mimemail never produces output that is not compatible with SMTP, "
Expand Down Expand Up @@ -191,7 +204,8 @@ gen_multipart_mail() ->
proper_types:list(
proper_types:oneof(
[gen_embedded_plaintext_mail(),
gen_embedded_html_mail()]))))
gen_embedded_html_mail(),
gen_embedded_attachment_mail()]))))
}.

%% top-level plaintext mimemail()
Expand All @@ -218,6 +232,12 @@ gen_embedded_html_mail() ->
gen_body(),
<<"<!doctype html><html><body><p>", Body/binary, "</p></body></html>">>)}.

gen_embedded_attachment_mail() ->
{<<"application">>, <<"pdf">>,
gen_headers(),
gen_attachment_props(),
proper_types:non_empty(proper_types:binary())}.

%% like gen_headers/0, but `From' is always there
gen_top_headers() ->
?LET(KV, gen_headers(), lists:ukeysort(1, [{<<"From">>, <<"[email protected]">>} | KV])).
Expand Down Expand Up @@ -265,6 +285,23 @@ gen_props() ->
),
maps:from_list(KV)).

gen_attachment_props() ->
?LET(KV,
proper_types:list(
proper_types:oneof(
[{content_type_params, gen_params()},
{disposition_params, gen_params()}]
)),
maps:from_list([{disposition, <<"attachment">>},
{transfer_encoding, <<"base64">>} | KV])).

gen_params() ->
proper_types:list(
{
header_name(),
header_name()
}).

%% binary(), guaranteed to be not `<<>>'. Also, try to generate relatively large body
gen_nonempty_body() ->
proper_types:oneof(
Expand Down