Feng Lee 6 лет назад
Родитель
Сommit
c067a43990

+ 22 - 22
test/emqx_access_SUITE.erl

@@ -344,35 +344,35 @@ t_compile_rule(_) ->
     {deny, all} = compile({deny, all}).
 
 t_match_rule(_) ->
-    Client1 = #{zone => external,
-                client_id => <<"testClient">>,
-                username => <<"TestUser">>,
-                peername => {{127,0,0,1}, 2948}
-               },
-    Client2 = #{zone => external,
-                client_id => <<"testClient">>,
-                username => <<"TestUser">>,
-                peername => {{192,168,0,10}, 3028}
-               },
-    {matched, allow} = match(Client1, <<"Test/Topic">>, {allow, all}),
-    {matched, deny} = match(Client1, <<"Test/Topic">>, {deny, all}),
-    {matched, allow} = match(Client1, <<"Test/Topic">>,
+    ClientInfo1 = #{zone => external,
+                    client_id => <<"testClient">>,
+                    username => <<"TestUser">>,
+                    peerhost => {127,0,0,1}
+                   },
+    ClientInfo2 = #{zone => external,
+                    client_id => <<"testClient">>,
+                    username => <<"TestUser">>,
+                    peerhost => {192,168,0,10}
+                   },
+    {matched, allow} = match(ClientInfo1, <<"Test/Topic">>, {allow, all}),
+    {matched, deny} = match(ClientInfo1, <<"Test/Topic">>, {deny, all}),
+    {matched, allow} = match(ClientInfo1, <<"Test/Topic">>,
                              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/#", "#"]})),
-    {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/#"]})),
-    nomatch = match(Client1, <<"d/e/f/x">>, compile({allow, {user, "admin"}, pubsub, ["d/e/f/#"]})),
-    {matched, allow} = match(Client1, <<"testTopics/testClient">>,
+    nomatch = match(ClientInfo1, <<"d/e/f/x">>, compile({allow, {user, "admin"}, pubsub, ["d/e/f/#"]})),
+    {matched, allow} = match(ClientInfo1, <<"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">>,
                              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">>}),
-    nomatch = match(Client1, <<"Topic">>, Rule),
+    nomatch = match(ClientInfo1, <<"Topic">>, Rule),
     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"]}),
-    {matched, allow} = match(Client1, <<"Topic">>, OrRule).
+    {matched, allow} = match(ClientInfo1, <<"Topic">>, OrRule).
 

+ 24 - 24
test/emqx_banned_SUITE.erl

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

+ 34 - 26
test/emqx_channel_SUITE.erl

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

+ 11 - 11
test/emqx_flapping_SUITE.erl

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

+ 28 - 21
test/emqx_message_SUITE.erl

@@ -19,22 +19,10 @@
 -compile(export_all).
 -compile(nowarn_export_all).
 
+-include("emqx.hrl").
 -include("emqx_mqtt.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).
 
 suite() ->
@@ -55,7 +43,12 @@ t_make(_) ->
     ?assertEqual(<<"topic">>, emqx_message:topic(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">>),
     Msg2 = emqx_message:set_flag(retain, false, Msg),
     Msg3 = emqx_message:set_flag(dup, Msg2),
@@ -67,19 +60,33 @@ t_flag(_) ->
     ?assertEqual(undefined, emqx_message:get_flag(retain, Msg5, undefined)),
     Msg6 = emqx_message:set_flags(#{dup => true, retain => true}, Msg5),
     ?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">>),
     Msg1 = emqx_message:set_headers(#{a => 1, b => 2}, Msg),
-    Msg2 = emqx_message:set_header(c, 3, Msg1),
-    ?assertEqual(1, emqx_message:get_header(a, Msg2)),
+    Msg2 = emqx_message:set_headers(#{c => 3}, Msg1),
+    ?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)),
-    Msg3 = emqx_message:remove_header(a, Msg2),
-    ?assertEqual(#{b => 2, c => 3}, emqx_message:get_headers(Msg3)).
+    Msg4 = emqx_message:remove_header(a, Msg3),
+    Msg4 = emqx_message:remove_header(a, Msg3),
+    ?assertEqual(#{b => 2, c => 3}, emqx_message:get_headers(Msg4)).
 
 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(_) ->
     Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>),

+ 1 - 1
test/emqx_session_SUITE.erl

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