emqttd_access_control_tests.erl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. %%%-----------------------------------------------------------------------------
  2. %%% @Copyright (C) 2012-2015, Feng Lee <feng@emqtt.io>
  3. %%%
  4. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  5. %%% of this software and associated documentation files (the "Software"), to deal
  6. %%% in the Software without restriction, including without limitation the rights
  7. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. %%% copies of the Software, and to permit persons to whom the Software is
  9. %%% furnished to do so, subject to the following conditions:
  10. %%%
  11. %%% The above copyright notice and this permission notice shall be included in all
  12. %%% copies or substantial portions of the Software.
  13. %%%
  14. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. %%% SOFTWARE.
  21. %%%-----------------------------------------------------------------------------
  22. %%% @doc
  23. %%% emqttd_access_control tests.
  24. %%%
  25. %%% @end
  26. %%%-----------------------------------------------------------------------------
  27. -module(emqttd_access_control_tests).
  28. -include("emqttd.hrl").
  29. -ifdef(TEST).
  30. -include_lib("eunit/include/eunit.hrl").
  31. reload_acl_test() ->
  32. with_acl(
  33. fun() ->
  34. ?assertEqual([ok], emqttd_access_control:reload_acl())
  35. end).
  36. register_mod_test() ->
  37. with_acl(
  38. fun() ->
  39. emqttd_access_control:register_mod(acl, emqttd_acl_test_mod, []),
  40. ?assertMatch([{emqttd_acl_test_mod, _, 0}, {emqttd_acl_internal, _, 0}],
  41. emqttd_access_control:lookup_mods(acl)),
  42. emqttd_access_control:register_mod(auth, emqttd_auth_anonymous_test_mod,[]),
  43. emqttd_access_control:register_mod(auth, emqttd_auth_dashboard, [], 99),
  44. ?assertMatch([{emqttd_auth_dashboard, _, 99},
  45. {emqttd_auth_anonymous_test_mod, _, 0},
  46. {emqttd_auth_anonymous, _, 0}],
  47. emqttd_access_control:lookup_mods(auth))
  48. end).
  49. unregister_mod_test() ->
  50. with_acl(
  51. fun() ->
  52. emqttd_access_control:register_mod(acl, emqttd_acl_test_mod, []),
  53. ?assertMatch([{emqttd_acl_test_mod, _, 0}, {emqttd_acl_internal, _, 0}],
  54. emqttd_access_control:lookup_mods(acl)),
  55. emqttd_access_control:unregister_mod(acl, emqttd_acl_test_mod),
  56. timer:sleep(5),
  57. ?assertMatch([{emqttd_acl_internal, _, 0}], emqttd_access_control:lookup_mods(acl)),
  58. emqttd_access_control:register_mod(auth, emqttd_auth_anonymous_test_mod,[]),
  59. ?assertMatch([{emqttd_auth_anonymous_test_mod, _, 0}, {emqttd_auth_anonymous, _, 0}],
  60. emqttd_access_control:lookup_mods(auth)),
  61. emqttd_access_control:unregister_mod(auth, emqttd_auth_anonymous_test_mod),
  62. timer:sleep(5),
  63. ?assertMatch([{emqttd_auth_anonymous, _, 0}], emqttd_access_control:lookup_mods(auth))
  64. end).
  65. check_acl_test() ->
  66. with_acl(
  67. fun() ->
  68. User1 = #mqtt_client{client_id = <<"client1">>, username = <<"testuser">>},
  69. User2 = #mqtt_client{client_id = <<"client2">>, username = <<"xyz">>},
  70. ?assertEqual(allow, emqttd_access_control:check_acl(User1, subscribe, <<"users/testuser/1">>)),
  71. ?assertEqual(allow, emqttd_access_control:check_acl(User1, subscribe, <<"clients/client1">>)),
  72. ?assertEqual(deny, emqttd_access_control:check_acl(User1, subscribe, <<"clients/client1/x/y">>)),
  73. ?assertEqual(allow, emqttd_access_control:check_acl(User1, publish, <<"users/testuser/1">>)),
  74. ?assertEqual(allow, emqttd_access_control:check_acl(User1, subscribe, <<"a/b/c">>)),
  75. ?assertEqual(deny, emqttd_access_control:check_acl(User2, subscribe, <<"a/b/c">>))
  76. end).
  77. with_acl(Fun) ->
  78. process_flag(trap_exit, true),
  79. AclOpts = [
  80. {auth, [
  81. %% Authentication with username, password
  82. %{username, []},
  83. %% Authentication with clientid
  84. %{clientid, [{password, no}, {file, "etc/clients.config"}]},
  85. %% Allow all
  86. {anonymous, []}
  87. ]},
  88. %% ACL config
  89. {acl, [
  90. %% Internal ACL module
  91. {internal, [{file, "../test/test_acl.config"}, {nomatch, allow}]}
  92. ]}
  93. ],
  94. %application:set_env(emqttd, access, AclOpts),
  95. emqttd_access_control:start_link(AclOpts),
  96. Fun(),
  97. emqttd_access_control:stop().
  98. -endif.