From ec183f1d4cf5f3fa3a2e7d88e660fa477ec700dd Mon Sep 17 00:00:00 2001 From: firest Date: Sat, 13 Jul 2024 18:54:57 +0800 Subject: [PATCH] test(coap): fix ci errors --- .../test/emqx_gateway_authz_SUITE.erl | 6 ++-- .../src/emqx_coap_channel.erl | 29 ++++++++++++++++--- .../src/emqx_coap_proxy_conn.erl | 16 +++++++--- .../test/emqx_coap_SUITE.erl | 3 +- .../test/emqx_coap_api_SUITE.erl | 3 +- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/apps/emqx_gateway/test/emqx_gateway_authz_SUITE.erl b/apps/emqx_gateway/test/emqx_gateway_authz_SUITE.erl index 5b262158f..87db2f64c 100644 --- a/apps/emqx_gateway/test/emqx_gateway_authz_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_gateway_authz_SUITE.erl @@ -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). diff --git a/apps/emqx_gateway_coap/src/emqx_coap_channel.erl b/apps/emqx_gateway_coap/src/emqx_coap_channel.erl index a10fa04b2..b2aac2a4c 100644 --- a/apps/emqx_gateway_coap/src/emqx_coap_channel.erl +++ b/apps/emqx_gateway_coap/src/emqx_coap_channel.erl @@ -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( diff --git a/apps/emqx_gateway_coap/src/emqx_coap_proxy_conn.erl b/apps/emqx_gateway_coap/src/emqx_coap_proxy_conn.erl index 703450d7e..2eb8419f4 100644 --- a/apps/emqx_gateway_coap/src/emqx_coap_proxy_conn.erl +++ b/apps/emqx_gateway_coap/src/emqx_coap_proxy_conn.erl @@ -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 diff --git a/apps/emqx_gateway_coap/test/emqx_coap_SUITE.erl b/apps/emqx_gateway_coap/test/emqx_coap_SUITE.erl index bb244c8d4..5a4a027ab 100644 --- a/apps/emqx_gateway_coap/test/emqx_coap_SUITE.erl +++ b/apps/emqx_gateway_coap/test/emqx_coap_SUITE.erl @@ -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) -> diff --git a/apps/emqx_gateway_coap/test/emqx_coap_api_SUITE.erl b/apps/emqx_gateway_coap/test/emqx_coap_api_SUITE.erl index 79975d331..6bfa3a90b 100644 --- a/apps/emqx_gateway_coap/test/emqx_coap_api_SUITE.erl +++ b/apps/emqx_gateway_coap/test/emqx_coap_api_SUITE.erl @@ -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).