Procházet zdrojové kódy

Add more test cases for emqx_packet

zhanghongtong před 6 roky
rodič
revize
73bfaa038b
1 změnil soubory, kde provedl 36 přidání a 9 odebrání
  1. 36 9
      test/emqx_packet_SUITE.erl

+ 36 - 9
test/emqx_packet_SUITE.erl

@@ -82,17 +82,20 @@ t_check_publish(_) ->
     Props = #{'Response-Topic' => <<"responsetopic">>, 'Topic-Alias' => 1},
     Props = #{'Response-Topic' => <<"responsetopic">>, 'Topic-Alias' => 1},
     ok = emqx_packet:check(?PUBLISH_PACKET(?QOS_1, <<"topic">>, 1, Props, <<"payload">>)),
     ok = emqx_packet:check(?PUBLISH_PACKET(?QOS_1, <<"topic">>, 1, Props, <<"payload">>)),
     ok = emqx_packet:check(#mqtt_packet_publish{packet_id = 1, topic_name = <<"t">>}),
     ok = emqx_packet:check(#mqtt_packet_publish{packet_id = 1, topic_name = <<"t">>}),
+    ok = emqx_packet:check(#mqtt_packet_publish{topic_name = <<>>, properties = #{'Topic-Alias'=> 0}}),
     {error, ?RC_PROTOCOL_ERROR} = emqx_packet:check(?PUBLISH_PACKET(?QOS_1, <<>>, 1, #{}, <<"payload">>)),
     {error, ?RC_PROTOCOL_ERROR} = emqx_packet:check(?PUBLISH_PACKET(?QOS_1, <<>>, 1, #{}, <<"payload">>)),
     {error, ?RC_TOPIC_NAME_INVALID} = emqx_packet:check(?PUBLISH_PACKET(?QOS_1, <<"+/+">>, 1, #{}, <<"payload">>)),
     {error, ?RC_TOPIC_NAME_INVALID} = emqx_packet:check(?PUBLISH_PACKET(?QOS_1, <<"+/+">>, 1, #{}, <<"payload">>)),
     {error, ?RC_TOPIC_ALIAS_INVALID} = emqx_packet:check(?PUBLISH_PACKET(1, <<"topic">>, 1, #{'Topic-Alias' => 0}, <<"payload">>)),
     {error, ?RC_TOPIC_ALIAS_INVALID} = emqx_packet:check(?PUBLISH_PACKET(1, <<"topic">>, 1, #{'Topic-Alias' => 0}, <<"payload">>)),
     %% TODO::
     %% TODO::
     %% {error, ?RC_PROTOCOL_ERROR} = emqx_packet:check(?PUBLISH_PACKET(1, <<"topic">>, 1, #{'Subscription-Identifier' => 10}, <<"payload">>)),
     %% {error, ?RC_PROTOCOL_ERROR} = emqx_packet:check(?PUBLISH_PACKET(1, <<"topic">>, 1, #{'Subscription-Identifier' => 10}, <<"payload">>)),
     ok = emqx_packet:check(?PUBLISH_PACKET(1, <<"topic">>, 1, #{'Subscription-Identifier' => 10}, <<"payload">>)),
     ok = emqx_packet:check(?PUBLISH_PACKET(1, <<"topic">>, 1, #{'Subscription-Identifier' => 10}, <<"payload">>)),
+    {error, ?RC_PROTOCOL_ERROR} = emqx_packet:check(?PUBLISH_PACKET(1, <<"topic">>, 1, #{'Subscription-Identifier' => 0}, <<"payload">>)),
     {error, ?RC_PROTOCOL_ERROR} = emqx_packet:check(?PUBLISH_PACKET(1, <<"topic">>, 1, #{'Response-Topic' => <<"+/+">>}, <<"payload">>)).
     {error, ?RC_PROTOCOL_ERROR} = emqx_packet:check(?PUBLISH_PACKET(1, <<"topic">>, 1, #{'Response-Topic' => <<"+/+">>}, <<"payload">>)).
 
 
 t_check_subscribe(_) ->
 t_check_subscribe(_) ->
     ok = emqx_packet:check(?SUBSCRIBE_PACKET(1, #{'Subscription-Identifier' => 1},
     ok = emqx_packet:check(?SUBSCRIBE_PACKET(1, #{'Subscription-Identifier' => 1},
                                              [{<<"topic">>, #{qos => ?QOS_0}}])),
                                              [{<<"topic">>, #{qos => ?QOS_0}}])),
+    {error, ?RC_TOPIC_FILTER_INVALID} = emqx_packet:check(#mqtt_packet_subscribe{topic_filters = []}),
     {error, ?RC_SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED} =
     {error, ?RC_SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED} =
     emqx_packet:check(?SUBSCRIBE_PACKET(1, #{'Subscription-Identifier' => -1},
     emqx_packet:check(?SUBSCRIBE_PACKET(1, #{'Subscription-Identifier' => -1},
                                         [{<<"topic">>, #{qos => ?QOS_0, rp => 0}}])).
                                         [{<<"topic">>, #{qos => ?QOS_0, rp => 0}}])).
@@ -104,7 +107,11 @@ t_check_unsubscribe(_) ->
 t_check_connect(_) ->
 t_check_connect(_) ->
     Opts = #{max_clientid_len => 5, mqtt_retain_available => false},
     Opts = #{max_clientid_len => 5, mqtt_retain_available => false},
     ok = emqx_packet:check(#mqtt_packet_connect{}, Opts),
     ok = emqx_packet:check(#mqtt_packet_connect{}, Opts),
-    ok = emqx_packet:check(?CONNECT_PACKET(#mqtt_packet_connect{properties = #{'Receive-Maximum' => 1}}), Opts),
+    ok = emqx_packet:check(?CONNECT_PACKET(#mqtt_packet_connect{clientid = <<1>>,
+                                                                properties = #{'Receive-Maximum' => 1},
+                                                                will_flag  = true,
+                                                                will_topic = <<"will_topic">>}
+                                                            ), Opts),
     ConnPkt1 = #mqtt_packet_connect{proto_name = <<"MQIsdp">>,
     ConnPkt1 = #mqtt_packet_connect{proto_name = <<"MQIsdp">>,
                                     proto_ver  = ?MQTT_PROTO_V5
                                     proto_ver  = ?MQTT_PROTO_V5
                                    },
                                    },
@@ -137,7 +144,9 @@ t_check_connect(_) ->
                                                        properties = #{'Request-Problem-Information' => 2}}), Opts),
                                                        properties = #{'Request-Problem-Information' => 2}}), Opts),
     {error, ?RC_PROTOCOL_ERROR} = emqx_packet:check(
     {error, ?RC_PROTOCOL_ERROR} = emqx_packet:check(
                                     ?CONNECT_PACKET(#mqtt_packet_connect{
                                     ?CONNECT_PACKET(#mqtt_packet_connect{
-                                                       properties = #{'Receive-Maximum' => 0}}), Opts).
+                                                       properties = #{'Receive-Maximum' => 0}}), Opts),
+    ConnPkt7 = #mqtt_packet_connect{clientid   = <<>>, clean_start = false},
+    {error, ?RC_CLIENT_IDENTIFIER_NOT_VALID} = emqx_packet:check(ConnPkt7, Opts).                                                
 
 
 t_from_to_message(_) ->
 t_from_to_message(_) ->
     ExpectedMsg = emqx_message:make(<<"clientid">>, ?QOS_0, <<"topic">>, <<"payload">>),
     ExpectedMsg = emqx_message:make(<<"clientid">>, ?QOS_0, <<"topic">>, <<"payload">>),
@@ -160,6 +169,7 @@ t_from_to_message(_) ->
                                                  }).
                                                  }).
 
 
 t_will_msg(_) ->
 t_will_msg(_) ->
+    ?assertEqual(undefined, emqx_packet:will_msg(#mqtt_packet_connect{will_flag = false})),
     Pkt = #mqtt_packet_connect{will_flag = true,
     Pkt = #mqtt_packet_connect{will_flag = true,
                                clientid = <<"clientid">>,
                                clientid = <<"clientid">>,
                                username = "test",
                                username = "test",
@@ -171,14 +181,30 @@ t_will_msg(_) ->
                               },
                               },
     Msg = emqx_packet:will_msg(Pkt),
     Msg = emqx_packet:will_msg(Pkt),
     ?assertEqual(<<"clientid">>, Msg#message.from),
     ?assertEqual(<<"clientid">>, Msg#message.from),
-    ?assertEqual(<<"topic">>, Msg#message.topic).
+    ?assertEqual(<<"topic">>, Msg#message.topic),
+    Pkt2 = #mqtt_packet_connect{will_flag = true,
+                               clientid = <<"clientid">>,
+                               username = "test",
+                               will_retain = true,
+                               will_qos = ?QOS_2,
+                               will_topic = <<"topic">>,
+                               will_props = undefined,
+                               will_payload = <<"payload">>
+                              },
+    Msg2 = emqx_packet:will_msg(Pkt2),
+    ?assertEqual(<<"clientid">>, Msg2#message.from),
+    ?assertEqual(<<"topic">>, Msg2#message.topic).
     
     
-t_to_message(_) ->
-    error('TODO').
-
-
 t_format(_) ->
 t_format(_) ->
-    io:format("~s", [emqx_packet:format(?CONNECT_PACKET(#mqtt_packet_connect{}))]),
+    io:format("~s", [emqx_packet:format(#mqtt_packet{header = #mqtt_packet_header{type = ?CONNACK, retain = true, dup = 0}, variable = undefined})]),
+    io:format("~s", [emqx_packet:format(#mqtt_packet{header = #mqtt_packet_header{type = ?CONNACK}, variable = 1, payload = <<"payload">>})]),
+    io:format("~s", [emqx_packet:format(?CONNECT_PACKET(#mqtt_packet_connect{will_flag = true,
+                                                                             will_retain = true,
+                                                                             will_qos = ?QOS_2,
+                                                                             will_topic = <<"topic">>,
+                                                                             will_props = undefined,
+                                                                             will_payload = <<"payload">>}))]),
+    io:format("~s", [emqx_packet:format(?CONNECT_PACKET(#mqtt_packet_connect{password = password}))]),
     io:format("~s", [emqx_packet:format(?CONNACK_PACKET(?CONNACK_SERVER))]),
     io:format("~s", [emqx_packet:format(?CONNACK_PACKET(?CONNACK_SERVER))]),
     io:format("~s", [emqx_packet:format(?PUBLISH_PACKET(?QOS_1, 1))]),
     io:format("~s", [emqx_packet:format(?PUBLISH_PACKET(?QOS_1, 1))]),
     io:format("~s", [emqx_packet:format(?PUBLISH_PACKET(?QOS_2, <<"topic">>, 10, <<"payload">>))]),
     io:format("~s", [emqx_packet:format(?PUBLISH_PACKET(?QOS_2, <<"topic">>, 10, <<"payload">>))]),
@@ -187,5 +213,6 @@ t_format(_) ->
     io:format("~s", [emqx_packet:format(?SUBSCRIBE_PACKET(15, [{<<"topic">>, ?QOS_0}, {<<"topic1">>, ?QOS_1}]))]),
     io:format("~s", [emqx_packet:format(?SUBSCRIBE_PACKET(15, [{<<"topic">>, ?QOS_0}, {<<"topic1">>, ?QOS_1}]))]),
     io:format("~s", [emqx_packet:format(?SUBACK_PACKET(40, [?QOS_0, ?QOS_1]))]),
     io:format("~s", [emqx_packet:format(?SUBACK_PACKET(40, [?QOS_0, ?QOS_1]))]),
     io:format("~s", [emqx_packet:format(?UNSUBSCRIBE_PACKET(89, [<<"t">>, <<"t2">>]))]),
     io:format("~s", [emqx_packet:format(?UNSUBSCRIBE_PACKET(89, [<<"t">>, <<"t2">>]))]),
-    io:format("~s", [emqx_packet:format(?UNSUBACK_PACKET(90))]).
+    io:format("~s", [emqx_packet:format(?UNSUBACK_PACKET(90))]),
+    io:format("~s", [emqx_packet:format(?DISCONNECT_PACKET(128))]).