test(coap): fix ci errors

This commit is contained in:
firest 2024-07-13 18:54:57 +08:00
parent 854754eb60
commit ec183f1d4c
5 changed files with 45 additions and 12 deletions

View File

@ -97,7 +97,8 @@ t_case_coap_publish(_) ->
end,
Case = fun(Channel, Token) ->
Fun(Channel, Token, <<"/publish">>, ?checkMatch({ok, changed, _})),
Fun(Channel, Token, <<"/badpublish">>, ?checkMatch({error, uauthorized}))
Fun(Channel, Token, <<"/badpublish">>, ?checkMatch({error, uauthorized})),
true
end,
Mod:with_connection(Case).
@ -113,7 +114,8 @@ t_case_coap_subscribe(_) ->
end,
Case = fun(Channel, Token) ->
Fun(Channel, Token, <<"/subscribe">>, ?checkMatch({ok, content, _})),
Fun(Channel, Token, <<"/badsubscribe">>, ?checkMatch({error, uauthorized}))
Fun(Channel, Token, <<"/badsubscribe">>, ?checkMatch({error, uauthorized})),
true
end,
Mod:with_connection(Case).

View File

@ -410,6 +410,19 @@ is_create_connection_request(Msg = #coap_message{method = Method}) when
is_create_connection_request(_Msg) ->
false.
is_delete_connection_request(Msg = #coap_message{method = Method}) when
is_atom(Method) andalso Method =/= undefined
->
URIPath = emqx_coap_message:get_option(uri_path, Msg, []),
case URIPath of
[<<"mqtt">>, <<"connection">>] when Method == delete ->
true;
_ ->
false
end;
is_delete_connection_request(_Msg) ->
false.
check_token(
Msg,
#channel{
@ -424,10 +437,18 @@ check_token(
<<"token">> := Token
} ->
call_session(handle_request, Msg, Channel);
_ ->
ErrMsg = <<"Missing token or clientid in connection mode">>,
Reply = emqx_coap_message:piggyback({error, bad_request}, ErrMsg, Msg),
{ok, {outgoing, Reply}, Channel}
Any ->
%% This channel is create by this DELETE command, so here can safely close this channel
case Token =:= undefined andalso is_delete_connection_request(Msg) of
true ->
Reply = emqx_coap_message:piggyback({ok, deleted}, Msg),
{shutdown, normal, Reply, Channel};
false ->
io:format(">>> C1:~p, T1:~p~nC2:~p~n", [ClientId, Token, Any]),
ErrMsg = <<"Missing token or clientid in connection mode">>,
Reply = emqx_coap_message:piggyback({error, bad_request}, ErrMsg, Msg),
{ok, {outgoing, Reply}, Channel}
end
end.
run_conn_hooks(

View File

@ -20,7 +20,7 @@
-include("emqx_coap.hrl").
-export([initialize/1, create/3, get_connection_id/4, dispatch/3, close/2]).
-export([initialize/1, find_or_create/4, get_connection_id/4, dispatch/3, close/2]).
%%--------------------------------------------------------------------
%% Callbacks
@ -28,8 +28,13 @@
initialize(_Opts) ->
emqx_coap_frame:initial_parse_state(#{}).
create(Transport, Peer, Opts) ->
emqx_gateway_conn:start_link(Transport, Peer, Opts).
find_or_create(CId, Transport, Peer, Opts) ->
case emqx_gateway_cm_registry:lookup_channels(coap, CId) of
[Pid] ->
{ok, Pid};
[] ->
emqx_gateway_conn:start_link(Transport, Peer, Opts)
end.
get_connection_id(_Transport, _Peer, State, Data) ->
case parse_incoming(Data, [], State) of
@ -40,7 +45,10 @@ get_connection_id(_Transport, _Peer, State, Data) ->
} ->
{ok, ClientId, Packets, NState};
_ ->
invalid
ErrMsg = <<"Missing token or clientid in connection mode">>,
Reply = emqx_coap_message:piggyback({error, bad_request}, ErrMsg, Msg),
Bin = emqx_coap_frame:serialize_pkt(Reply, emqx_coap_frame:serialize_opts()),
{error, Bin}
end;
_Error ->
invalid

View File

@ -165,7 +165,8 @@ t_connection(_) ->
emqx_gateway_cm_registry:lookup_channels(coap, <<"client1">>)
)
end,
do(Action).
do(Action),
ok.
t_connection_with_short_param_name(_) ->
Action = fun(Channel) ->

View File

@ -207,7 +207,8 @@ test_recv_coap_request(UdpSock) ->
test_send_coap_response(UdpSock, Host, Port, Code, Content, Request) ->
is_list(Host) orelse error("Host is not a string"),
{ok, IpAddr} = inet:getaddr(Host, inet),
Response = emqx_coap_message:piggyback(Code, Content, Request),
Response0 = emqx_coap_message:piggyback(Code, Content, Request),
Response = Response0#coap_message{options = #{uri_query => [<<"clientid=client1">>]}},
?LOGT("test_send_coap_response Response=~p", [Response]),
Binary = emqx_coap_frame:serialize_pkt(Response, undefined),
ok = gen_udp:send(UdpSock, IpAddr, Port, Binary).