emqttd_protocol_SUITE.erl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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_pubrec,
  33. parse_pubrel,
  34. parse_pubcomp,
  35. parse_subscribe,
  36. parse_unsubscribe,
  37. parse_pingreq,
  38. parse_disconnect]},
  39. {serializer, [],
  40. [serialize_connect,
  41. serialize_connack,
  42. serialize_publish,
  43. serialize_puback,
  44. serialize_pubrel,
  45. serialize_subscribe,
  46. serialize_suback,
  47. serialize_unsubscribe,
  48. serialize_unsuback,
  49. serialize_pingreq,
  50. serialize_pingresp,
  51. serialize_disconnect]},
  52. {packet, [],
  53. [packet_proto_name,
  54. packet_type_name,
  55. packet_connack_name,
  56. packet_format]},
  57. {message, [],
  58. [message_make,
  59. message_from_packet,
  60. message_flag]}].
  61. %%--------------------------------------------------------------------
  62. %% Parse Cases
  63. %%--------------------------------------------------------------------
  64. parse_connect(_) ->
  65. Parser = emqttd_parser:new([]),
  66. %% CONNECT(Q0, R0, D0, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
  67. 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>>,
  68. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT,
  69. dup = false,
  70. qos = 0,
  71. retain = false},
  72. variable = #mqtt_packet_connect{proto_ver = 3,
  73. proto_name = <<"MQIsdp">>,
  74. client_id = <<"mosqpub/10451-iMac.loca">>,
  75. clean_sess = true,
  76. keep_alive = 60}}, <<>>} = Parser(V31ConnBin),
  77. %% CONNECT(Q0, R0, D0, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
  78. 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>>,
  79. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT,
  80. dup = false,
  81. qos = 0,
  82. retain = false},
  83. variable = #mqtt_packet_connect{proto_ver = 4,
  84. proto_name = <<"MQTT">>,
  85. client_id = <<"mosqpub/10451-iMac.loca">>,
  86. clean_sess = true,
  87. keep_alive = 60 } }, <<>>} = Parser(V311ConnBin),
  88. %% CONNECT(Qos=0, Retain=false, Dup=false, ClientId="", ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60)
  89. V311ConnWithoutClientId = <<16,12,0,4,77,81,84,84,4,2,0,60,0,0>>,
  90. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT,
  91. dup = false,
  92. qos = 0,
  93. retain = false},
  94. variable = #mqtt_packet_connect{proto_ver = 4,
  95. proto_name = <<"MQTT">>,
  96. client_id = <<>>,
  97. clean_sess = true,
  98. keep_alive = 60 } }, <<>>} = Parser(V311ConnWithoutClientId),
  99. %%CONNECT(Q0, R0, D0, ClientId=mosqpub/10452-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60,
  100. %% Username=test, Password=******, Will(Qos=1, Retain=false, Topic=/will, Msg=willmsg))
  101. 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>>,
  102. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?CONNECT,
  103. dup = false,
  104. qos = 0,
  105. retain = false},
  106. variable = #mqtt_packet_connect{proto_ver = 3,
  107. proto_name = <<"MQIsdp">>,
  108. client_id = <<"mosqpub/10452-iMac.loca">>,
  109. clean_sess = true,
  110. keep_alive = 60,
  111. will_retain = false,
  112. will_qos = 1,
  113. will_flag = true,
  114. will_topic = <<"/will">>,
  115. will_msg = <<"willmsg">>,
  116. username = <<"test">>,
  117. password = <<"public">>}}, <<>>} = Parser(ConnBinWithWill),
  118. ok.
  119. parse_bridge(_) ->
  120. Parser = emqttd_parser:new([]),
  121. 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,
  122. 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,
  123. 67,58,50,57,58,50,66,58,55,55,58,53,50,47,115,116,97,116,101,0,1,48>>,
  124. %% CONNECT(Q0, R0, D0, ClientId=C_00:0C:29:2B:77:52, ProtoName=MQIsdp, ProtoVsn=131, CleanSess=false, KeepAlive=60,
  125. %% Username=undefined, Password=undefined, Will(Q1, R1, Topic=$SYS/broker/connection/C_00:0C:29:2B:77:52/state, Msg=0))
  126. {ok, #mqtt_packet{variable = Variable}, <<>>} = Parser(Data),
  127. #mqtt_packet_connect{client_id = <<"C_00:0C:29:2B:77:52">>,
  128. proto_ver = 16#03,
  129. proto_name = <<"MQIsdp">>,
  130. will_retain = true,
  131. will_qos = 1,
  132. will_flag = true,
  133. clean_sess = false,
  134. keep_alive = 60,
  135. will_topic = <<"$SYS/broker/connection/C_00:0C:29:2B:77:52/state">>,
  136. will_msg = <<"0">>} = Variable.
  137. parse_publish(_) ->
  138. Parser = emqttd_parser:new([]),
  139. %%PUBLISH(Qos=1, Retain=false, Dup=false, TopicName=a/b/c, PacketId=1, Payload=<<"hahah">>)
  140. PubBin = <<50,14,0,5,97,47,98,47,99,0,1,104,97,104,97,104>>,
  141. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
  142. dup = false,
  143. qos = 1,
  144. retain = false},
  145. variable = #mqtt_packet_publish{topic_name = <<"a/b/c">>,
  146. packet_id = 1},
  147. payload = <<"hahah">> }, <<>>} = Parser(PubBin),
  148. %PUBLISH(Qos=0, Retain=false, Dup=false, TopicName=xxx/yyy, PacketId=undefined, Payload=<<"hello">>)
  149. %DISCONNECT(Qos=0, Retain=false, Dup=false)
  150. PubBin1 = <<48,14,0,7,120,120,120,47,121,121,121,104,101,108,108,111,224,0>>,
  151. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
  152. dup = false,
  153. qos = 0,
  154. retain = false},
  155. variable = #mqtt_packet_publish{topic_name = <<"xxx/yyy">>,
  156. packet_id = undefined},
  157. payload = <<"hello">> }, <<224,0>>} = Parser(PubBin1),
  158. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?DISCONNECT,
  159. dup = false,
  160. qos = 0,
  161. retain = false}}, <<>>} = Parser(<<224, 0>>).
  162. parse_puback(_) ->
  163. Parser = emqttd_parser:new([]),
  164. %%PUBACK(Qos=0, Retain=false, Dup=false, PacketId=1)
  165. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBACK,
  166. dup = false,
  167. qos = 0,
  168. retain = false}}, <<>>} = Parser(<<64,2,0,1>>).
  169. parse_pubrec(_) ->
  170. Parser = emqttd_parser:new([]),
  171. %%PUBREC(Qos=0, Retain=false, Dup=false, PacketId=1)
  172. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBREC,
  173. dup = false,
  174. qos = 0,
  175. retain = false}}, <<>>} = Parser(<<5:4,0:4,2,0,1>>).
  176. parse_pubrel(_) ->
  177. Parser = emqttd_parser:new([]),
  178. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBREL,
  179. dup = false,
  180. qos = 1,
  181. retain = false}}, <<>>} = Parser(<<6:4,2:4,2,0,1>>).
  182. parse_pubcomp(_) ->
  183. Parser = emqttd_parser:new([]),
  184. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PUBCOMP,
  185. dup = false,
  186. qos = 0,
  187. retain = false}}, <<>>} = Parser(<<7:4,0:4,2,0,1>>).
  188. parse_subscribe(_) ->
  189. Parser = emqttd_parser:new([]),
  190. %% SUBSCRIBE(Q1, R0, D0, PacketId=2, TopicTable=[{<<"TopicA">>,2}])
  191. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?SUBSCRIBE,
  192. dup = false,
  193. qos = 1,
  194. retain = false},
  195. variable = #mqtt_packet_subscribe{packet_id = 2,
  196. topic_table = [{<<"TopicA">>,2}]} }, <<>>}
  197. = Parser(<<130,11,0,2,0,6,84,111,112,105,99,65,2>>).
  198. parse_unsubscribe(_) ->
  199. Parser = emqttd_parser:new([]),
  200. %% UNSUBSCRIBE(Q1, R0, D0, PacketId=2, TopicTable=[<<"TopicA">>])
  201. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?UNSUBSCRIBE,
  202. dup = false,
  203. qos = 1,
  204. retain = false},
  205. variable = #mqtt_packet_unsubscribe{packet_id = 2,
  206. topics = [<<"TopicA">>]}}, <<>>}
  207. = Parser(<<162,10,0,2,0,6,84,111,112,105,99,65>>).
  208. parse_pingreq(_) ->
  209. Parser = emqttd_parser:new([]),
  210. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?PINGREQ,
  211. dup = false,
  212. qos = 0,
  213. retain = false}}, <<>>}
  214. = Parser(<<?PINGREQ:4, 0:4, 0:8>>).
  215. parse_disconnect(_) ->
  216. Parser = emqttd_parser:new([]),
  217. %DISCONNECT(Qos=0, Retain=false, Dup=false)
  218. Bin = <<224, 0>>,
  219. {ok, #mqtt_packet{header = #mqtt_packet_header{type = ?DISCONNECT,
  220. dup = false,
  221. qos = 0,
  222. retain = false}}, <<>>} = Parser(Bin).
  223. %%--------------------------------------------------------------------
  224. %% Serialize Cases
  225. %%--------------------------------------------------------------------
  226. serialize_connect(_) ->
  227. serialize(?CONNECT_PACKET(#mqtt_packet_connect{})),
  228. serialize(?CONNECT_PACKET(#mqtt_packet_connect{
  229. client_id = <<"clientId">>,
  230. will_qos = ?QOS1,
  231. will_flag = true,
  232. will_retain = true,
  233. will_topic = <<"will">>,
  234. will_msg = <<"haha">>,
  235. clean_sess = true})).
  236. serialize_connack(_) ->
  237. ConnAck = #mqtt_packet{header = #mqtt_packet_header{type = ?CONNACK},
  238. variable = #mqtt_packet_connack{ack_flags = 0, return_code = 0}},
  239. <<32,2,0,0>> = serialize(ConnAck).
  240. serialize_publish(_) ->
  241. serialize(?PUBLISH_PACKET(?QOS_0, <<"Topic">>, undefined, <<"Payload">>)),
  242. serialize(?PUBLISH_PACKET(?QOS_1, <<"Topic">>, 938, <<"Payload">>)),
  243. serialize(?PUBLISH_PACKET(?QOS_2, <<"Topic">>, 99, long_payload())).
  244. serialize_puback(_) ->
  245. serialize(?PUBACK_PACKET(?PUBACK, 10384)).
  246. serialize_pubrel(_) ->
  247. serialize(?PUBREL_PACKET(10384)).
  248. serialize_subscribe(_) ->
  249. TopicTable = [{<<"TopicQos0">>, ?QOS_0}, {<<"TopicQos1">>, ?QOS_1}, {<<"TopicQos2">>, ?QOS_2}],
  250. serialize(?SUBSCRIBE_PACKET(10, TopicTable)).
  251. serialize_suback(_) ->
  252. serialize(?SUBACK_PACKET(10, [?QOS_0, ?QOS_1, 128])).
  253. serialize_unsubscribe(_) ->
  254. serialize(?UNSUBSCRIBE_PACKET(10, [<<"Topic1">>, <<"Topic2">>])).
  255. serialize_unsuback(_) ->
  256. serialize(?UNSUBACK_PACKET(10)).
  257. serialize_pingreq(_) ->
  258. serialize(?PACKET(?PINGREQ)).
  259. serialize_pingresp(_) ->
  260. serialize(?PACKET(?PINGRESP)).
  261. serialize_disconnect(_) ->
  262. serialize(?PACKET(?DISCONNECT)).
  263. long_payload() ->
  264. iolist_to_binary(["payload." || _I <- lists:seq(1, 100)]).
  265. %%--------------------------------------------------------------------
  266. %% Packet Cases
  267. %%--------------------------------------------------------------------
  268. packet_proto_name(_) ->
  269. <<"MQIsdp">> = emqttd_packet:protocol_name(3),
  270. <<"MQTT">> = emqttd_packet:protocol_name(4).
  271. packet_type_name(_) ->
  272. 'CONNECT' = emqttd_packet:type_name(?CONNECT),
  273. 'UNSUBSCRIBE' = emqttd_packet:type_name(?UNSUBSCRIBE).
  274. packet_connack_name(_) ->
  275. 'CONNACK_ACCEPT' = emqttd_packet:connack_name(?CONNACK_ACCEPT),
  276. 'CONNACK_PROTO_VER' = emqttd_packet:connack_name(?CONNACK_PROTO_VER),
  277. 'CONNACK_INVALID_ID' = emqttd_packet:connack_name(?CONNACK_INVALID_ID),
  278. 'CONNACK_SERVER' = emqttd_packet:connack_name(?CONNACK_SERVER),
  279. 'CONNACK_CREDENTIALS' = emqttd_packet:connack_name(?CONNACK_CREDENTIALS),
  280. 'CONNACK_AUTH' = emqttd_packet:connack_name(?CONNACK_AUTH).
  281. packet_format(_) ->
  282. io:format("~s", [emqttd_packet:format(?CONNECT_PACKET(#mqtt_packet_connect{}))]),
  283. io:format("~s", [emqttd_packet:format(?CONNACK_PACKET(?CONNACK_SERVER))]),
  284. io:format("~s", [emqttd_packet:format(?PUBLISH_PACKET(?QOS_1, 1))]),
  285. io:format("~s", [emqttd_packet:format(?PUBLISH_PACKET(?QOS_2, <<"topic">>, 10, <<"payload">>))]),
  286. io:format("~s", [emqttd_packet:format(?PUBACK_PACKET(?PUBACK, 98))]),
  287. io:format("~s", [emqttd_packet:format(?PUBREL_PACKET(99))]),
  288. io:format("~s", [emqttd_packet:format(?SUBSCRIBE_PACKET(15, [{<<"topic">>, ?QOS0}, {<<"topic1">>, ?QOS1}]))]),
  289. io:format("~s", [emqttd_packet:format(?SUBACK_PACKET(40, [?QOS0, ?QOS1]))]),
  290. io:format("~s", [emqttd_packet:format(?UNSUBSCRIBE_PACKET(89, [<<"t">>, <<"t2">>]))]),
  291. io:format("~s", [emqttd_packet:format(?UNSUBACK_PACKET(90))]).
  292. %%--------------------------------------------------------------------
  293. %% Message Cases
  294. %%--------------------------------------------------------------------
  295. message_make(_) ->
  296. Msg = emqttd_message:make(<<"clientid">>, <<"topic">>, <<"payload">>),
  297. 0 = Msg#mqtt_message.qos,
  298. Msg1 = emqttd_message:make(<<"clientid">>, qos2, <<"topic">>, <<"payload">>),
  299. true = is_binary(Msg1#mqtt_message.id),
  300. 2 = Msg1#mqtt_message.qos.
  301. message_from_packet(_) ->
  302. Msg = emqttd_message:from_packet(?PUBLISH_PACKET(1, <<"topic">>, 10, <<"payload">>)),
  303. 1 = Msg#mqtt_message.qos,
  304. 10 = Msg#mqtt_message.pktid,
  305. <<"topic">> = Msg#mqtt_message.topic,
  306. WillMsg = emqttd_message:from_packet(#mqtt_packet_connect{will_flag = true,
  307. will_topic = <<"WillTopic">>,
  308. will_msg = <<"WillMsg">>}),
  309. <<"WillTopic">> = WillMsg#mqtt_message.topic,
  310. <<"WillMsg">> = WillMsg#mqtt_message.payload,
  311. Msg2 = emqttd_message:from_packet(<<"username">>, <<"clientid">>,
  312. ?PUBLISH_PACKET(1, <<"topic">>, 20, <<"payload">>)),
  313. {<<"clientid">>, <<"username">>} = Msg2#mqtt_message.from,
  314. io:format("~s", [emqttd_message:format(Msg2)]).
  315. message_flag(_) ->
  316. Pkt = ?PUBLISH_PACKET(1, <<"t">>, 2, <<"payload">>),
  317. Msg2 = emqttd_message:from_packet(<<"clientid">>, Pkt),
  318. Msg3 = emqttd_message:set_flag(retain, Msg2),
  319. Msg4 = emqttd_message:set_flag(dup, Msg3),
  320. true = Msg4#mqtt_message.dup,
  321. true = Msg4#mqtt_message.retain,
  322. Msg5 = emqttd_message:set_flag(Msg4),
  323. Msg6 = emqttd_message:unset_flag(dup, Msg5),
  324. Msg7 = emqttd_message:unset_flag(retain, Msg6),
  325. false = Msg7#mqtt_message.dup,
  326. false = Msg7#mqtt_message.retain,
  327. emqttd_message:unset_flag(Msg7),
  328. emqttd_message:to_packet(Msg7).