Add 'emqx_packet:info/2' function and test cases

This commit is contained in:
Feng Lee 2019-12-28 20:06:32 +08:00 committed by turtleDeng
parent b7ca3905a6
commit 9a3d16c654
2 changed files with 175 additions and 0 deletions

View File

@ -27,8 +27,10 @@
, retain/1
]).
%% Field APIs
-export([ proto_name/1
, proto_ver/1
, info/2
]).
%% Check API
@ -95,6 +97,100 @@ proto_ver(?CONNECT_PACKET(ConnPkt)) ->
proto_ver(#mqtt_packet_connect{proto_ver = Ver}) ->
Ver.
%%--------------------------------------------------------------------
%% Field Info
%%--------------------------------------------------------------------
info(proto_name, #mqtt_packet_connect{proto_name = Name}) ->
Name;
info(proto_ver, #mqtt_packet_connect{proto_ver = Ver}) ->
Ver;
info(is_bridge, #mqtt_packet_connect{is_bridge = IsBridge}) ->
IsBridge;
info(clean_start, #mqtt_packet_connect{clean_start = CleanStart}) ->
CleanStart;
info(will_flag, #mqtt_packet_connect{will_flag = WillFlag}) ->
WillFlag;
info(will_qos, #mqtt_packet_connect{will_qos = WillQoS}) ->
WillQoS;
info(will_retain, #mqtt_packet_connect{will_retain = WillRetain}) ->
WillRetain;
info(keepalive, #mqtt_packet_connect{keepalive = KeepAlive}) ->
KeepAlive;
info(properties, #mqtt_packet_connect{properties = Props}) ->
Props;
info(clientid, #mqtt_packet_connect{clientid = ClientId}) ->
ClientId;
info(will_props, #mqtt_packet_connect{will_props = WillProps}) ->
WillProps;
info(will_topic, #mqtt_packet_connect{will_topic = WillTopic}) ->
WillTopic;
info(will_payload, #mqtt_packet_connect{will_payload = Payload}) ->
Payload;
info(username, #mqtt_packet_connect{username = Username}) ->
Username;
info(password, #mqtt_packet_connect{password = Password}) ->
Password;
info(ack_flags, #mqtt_packet_connack{ack_flags = Flags}) ->
Flags;
info(reason_code, #mqtt_packet_connack{reason_code = RC}) ->
RC;
info(properties, #mqtt_packet_connack{properties = Props}) ->
Props;
info(topic_name, #mqtt_packet_publish{topic_name = Topic}) ->
Topic;
info(packet_id, #mqtt_packet_publish{packet_id = PacketId}) ->
PacketId;
info(properties, #mqtt_packet_publish{properties = Props}) ->
Props;
info(packet_id, #mqtt_packet_puback{packet_id = PacketId}) ->
PacketId;
info(reason_code, #mqtt_packet_puback{reason_code = RC}) ->
RC;
info(properties, #mqtt_packet_puback{properties = Props}) ->
Props;
info(packet_id, #mqtt_packet_subscribe{packet_id = PacketId}) ->
PacketId;
info(properties, #mqtt_packet_subscribe{properties = Props}) ->
Props;
info(topic_filters, #mqtt_packet_subscribe{topic_filters = Topics}) ->
Topics;
info(packet_id, #mqtt_packet_suback{packet_id = PacketId}) ->
PacketId;
info(properties, #mqtt_packet_suback{properties = Props}) ->
Props;
info(reason_codes, #mqtt_packet_suback{reason_codes = RCs}) ->
RCs;
info(packet_id, #mqtt_packet_unsubscribe{packet_id = PacketId}) ->
PacketId;
info(properties, #mqtt_packet_unsubscribe{properties = Props}) ->
Props;
info(topic_filters, #mqtt_packet_unsubscribe{topic_filters = Topics}) ->
Topics;
info(packet_id, #mqtt_packet_unsuback{packet_id = PacketId}) ->
PacketId;
info(properties, #mqtt_packet_unsuback{properties = Props}) ->
Props;
info(reason_codes, #mqtt_packet_unsuback{reason_codes = RCs}) ->
RCs;
info(reason_code, #mqtt_packet_disconnect{reason_code = RC}) ->
RC;
info(properties, #mqtt_packet_disconnect{properties = Props}) ->
Props;
info(reason_code, #mqtt_packet_auth{reason_code = RC}) ->
RC;
info(properties, #mqtt_packet_auth{properties = Props}) ->
Props.
%%--------------------------------------------------------------------
%% Check MQTT Packet
%%--------------------------------------------------------------------

View File

@ -78,6 +78,85 @@ t_proto_ver(_) ->
?assertEqual(Ver, emqx_packet:proto_ver(ConnPkt))
end, [?MQTT_PROTO_V3, ?MQTT_PROTO_V4, ?MQTT_PROTO_V5]).
t_connect_info(_) ->
ConnPkt = #mqtt_packet_connect{will_flag = true,
clientid = <<"clientid">>,
username = <<"username">>,
will_retain = true,
will_qos = ?QOS_2,
will_topic = <<"topic">>,
will_props = undefined,
will_payload = <<"payload">>
},
?assertEqual(<<"MQTT">>, emqx_packet:info(proto_name, ConnPkt)),
?assertEqual(4, emqx_packet:info(proto_ver, ConnPkt)),
?assertEqual(false, emqx_packet:info(is_bridge, ConnPkt)),
?assertEqual(true, emqx_packet:info(clean_start, ConnPkt)),
?assertEqual(true, emqx_packet:info(will_flag, ConnPkt)),
?assertEqual(?QOS_2, emqx_packet:info(will_qos, ConnPkt)),
?assertEqual(true, emqx_packet:info(will_retain, ConnPkt)),
?assertEqual(0, emqx_packet:info(keepalive, ConnPkt)),
?assertEqual(undefined, emqx_packet:info(properties, ConnPkt)),
?assertEqual(<<"clientid">>, emqx_packet:info(clientid, ConnPkt)),
?assertEqual(undefined, emqx_packet:info(will_props, ConnPkt)),
?assertEqual(<<"topic">>, emqx_packet:info(will_topic, ConnPkt)),
?assertEqual(<<"payload">>, emqx_packet:info(will_payload, ConnPkt)),
?assertEqual(<<"username">>, emqx_packet:info(username, ConnPkt)),
?assertEqual(undefined, emqx_packet:info(password, ConnPkt)).
t_connack_info(_) ->
AckPkt = #mqtt_packet_connack{ack_flags = 0, reason_code = 0},
?assertEqual(0, emqx_packet:info(ack_flags, AckPkt)),
?assertEqual(0, emqx_packet:info(reason_code, AckPkt)),
?assertEqual(undefined, emqx_packet:info(properties, AckPkt)).
t_publish_info(_) ->
PubPkt = #mqtt_packet_publish{topic_name = <<"t">>, packet_id = 1},
?assertEqual(1, emqx_packet:info(packet_id, PubPkt)),
?assertEqual(<<"t">>, emqx_packet:info(topic_name, PubPkt)),
?assertEqual(undefined, emqx_packet:info(properties, PubPkt)).
t_puback_info(_) ->
AckPkt = #mqtt_packet_puback{packet_id = 1, reason_code = 0},
?assertEqual(1, emqx_packet:info(packet_id, AckPkt)),
?assertEqual(0, emqx_packet:info(reason_code, AckPkt)),
?assertEqual(undefined, emqx_packet:info(properties, AckPkt)).
t_subscribe_info(_) ->
TopicFilters = [{<<"t/#">>, #{}}],
SubPkt = #mqtt_packet_subscribe{packet_id = 1, topic_filters = TopicFilters},
?assertEqual(1, emqx_packet:info(packet_id, SubPkt)),
?assertEqual(undefined, emqx_packet:info(properties, SubPkt)),
?assertEqual(TopicFilters, emqx_packet:info(topic_filters, SubPkt)).
t_suback_info(_) ->
SubackPkt = #mqtt_packet_suback{packet_id = 1, reason_codes = [0]},
?assertEqual(1, emqx_packet:info(packet_id, SubackPkt)),
?assertEqual(undefined, emqx_packet:info(properties, SubackPkt)),
?assertEqual([0], emqx_packet:info(reason_codes, SubackPkt)).
t_unsubscribe_info(_) ->
UnsubPkt = #mqtt_packet_unsubscribe{packet_id = 1, topic_filters = [<<"t/#">>]},
?assertEqual(1, emqx_packet:info(packet_id, UnsubPkt)),
?assertEqual(undefined, emqx_packet:info(properties, UnsubPkt)),
?assertEqual([<<"t/#">>], emqx_packet:info(topic_filters, UnsubPkt)).
t_unsuback_info(_) ->
AckPkt = #mqtt_packet_unsuback{packet_id = 1, reason_codes = [0]},
?assertEqual(1, emqx_packet:info(packet_id, AckPkt)),
?assertEqual([0], emqx_packet:info(reason_codes, AckPkt)),
?assertEqual(undefined, emqx_packet:info(properties, AckPkt)).
t_disconnect_info(_) ->
DisconnPkt = #mqtt_packet_disconnect{reason_code = 0},
?assertEqual(0, emqx_packet:info(reason_code, DisconnPkt)),
?assertEqual(undefined, emqx_packet:info(properties, DisconnPkt)).
t_auth_info(_) ->
AuthPkt = #mqtt_packet_auth{reason_code = 0},
?assertEqual(0, emqx_packet:info(reason_code, AuthPkt)),
?assertEqual(undefined, emqx_packet:info(properties, AuthPkt)).
t_check_publish(_) ->
Props = #{'Response-Topic' => <<"responsetopic">>, 'Topic-Alias' => 1},
ok = emqx_packet:check(?PUBLISH_PACKET(?QOS_1, <<"topic">>, 1, Props, <<"payload">>)),