emqttd_protocol_SUITE.erl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2012-2016 Feng Lee <feng@emqtt.io>.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqttd_protocol_SUITE).
  17. -compile(export_all).
  18. -import(emqttd_serializer, [serialize/1]).
  19. -include("emqttd.hrl").
  20. -include("emqttd_protocol.hrl").
  21. all() ->
  22. [{group, parser},
  23. {group, serializer},
  24. {group, packet},
  25. {group, message}].
  26. groups() ->
  27. [{parser, [],
  28. [parse_connect,
  29. parse_bridge,
  30. parse_publish,
  31. parse_puback,
  32. parse_subscribe,
  33. parse_pingreq,
  34. parse_disconnect]},
  35. {serializer, [],
  36. [serialize_connect,
  37. serialize_connack,
  38. serialize_publish,
  39. serialize_puback,
  40. serialize_pubrel,
  41. serialize_subscribe,
  42. serialize_suback,
  43. serialize_unsubscribe,
  44. serialize_unsuback,
  45. serialize_pingreq,
  46. serialize_pingresp,
  47. serialize_disconnect]},
  48. {packet, [],
  49. [packet_proto_name,
  50. packet_type_name,
  51. packet_connack_name,
  52. packet_format]},
  53. {message, [],
  54. [message_make,
  55. message_from_packet,
  56. message_flag]}].
  57. %%--------------------------------------------------------------------
  58. %% Parse Cases
  59. %%--------------------------------------------------------------------
  60. parse_connect(_) ->
  61. Parser = emqttd_parser:new([]),
  62. %% CONNECT(Q0, R0, D0, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
  63. V31ConnBin = <<16,37,0,6,77,81,73,115,100,112,3,2,0,60,0,23,109,111,115,113,112,117,98,47,49,48,52,53,49,45,105,77,97,99,46,108,111,99,97>>,
  64. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT,
  65. dup = false,
  66. qos = 0,
  67. retain = false},
  68. variable = #mqtt_packet_connect{proto_ver = 3,
  69. proto_name = <<"MQIsdp">>,
  70. client_id = <<"mosqpub/10451-iMac.loca">>,
  71. clean_sess = true,
  72. keep_alive = 60}}, <<>>} = Parser(V31ConnBin),
  73. %% CONNECT(Q0, R0, D0, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
  74. V311ConnBin = <<16,35,0,4,77,81,84,84,4,2,0,60,0,23,109,111,115,113,112,117,98,47,49,48,52,53,49,45,105,77,97,99,46,108,111,99,97>>,
  75. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT,
  76. dup = false,
  77. qos = 0,
  78. retain = false},
  79. variable = #mqtt_packet_connect{proto_ver = 4,
  80. proto_name = <<"MQTT">>,
  81. client_id = <<"mosqpub/10451-iMac.loca">>,
  82. clean_sess = true,
  83. keep_alive = 60 } }, <<>>} = Parser(V311ConnBin),
  84. %% CONNECT(Qos=0, Retain=false, Dup=false, ClientId="", ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60)
  85. V311ConnWithoutClientId = <<16,12,0,4,77,81,84,84,4,2,0,60,0,0>>,
  86. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT,
  87. dup = false,
  88. qos = 0,
  89. retain = false},
  90. variable = #mqtt_packet_connect{proto_ver = 4,
  91. proto_name = <<"MQTT">>,
  92. client_id = <<>>,
  93. clean_sess = true,
  94. keep_alive = 60 } }, <<>>} = Parser(V311ConnWithoutClientId),
  95. %%CONNECT(Q0, R0, D0, ClientId=mosqpub/10452-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60,
  96. %% Username=test, Password=******, Will(Qos=1, Retain=false, Topic=/will, Msg=willmsg))
  97. ConnBinWithWill = <<16,67,0,6,77,81,73,115,100,112,3,206,0,60,0,23,109,111,115,113,112,117,98,47,49,48,52,53,50,45,105,77,97,99,46,108,111,99,97,0,5,47,119,105,108,108,0,7,119,105,108,108,109,115,103,0,4,116,101,115,116,0,6,112,117,98,108,105,99>>,
  98. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT,
  99. dup = false,
  100. qos = 0,
  101. retain = false},
  102. variable = #mqtt_packet_connect{proto_ver = 3,
  103. proto_name = <<"MQIsdp">>,
  104. client_id = <<"mosqpub/10452-iMac.loca">>,
  105. clean_sess = true,
  106. keep_alive = 60,
  107. will_retain = false,
  108. will_qos = 1,
  109. will_flag = true,
  110. will_topic = <<"/will">>,
  111. will_msg = <<"willmsg">>,
  112. username = <<"test">>,
  113. password = <<"public">>}}, <<>>} = Parser(ConnBinWithWill),
  114. ok.
  115. parse_bridge(_) ->
  116. Parser = emqttd_parser:new([]),
  117. Data = <<16,86,0,6,77,81,73,115,100,112,131,44,0,60,0,19,67,95,48,48,58,48,67,58,50,57,58,50,66,58,55,55,58,53,50,
  118. 0,48,36,83,89,83,47,98,114,111,107,101,114,47,99,111,110,110,101,99,116,105,111,110,47,67,95,48,48,58,48,
  119. 67,58,50,57,58,50,66,58,55,55,58,53,50,47,115,116,97,116,101,0,1,48>>,
  120. %% CONNECT(Q0, R0, D0, ClientId=C_00:0C:29:2B:77:52, ProtoName=MQIsdp, ProtoVsn=131, CleanSess=false, KeepAlive=60,
  121. %% Username=undefined, Password=undefined, Will(Q1, R1, Topic=$SYS/broker/connection/C_00:0C:29:2B:77:52/state, Msg=0))
  122. {ok, #mqtt_packet{variable = Variable}, <<>>} = Parser(Data),
  123. #mqtt_packet_connect{client_id = <<"C_00:0C:29:2B:77:52">>,
  124. proto_ver = 16#03,
  125. proto_name = <<"MQIsdp">>,
  126. will_retain = true,
  127. will_qos = 1,
  128. will_flag = true,
  129. clean_sess = false,
  130. keep_alive = 60,
  131. will_topic = <<"$SYS/broker/connection/C_00:0C:29:2B:77:52/state">>,
  132. will_msg = <<"0">>} = Variable.
  133. parse_publish(_) ->
  134. Parser = emqttd_parser:new([]),
  135. %%PUBLISH(Qos=1, Retain=false, Dup=false, TopicName=a/b/c, PacketId=1, Payload=<<"hahah">>)
  136. PubBin = <<50,14,0,5,97,47,98,47,99,0,1,104,97,104,97,104>>,
  137. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
  138. dup = false,
  139. qos = 1,
  140. retain = false},
  141. variable = #mqtt_packet_publish{topic_name = <<"a/b/c">>,
  142. packet_id = 1},
  143. payload = <<"hahah">> }, <<>>} = Parser(PubBin),
  144. %PUBLISH(Qos=0, Retain=false, Dup=false, TopicName=xxx/yyy, PacketId=undefined, Payload=<<"hello">>)
  145. %DISCONNECT(Qos=0, Retain=false, Dup=false)
  146. PubBin1 = <<48,14,0,7,120,120,120,47,121,121,121,104,101,108,108,111,224,0>>,
  147. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
  148. dup = false,
  149. qos = 0,
  150. retain = false},
  151. variable = #mqtt_packet_publish{topic_name = <<"xxx/yyy">>,
  152. packet_id = undefined},
  153. payload = <<"hello">> }, <<224,0>>} = Parser(PubBin1),
  154. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?DISCONNECT,
  155. dup = false,
  156. qos = 0,
  157. retain = false}}, <<>>} = Parser(<<224, 0>>).
  158. parse_puback(_) ->
  159. Parser = emqttd_parser:new([]),
  160. %%PUBACK(Qos=0, Retain=false, Dup=false, PacketId=1)
  161. PubAckBin = <<64,2,0,1>>,
  162. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBACK,
  163. dup = false,
  164. qos = 0,
  165. retain = false}}, <<>>} = Parser(PubAckBin).
  166. parse_subscribe(_) ->
  167. ok.
  168. parse_pingreq(_) ->
  169. ok.
  170. parse_disconnect(_) ->
  171. Parser = emqttd_parser:new([]),
  172. %DISCONNECT(Qos=0, Retain=false, Dup=false)
  173. Bin = <<224, 0>>,
  174. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?DISCONNECT,
  175. dup = false,
  176. qos = 0,
  177. retain = false}}, <<>>} = Parser(Bin).
  178. %%--------------------------------------------------------------------
  179. %% Serialize Cases
  180. %%--------------------------------------------------------------------
  181. serialize_connect(_) ->
  182. serialize(?CONNECT_PACKET(#mqtt_packet_connect{})),
  183. serialize(?CONNECT_PACKET(#mqtt_packet_connect{
  184. client_id = <<"clientId">>,
  185. will_qos = ?QOS1,
  186. will_flag = true,
  187. will_retain = true,
  188. will_topic = <<"will">>,
  189. will_msg = <<"haha">>,
  190. clean_sess = true})).
  191. serialize_connack(_) ->
  192. ConnAck = #mqtt_packet{header = #mqtt_packet_header{type = ?CONNACK},
  193. variable = #mqtt_packet_connack{ack_flags = 0, return_code = 0}},
  194. <<32,2,0,0>> = serialize(ConnAck).
  195. serialize_publish(_) ->
  196. serialize(?PUBLISH_PACKET(?QOS_0, <<"Topic">>, undefined, <<"Payload">>)),
  197. serialize(?PUBLISH_PACKET(?QOS_1, <<"Topic">>, 938, <<"Payload">>)),
  198. serialize(?PUBLISH_PACKET(?QOS_2, <<"Topic">>, 99, long_payload())).
  199. serialize_puback(_) ->
  200. serialize(?PUBACK_PACKET(?PUBACK, 10384)).
  201. serialize_pubrel(_) ->
  202. serialize(?PUBREL_PACKET(10384)).
  203. serialize_subscribe(_) ->
  204. TopicTable = [{<<"TopicQos0">>, ?QOS_0}, {<<"TopicQos1">>, ?QOS_1}, {<<"TopicQos2">>, ?QOS_2}],
  205. serialize(?SUBSCRIBE_PACKET(10, TopicTable)).
  206. serialize_suback(_) ->
  207. serialize(?SUBACK_PACKET(10, [?QOS_0, ?QOS_1, 128])).
  208. serialize_unsubscribe(_) ->
  209. serialize(?UNSUBSCRIBE_PACKET(10, [<<"Topic1">>, <<"Topic2">>])).
  210. serialize_unsuback(_) ->
  211. serialize(?UNSUBACK_PACKET(10)).
  212. serialize_pingreq(_) ->
  213. serialize(?PACKET(?PINGREQ)).
  214. serialize_pingresp(_) ->
  215. serialize(?PACKET(?PINGRESP)).
  216. serialize_disconnect(_) ->
  217. serialize(?PACKET(?DISCONNECT)).
  218. long_payload() ->
  219. iolist_to_binary(["payload." || _I <- lists:seq(1, 100)]).
  220. %%--------------------------------------------------------------------
  221. %% Packet Cases
  222. %%--------------------------------------------------------------------
  223. packet_proto_name(_) ->
  224. <<"MQIsdp">> = emqttd_packet:protocol_name(3),
  225. <<"MQTT">> = emqttd_packet:protocol_name(4).
  226. packet_type_name(_) ->
  227. 'CONNECT' = emqttd_packet:type_name(?CONNECT),
  228. 'UNSUBSCRIBE' = emqttd_packet:type_name(?UNSUBSCRIBE).
  229. packet_connack_name(_) ->
  230. 'CONNACK_ACCEPT' = emqttd_packet:connack_name(?CONNACK_ACCEPT),
  231. 'CONNACK_PROTO_VER' = emqttd_packet:connack_name(?CONNACK_PROTO_VER),
  232. 'CONNACK_INVALID_ID' = emqttd_packet:connack_name(?CONNACK_INVALID_ID),
  233. 'CONNACK_SERVER' = emqttd_packet:connack_name(?CONNACK_SERVER),
  234. 'CONNACK_CREDENTIALS' = emqttd_packet:connack_name(?CONNACK_CREDENTIALS),
  235. 'CONNACK_AUTH' = emqttd_packet:connack_name(?CONNACK_AUTH).
  236. packet_format(_) ->
  237. io:format("~s", [emqttd_packet:format(?CONNECT_PACKET(#mqtt_packet_connect{}))]),
  238. io:format("~s", [emqttd_packet:format(?CONNACK_PACKET(?CONNACK_SERVER))]),
  239. io:format("~s", [emqttd_packet:format(?PUBLISH_PACKET(?QOS_1, 1))]),
  240. io:format("~s", [emqttd_packet:format(?PUBLISH_PACKET(?QOS_2, <<"topic">>, 10, <<"payload">>))]),
  241. io:format("~s", [emqttd_packet:format(?PUBACK_PACKET(?PUBACK, 98))]),
  242. io:format("~s", [emqttd_packet:format(?PUBREL_PACKET(99))]),
  243. io:format("~s", [emqttd_packet:format(?SUBSCRIBE_PACKET(15, [{<<"topic">>, ?QOS0}, {<<"topic1">>, ?QOS1}]))]),
  244. io:format("~s", [emqttd_packet:format(?SUBACK_PACKET(40, [?QOS0, ?QOS1]))]),
  245. io:format("~s", [emqttd_packet:format(?UNSUBSCRIBE_PACKET(89, [<<"t">>, <<"t2">>]))]),
  246. io:format("~s", [emqttd_packet:format(?UNSUBACK_PACKET(90))]).
  247. %%--------------------------------------------------------------------
  248. %% Message Cases
  249. %%--------------------------------------------------------------------
  250. message_make(_) ->
  251. Msg = emqttd_message:make(<<"clientid">>, <<"topic">>, <<"payload">>),
  252. 0 = Msg#mqtt_message.qos,
  253. undefined = Msg#mqtt_message.msgid,
  254. Msg1 = emqttd_message:make(<<"clientid">>, qos2, <<"topic">>, <<"payload">>),
  255. true = is_binary(Msg1#mqtt_message.msgid),
  256. 2 = Msg1#mqtt_message.qos.
  257. message_from_packet(_) ->
  258. Msg = emqttd_message:from_packet(?PUBLISH_PACKET(1, <<"topic">>, 10, <<"payload">>)),
  259. 1 = Msg#mqtt_message.qos,
  260. 10 = Msg#mqtt_message.pktid,
  261. <<"topic">> = Msg#mqtt_message.topic,
  262. WillMsg = emqttd_message:from_packet(#mqtt_packet_connect{will_flag = true,
  263. will_topic = <<"WillTopic">>,
  264. will_msg = <<"WillMsg">>}),
  265. <<"WillTopic">> = WillMsg#mqtt_message.topic,
  266. <<"WillMsg">> = WillMsg#mqtt_message.payload,
  267. Msg2 = emqttd_message:from_packet(<<"username">>, <<"clientid">>,
  268. ?PUBLISH_PACKET(1, <<"topic">>, 20, <<"payload">>)),
  269. <<"clientid">> = Msg2#mqtt_message.from,
  270. <<"username">> = Msg2#mqtt_message.sender,
  271. io:format("~s", [emqttd_message:format(Msg2)]).
  272. message_flag(_) ->
  273. Pkt = ?PUBLISH_PACKET(1, <<"t">>, 2, <<"payload">>),
  274. Msg2 = emqttd_message:from_packet(<<"clientid">>, Pkt),
  275. Msg3 = emqttd_message:set_flag(retain, Msg2),
  276. Msg4 = emqttd_message:set_flag(dup, Msg3),
  277. true = Msg4#mqtt_message.dup,
  278. true = Msg4#mqtt_message.retain,
  279. Msg5 = emqttd_message:set_flag(Msg4),
  280. Msg6 = emqttd_message:unset_flag(dup, Msg5),
  281. Msg7 = emqttd_message:unset_flag(retain, Msg6),
  282. false = Msg7#mqtt_message.dup,
  283. false = Msg7#mqtt_message.retain,
  284. emqttd_message:unset_flag(Msg7),
  285. emqttd_message:to_packet(Msg7).