Update test cases

This commit is contained in:
Feng Lee 2019-09-19 06:06:25 +08:00
parent 1d429dad8d
commit c067a43990
6 changed files with 120 additions and 105 deletions

View File

@ -344,35 +344,35 @@ t_compile_rule(_) ->
{deny, all} = compile({deny, all}). {deny, all} = compile({deny, all}).
t_match_rule(_) -> t_match_rule(_) ->
Client1 = #{zone => external, ClientInfo1 = #{zone => external,
client_id => <<"testClient">>, client_id => <<"testClient">>,
username => <<"TestUser">>, username => <<"TestUser">>,
peername => {{127,0,0,1}, 2948} peerhost => {127,0,0,1}
}, },
Client2 = #{zone => external, ClientInfo2 = #{zone => external,
client_id => <<"testClient">>, client_id => <<"testClient">>,
username => <<"TestUser">>, username => <<"TestUser">>,
peername => {{192,168,0,10}, 3028} peerhost => {192,168,0,10}
}, },
{matched, allow} = match(Client1, <<"Test/Topic">>, {allow, all}), {matched, allow} = match(ClientInfo1, <<"Test/Topic">>, {allow, all}),
{matched, deny} = match(Client1, <<"Test/Topic">>, {deny, all}), {matched, deny} = match(ClientInfo1, <<"Test/Topic">>, {deny, all}),
{matched, allow} = match(Client1, <<"Test/Topic">>, {matched, allow} = match(ClientInfo1, <<"Test/Topic">>,
compile({allow, {ipaddr, "127.0.0.1"}, subscribe, ["$SYS/#", "#"]})), compile({allow, {ipaddr, "127.0.0.1"}, subscribe, ["$SYS/#", "#"]})),
{matched, allow} = match(Client2, <<"Test/Topic">>, {matched, allow} = match(ClientInfo2, <<"Test/Topic">>,
compile({allow, {ipaddr, "192.168.0.1/24"}, subscribe, ["$SYS/#", "#"]})), compile({allow, {ipaddr, "192.168.0.1/24"}, subscribe, ["$SYS/#", "#"]})),
{matched, allow} = match(Client1, <<"d/e/f/x">>, {matched, allow} = match(ClientInfo1, <<"d/e/f/x">>,
compile({allow, {user, "TestUser"}, subscribe, ["a/b/c", "d/e/f/#"]})), compile({allow, {user, "TestUser"}, subscribe, ["a/b/c", "d/e/f/#"]})),
nomatch = match(Client1, <<"d/e/f/x">>, compile({allow, {user, "admin"}, pubsub, ["d/e/f/#"]})), nomatch = match(ClientInfo1, <<"d/e/f/x">>, compile({allow, {user, "admin"}, pubsub, ["d/e/f/#"]})),
{matched, allow} = match(Client1, <<"testTopics/testClient">>, {matched, allow} = match(ClientInfo1, <<"testTopics/testClient">>,
compile({allow, {client, "testClient"}, publish, ["testTopics/testClient"]})), compile({allow, {client, "testClient"}, publish, ["testTopics/testClient"]})),
{matched, allow} = match(Client1, <<"clients/testClient">>, compile({allow, all, pubsub, ["clients/%c"]})), {matched, allow} = match(ClientInfo1, <<"clients/testClient">>, compile({allow, all, pubsub, ["clients/%c"]})),
{matched, allow} = match(#{username => <<"user2">>}, <<"users/user2/abc/def">>, {matched, allow} = match(#{username => <<"user2">>}, <<"users/user2/abc/def">>,
compile({allow, all, subscribe, ["users/%u/#"]})), compile({allow, all, subscribe, ["users/%u/#"]})),
{matched, deny} = match(Client1, <<"d/e/f">>, compile({deny, all, subscribe, ["$SYS/#", "#"]})), {matched, deny} = match(ClientInfo1, <<"d/e/f">>, compile({deny, all, subscribe, ["$SYS/#", "#"]})),
Rule = compile({allow, {'and', [{ipaddr, "127.0.0.1"}, {user, <<"WrongUser">>}]}, publish, <<"Topic">>}), Rule = compile({allow, {'and', [{ipaddr, "127.0.0.1"}, {user, <<"WrongUser">>}]}, publish, <<"Topic">>}),
nomatch = match(Client1, <<"Topic">>, Rule), nomatch = match(ClientInfo1, <<"Topic">>, Rule),
AndRule = compile({allow, {'and', [{ipaddr, "127.0.0.1"}, {user, <<"TestUser">>}]}, publish, <<"Topic">>}), AndRule = compile({allow, {'and', [{ipaddr, "127.0.0.1"}, {user, <<"TestUser">>}]}, publish, <<"Topic">>}),
{matched, allow} = match(Client1, <<"Topic">>, AndRule), {matched, allow} = match(ClientInfo1, <<"Topic">>, AndRule),
OrRule = compile({allow, {'or', [{ipaddr, "127.0.0.1"}, {user, <<"WrongUser">>}]}, publish, ["Topic"]}), OrRule = compile({allow, {'or', [{ipaddr, "127.0.0.1"}, {user, <<"WrongUser">>}]}, publish, ["Topic"]}),
{matched, allow} = match(Client1, <<"Topic">>, OrRule). {matched, allow} = match(ClientInfo1, <<"Topic">>, OrRule).

