emqttd_access_rule_tests.erl 5.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_rule tests.
  24. %%%
  25. %%% @end
  26. %%%-----------------------------------------------------------------------------
  27. -module(emqttd_access_rule_tests).
  28. -import(emqttd_access_rule, [compile/1, match/3]).
  29. -include("emqttd.hrl").
  30. -ifdef(TEST).
  31. -include_lib("eunit/include/eunit.hrl").
  32. compile_test() ->
  33. ?assertMatch({allow, {'and', [{ipaddr, {"127.0.0.1", _I, _I}},
  34. {user, <<"user">>}]}, subscribe, [ [<<"$SYS">>, '#'], ['#'] ]},
  35. compile({allow, {'and', [{ipaddr, "127.0.0.1"}, {user, <<"user">>}]}, subscribe, ["$SYS/#", "#"]})),
  36. ?assertMatch({allow, {'or', [{ipaddr, {"127.0.0.1", _I, _I}},
  37. {user, <<"user">>}]}, subscribe, [ [<<"$SYS">>, '#'], ['#'] ]},
  38. compile({allow, {'or', [{ipaddr, "127.0.0.1"}, {user, <<"user">>}]}, subscribe, ["$SYS/#", "#"]})),
  39. ?assertMatch({allow, {ipaddr, {"127.0.0.1", _I, _I}}, subscribe, [ [<<"$SYS">>, '#'], ['#'] ]},
  40. compile({allow, {ipaddr, "127.0.0.1"}, subscribe, ["$SYS/#", "#"]})),
  41. ?assertMatch({allow, {user, <<"testuser">>}, subscribe, [ [<<"a">>, <<"b">>, <<"c">>], [<<"d">>, <<"e">>, <<"f">>, '#'] ]},
  42. compile({allow, {user, "testuser"}, subscribe, ["a/b/c", "d/e/f/#"]})),
  43. ?assertEqual({allow, {user, <<"admin">>}, pubsub, [ [<<"d">>, <<"e">>, <<"f">>, '#'] ]},
  44. compile({allow, {user, "admin"}, pubsub, ["d/e/f/#"]})),
  45. ?assertEqual({allow, {client, <<"testClient">>}, publish, [ [<<"testTopics">>, <<"testClient">>] ]},
  46. compile({allow, {client, "testClient"}, publish, ["testTopics/testClient"]})),
  47. ?assertEqual({allow, all, pubsub, [{pattern, [<<"clients">>, <<"$c">>]}]},
  48. compile({allow, all, pubsub, ["clients/$c"]})),
  49. ?assertEqual({allow, all, subscribe, [{pattern, [<<"users">>, <<"$u">>, '#']}]},
  50. compile({allow, all, subscribe, ["users/$u/#"]})),
  51. ?assertEqual({deny, all, subscribe, [ [<<"$SYS">>, '#'], ['#'] ]},
  52. compile({deny, all, subscribe, ["$SYS/#", "#"]})),
  53. ?assertEqual({allow, all}, compile({allow, all})),
  54. ?assertEqual({deny, all}, compile({deny, all})).
  55. match_test() ->
  56. User = #mqtt_client{peername = {{127,0,0,1}, 2948}, client_id = <<"testClient">>, username = <<"TestUser">>},
  57. User2 = #mqtt_client{peername = {{192,168,0,10}, 3028}, client_id = <<"testClient">>, username = <<"TestUser">>},
  58. ?assertEqual({matched, allow}, match(User, <<"Test/Topic">>, {allow, all})),
  59. ?assertEqual({matched, deny}, match(User, <<"Test/Topic">>, {deny, all})),
  60. ?assertMatch({matched, allow}, match(User, <<"Test/Topic">>,
  61. compile({allow, {ipaddr, "127.0.0.1"}, subscribe, ["$SYS/#", "#"]}))),
  62. ?assertMatch({matched, allow}, match(User2, <<"Test/Topic">>,
  63. compile({allow, {ipaddr, "192.168.0.1/24"}, subscribe, ["$SYS/#", "#"]}))),
  64. ?assertMatch({matched, allow}, match(User, <<"d/e/f/x">>, compile({allow, {user, "TestUser"}, subscribe, ["a/b/c", "d/e/f/#"]}))),
  65. ?assertEqual(nomatch, match(User, <<"d/e/f/x">>, compile({allow, {user, "admin"}, pubsub, ["d/e/f/#"]}))),
  66. ?assertMatch({matched, allow}, match(User, <<"testTopics/testClient">>,
  67. compile({allow, {client, "testClient"}, publish, ["testTopics/testClient"]}))),
  68. ?assertMatch({matched, allow}, match(User, <<"clients/testClient">>,
  69. compile({allow, all, pubsub, ["clients/$c"]}))),
  70. ?assertMatch({matched, allow}, match(#mqtt_client{username = <<"user2">>}, <<"users/user2/abc/def">>,
  71. compile({allow, all, subscribe, ["users/$u/#"]}))),
  72. ?assertMatch({matched, deny}, match(User, <<"d/e/f">>,
  73. compile({deny, all, subscribe, ["$SYS/#", "#"]}))),
  74. Rule = compile({allow, {'and', [{ipaddr, "127.0.0.1"}, {user, <<"WrongUser">>}]}, publish, <<"Topic">>}),
  75. ?assertMatch(nomatch, match(User, <<"Topic">>, Rule)),
  76. AndRule = compile({allow, {'and', [{ipaddr, "127.0.0.1"}, {user, <<"TestUser">>}]}, publish, <<"Topic">>}),
  77. ?assertMatch({matched, allow}, match(User, <<"Topic">>, AndRule)),
  78. OrRule = compile({allow, {'or', [{ipaddr, "127.0.0.1"}, {user, <<"WrongUser">>}]}, publish, ["Topic"]}),
  79. ?assertMatch({matched, allow}, match(User, <<"Topic">>, OrRule)).
  80. -endif.