prop_emqx_frame.erl 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
  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(prop_emqx_frame).
  17. -include_lib("emqx/include/emqx_mqtt.hrl").
  18. -include_lib("proper/include/proper.hrl").
  19. %%--------------------------------------------------------------------
  20. %% Properties
  21. %%--------------------------------------------------------------------
  22. prop_serialize_parse_connect() ->
  23. ?FORALL(
  24. Opts = #{version := ProtoVer},
  25. parse_opts(),
  26. begin
  27. ProtoName = proplists:get_value(ProtoVer, ?PROTOCOL_NAMES),
  28. Packet = ?CONNECT_PACKET(#mqtt_packet_connect{
  29. proto_name = ProtoName,
  30. proto_ver = ProtoVer,
  31. clientid = <<"clientId">>,
  32. will_qos = ?QOS_1,
  33. will_flag = true,
  34. will_retain = true,
  35. will_topic = <<"will">>,
  36. will_props = #{},
  37. will_payload = <<"bye">>,
  38. clean_start = true,
  39. properties = #{}
  40. }),
  41. Packet =:= parse_serialize(Packet, Opts)
  42. end
  43. ).
  44. %%--------------------------------------------------------------------
  45. %% Helpers
  46. %%--------------------------------------------------------------------
  47. parse_serialize(Packet, Opts) when is_map(Opts) ->
  48. Ver = maps:get(version, Opts, ?MQTT_PROTO_V4),
  49. Bin = iolist_to_binary(emqx_frame:serialize(Packet, Ver)),
  50. ParseState = emqx_frame:initial_parse_state(Opts),
  51. {ok, NPacket, <<>>, _} = emqx_frame:parse(Bin, ParseState),
  52. NPacket.
  53. %%--------------------------------------------------------------------
  54. %% Generators
  55. %%--------------------------------------------------------------------
  56. parse_opts() ->
  57. ?LET(PropList, [{strict_mode, boolean()}, {version, range(4, 5)}], maps:from_list(PropList)).