emqx_authz_cache_SUITE.erl 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 = atom_to_binary(?FUNCTION_NAME),
  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. ClientId = atom_to_binary(?FUNCTION_NAME),
  42. {ok, Client} = emqtt:start_link([{clientid, ClientId}]),
  43. {ok, _} = emqtt:connect(Client),
  44. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  45. emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, 0),
  46. ClientPid = find_client_pid(ClientId),
  47. Caches = list_cache(ClientPid),
  48. ct:log("authz caches: ~p", [Caches]),
  49. ?assert(length(Caches) > 0),
  50. erlang:send(ClientPid, clean_authz_cache),
  51. ?assertEqual([], list_cache(ClientPid)),
  52. emqtt:stop(Client).
  53. t_drain_authz_cache(_) ->
  54. ClientId = atom_to_binary(?FUNCTION_NAME),
  55. {ok, Client} = emqtt:start_link([{clientid, ClientId}]),
  56. {ok, _} = emqtt:connect(Client),
  57. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  58. emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, 0),
  59. ClientPid = find_client_pid(ClientId),
  60. Caches = list_cache(ClientPid),
  61. ct:log("authz caches: ~p", [Caches]),
  62. ?assert(length(Caches) > 0),
  63. emqx_authz_cache:drain_cache(),
  64. ?assertEqual([], list_cache(ClientPid)),
  65. ct:sleep(100),
  66. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  67. ?assert(length(list_cache(ClientPid)) > 0),
  68. emqtt:stop(Client).
  69. list_cache(ClientId) when is_binary(ClientId) ->
  70. ClientPid = find_client_pid(ClientId),
  71. list_cache(ClientPid);
  72. list_cache(ClientPid) ->
  73. gen_server:call(ClientPid, list_authz_cache).
  74. find_client_pid(ClientId) ->
  75. ?retry(_Inteval = 100, _Attempts = 10, do_find_client_pid(ClientId)).
  76. do_find_client_pid(ClientId) ->
  77. case emqx_cm:lookup_channels(ClientId) of
  78. Pids when is_list(Pids) ->
  79. lists:last(Pids);
  80. _ ->
  81. throw({not_found, ClientId})
  82. end.