From c6785b1a34272eb129e832364fb2a97137e1363b Mon Sep 17 00:00:00 2001 From: Yudai Kiyofuji <57182816+z8674558@users.noreply.github.com> Date: Thu, 21 Jan 2021 11:03:06 +0900 Subject: [PATCH] test(coap): add test on acl and connection discarding (#4039) --- apps/emqx_coap/test/emqx_coap_SUITE.erl | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/apps/emqx_coap/test/emqx_coap_SUITE.erl b/apps/emqx_coap/test/emqx_coap_SUITE.erl index b8d01cf4c..672113e57 100644 --- a/apps/emqx_coap/test/emqx_coap_SUITE.erl +++ b/apps/emqx_coap/test/emqx_coap_SUITE.erl @@ -218,6 +218,46 @@ t_invalid_topic(_Config) -> Reply4 = er_coap_client:request(put, URI4, #coap_content{format = <<"application/octet-stream">>, payload = Payload3}), ?assertMatch({error,bad_request}, Reply4). +% mqtt connection kicked by coap with same client id +t_kick_1(_Config) -> + URI = "coap://127.0.0.1/mqtt/abc?c=clientid&u=tom&p=secret", + % workaround: emqx:subscribe does not kick same client id. + spawn_monitor(fun() -> + {ok, C} = emqtt:start_link([{host, "localhost"}, + {clientid, <<"clientid">>}, + {username, <<"plain">>}, + {password, <<"plain">>}]), + {ok, _} = emqtt:connect(C) end), + er_coap_client:request(put, URI, #coap_content{format = <<"application/octet-stream">>, + payload = <<"123">>}), + receive + {'DOWN', _, _, _, _} -> ok + after 2000 -> + ?assert(false) + end. + +% mqtt connection kicked by coap with same client id +t_acl(Config) -> + %% Update acl file and reload mod_acl_internal + Path = filename:join([testdir(proplists:get_value(data_dir, Config)), "deny.conf"]), + ok = file:write_file(Path, <<"{deny, {user, \"coap\"}, publish, [\"abc\"]}.">>), + OldPath = emqx:get_env(acl_file), + emqx_mod_acl_internal:reload([{acl_file, Path}]), + + emqx:subscribe(<<"abc">>), + URI = "coap://127.0.0.1/mqtt/adbc?c=client1&u=coap&p=secret", + er_coap_client:request(put, URI, #coap_content{format = <<"application/octet-stream">>, + payload = <<"123">>}), + receive + _Something -> ?assert(false) + after 2000 -> + ok + end, + + application:set_env(emqx, acl_file, OldPath), + file:delete(Path), + emqx_mod_acl_internal:reload([{acl_file, OldPath}]). + t_stats(_) -> ok. @@ -238,3 +278,6 @@ receive_notification() -> receive_notification_timeout end. +testdir(DataPath) -> + Ls = filename:split(DataPath), + filename:join(lists:sublist(Ls, 1, length(Ls) - 1)).