94 lines
4.0 KiB
Erlang
94 lines
4.0 KiB
Erlang
%% Copyright (c) 2018 EMQ Technologies Co., Ltd. All Rights Reserved.
|
|
%%
|
|
%% Licensed under the Apache License, Version 2.0 (the "License");
|
|
%% you may not use this file except in compliance with the License.
|
|
%% You may obtain a copy of the License at
|
|
%%
|
|
%% http://www.apache.org/licenses/LICENSE-2.0
|
|
%%
|
|
%% Unless required by applicable law or agreed to in writing, software
|
|
%% distributed under the License is distributed on an "AS IS" BASIS,
|
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
%% See the License for the specific language governing permissions and
|
|
%% limitations under the License.
|
|
|
|
|
|
-module(emqx_packet_SUITE).
|
|
|
|
-compile(export_all).
|
|
-compile(nowarn_export_all).
|
|
|
|
-include("emqx.hrl").
|
|
|
|
-include("emqx_mqtt.hrl").
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
all() ->
|
|
[
|
|
packet_proto_name,
|
|
packet_type_name,
|
|
packet_validate,
|
|
packet_message,
|
|
packet_format,
|
|
packet_will_msg
|
|
].
|
|
|
|
packet_proto_name(_) ->
|
|
?assertEqual(<<"MQIsdp">>, emqx_packet:protocol_name(3)),
|
|
?assertEqual(<<"MQTT">>, emqx_packet:protocol_name(4)),
|
|
?assertEqual(<<"MQTT">>, emqx_packet:protocol_name(5)).
|
|
|
|
packet_type_name(_) ->
|
|
?assertEqual('CONNECT', emqx_packet:type_name(?CONNECT)),
|
|
?assertEqual('UNSUBSCRIBE', emqx_packet:type_name(?UNSUBSCRIBE)).
|
|
|
|
packet_validate(_) ->
|
|
?assertEqual(true, emqx_packet:validate(?SUBSCRIBE_PACKET(15, #{'Subscription-Identifier' => 1}, [{<<"topic">>, #{qos => ?QOS0}}]))),
|
|
?assertEqual(true, emqx_packet:validate(?UNSUBSCRIBE_PACKET(89, [<<"topic">>]))),
|
|
?assertEqual(true, emqx_packet:validate(?CONNECT_PACKET(#mqtt_packet_connect{}))).
|
|
|
|
packet_message(_) ->
|
|
Pkt = #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
|
|
qos = ?QOS0,
|
|
retain = false,
|
|
dup = false},
|
|
variable = #mqtt_packet_publish{topic_name = <<"topic">>,
|
|
packet_id = 10,
|
|
properties = #{}},
|
|
payload = <<"payload">>},
|
|
Msg = emqx_message:make(<<"clientid">>, ?QOS0, <<"topic">>, <<"payload">>),
|
|
Msg2 = emqx_message:set_flag(retain, false, Msg),
|
|
Pkt = emqx_packet:from_message(10, Msg2),
|
|
Msg3 = emqx_message:set_header(username, "test", Msg2),
|
|
Msg4 = emqx_packet:to_message(#{client_id => <<"clientid">>, username => "test"}, Pkt),
|
|
Msg5 = Msg4#message{timestamp = Msg3#message.timestamp},
|
|
Msg5 = Msg3.
|
|
|
|
packet_format(_) ->
|
|
io:format("~s", [emqx_packet:format(?CONNECT_PACKET(#mqtt_packet_connect{}))]),
|
|
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_2, <<"topic">>, 10, <<"payload">>))]),
|
|
io:format("~s", [emqx_packet:format(?PUBACK_PACKET(?PUBACK, 98))]),
|
|
io:format("~s", [emqx_packet:format(?PUBREL_PACKET(99))]),
|
|
io:format("~s", [emqx_packet:format(?SUBSCRIBE_PACKET(15, [{<<"topic">>, ?QOS0}, {<<"topic1">>, ?QOS1}]))]),
|
|
io:format("~s", [emqx_packet:format(?SUBACK_PACKET(40, [?QOS0, ?QOS1]))]),
|
|
io:format("~s", [emqx_packet:format(?UNSUBSCRIBE_PACKET(89, [<<"t">>, <<"t2">>]))]),
|
|
io:format("~s", [emqx_packet:format(?UNSUBACK_PACKET(90))]).
|
|
|
|
packet_will_msg(_) ->
|
|
Pkt = #mqtt_packet_connect{ will_flag = true,
|
|
client_id = <<"clientid">>,
|
|
username = "test",
|
|
will_retain = true,
|
|
will_qos = ?QOS2,
|
|
will_topic = <<"topic">>,
|
|
will_props = #{},
|
|
will_payload = <<"payload">>},
|
|
Msg = emqx_packet:will_msg(Pkt),
|
|
?assertEqual(<<"clientid">>, Msg#message.from),
|
|
?assertEqual(<<"topic">>, Msg#message.topic).
|
|
|
|
|