View File

@ -51,32 +51,32 @@ t_check(_) ->
ok = emqx_banned:add(#banned{who = {username, <<"BannedUser">>}}), ok = emqx_banned:add(#banned{who = {username, <<"BannedUser">>}}),
ok = emqx_banned:add(#banned{who = {ipaddr, {192,168,0,1}}}), ok = emqx_banned:add(#banned{who = {ipaddr, {192,168,0,1}}}),
?assertEqual(3, emqx_banned:info(size)), ?assertEqual(3, emqx_banned:info(size)),
Client1 = #{client_id => <<"BannedClient">>, ClientInfo1 = #{client_id => <<"BannedClient">>,
username => <<"user">>, username => <<"user">>,
peername => {{127,0,0,1}, 5000} peerhost => {127,0,0,1}
}, },
Client2 = #{client_id => <<"client">>, ClientInfo2 = #{client_id => <<"client">>,
username => <<"BannedUser">>, username => <<"BannedUser">>,
peername => {{127,0,0,1}, 5000} peerhost => {127,0,0,1}
}, },
Client3 = #{client_id => <<"client">>, ClientInfo3 = #{client_id => <<"client">>,
username => <<"user">>, username => <<"user">>,
peername => {{192,168,0,1}, 5000} peerhost => {192,168,0,1}
}, },
Client4 = #{client_id => <<"client">>, ClientInfo4 = #{client_id => <<"client">>,
username => <<"user">>, username => <<"user">>,
peername => {{127,0,0,1}, 5000} peerhost => {127,0,0,1}
}, },
?assert(emqx_banned:check(Client1)), ?assert(emqx_banned:check(ClientInfo1)),
?assert(emqx_banned:check(Client2)), ?assert(emqx_banned:check(ClientInfo2)),
?assert(emqx_banned:check(Client3)), ?assert(emqx_banned:check(ClientInfo3)),
?assertNot(emqx_banned:check(Client4)), ?assertNot(emqx_banned:check(ClientInfo4)),
ok = emqx_banned:delete({client_id, <<"BannedClient">>}), ok = emqx_banned:delete({client_id, <<"BannedClient">>}),
ok = emqx_banned:delete({username, <<"BannedUser">>}), ok = emqx_banned:delete({username, <<"BannedUser">>}),
ok = emqx_banned:delete({ipaddr, {192,168,0,1}}), ok = emqx_banned:delete({ipaddr, {192,168,0,1}}),
?assertNot(emqx_banned:check(Client1)), ?assertNot(emqx_banned:check(ClientInfo1)),
?assertNot(emqx_banned:check(Client2)), ?assertNot(emqx_banned:check(ClientInfo2)),
?assertNot(emqx_banned:check(Client3)), ?assertNot(emqx_banned:check(ClientInfo3)),
?assertNot(emqx_banned:check(Client4)), ?assertNot(emqx_banned:check(ClientInfo4)),
?assertEqual(0, emqx_banned:info(size)). ?assertEqual(0, emqx_banned:info(size)).

View File

