prop_emqx_reason_codes.erl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 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_reason_codes).
  17. -include_lib("emqx/include/emqx_mqtt.hrl").
  18. -include_lib("proper/include/proper.hrl").
  19. %%--------------------------------------------------------------------
  20. %% Properties
  21. %%--------------------------------------------------------------------
  22. prop_name_text() ->
  23. ?FORALL(UnionArgs, union_args(),
  24. is_atom(apply_fun(name, UnionArgs)) andalso
  25. is_binary(apply_fun(text, UnionArgs))).
  26. prop_compat() ->
  27. ?FORALL(CompatArgs, compat_args(),
  28. begin
  29. Result = apply_fun(compat, CompatArgs),
  30. is_number(Result) orelse Result =:= undefined
  31. end).
  32. prop_connack_error() ->
  33. ?FORALL(CONNACK_ERROR_ARGS, connack_error_args(),
  34. is_integer(apply_fun(connack_error, CONNACK_ERROR_ARGS))).
  35. %%--------------------------------------------------------------------
  36. %% Helper
  37. %%--------------------------------------------------------------------
  38. apply_fun(Fun, Args) ->
  39. apply(emqx_reason_codes, Fun, Args).
  40. %%--------------------------------------------------------------------
  41. %% Generator
  42. %%--------------------------------------------------------------------
  43. union_args() ->
  44. frequency([{6, [real_mqttv3_rc(), mqttv3_version()]},
  45. {43, [real_mqttv5_rc(), mqttv5_version()]}]).
  46. compat_args() ->
  47. frequency([{18, [connack, compat_rc()]},
  48. {2, [suback, compat_rc()]},
  49. {1, [unsuback, compat_rc()]}]).
  50. connack_error_args() ->
  51. [frequency([{10, connack_error()},
  52. {1, unexpected_connack_error()}])].
  53. connack_error() ->
  54. oneof([client_identifier_not_valid,
  55. bad_username_or_password,
  56. bad_clientid_or_password,
  57. username_or_password_undefined,
  58. password_error,
  59. not_authorized,
  60. server_unavailable,
  61. server_busy,
  62. banned,
  63. bad_authentication_method]).
  64. unexpected_connack_error() ->
  65. oneof([who_knows]).
  66. real_mqttv3_rc() ->
  67. frequency([{6, mqttv3_rc()},
  68. {1, unexpected_rc()}]).
  69. real_mqttv5_rc() ->
  70. frequency([{43, mqttv5_rc()},
  71. {2, unexpected_rc()}]).
  72. compat_rc() ->
  73. frequency([{95, ?SUCHTHAT(RC , mqttv5_rc(), RC >= 16#80 orelse RC =< 2)},
  74. {5, unexpected_rc()}]).
  75. mqttv3_rc() ->
  76. oneof(mqttv3_rcs()).
  77. mqttv5_rc() ->
  78. oneof(mqttv5_rcs()).
  79. unexpected_rc() ->
  80. oneof(unexpected_rcs()).
  81. mqttv3_rcs() ->
  82. [0, 1, 2, 3, 4, 5].
  83. mqttv5_rcs() ->
  84. [16#00, 16#01, 16#02, 16#04, 16#10, 16#11, 16#18, 16#19,
  85. 16#80, 16#81, 16#82, 16#83, 16#84, 16#85, 16#86, 16#87,
  86. 16#88, 16#89, 16#8A, 16#8B, 16#8C, 16#8D, 16#8E, 16#8F,
  87. 16#90, 16#91, 16#92, 16#93, 16#94, 16#95, 16#96, 16#97,
  88. 16#98, 16#99, 16#9A, 16#9B, 16#9C, 16#9D, 16#9E, 16#9F,
  89. 16#A0, 16#A1, 16#A2].
  90. unexpected_rcs() ->
  91. ReasonCodes = mqttv3_rcs() ++ mqttv5_rcs(),
  92. Unexpected = lists:seq(0, 16#FF) -- ReasonCodes,
  93. lists:sublist(Unexpected, 5).
  94. mqttv5_version() ->
  95. ?MQTT_PROTO_V5.
  96. mqttv3_version() ->
  97. oneof([?MQTT_PROTO_V3, ?MQTT_PROTO_V4]).