emqx_authz_cache_SUITE.erl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_authz_cache_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -include_lib("snabbkaffe/include/snabbkaffe.hrl").
  21. all() -> emqx_common_test_helpers:all(?MODULE).
  22. init_per_suite(Config) ->
  23. Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
  24. emqx_config:put([authorization, cache, excludes], [<<"nocache/#">>]),
  25. [{apps, Apps} | Config].
  26. end_per_suite(Config) ->
  27. emqx_cth_suite:stop(proplists:get_value(apps, Config)).
  28. %%--------------------------------------------------------------------
  29. %% Test cases
  30. %%--------------------------------------------------------------------
  31. t_cache_exclude(_) ->
  32. ClientId = <<"test-id1">>,
  33. {ok, Client} = emqtt:start_link([{clientid, ClientId}]),
  34. {ok, _} = emqtt:connect(Client),
  35. {ok, _, _} = emqtt:subscribe(Client, <<"nocache/+/#">>, 0),
  36. emqtt:publish(Client, <<"nocache/1">>, <<"{\"x\":1}">>, 0),
  37. Caches = list_cache(ClientId),
  38. ?assertEqual([], Caches),
  39. emqtt:stop(Client).
  40. t_clean_authz_cache(_) ->
  41. {ok, Client} = emqtt:start_link([{clientid, <<"emqx_c">>}]),
  42. {ok, _} = emqtt:connect(Client),
  43. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  44. emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, 0),
  45. ClientPid = find_client_pid(<<"emqx_c">>),
  46. Caches = list_cache(ClientPid),
  47. ct:log("authz caches: ~p", [Caches]),
  48. ?assert(length(Caches) > 0),
  49. erlang:send(ClientPid, clean_authz_cache),
  50. ?assertEqual([], list_cache(ClientPid)),
  51. emqtt:stop(Client).
  52. t_drain_authz_cache(_) ->
  53. {ok, Client} = emqtt:start_link([{clientid, <<"emqx_c">>}]),
  54. {ok, _} = emqtt:connect(Client),
  55. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  56. emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, 0),
  57. ClientPid = find_client_pid(<<"emqx_c">>),
  58. Caches = list_cache(ClientPid),
  59. ct:log("authz caches: ~p", [Caches]),
  60. ?assert(length(Caches) > 0),
  61. emqx_authz_cache:drain_cache(),
  62. ?assertEqual([], list_cache(ClientPid)),
  63. ct:sleep(100),
  64. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  65. ?assert(length(list_cache(ClientPid)) > 0),
  66. emqtt:stop(Client).
  67. list_cache(ClientId) when is_binary(ClientId) ->
  68. ClientPid = find_client_pid(ClientId),
  69. list_cache(ClientPid);
  70. list_cache(ClientPid) ->
  71. gen_server:call(ClientPid, list_authz_cache).
  72. find_client_pid(ClientId) ->
  73. ?retry(_Inteval = 100, _Attempts = 10, do_find_client_pid(ClientId)).
  74. do_find_client_pid(ClientId) ->
  75. case emqx_cm:lookup_channels(ClientId) of
  76. Pids when is_list(Pids) ->
  77. lists:last(Pids);
  78. _ ->
  79. throw({not_found, ClientId})
  80. end.