fix tests
This commit is contained in:
parent
47f99c5cca
commit
2ce7683da0
|
@ -0,0 +1,42 @@
|
|||
%%%-----------------------------------------------------------------------------
|
||||
%%% @Copyright (C) 2012-2015, Feng Lee <feng@emqtt.io>
|
||||
%%%
|
||||
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
%%% of this software and associated documentation files (the "Software"), to deal
|
||||
%%% in the Software without restriction, including without limitation the rights
|
||||
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
%%% copies of the Software, and to permit persons to whom the Software is
|
||||
%%% furnished to do so, subject to the following conditions:
|
||||
%%%
|
||||
%%% The above copyright notice and this permission notice shall be included in all
|
||||
%%% copies or substantial portions of the Software.
|
||||
%%%
|
||||
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
%%% SOFTWARE.
|
||||
%%%-----------------------------------------------------------------------------
|
||||
%%% @doc
|
||||
%%% Test ACL Module.
|
||||
%%%
|
||||
%%% @end
|
||||
%%%-----------------------------------------------------------------------------
|
||||
-module(emqttd_acl_test_mod).
|
||||
|
||||
%% ACL callbacks
|
||||
-export([init/1, check_acl/2, reload_acl/1, description/0]).
|
||||
|
||||
init(AclOpts) ->
|
||||
{ok, AclOpts}.
|
||||
|
||||
check_acl({_User, _PubSub, _Topic}, _State) ->
|
||||
allow.
|
||||
|
||||
reload_acl(_State) ->
|
||||
ok.
|
||||
|
||||
description() ->
|
||||
"Test ACL Mod".
|
|
@ -35,7 +35,7 @@
|
|||
all_modules_test() ->
|
||||
with_acl(
|
||||
fun() ->
|
||||
?assertEqual([emqttd_acl_internal], emqttd_acl:all_modules())
|
||||
?assertMatch([{emqttd_acl_internal, _State}], emqttd_acl:all_modules())
|
||||
end).
|
||||
|
||||
reload_test() ->
|
||||
|
@ -47,18 +47,20 @@ reload_test() ->
|
|||
register_mod_test() ->
|
||||
with_acl(
|
||||
fun() ->
|
||||
emqttd_acl:register_mod(acl_mysql),
|
||||
?assertEqual([acl_mysql, emqttd_acl_internal], emqttd_acl:all_modules())
|
||||
emqttd_acl:register_mod(emqttd_acl_test_mod, []),
|
||||
?assertMatch([{emqttd_acl_test_mod, _}, {emqttd_acl_internal, _}],
|
||||
emqttd_acl:all_modules())
|
||||
end).
|
||||
|
||||
unregister_mod_test() ->
|
||||
with_acl(
|
||||
fun() ->
|
||||
emqttd_acl:register_mod(acl_mysql),
|
||||
?assertEqual([acl_mysql, emqttd_acl_internal], emqttd_acl:all_modules()),
|
||||
emqttd_acl:unregister_mod(acl_mysql),
|
||||
emqttd_acl:register_mod(emqttd_acl_test_mod, []),
|
||||
?assertMatch([{emqttd_acl_test_mod, _}, {emqttd_acl_internal, _}],
|
||||
emqttd_acl:all_modules()),
|
||||
emqttd_acl:unregister_mod(emqttd_acl_test_mod),
|
||||
timer:sleep(5),
|
||||
?assertEqual([emqttd_acl_internal], emqttd_acl:all_modules())
|
||||
?assertMatch([{emqttd_acl_internal, _}], emqttd_acl:all_modules())
|
||||
end).
|
||||
|
||||
check_test() ->
|
||||
|
@ -66,21 +68,20 @@ check_test() ->
|
|||
fun() ->
|
||||
User1 = #mqtt_user{clientid = <<"client1">>, username = <<"testuser">>},
|
||||
User2 = #mqtt_user{clientid = <<"client2">>, username = <<"xyz">>},
|
||||
?assertEqual({ok, allow}, emqttd_acl:check(User1, subscribe, <<"users/testuser/1">>)),
|
||||
?assertEqual({ok, allow}, emqttd_acl:check(User1, subscribe, <<"clients/client1">>)),
|
||||
?assertEqual({ok, deny}, emqttd_acl:check(User1, subscribe, <<"clients/client1/x/y">>)),
|
||||
?assertEqual({ok, allow}, emqttd_acl:check(User1, publish, <<"users/testuser/1">>)),
|
||||
?assertEqual({ok, allow}, emqttd_acl:check(User1, subscribe, <<"a/b/c">>)),
|
||||
?assertEqual({ok, deny}, emqttd_acl:check(User2, subscribe, <<"a/b/c">>))
|
||||
?assertEqual(allow, emqttd_acl:check({User1, subscribe, <<"users/testuser/1">>})),
|
||||
?assertEqual(allow, emqttd_acl:check({User1, subscribe, <<"clients/client1">>})),
|
||||
?assertEqual(deny, emqttd_acl:check({User1, subscribe, <<"clients/client1/x/y">>})),
|
||||
?assertEqual(allow, emqttd_acl:check({User1, publish, <<"users/testuser/1">>})),
|
||||
?assertEqual(allow, emqttd_acl:check({User1, subscribe, <<"a/b/c">>})),
|
||||
?assertEqual(deny, emqttd_acl:check({User2, subscribe, <<"a/b/c">>}))
|
||||
end).
|
||||
|
||||
with_acl(Fun) ->
|
||||
process_flag(trap_exit, true),
|
||||
AclOpts = [{file, "../test/test_acl.config"}],
|
||||
AclOpts = [{internal, [{file, "../test/test_acl.config"},
|
||||
{nomatch, allow}]}],
|
||||
{ok, _AclSrv} = emqttd_acl:start_link(AclOpts),
|
||||
{ok, _InternalAcl} = emqttd_acl_internal:start_link(AclOpts),
|
||||
Fun(),
|
||||
emqttd_acl_internal:stop(),
|
||||
emqttd_acl:stop().
|
||||
|
||||
-endif.
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
-include("emqttd_topic.hrl").
|
||||
|
||||
-import(emqttd_topic, [validate/1, type/1, match/2, triples/1, words/1]).
|
||||
-import(emqttd_topic, [validate/1, wildcard/1, match/2, triples/1, words/1]).
|
||||
|
||||
-ifdef(TEST).
|
||||
|
||||
|
@ -101,9 +101,9 @@ triples_perf_test() ->
|
|||
ok.
|
||||
|
||||
type_test() ->
|
||||
?assertEqual(direct, type(#topic{name = <<"/a/b/cdkd">>})),
|
||||
?assertEqual(wildcard, type(#topic{name = <<"/a/+/d">>})),
|
||||
?assertEqual(wildcard, type(#topic{name = <<"/a/b/#">>})).
|
||||
?assertEqual(false, wildcard(#topic{name = <<"/a/b/cdkd">>})),
|
||||
?assertEqual(true, wildcard(#topic{name = <<"/a/+/d">>})),
|
||||
?assertEqual(true, wildcard(#topic{name = <<"/a/b/#">>})).
|
||||
|
||||
words_test() ->
|
||||
?assertMatch(['', <<"abkc">>, <<"19383">>, '+', <<"akakdkkdkak">>, '#'], words(<<"/abkc/19383/+/akakdkkdkak/#">>)),
|
||||
|
|
Loading…
Reference in New Issue