From 6a0b5473820b31761eb4d5a4941d0a0980d2d6a3 Mon Sep 17 00:00:00 2001 From: William Yang Date: Thu, 17 Oct 2024 15:46:12 +0200 Subject: [PATCH] test: send_dgram exceed max MTU --- src/quicer.erl | 4 ++-- src/quicer_nif.erl | 2 +- test/prop_stateful_server_conn.erl | 18 +++++++++------- test/quicer_SUITE.erl | 33 ++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/quicer.erl b/src/quicer.erl index 01b4637f..49f1a111 100644 --- a/src/quicer.erl +++ b/src/quicer.erl @@ -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). @@ -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 diff --git a/src/quicer_nif.erl b/src/quicer_nif.erl index be95e7b6..d116d0ac 100644 --- a/src/quicer_nif.erl +++ b/src/quicer_nif.erl @@ -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). diff --git a/test/prop_stateful_server_conn.erl b/test/prop_stateful_server_conn.erl index 728c4615..55252fcf 100644 --- a/test/prop_stateful_server_conn.erl +++ b/test/prop_stateful_server_conn.erl @@ -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}) -> @@ -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; @@ -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. @@ -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() -> @@ -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} ]. diff --git a/test/quicer_SUITE.erl b/test/quicer_SUITE.erl index fd3d6293..65a3358b 100644 --- a/test/quicer_SUITE.erl +++ b/test/quicer_SUITE.erl @@ -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, @@ -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(),