Skip to content

Commit

Permalink
test: send_dgram exceed max MTU
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Dec 11, 2024
1 parent ac85fa0 commit 6a0b547
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/quicer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ do_recv(Stream, Count, Buff) ->
%% @see send/2 send_dgram/2
-spec async_send_dgram(connection_handle(), binary()) ->
{ok, non_neg_integer()}
| {error, badarg | not_enough_mem | closed}
| {error, badarg | not_enough_mem | invalid_parameter | closed}
| {error, dgram_send_error, atom_reason()}.
async_send_dgram(Conn, Data) ->
quicer_nif:send_dgram(Conn, Data, _IsSyncRel = 1).
Expand All @@ -870,7 +870,7 @@ async_send_dgram(Conn, Data) ->
%% @see send/2, async_send_dgram/2
-spec send_dgram(connection_handle(), binary()) ->
{ok, BytesSent :: non_neg_integer()}
| {error, badarg | not_enough_mem | closed}
| {error, badarg | not_enough_mem | invalid_parameter | closed}
| {error, dgram_send_error, atom_reason()}.
send_dgram(Conn, Data) ->
case quicer_nif:send_dgram(Conn, Data, _IsSync = 1) of
Expand Down
2 changes: 1 addition & 1 deletion src/quicer_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ recv(_Stream, _Len) ->

-spec send_dgram(connection_handle(), iodata(), send_flags()) ->
{ok, BytesSent :: pos_integer()}
| {error, badarg | not_enough_memory | closed}
| {error, badarg | not_enough_memory | invalid_parameter | closed}
| {error, dgram_send_error, atom_reason()}.
send_dgram(_Conn, _Data, _Flags) ->
erlang:nif_error(nif_library_not_loaded).
Expand Down
18 changes: 11 additions & 7 deletions test/prop_stateful_server_conn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ postcondition(#{state := S}, {call, quicer, handshake, _Args}, {error, invalid_s
S =/= accepted
->
true;
postcondition(#{state := S}, {call, quicer, handshake, _Args}, {error, timeout}) when
S =/= accepted
->
true;
postcondition(_State, {call, quicer, getopt, _Args}, {ok, _}) ->
true;
postcondition(_State, {call, quicer, getopt, [_, password]}, {error, badarg}) ->
Expand Down Expand Up @@ -335,7 +339,9 @@ postcondition(_State, {call, _Mod, _Fun, _Args} = _Call, _Res) ->
next_state(State, Res, Call) ->
step_calls(do_next_state(State, Res, Call)).

do_next_state(#{state := _} = State, {error, closed}, {call, quicer, _, _Args}) ->
do_next_state(
#{state := _} = State, {error, closed}, {call, _M, _F, _A}
) ->
State#{state := closed};
do_next_state(#{state := accepted} = State, {error, _}, {call, quicer, handshake, _Args}) ->
State;
Expand All @@ -353,10 +359,6 @@ do_next_state(
#{state := _} = State, ok, {call, quicer, controlling_process, [_, Owner]}
) ->
State#{owner := Owner};
do_next_state(
#{state := _} = State, {error, closed}, {call, _M, _F, _A}
) ->
State#{state := closed};
do_next_state(State, _Res, {call, _Mod, _Fun, _Args}) ->
State.

Expand Down Expand Up @@ -405,7 +407,8 @@ default_listen_opts() ->
{handshake_idle_timeout_ms, 100},
% QUIC_SERVER_RESUME_AND_ZERORTT
{server_resumption_level, 2},
{peer_bidi_stream_count, 10}
{peer_bidi_stream_count, 10},
{datagram_receive_enabled, true}
].

default_conn_opts() ->
Expand All @@ -416,5 +419,6 @@ default_conn_opts() ->
{idle_timeout_ms, 5000},
{cacertfile, "./msquic/submodules/openssl/test/certs/rootCA.pem"},
{certfile, "./msquic/submodules/openssl/test/certs/servercert.pem"},
{keyfile, "./msquic/submodules/openssl/test/certs/serverkey.pem"}
{keyfile, "./msquic/submodules/openssl/test/certs/serverkey.pem"},
{datagram_receive_enabled, true}
].
33 changes: 33 additions & 0 deletions test/quicer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
tc_dgram_client_send/1,
tc_dgram_client_send_iolist/1,
tc_dgram_client_send_fail/1,
tc_dgram_client_send_exceed_mtu/1,

% , tc_getopt_raw/1
tc_getopt/1,
Expand Down Expand Up @@ -894,6 +895,38 @@ tc_dgram_client_send_fail(_) ->
),
ok.

tc_dgram_client_send_exceed_mtu(Config) ->
Port = select_port(),
Owner = self(),
{SPid, Ref} = spawn_monitor(fun() -> ping_pong_server_dgram(Owner, Config, Port) end),
receive
listener_ready ->
ok
after 1000 ->
ct:fail("timeout here")
end,
%% GIVEN: datagram is enabled
Opts = default_conn_opts() ++ [{datagram_receive_enabled, 1}],
{ok, Conn} = quicer:connect("localhost", Port, Opts, 5000),
{ok, Stm} = quicer:start_stream(Conn, []),
{ok, 4} = quicer:send(Stm, <<"ping">>),
{ok, V2stats} = quicer:getopt(Conn, statistics_v2),
MtuMax = proplists:get_value(send_path_mtu, V2stats),
%% WHEN: send a datagram that is less than MTU-100
Length = MtuMax - 100,
%% THEN: send should success
{ok, Length} = quicer:send_dgram(Conn, crypto:strong_rand_bytes(Length)),
?assertEqual(
{error, dgram_send_error, invalid_parameter},
quicer:send_dgram(Conn, crypto:strong_rand_bytes(MtuMax * 2))
),
flush_streams_available(Conn),
flush_datagram_state_changed(Conn),
%% WHEN: send a datagram that is 2xMTU size
%% THEN: send should fail
SPid ! done,
ok = ensure_server_exit_normal(Ref).

tc_dgram_client_send(Config) ->
Port = select_port(),
Owner = self(),
Expand Down

0 comments on commit 6a0b547

Please sign in to comment.