emqttd_parser_tests.erl 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. %%%-----------------------------------------------------------------------------
  2. %%% @Copyright (C) 2012-2015, Feng Lee <feng@emqtt.io>
  3. %%%
  4. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  5. %%% of this software and associated documentation files (the "Software"), to deal
  6. %%% in the Software without restriction, including without limitation the rights
  7. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. %%% copies of the Software, and to permit persons to whom the Software is
  9. %%% furnished to do so, subject to the following conditions:
  10. %%%
  11. %%% The above copyright notice and this permission notice shall be included in all
  12. %%% copies or substantial portions of the Software.
  13. %%%
  14. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. %%% SOFTWARE.
  21. %%%-----------------------------------------------------------------------------
  22. %%% @doc
  23. %%% emqttd_parser tests.
  24. %%%
  25. %%% @end
  26. %%%-----------------------------------------------------------------------------
  27. -module(emqttd_parser_tests).
  28. -ifdef(TEST).
  29. -include("emqttd_protocol.hrl").
  30. -include_lib("eunit/include/eunit.hrl").
  31. parse_connect_test() ->
  32. Parser = emqttd_parser:new([]),
  33. %% CONNECT(Qos=0, Retain=false, Dup=false, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
  34. 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>>,
  35. ?assertMatch({ok, #mqtt_packet{
  36. header = #mqtt_packet_header{type = ?CONNECT,
  37. dup = false,
  38. qos = 0,
  39. retain = false},
  40. variable = #mqtt_packet_connect{proto_ver = 3,
  41. proto_name = <<"MQIsdp">>,
  42. client_id = <<"mosqpub/10451-iMac.loca">>,
  43. clean_sess = true,
  44. keep_alive = 60}}, <<>>}, Parser(V31ConnBin)),
  45. %% CONNECT(Qos=0, Retain=false, Dup=false, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
  46. 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>>,
  47. ?assertMatch({ok, #mqtt_packet{
  48. header = #mqtt_packet_header{type = ?CONNECT,
  49. dup = false,
  50. qos = 0,
  51. retain = false},
  52. variable = #mqtt_packet_connect{proto_ver = 4,
  53. proto_name = <<"MQTT">>,
  54. client_id = <<"mosqpub/10451-iMac.loca">>,
  55. clean_sess = true,
  56. keep_alive = 60 } }, <<>>}, Parser(V311ConnBin)),
  57. %% CONNECT(Qos=0, Retain=false, Dup=false, ClientId="", ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60)
  58. V311ConnWithoutClientId = <<16,12,0,4,77,81,84,84,4,2,0,60,0,0>>,
  59. ?assertMatch({ok, #mqtt_packet{
  60. header = #mqtt_packet_header{type = ?CONNECT,
  61. dup = false,
  62. qos = 0,
  63. retain = false},
  64. variable = #mqtt_packet_connect{proto_ver = 4,
  65. proto_name = <<"MQTT">>,
  66. client_id = <<>>,
  67. clean_sess = true,
  68. keep_alive = 60 } }, <<>>}, Parser(V311ConnWithoutClientId)),
  69. %%CONNECT(Qos=0, Retain=false, Dup=false, ClientId=mosqpub/10452-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60, Username=test, Password=******, Will(Qos=1, Retain=false, Topic=/will, Msg=willmsg))
  70. 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>>,
  71. ?assertMatch({ok, #mqtt_packet{
  72. header = #mqtt_packet_header{type = ?CONNECT,
  73. dup = false,
  74. qos = 0,
  75. retain = false},
  76. variable = #mqtt_packet_connect{proto_ver = 3,
  77. proto_name = <<"MQIsdp">>,
  78. client_id = <<"mosqpub/10452-iMac.loca">>,
  79. clean_sess = true,
  80. keep_alive = 60,
  81. will_retain = false,
  82. will_qos = 1,
  83. will_flag = true,
  84. will_topic = <<"/will">>,
  85. will_msg = <<"willmsg">> ,
  86. username = <<"test">>,
  87. password = <<"public">>}},
  88. <<>> }, Parser(ConnBinWithWill)),
  89. ok.
  90. parse_publish_test() ->
  91. Parser = emqttd_parser:new([]),
  92. %%PUBLISH(Qos=1, Retain=false, Dup=false, TopicName=a/b/c, PacketId=1, Payload=<<"hahah">>)
  93. PubBin = <<50,14,0,5,97,47,98,47,99,0,1,104,97,104,97,104>>,
  94. ?assertMatch({ok, #mqtt_packet{
  95. header = #mqtt_packet_header{type = ?PUBLISH,
  96. dup = false,
  97. qos = 1,
  98. retain = false},
  99. variable = #mqtt_packet_publish{topic_name = <<"a/b/c">>,
  100. packet_id = 1},
  101. payload = <<"hahah">> }, <<>>}, Parser(PubBin)),
  102. %PUBLISH(Qos=0, Retain=false, Dup=false, TopicName=xxx/yyy, PacketId=undefined, Payload=<<"hello">>)
  103. %DISCONNECT(Qos=0, Retain=false, Dup=false)
  104. PubBin1 = <<48,14,0,7,120,120,120,47,121,121,121,104,101,108,108,111,224,0>>,
  105. ?assertMatch({ok, #mqtt_packet {
  106. header = #mqtt_packet_header{type = ?PUBLISH,
  107. dup = false,
  108. qos = 0,
  109. retain = false},
  110. variable = #mqtt_packet_publish{topic_name = <<"xxx/yyy">>,
  111. packet_id = undefined},
  112. payload = <<"hello">> }, <<224,0>>}, Parser(PubBin1)),
  113. ?assertMatch({ok, #mqtt_packet{
  114. header = #mqtt_packet_header{type = ?DISCONNECT,
  115. dup = false,
  116. qos = 0,
  117. retain = false}
  118. }, <<>>}, Parser(<<224, 0>>)).
  119. parse_puback_test() ->
  120. Parser = emqttd_parser:new([]),
  121. %%PUBACK(Qos=0, Retain=false, Dup=false, PacketId=1)
  122. PubAckBin = <<64,2,0,1>>,
  123. ?assertMatch({ok, #mqtt_packet {
  124. header = #mqtt_packet_header{type = ?PUBACK,
  125. dup = false,
  126. qos = 0,
  127. retain = false }
  128. }, <<>>}, Parser(PubAckBin)),
  129. ok.
  130. parse_subscribe_test() ->
  131. ok.
  132. parse_pingreq_test() ->
  133. ok.
  134. parse_disconnect_test() ->
  135. Parser = emqttd_parser:new([]),
  136. %DISCONNECT(Qos=0, Retain=false, Dup=false)
  137. Bin = <<224, 0>>,
  138. ?assertMatch({ok, #mqtt_packet{
  139. header = #mqtt_packet_header{type = ?DISCONNECT,
  140. dup = false,
  141. qos = 0,
  142. retain = false}
  143. }, <<>>}, Parser(Bin)).
  144. -endif.