@ -176,13 +176,20 @@ t_handle_deliver(_) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
t_handle_connack(_) -> t_handle_connack(_) ->
ConnPkt = #mqtt_packet_connect{
proto_name = <<"MQTT">>,
proto_ver = ?MQTT_PROTO_V4,
clean_start = true,
properties = #{},
client_id = <<"clientid">>
},
with_channel( with_channel(
fun(Channel) -> fun(Channel) ->
{ok, ?CONNACK_PACKET(?RC_SUCCESS, SP, _), _} {ok, ?CONNACK_PACKET(?RC_SUCCESS, SP, _), _}
= handle_out({connack, ?RC_SUCCESS, 0}, Channel), = handle_out({connack, ?RC_SUCCESS, 0, ConnPkt}, Channel),
{stop, {shutdown, not_authorized}, {stop, {shutdown, not_authorized},
?CONNACK_PACKET(?RC_NOT_AUTHORIZED), _} ?CONNACK_PACKET(?RC_NOT_AUTHORIZED), _}
= handle_out({connack, ?RC_NOT_AUTHORIZED}, Channel) = handle_out({connack, ?RC_NOT_AUTHORIZED, ConnPkt}, Channel)
end). end).
t_handle_out_publish(_) -> t_handle_out_publish(_) ->
@ -271,30 +278,31 @@ t_terminate(_) ->
%% Helper functions %% Helper functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
with_channel(Fun) -> with_channel(TestFun) ->
ConnInfo = #{peername => {{127,0,0,1}, 3456}, ConnInfo = #{peername => {{127,0,0,1}, 3456},
sockname => {{127,0,0,1}, 1883}, sockname => {{127,0,0,1}, 1883},
conn_mod => emqx_connection,
proto_name => <<"MQTT">>,
proto_ver => ?MQTT_PROTO_V5,
clean_start => true,
keepalive => 30,
client_id => <<"clientid">>, client_id => <<"clientid">>,
username => <<"username">> username => <<"username">>,
conn_props => #{},
receive_maximum => 100,
expiry_interval => 60
}, },
Options = [{zone, testing}], ClientInfo = #{zone => <<"external">>,
Channel = emqx_channel:init(ConnInfo, Options), peerhost => {127,0,0,1},
ConnPkt = #mqtt_packet_connect{ client_id => <<"clientid">>,
proto_name = <<"MQTT">>, username => <<"username">>,
proto_ver = ?MQTT_PROTO_V5, peercert => undefined,
clean_start = true, is_bridge => false,
keepalive = 30, is_superuser => false,
properties = #{}, mountpoint => undefined
client_id = <<"clientid">>,
username = <<"username">>,
password = <<"passwd">>
}, },
Protocol = emqx_protocol:init(ConnPkt, testing), Channel = emqx_channel:init(ConnInfo, [{zone, testing}]),
Session = emqx_session:init(#{zone => testing}, Session = emqx_session:init(ClientInfo, ConnInfo),
#{max_inflight => 100, Channel1 = emqx_channel:set_field(client, ClientInfo, Channel),
expiry_interval => 0 TestFun(emqx_channel:set_field(session, Session, Channel1)).
}),
Fun(emqx_channel:set_field(protocol, Protocol,
emqx_channel:set_field(
session, Session, Channel))).

View File

@ -38,18 +38,18 @@ end_per_suite(_Config) ->
t_detect_check(_) -> t_detect_check(_) ->
{ok, _Pid} = emqx_flapping:start_link(), {ok, _Pid} = emqx_flapping:start_link(),
Client = #{zone => external, ClientInfo = #{zone => external,
client_id => <<"clientid">>, client_id => <<"clientid">>,
peername => {{127,0,0,1}, 5000} peerhost => {127,0,0,1}
}, },
false = emqx_flapping:detect(Client), false = emqx_flapping:detect(ClientInfo),
false = emqx_flapping:check(Client), false = emqx_flapping:check(ClientInfo),
false = emqx_flapping:detect(Client), false = emqx_flapping:detect(ClientInfo),
false = emqx_flapping:check(Client), false = emqx_flapping:check(ClientInfo),
true = emqx_flapping:detect(Client), true = emqx_flapping:detect(ClientInfo),
timer:sleep(50), timer:sleep(50),
true = emqx_flapping:check(Client), true = emqx_flapping:check(ClientInfo),
timer:sleep(300), timer:sleep(300),
false = emqx_flapping:check(Client), false = emqx_flapping:check(ClientInfo),
ok = emqx_flapping:stop(). ok = emqx_flapping:stop().

View File

