update acl test cases
This commit is contained in:
parent
ee11627828
commit
737fe19331
|
@ -3,7 +3,8 @@
|
|||
{vsn,"3.0"},
|
||||
{modules,[]},
|
||||
{registered,[emqx_sup]},
|
||||
{applications,[kernel,stdlib,jsx,gproc,gen_rpc,lager,esockd,cowboy]},
|
||||
{applications,[kernel,stdlib,jsx,gproc,gen_rpc,lager,esockd,cowboy,
|
||||
minirest]},
|
||||
{env,[]},
|
||||
{mod,{emqx_app,[]}},
|
||||
{maintainers,["Feng Lee <feng@emqx.io>"]},
|
||||
|
|
|
@ -109,11 +109,12 @@ reload_acl(#state{config = undefined}) ->
|
|||
reload_acl(State) ->
|
||||
case catch load_rules_from_file(State) of
|
||||
{'EXIT', Error} -> {error, Error};
|
||||
true -> io:format("~s~n", ["reload acl_internal successfully"]), ok
|
||||
#state{config=File} ->
|
||||
io:format("reload acl_internal successfully: ~p~n", [File]),
|
||||
ok
|
||||
end.
|
||||
|
||||
%% @doc ACL Module Description
|
||||
-spec(description() -> string()).
|
||||
description() ->
|
||||
"Internal ACL with etc/acl.conf".
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
-compile(export_all).
|
||||
-compile(nowarn_export_all).
|
||||
|
||||
-include_lib("emqttc/include/emqttc_packet.hrl").
|
||||
-include("emqx_mqtt.hrl").
|
||||
|
||||
-define(APP, emqx).
|
||||
|
||||
|
@ -79,7 +79,7 @@ mqtt_connect_with_tcp(_) ->
|
|||
Packet = raw_send_serialise(?CLIENT),
|
||||
gen_tcp:send(Sock, Packet),
|
||||
{ok, Data} = gen_tcp:recv(Sock, 0),
|
||||
{ok, ?CONNACK_PACKET(?CONNACK_ACCEPT), _} = raw_recv_pase(Data),
|
||||
{ok, ?CONNACK_PACKET(0), _} = raw_recv_pase(Data),
|
||||
gen_tcp:close(Sock).
|
||||
|
||||
mqtt_connect_with_ssl_oneway(_) ->
|
||||
|
@ -133,7 +133,7 @@ mqtt_connect_with_ws(_Config) ->
|
|||
Packet = raw_send_serialise(?CLIENT),
|
||||
ok = rfc6455_client:send_binary(WS, Packet),
|
||||
{binary, P} = rfc6455_client:recv(WS),
|
||||
{ok, ?CONNACK_PACKET(?CONNACK_ACCEPT), _} = raw_recv_pase(P),
|
||||
{ok, ?CONNACK_PACKET(0), _} = raw_recv_pase(P),
|
||||
{close, _} = rfc6455_client:close(WS),
|
||||
ok.
|
||||
|
||||
|
|
|
@ -59,11 +59,8 @@ prepare_config() ->
|
|||
{deny, all, subscribe, ["$SYS/#", "#"]},
|
||||
{deny, all}],
|
||||
write_config("access_SUITE_acl.conf", Rules),
|
||||
Config = [{auth, anonymous, []},
|
||||
{acl, internal, [{config, "access_SUITE_acl.conf"},
|
||||
{nomatch, allow}]}],
|
||||
write_config("access_SUITE_emqx.conf", Config),
|
||||
application:set_env(emqx, conf, "access_SUITE_emqx.conf").
|
||||
application:set_env(emqx, acl_file, "access_SUITE_acl.conf"),
|
||||
application:set_env(emqx, acl_cache, false).
|
||||
|
||||
write_config(Filename, Terms) ->
|
||||
file:write_file(Filename, [io_lib:format("~tp.~n", [Term]) || Term <- Terms]).
|
||||
|
@ -119,14 +116,14 @@ unregister_mod(_) ->
|
|||
[] = ?AC:lookup_mods(auth).
|
||||
|
||||
check_acl(_) ->
|
||||
User1 = #client{client_id = <<"client1">>, username = <<"testuser">>},
|
||||
User2 = #client{client_id = <<"client2">>, username = <<"xyz">>},
|
||||
User1 = #client{id = <<"client1">>, username = <<"testuser">>},
|
||||
User2 = #client{id = <<"client2">>, username = <<"xyz">>},
|
||||
allow = ?AC:check_acl(User1, subscribe, <<"users/testuser/1">>),
|
||||
allow = ?AC:check_acl(User1, subscribe, <<"clients/client1">>),
|
||||
allow = ?AC:check_acl(User1, subscribe, <<"clients/client1/x/y">>),
|
||||
deny = ?AC:check_acl(User1, subscribe, <<"clients/client1/x/y">>),
|
||||
allow = ?AC:check_acl(User1, publish, <<"users/testuser/1">>),
|
||||
allow = ?AC:check_acl(User1, subscribe, <<"a/b/c">>),
|
||||
allow = ?AC:check_acl(User2, subscribe, <<"a/b/c">>).
|
||||
deny = ?AC:check_acl(User2, subscribe, <<"a/b/c">>).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% emqx_access_rule
|
||||
|
@ -159,9 +156,9 @@ compile_rule(_) ->
|
|||
{deny, all} = compile({deny, all}).
|
||||
|
||||
match_rule(_) ->
|
||||
User = #client{peername = {{127,0,0,1}, 2948}, client_id = <<"testClient">>, username = <<"TestUser">>},
|
||||
User2 = #client{peername = {{192,168,0,10}, 3028}, client_id = <<"testClient">>, username = <<"TestUser">>},
|
||||
|
||||
User = #client{peername = {{127,0,0,1}, 2948}, id = <<"testClient">>, username = <<"TestUser">>},
|
||||
User2 = #client{peername = {{192,168,0,10}, 3028}, id = <<"testClient">>, username = <<"TestUser">>},
|
||||
|
||||
{matched, allow} = match(User, <<"Test/Topic">>, {allow, all}),
|
||||
{matched, deny} = match(User, <<"Test/Topic">>, {deny, all}),
|
||||
{matched, allow} = match(User, <<"Test/Topic">>, compile({allow, {ipaddr, "127.0.0.1"}, subscribe, ["$SYS/#", "#"]})),
|
||||
|
@ -179,4 +176,3 @@ match_rule(_) ->
|
|||
{matched, allow} = match(User, <<"Topic">>, AndRule),
|
||||
OrRule = compile({allow, {'or', [{ipaddr, "127.0.0.1"}, {user, <<"WrongUser">>}]}, publish, ["Topic"]}),
|
||||
{matched, allow} = match(User, <<"Topic">>, OrRule).
|
||||
|
||||
|
|
|
@ -149,7 +149,7 @@ start_session(_) ->
|
|||
{ok, ClientPid} = emqx_mock_client:start_link(<<"clientId">>),
|
||||
{ok, SessPid} = emqx_mock_client:start_session(ClientPid),
|
||||
Message = emqx_message:make(<<"clientId">>, 2, <<"topic">>, <<"hello">>),
|
||||
Message1 = Message#mqtt_message{packet_id = 1},
|
||||
Message1 = Message#message{id = 1},
|
||||
emqx_session:publish(SessPid, Message1),
|
||||
emqx_session:pubrel(SessPid, 1),
|
||||
emqx_session:subscribe(SessPid, [{<<"topic/session">>, [{qos, 2}]}]),
|
||||
|
|
|
@ -35,22 +35,22 @@ t_in(_) ->
|
|||
{store_qos0, true}],
|
||||
Q = ?Q:new(<<"testQ">>, Opts, alarm_fun()),
|
||||
?assert(?Q:is_empty(Q)),
|
||||
Q1 = ?Q:in(#mqtt_message{}, Q),
|
||||
Q1 = ?Q:in(#message{}, Q),
|
||||
?assertEqual(1, ?Q:len(Q1)),
|
||||
Q2 = ?Q:in(#mqtt_message{qos = 1}, Q1),
|
||||
Q2 = ?Q:in(#message{qos = 1}, Q1),
|
||||
?assertEqual(2, ?Q:len(Q2)),
|
||||
Q3 = ?Q:in(#mqtt_message{qos = 2}, Q2),
|
||||
Q4 = ?Q:in(#mqtt_message{}, Q3),
|
||||
Q5 = ?Q:in(#mqtt_message{}, Q4),
|
||||
Q3 = ?Q:in(#message{qos = 2}, Q2),
|
||||
Q4 = ?Q:in(#message{}, Q3),
|
||||
Q5 = ?Q:in(#message{}, Q4),
|
||||
?assertEqual(5, ?Q:len(Q5)).
|
||||
|
||||
t_in_qos0(_) ->
|
||||
Opts = [{max_length, 5},
|
||||
{store_qos0, false}],
|
||||
Q = ?Q:new(<<"testQ">>, Opts, alarm_fun()),
|
||||
Q1 = ?Q:in(#mqtt_message{}, Q),
|
||||
Q1 = ?Q:in(#message{}, Q),
|
||||
?assert(?Q:is_empty(Q1)),
|
||||
Q2 = ?Q:in(#mqtt_message{qos = 0}, Q1),
|
||||
Q2 = ?Q:in(#message{qos = 0}, Q1),
|
||||
?assert(?Q:is_empty(Q2)).
|
||||
|
||||
t_out(_) ->
|
||||
|
@ -58,10 +58,10 @@ t_out(_) ->
|
|||
{store_qos0, true}],
|
||||
Q = ?Q:new(<<"testQ">>, Opts, alarm_fun()),
|
||||
{empty, Q} = ?Q:out(Q),
|
||||
Q1 = ?Q:in(#mqtt_message{}, Q),
|
||||
Q1 = ?Q:in(#message{}, Q),
|
||||
{Value, Q2} = ?Q:out(Q1),
|
||||
?assertEqual(0, ?Q:len(Q2)),
|
||||
?assertEqual({value, #mqtt_message{}}, Value).
|
||||
?assertEqual({value, #message{}}, Value).
|
||||
|
||||
t_simple_mqueue(_) ->
|
||||
Opts = [{type, simple},
|
||||
|
@ -74,13 +74,13 @@ t_simple_mqueue(_) ->
|
|||
?assertEqual(3, ?Q:max_len(Q)),
|
||||
?assertEqual(<<"simple_queue">>, ?Q:name(Q)),
|
||||
?assert(?Q:is_empty(Q)),
|
||||
Q1 = ?Q:in(#mqtt_message{qos = 1, payload = <<"1">>}, Q),
|
||||
Q2 = ?Q:in(#mqtt_message{qos = 1, payload = <<"2">>}, Q1),
|
||||
Q3 = ?Q:in(#mqtt_message{qos = 1, payload = <<"3">>}, Q2),
|
||||
Q4 = ?Q:in(#mqtt_message{qos = 1, payload = <<"4">>}, Q3),
|
||||
Q1 = ?Q:in(#message{qos = 1, payload = <<"1">>}, Q),
|
||||
Q2 = ?Q:in(#message{qos = 1, payload = <<"2">>}, Q1),
|
||||
Q3 = ?Q:in(#message{qos = 1, payload = <<"3">>}, Q2),
|
||||
Q4 = ?Q:in(#message{qos = 1, payload = <<"4">>}, Q3),
|
||||
?assertEqual(3, ?Q:len(Q4)),
|
||||
{{value, Msg}, Q5} = ?Q:out(Q4),
|
||||
?assertEqual(<<"2">>, Msg#mqtt_message.payload),
|
||||
?assertEqual(<<"2">>, Msg#message.payload),
|
||||
?assertEqual([{len, 2}, {max_len, 3}, {dropped, 1}], ?Q:stats(Q5)).
|
||||
|
||||
t_infinity_simple_mqueue(_) ->
|
||||
|
@ -93,12 +93,12 @@ t_infinity_simple_mqueue(_) ->
|
|||
?assert(?Q:is_empty(Q)),
|
||||
?assertEqual(0, ?Q:max_len(Q)),
|
||||
Qx = lists:foldl(fun(I, AccQ) ->
|
||||
?Q:in(#mqtt_message{qos = 1, payload = iolist_to_binary([I])}, AccQ)
|
||||
?Q:in(#message{qos = 1, payload = iolist_to_binary([I])}, AccQ)
|
||||
end, Q, lists:seq(1, 255)),
|
||||
?assertEqual(255, ?Q:len(Qx)),
|
||||
?assertEqual([{len, 255}, {max_len, 0}, {dropped, 0}], ?Q:stats(Qx)),
|
||||
{{value, V}, _Qy} = ?Q:out(Qx),
|
||||
?assertEqual(<<1>>, V#mqtt_message.payload).
|
||||
?assertEqual(<<1>>, V#message.payload).
|
||||
|
||||
t_priority_mqueue(_) ->
|
||||
Opts = [{type, priority},
|
||||
|
@ -113,18 +113,18 @@ t_priority_mqueue(_) ->
|
|||
?assertEqual(<<"priority_queue">>, ?Q:name(Q)),
|
||||
|
||||
?assert(?Q:is_empty(Q)),
|
||||
Q1 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q),
|
||||
Q2 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t">>}, Q1),
|
||||
Q3 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t2">>}, Q2),
|
||||
Q1 = ?Q:in(#message{qos = 1, topic = <<"t1">>}, Q),
|
||||
Q2 = ?Q:in(#message{qos = 1, topic = <<"t">>}, Q1),
|
||||
Q3 = ?Q:in(#message{qos = 1, topic = <<"t2">>}, Q2),
|
||||
?assertEqual(3, ?Q:len(Q3)),
|
||||
Q4 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q3),
|
||||
Q4 = ?Q:in(#message{qos = 1, topic = <<"t1">>}, Q3),
|
||||
?assertEqual(4, ?Q:len(Q4)),
|
||||
Q5 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q4),
|
||||
Q5 = ?Q:in(#message{qos = 1, topic = <<"t1">>}, Q4),
|
||||
?assertEqual(5, ?Q:len(Q5)),
|
||||
Q6 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q5),
|
||||
Q6 = ?Q:in(#message{qos = 1, topic = <<"t1">>}, Q5),
|
||||
?assertEqual(5, ?Q:len(Q6)),
|
||||
{{value, Msg}, _Q7} = ?Q:out(Q6),
|
||||
?assertEqual(<<"t">>, Msg#mqtt_message.topic).
|
||||
?assertEqual(<<"t">>, Msg#message.topic).
|
||||
|
||||
t_infinity_priority_mqueue(_) ->
|
||||
Opts = [{type, priority},
|
||||
|
@ -135,8 +135,8 @@ t_infinity_priority_mqueue(_) ->
|
|||
?assertEqual(0, ?Q:max_len(Q)),
|
||||
Qx = lists:foldl(fun(I, AccQ) ->
|
||||
AccQ1 =
|
||||
?Q:in(#mqtt_message{topic = <<"t1">>, qos = 1, payload = iolist_to_binary([I])}, AccQ),
|
||||
?Q:in(#mqtt_message{topic = <<"t">>, qos = 1, payload = iolist_to_binary([I])}, AccQ1)
|
||||
?Q:in(#message{topic = <<"t1">>, qos = 1, payload = iolist_to_binary([I])}, AccQ),
|
||||
?Q:in(#message{topic = <<"t">>, qos = 1, payload = iolist_to_binary([I])}, AccQ1)
|
||||
end, Q, lists:seq(1, 255)),
|
||||
?assertEqual(510, ?Q:len(Qx)),
|
||||
?assertEqual([{len, 510}, {max_len, 0}, {dropped, 0}], ?Q:stats(Qx)).
|
||||
|
@ -149,10 +149,10 @@ t_priority_mqueue2(_) ->
|
|||
{store_qos0, false}],
|
||||
Q = ?Q:new("priority_queue2_test", Opts, alarm_fun()),
|
||||
?assertEqual(2, ?Q:max_len(Q)),
|
||||
Q1 = ?Q:in(#mqtt_message{topic = <<"x">>, qos = 1, payload = <<1>>}, Q),
|
||||
Q2 = ?Q:in(#mqtt_message{topic = <<"x">>, qos = 1, payload = <<2>>}, Q1),
|
||||
Q3 = ?Q:in(#mqtt_message{topic = <<"y">>, qos = 1, payload = <<3>>}, Q2),
|
||||
Q4 = ?Q:in(#mqtt_message{topic = <<"y">>, qos = 1, payload = <<4>>}, Q3),
|
||||
Q1 = ?Q:in(#message{topic = <<"x">>, qos = 1, payload = <<1>>}, Q),
|
||||
Q2 = ?Q:in(#message{topic = <<"x">>, qos = 1, payload = <<2>>}, Q1),
|
||||
Q3 = ?Q:in(#message{topic = <<"y">>, qos = 1, payload = <<3>>}, Q2),
|
||||
Q4 = ?Q:in(#message{topic = <<"y">>, qos = 1, payload = <<4>>}, Q3),
|
||||
?assertEqual(4, ?Q:len(Q4)),
|
||||
{{value, _Val}, Q5} = ?Q:out(Q4),
|
||||
?assertEqual(3, ?Q:len(Q5)).
|
||||
|
|
Loading…
Reference in New Issue