From b2574237303a8d67c0569022db07f557ed10c799 Mon Sep 17 00:00:00 2001 From: Feng Date: Tue, 12 Apr 2016 11:19:55 +0800 Subject: [PATCH] more test cases for parer --- src/emqttd_parser.erl | 2 ++ test/emqttd_protocol_SUITE.erl | 60 ++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/emqttd_parser.erl b/src/emqttd_parser.erl index 5a6f22a41..a561b5c99 100644 --- a/src/emqttd_parser.erl +++ b/src/emqttd_parser.erl @@ -61,8 +61,10 @@ parse_remaining_len(_Bin, _Header, _Multiplier, Length, #mqtt_packet_limit{max_p {error, invalid_mqtt_frame_len}; parse_remaining_len(<<>>, Header, Multiplier, Length, Limit) -> {more, fun(Bin) -> parse_remaining_len(Bin, Header, Multiplier, Length, Limit) end}; +%% optimize: match PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK... parse_remaining_len(<<0:1, 2:7, Rest/binary>>, Header, 1, 0, _Limit) -> parse_frame(Rest, Header, 2); +%% optimize: match PINGREQ... parse_remaining_len(<<0:8, Rest/binary>>, Header, 1, 0, _Limit) -> parse_frame(Rest, Header, 0); parse_remaining_len(<<1:1, Len:7, Rest/binary>>, Header, Multiplier, Value, Limit) -> diff --git a/test/emqttd_protocol_SUITE.erl b/test/emqttd_protocol_SUITE.erl index 18984591d..d9344786d 100644 --- a/test/emqttd_protocol_SUITE.erl +++ b/test/emqttd_protocol_SUITE.erl @@ -36,7 +36,11 @@ groups() -> parse_bridge, parse_publish, parse_puback, + parse_pubrec, + parse_pubrel, + parse_pubcomp, parse_subscribe, + parse_unsubscribe, parse_pingreq, parse_disconnect]}, {serializer, [], @@ -173,25 +177,69 @@ parse_publish(_) -> parse_puback(_) -> Parser = emqttd_parser:new([]), %%PUBACK(Qos=0, Retain=false, Dup=false, PacketId=1) - PubAckBin = <<64,2,0,1>>, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBACK, dup = false, qos = 0, - retain = false}}, <<>>} = Parser(PubAckBin). + retain = false}}, <<>>} = Parser(<<64,2,0,1>>). +parse_pubrec(_) -> + Parser = emqttd_parser:new([]), + %%PUBREC(Qos=0, Retain=false, Dup=false, PacketId=1) + {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBREC, + dup = false, + qos = 0, + retain = false}}, <<>>} = Parser(<<5:4,0:4,2,0,1>>). + +parse_pubrel(_) -> + Parser = emqttd_parser:new([]), + {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBREL, + dup = false, + qos = 1, + retain = false}}, <<>>} = Parser(<<6:4,2:4,2,0,1>>). + +parse_pubcomp(_) -> + Parser = emqttd_parser:new([]), + {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBCOMP, + dup = false, + qos = 0, + retain = false}}, <<>>} = Parser(<<7:4,0:4,2,0,1>>). parse_subscribe(_) -> - ok. + Parser = emqttd_parser:new([]), + %% SUBSCRIBE(Q1, R0, D0, PacketId=2, TopicTable=[{<<"TopicA">>,2}]) + {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?SUBSCRIBE, + dup = false, + qos = 1, + retain = false}, + variable = #mqtt_packet_subscribe{packet_id = 2, + topic_table = [{<<"TopicA">>,2}]} }, <<>>} + = Parser(<<130,11,0,2,0,6,84,111,112,105,99,65,2>>). + +parse_unsubscribe(_) -> + Parser = emqttd_parser:new([]), + %% UNSUBSCRIBE(Q1, R0, D0, PacketId=2, TopicTable=[<<"TopicA">>]) + {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?UNSUBSCRIBE, + dup = false, + qos = 1, + retain = false}, + variable = #mqtt_packet_unsubscribe{packet_id = 2, + topics = [<<"TopicA">>]}}, <<>>} + = Parser(<<162,10,0,2,0,6,84,111,112,105,99,65>>). parse_pingreq(_) -> - ok. + Parser = emqttd_parser:new([]), + {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PINGREQ, + dup = false, + qos = 0, + retain = false}}, <<>>} + = Parser(<>). parse_disconnect(_) -> Parser = emqttd_parser:new([]), %DISCONNECT(Qos=0, Retain=false, Dup=false) Bin = <<224, 0>>, {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?DISCONNECT, - dup = false, - qos = 0, + dup = false, + qos = 0, retain = false}}, <<>>} = Parser(Bin). %%--------------------------------------------------------------------