@ -19,22 +19,10 @@
-compile(export_all). -compile(export_all).
-compile(nowarn_export_all). -compile(nowarn_export_all).
-include("emqx.hrl").
-include("emqx_mqtt.hrl"). -include("emqx_mqtt.hrl").
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-export([ t_make/1
, t_flag/1
, t_header/1
, t_format/1
, t_expired/1
, t_to_packet/1
, t_to_map/1
]).
-export([ all/0
, suite/0
]).
all() -> emqx_ct:all(?MODULE). all() -> emqx_ct:all(?MODULE).
suite() -> suite() ->
@ -55,7 +43,12 @@ t_make(_) ->
?assertEqual(<<"topic">>, emqx_message:topic(Msg2)), ?assertEqual(<<"topic">>, emqx_message:topic(Msg2)),
?assertEqual(<<"payload">>, emqx_message:payload(Msg2)). ?assertEqual(<<"payload">>, emqx_message:payload(Msg2)).
t_flag(_) -> t_get_set_flags(_) ->
Msg = #message{id = <<"id">>, qos = ?QOS_1, flags = undefined},
Msg1 = emqx_message:set_flags(#{retain => true}, Msg),
?assertEqual(#{retain => true}, emqx_message:get_flags(Msg1)).
t_get_set_flag(_) ->
Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>), Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>),
Msg2 = emqx_message:set_flag(retain, false, Msg), Msg2 = emqx_message:set_flag(retain, false, Msg),
Msg3 = emqx_message:set_flag(dup, Msg2), Msg3 = emqx_message:set_flag(dup, Msg2),
@ -67,19 +60,33 @@ t_flag(_) ->
?assertEqual(undefined, emqx_message:get_flag(retain, Msg5, undefined)), ?assertEqual(undefined, emqx_message:get_flag(retain, Msg5, undefined)),
Msg6 = emqx_message:set_flags(#{dup => true, retain => true}, Msg5), Msg6 = emqx_message:set_flags(#{dup => true, retain => true}, Msg5),
?assert(emqx_message:get_flag(dup, Msg6)), ?assert(emqx_message:get_flag(dup, Msg6)),
?assert(emqx_message:get_flag(retain, Msg6)). ?assert(emqx_message:get_flag(retain, Msg6)),
Msg7 = #message{id = <<"id">>, qos = ?QOS_1, flags = undefined},
Msg8 = emqx_message:set_flag(retain, Msg7),
Msg9 = emqx_message:set_flag(retain, true, Msg7),
?assertEqual(#{retain => true}, emqx_message:get_flags(Msg8)),
?assertEqual(#{retain => true}, emqx_message:get_flags(Msg9)).
t_header(_) -> t_get_set_headers(_) ->
Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>), Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>),
Msg1 = emqx_message:set_headers(#{a => 1, b => 2}, Msg), Msg1 = emqx_message:set_headers(#{a => 1, b => 2}, Msg),
Msg2 = emqx_message:set_header(c, 3, Msg1), Msg2 = emqx_message:set_headers(#{c => 3}, Msg1),
?assertEqual(1, emqx_message:get_header(a, Msg2)), ?assertEqual(#{a => 1, b => 2, c => 3}, emqx_message:get_headers(Msg2)).
t_get_set_header(_) ->
Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>),
Msg1 = emqx_message:set_header(a, 1, Msg),
Msg2 = emqx_message:set_header(b, 2, Msg1),
Msg3 = emqx_message:set_header(c, 3, Msg2),
?assertEqual(1, emqx_message:get_header(a, Msg3)),
?assertEqual(4, emqx_message:get_header(d, Msg2, 4)), ?assertEqual(4, emqx_message:get_header(d, Msg2, 4)),
Msg3 = emqx_message:remove_header(a, Msg2), Msg4 = emqx_message:remove_header(a, Msg3),
?assertEqual(#{b => 2, c => 3}, emqx_message:get_headers(Msg3)). Msg4 = emqx_message:remove_header(a, Msg3),
?assertEqual(#{b => 2, c => 3}, emqx_message:get_headers(Msg4)).
t_format(_) -> t_format(_) ->
io:format("~s", [emqx_message:format(emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>))]). Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>),
io:format("~s", [emqx_message:format(Msg)]).
t_expired(_) -> t_expired(_) ->
Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>), Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>),

View File

@ -273,7 +273,7 @@ max_inflight() -> choose(0, 10).
expiry_interval() -> ?LET(EI, choose(1, 10), EI * 3600). expiry_interval() -> ?LET(EI, choose(1, 10), EI * 3600).
option() -> option() ->
?LET(Option, [{max_inflight, max_inflight()}, ?LET(Option, [{receive_maximum , max_inflight()},
{expiry_interval, expiry_interval()}], {expiry_interval, expiry_interval()}],
maps:from_list(Option)). maps:from_list(Option)).