emqx_acl_cache_SUITE.erl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020 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_acl_cache_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. all() -> emqx_ct:all(?MODULE).
  21. init_per_suite(Config) ->
  22. emqx_ct_helpers:boot_modules(all),
  23. emqx_ct_helpers:start_apps([]),
  24. Config.
  25. end_per_suite(_Config) ->
  26. emqx_ct_helpers:stop_apps([]).
  27. %%--------------------------------------------------------------------
  28. %% Test cases
  29. %%--------------------------------------------------------------------
  30. t_clean_acl_cache(_) ->
  31. {ok, Client} = emqtt:start_link([{clientid, <<"emqx_c">>}]),
  32. {ok, _} = emqtt:connect(Client),
  33. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  34. emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, 0),
  35. ct:sleep(100),
  36. ClientPid = case emqx_cm:lookup_channels(<<"emqx_c">>) of
  37. [Pid] when is_pid(Pid) ->
  38. Pid;
  39. Pids when is_list(Pids) ->
  40. lists:last(Pids);
  41. _ -> {error, not_found}
  42. end,
  43. Caches = gen_server:call(ClientPid, list_acl_cache),
  44. ct:log("acl caches: ~p", [Caches]),
  45. ?assert(length(Caches) > 0),
  46. erlang:send(ClientPid, clean_acl_cache),
  47. ?assertEqual(0, length(gen_server:call(ClientPid, list_acl_cache))),
  48. emqtt:stop(Client).
  49. % optimize??
  50. t_reload_aclfile_and_cleanall(Config) ->
  51. RasieMsg = fun() -> Self = self(), #{puback => fun(Msg) -> Self ! {puback, Msg} end,
  52. disconnected => fun(_) -> ok end,
  53. publish => fun(_) -> ok end } end,
  54. {ok, Client} = emqtt:start_link([{clientid, <<"emqx_c">>}, {proto_ver, v5}, {msg_handler, RasieMsg()}]),
  55. {ok, _} = emqtt:connect(Client),
  56. {ok, PktId} = emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, qos1),
  57. %% Success publish to broker
  58. receive
  59. {puback, #{packet_id := PktId, reason_code := Rc}} ->
  60. ?assertEqual(16#10, Rc);
  61. _ ->
  62. ?assert(false)
  63. end,
  64. %% Check acl cache list
  65. [ClientPid] = emqx_cm:lookup_channels(<<"emqx_c">>),
  66. ?assert(length(gen_server:call(ClientPid, list_acl_cache)) > 0),
  67. %% Update acl file and reload mod_acl_internal
  68. Path = filename:join([testdir(proplists:get_value(data_dir, Config)), "acl2.conf"]),
  69. ok = file:write_file(Path, <<"{deny, all}.">>),
  70. OldPath = emqx:get_env(acl_file),
  71. % application:set_env(emqx, acl_file, Path),
  72. emqx_mod_acl_internal:reload([{acl_file, Path}]),
  73. ?assert(length(gen_server:call(ClientPid, list_acl_cache)) == 0),
  74. {ok, PktId2} = emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, qos1),
  75. receive
  76. {puback, #{packet_id := PktId2, reason_code := Rc2}} ->
  77. %% Not authorized
  78. ?assertEqual(16#87, Rc2);
  79. _ ->
  80. ?assert(false)
  81. end,
  82. application:set_env(emqx, acl_file, OldPath),
  83. file:delete(Path),
  84. emqx_mod_acl_internal:reload([{acl_file, OldPath}]),
  85. emqtt:stop(Client).
  86. %% @private
  87. testdir(DataPath) ->
  88. Ls = filename:split(DataPath),
  89. filename:join(lists:sublist(Ls, 1, length(Ls) - 1)).
  90. % t_cache_k(_) ->
  91. % error('TODO').
  92. % t_cache_v(_) ->
  93. % error('TODO').
  94. % t_cleanup_acl_cache(_) ->
  95. % error('TODO').
  96. % t_get_oldest_key(_) ->
  97. % error('TODO').
  98. % t_get_newest_key(_) ->
  99. % error('TODO').
  100. % t_get_cache_max_size(_) ->
  101. % error('TODO').
  102. % t_get_cache_size(_) ->
  103. % error('TODO').
  104. % t_dump_acl_cache(_) ->
  105. % error('TODO').
  106. % t_empty_acl_cache(_) ->
  107. % error('TODO').
  108. % t_put_acl_cache(_) ->
  109. % error('TODO').
  110. % t_get_acl_cache(_) ->
  111. % error('TODO').
  112. % t_is_enabled(_) ->
  113. % error('TODO').