emqttd_parser_tests.erl 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_parser_tests).
  17. -ifdef(TEST).
  18. -include("emqttd_protocol.hrl").
  19. -include_lib("eunit/include/eunit.hrl").
  20. parse_connect_test() ->
  21. Parser = emqttd_parser:new([]),
  22. %% CONNECT(Qos=0, Retain=false, Dup=false, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQIsdp, ProtoVsn=3, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
  23. 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>>,
  24. ?assertMatch({ok, #mqtt_packet{
  25. header = #mqtt_packet_header{type = ?CONNECT,
  26. dup = false,
  27. qos = 0,
  28. retain = false},
  29. variable = #mqtt_packet_connect{proto_ver = 3,
  30. proto_name = <<"MQIsdp">>,
  31. client_id = <<"mosqpub/10451-iMac.loca">>,
  32. clean_sess = true,
  33. keep_alive = 60}}, <<>>}, Parser(V31ConnBin)),
  34. %% CONNECT(Qos=0, Retain=false, Dup=false, ClientId=mosqpub/10451-iMac.loca, ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60, Username=undefined, Password=undefined)
  35. 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>>,
  36. ?assertMatch({ok, #mqtt_packet{
  37. header = #mqtt_packet_header{type = ?CONNECT,
  38. dup = false,
  39. qos = 0,
  40. retain = false},
  41. variable = #mqtt_packet_connect{proto_ver = 4,
  42. proto_name = <<"MQTT">>,
  43. client_id = <<"mosqpub/10451-iMac.loca">>,
  44. clean_sess = true,
  45. keep_alive = 60 } }, <<>>}, Parser(V311ConnBin)),
  46. %% CONNECT(Qos=0, Retain=false, Dup=false, ClientId="", ProtoName=MQTT, ProtoVsn=4, CleanSess=true, KeepAlive=60)
  47. V311ConnWithoutClientId = <<16,12,0,4,77,81,84,84,4,2,0,60,0,0>>,
  48. ?assertMatch({ok, #mqtt_packet{
  49. header = #mqtt_packet_header{type = ?CONNECT,
  50. dup = false,
  51. qos = 0,
  52. retain = false},
  53. variable = #mqtt_packet_connect{proto_ver = 4,
  54. proto_name = <<"MQTT">>,
  55. client_id = <<>>,
  56. clean_sess = true,
  57. keep_alive = 60 } }, <<>>}, Parser(V311ConnWithoutClientId)),
  58. %%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))
  59. 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>>,
  60. ?assertMatch({ok, #mqtt_packet{
  61. header = #mqtt_packet_header{type = ?CONNECT,
  62. dup = false,
  63. qos = 0,
  64. retain = false},
  65. variable = #mqtt_packet_connect{proto_ver = 3,
  66. proto_name = <<"MQIsdp">>,
  67. client_id = <<"mosqpub/10452-iMac.loca">>,
  68. clean_sess = true,
  69. keep_alive = 60,
  70. will_retain = false,
  71. will_qos = 1,
  72. will_flag = true,
  73. will_topic = <<"/will">>,
  74. will_msg = <<"willmsg">> ,
  75. username = <<"test">>,
  76. password = <<"public">>}},
  77. <<>> }, Parser(ConnBinWithWill)),
  78. ok.
  79. parse_publish_test() ->
  80. Parser = emqttd_parser:new([]),
  81. %%PUBLISH(Qos=1, Retain=false, Dup=false, TopicName=a/b/c, PacketId=1, Payload=<<"hahah">>)
  82. PubBin = <<50,14,0,5,97,47,98,47,99,0,1,104,97,104,97,104>>,
  83. ?assertMatch({ok, #mqtt_packet{
  84. header = #mqtt_packet_header{type = ?PUBLISH,
  85. dup = false,
  86. qos = 1,
  87. retain = false},
  88. variable = #mqtt_packet_publish{topic_name = <<"a/b/c">>,
  89. packet_id = 1},
  90. payload = <<"hahah">> }, <<>>}, Parser(PubBin)),
  91. %PUBLISH(Qos=0, Retain=false, Dup=false, TopicName=xxx/yyy, PacketId=undefined, Payload=<<"hello">>)
  92. %DISCONNECT(Qos=0, Retain=false, Dup=false)
  93. PubBin1 = <<48,14,0,7,120,120,120,47,121,121,121,104,101,108,108,111,224,0>>,
  94. ?assertMatch({ok, #mqtt_packet {
  95. header = #mqtt_packet_header{type = ?PUBLISH,
  96. dup = false,
  97. qos = 0,
  98. retain = false},
  99. variable = #mqtt_packet_publish{topic_name = <<"xxx/yyy">>,
  100. packet_id = undefined},
  101. payload = <<"hello">> }, <<224,0>>}, Parser(PubBin1)),
  102. ?assertMatch({ok, #mqtt_packet{
  103. header = #mqtt_packet_header{type = ?DISCONNECT,
  104. dup = false,
  105. qos = 0,
  106. retain = false}
  107. }, <<>>}, Parser(<<224, 0>>)).
  108. parse_puback_test() ->
  109. Parser = emqttd_parser:new([]),
  110. %%PUBACK(Qos=0, Retain=false, Dup=false, PacketId=1)
  111. PubAckBin = <<64,2,0,1>>,
  112. ?assertMatch({ok, #mqtt_packet {
  113. header = #mqtt_packet_header{type = ?PUBACK,
  114. dup = false,
  115. qos = 0,
  116. retain = false }
  117. }, <<>>}, Parser(PubAckBin)),
  118. ok.
  119. parse_subscribe_test() ->
  120. ok.
  121. parse_pingreq_test() ->
  122. ok.
  123. parse_disconnect_test() ->
  124. Parser = emqttd_parser:new([]),
  125. %DISCONNECT(Qos=0, Retain=false, Dup=false)
  126. Bin = <<224, 0>>,
  127. ?assertMatch({ok, #mqtt_packet{
  128. header = #mqtt_packet_header{type = ?DISCONNECT,
  129. dup = false,
  130. qos = 0,
  131. retain = false}
  132. }, <<>>}, Parser(Bin)).
  133. -endif.