emqttd_serializer_tests.erl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_serializer tests.
  24. %%%
  25. %%% @end
  26. %%%-----------------------------------------------------------------------------
  27. -module(emqttd_serializer_tests).
  28. -ifdef(TEST).
  29. -include("emqttd_protocol.hrl").
  30. -include_lib("eunit/include/eunit.hrl").
  31. -import(emqttd_serializer, [serialize/1]).
  32. serialize_connect_test() ->
  33. serialize(?CONNECT_PACKET(#mqtt_packet_connect{})).
  34. serialize_connack_test() ->
  35. ConnAck = #mqtt_packet{header = #mqtt_packet_header{type = ?CONNACK},
  36. variable = #mqtt_packet_connack{ack_flags = 0, return_code = 0}},
  37. ?assertEqual(<<32,2,0,0>>, serialize(ConnAck)).
  38. serialize_publish_test() ->
  39. serialize(?PUBLISH_PACKET(?QOS_0, <<"Topic">>, undefined, <<"Payload">>)),
  40. serialize(?PUBLISH_PACKET(?QOS_1, <<"Topic">>, 938, <<"Payload">>)).
  41. serialize_puback_test() ->
  42. serialize(?PUBACK_PACKET(?PUBACK, 10384)).
  43. serialize_pubrel_test() ->
  44. serialize(?PUBREL_PACKET(10384)).
  45. serialize_subscribe_test() ->
  46. TopicTable = [{<<"TopicQos0">>, ?QOS_0}, {<<"TopicQos1">>, ?QOS_1}, {<<"TopicQos2">>, ?QOS_2}],
  47. serialize(?SUBSCRIBE_PACKET(10, TopicTable)).
  48. serialize_suback_test() ->
  49. serialize(?SUBACK_PACKET(10, [?QOS_0, ?QOS_1, 128])).
  50. serialize_unsubscribe_test() ->
  51. serialize(?UNSUBSCRIBE_PACKET(10, [<<"Topic1">>, <<"Topic2">>])).
  52. serialize_unsuback_test() ->
  53. serialize(?UNSUBACK_PACKET(10)).
  54. serialize_pingreq_test() ->
  55. serialize(?PACKET(?PINGREQ)).
  56. serialize_pingresp_test() ->
  57. serialize(?PACKET(?PINGRESP)).
  58. serialize_disconnect_test() ->
  59. serialize(?PACKET(?DISCONNECT)).
  60. -endif.