emqttd_access_control_tests.erl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2012-2016 Feng Lee <feng@emqtt.io>.
  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(emqttd_access_control_tests).
  17. -ifdef(TEST).
  18. -include("emqttd.hrl").
  19. -include_lib("eunit/include/eunit.hrl").
  20. reload_acl_test() ->
  21. with_acl(
  22. fun() ->
  23. ?assertEqual([ok], emqttd_access_control:reload_acl())
  24. end).
  25. register_mod_test() ->
  26. with_acl(
  27. fun() ->
  28. emqttd_access_control:register_mod(acl, emqttd_acl_test_mod, []),
  29. ?assertMatch([{emqttd_acl_test_mod, _, 0}, {emqttd_acl_internal, _, 0}],
  30. emqttd_access_control:lookup_mods(acl)),
  31. emqttd_access_control:register_mod(auth, emqttd_auth_anonymous_test_mod,[]),
  32. emqttd_access_control:register_mod(auth, emqttd_auth_dashboard, [], 99),
  33. ?assertMatch([{emqttd_auth_dashboard, _, 99},
  34. {emqttd_auth_anonymous_test_mod, _, 0},
  35. {emqttd_auth_anonymous, _, 0}],
  36. emqttd_access_control:lookup_mods(auth))
  37. end).
  38. unregister_mod_test() ->
  39. with_acl(
  40. fun() ->
  41. emqttd_access_control:register_mod(acl, emqttd_acl_test_mod, []),
  42. ?assertMatch([{emqttd_acl_test_mod, _, 0}, {emqttd_acl_internal, _, 0}],
  43. emqttd_access_control:lookup_mods(acl)),
  44. emqttd_access_control:unregister_mod(acl, emqttd_acl_test_mod),
  45. timer:sleep(5),
  46. ?assertMatch([{emqttd_acl_internal, _, 0}], emqttd_access_control:lookup_mods(acl)),
  47. emqttd_access_control:register_mod(auth, emqttd_auth_anonymous_test_mod,[]),
  48. ?assertMatch([{emqttd_auth_anonymous_test_mod, _, 0}, {emqttd_auth_anonymous, _, 0}],
  49. emqttd_access_control:lookup_mods(auth)),
  50. emqttd_access_control:unregister_mod(auth, emqttd_auth_anonymous_test_mod),
  51. timer:sleep(5),
  52. ?assertMatch([{emqttd_auth_anonymous, _, 0}], emqttd_access_control:lookup_mods(auth))
  53. end).
  54. check_acl_test() ->
  55. with_acl(
  56. fun() ->
  57. User1 = #mqtt_client{client_id = <<"client1">>, username = <<"testuser">>},
  58. User2 = #mqtt_client{client_id = <<"client2">>, username = <<"xyz">>},
  59. ?assertEqual(allow, emqttd_access_control:check_acl(User1, subscribe, <<"users/testuser/1">>)),
  60. ?assertEqual(allow, emqttd_access_control:check_acl(User1, subscribe, <<"clients/client1">>)),
  61. ?assertEqual(deny, emqttd_access_control:check_acl(User1, subscribe, <<"clients/client1/x/y">>)),
  62. ?assertEqual(allow, emqttd_access_control:check_acl(User1, publish, <<"users/testuser/1">>)),
  63. ?assertEqual(allow, emqttd_access_control:check_acl(User1, subscribe, <<"a/b/c">>)),
  64. ?assertEqual(deny, emqttd_access_control:check_acl(User2, subscribe, <<"a/b/c">>))
  65. end).
  66. with_acl(Fun) ->
  67. process_flag(trap_exit, true),
  68. AclOpts = [
  69. {auth, [
  70. %% Authentication with username, password
  71. %{username, []},
  72. %% Authentication with clientid
  73. %{clientid, [{password, no}, {file, "etc/clients.config"}]},
  74. %% Allow all
  75. {anonymous, []}
  76. ]},
  77. %% ACL config
  78. {acl, [
  79. %% Internal ACL module
  80. {internal, [{file, "../test/test_acl.config"}, {nomatch, allow}]}
  81. ]}
  82. ],
  83. %application:set_env(emqttd, access, AclOpts),
  84. emqttd_access_control:start_link(AclOpts),
  85. Fun(),
  86. emqttd_access_control:stop().
  87. -endif.