Kaynağa Gözat

test(coap): fix ci errors

firest 1 yıl önce
ebeveyn
işleme
ec183f1d4c

+ 4 - 2
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).
 

+ 25 - 4
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(

+ 12 - 4
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

+ 2 - 1
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) ->

+ 2 - 1
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).