emqx_reason_codes_tests.erl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. %% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved.
  2. %%
  3. %% Licensed under the Apache License, Version 2.0 (the "License")
  4. %% you may not use this file except in compliance with the License.
  5. %% You may obtain a copy of the License at
  6. %%
  7. %% http://www.apache.org/licenses/LICENSE-2.0
  8. %%
  9. %% Unless required by applicable law or agreed to in writing, software
  10. %% distributed under the License is distributed on an "AS IS" BASIS,
  11. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. %% See the License for the specific language governing permissions and
  13. %% limitations under the License.
  14. -module(emqx_reason_codes_tests).
  15. -include_lib("eunit/include/eunit.hrl").
  16. -include("emqx_mqtt.hrl").
  17. -import(lists, [seq/2, zip/2, foreach/2]).
  18. -define(MQTTV4_CODE_NAMES, [connection_acceptd,
  19. unacceptable_protocol_version,
  20. client_identifier_not_valid,
  21. server_unavaliable,
  22. malformed_username_or_password,
  23. unauthorized_client,
  24. unknown_error]).
  25. -define(MQTTV5_CODE_NAMES, [success, granted_qos1, granted_qos2, disconnect_with_will_message,
  26. no_matching_subscribers, no_subscription_existed, continue_authentication,
  27. re_authenticate, unspecified_error, malformed_Packet, protocol_error,
  28. implementation_specific_error, unsupported_protocol_version,
  29. client_identifier_not_valid, bad_username_or_password, not_authorized,
  30. server_unavailable, server_busy, banned,server_shutting_down,
  31. bad_authentication_method, keepalive_timeout, session_taken_over,
  32. topic_filter_invalid, topic_name_invalid, packet_identifier_inuse,
  33. packet_identifier_not_found, receive_maximum_exceeded, topic_alias_invalid,
  34. packet_too_large, message_rate_too_high, quota_exceeded,
  35. administrative_action, payload_format_invalid, retain_not_supported,
  36. qos_not_supported, use_another_server, server_moved,
  37. shared_subscriptions_not_supported, connection_rate_exceeded,
  38. maximum_connect_time, subscription_identifiers_not_supported,
  39. wildcard_subscriptions_not_supported, unknown_error]).
  40. -define(MQTTV5_CODES, [16#00, 16#01, 16#02, 16#04, 16#10, 16#11, 16#18, 16#19, 16#80, 16#81, 16#82,
  41. 16#83, 16#84, 16#85, 16#86, 16#87, 16#88, 16#89, 16#8A, 16#8B, 16#8C, 16#8D,
  42. 16#8E, 16#8F, 16#90, 16#91, 16#92, 16#93, 16#94, 16#95, 16#96, 16#97, 16#98,
  43. 16#99, 16#9A, 16#9B, 16#9C, 16#9D, 16#9E, 16#9F, 16#A0, 16#A1, 16#A2, code]).
  44. -define(MQTTV5_TXT, [<<"Success">>, <<"Granted QoS 1">>, <<"Granted QoS 2">>,
  45. <<"Disconnect with Will Message">>, <<"No matching subscribers">>,
  46. <<"No subscription existed">>, <<"Continue authentication">>,
  47. <<"Re-authenticate">>, <<"Unspecified error">>, <<"Malformed Packet">>,
  48. <<"Protocol Error">>, <<"Implementation specific error">>,
  49. <<"Unsupported Protocol Version">>, <<"Client Identifier not valid">>,
  50. <<"Bad User Name or Password">>, <<"Not authorized">>,
  51. <<"Server unavailable">>, <<"Server busy">>, <<"Banned">>,
  52. <<"Server shutting down">>, <<"Bad authentication method">>,
  53. <<"Keep Alive timeout">>, <<"Session taken over">>,
  54. <<"Topic Filter invalid">>, <<"Topic Name invalid">>,
  55. <<"Packet Identifier in use">>, <<"Packet Identifier not found">>,
  56. <<"Receive Maximum exceeded">>, <<"Topic Alias invalid">>,
  57. <<"Packet too large">>, <<"Message rate too high">>, <<"Quota exceeded">>,
  58. <<"Administrative action">>, <<"Payload format invalid">>,
  59. <<"Retain not supported">>, <<"QoS not supported">>,
  60. <<"Use another server">>, <<"Server moved">>,
  61. <<"Shared Subscriptions not supported">>, <<"Connection rate exceeded">>,
  62. <<"Maximum connect time">>, <<"Subscription Identifiers not supported">>,
  63. <<"Wildcard Subscriptions not supported">>, <<"Unknown error">>]).
  64. -define(COMPAT_CODES_V5, [16#80, 16#81, 16#82, 16#83, 16#84, 16#85, 16#86, 16#87,
  65. 16#88, 16#89, 16#8A, 16#8B, 16#8C, 16#97, 16#9C, 16#9D,
  66. 16#9F]).
  67. -define(COMPAT_CODES_V4, [?CONNACK_PROTO_VER, ?CONNACK_PROTO_VER, ?CONNACK_PROTO_VER,
  68. ?CONNACK_PROTO_VER, ?CONNACK_PROTO_VER,
  69. ?CONNACK_INVALID_ID,
  70. ?CONNACK_CREDENTIALS,
  71. ?CONNACK_AUTH,
  72. ?CONNACK_SERVER,
  73. ?CONNACK_SERVER,
  74. ?CONNACK_AUTH,
  75. ?CONNACK_SERVER,
  76. ?CONNACK_AUTH,
  77. ?CONNACK_SERVER, ?CONNACK_SERVER, ?CONNACK_SERVER, ?CONNACK_SERVER]).
  78. mqttv4_name_test() ->
  79. (((codes_test(?MQTT_PROTO_V4))
  80. (seq(0,6)))
  81. (?MQTTV4_CODE_NAMES))
  82. (fun emqx_reason_codes:name/2).
  83. mqttv5_name_test() ->
  84. (((codes_test(?MQTT_PROTO_V5))
  85. (?MQTTV5_CODES))
  86. (?MQTTV5_CODE_NAMES))
  87. (fun emqx_reason_codes:name/2).
  88. text_test() ->
  89. (((codes_test(?MQTT_PROTO_V5))
  90. (?MQTTV5_CODES))
  91. (?MQTTV5_TXT))
  92. (fun emqx_reason_codes:text/1).
  93. compat_test() ->
  94. (((codes_test(connack))
  95. (?COMPAT_CODES_V5))
  96. (?COMPAT_CODES_V4))
  97. (fun emqx_reason_codes:compat/2),
  98. (((codes_test(suback))
  99. ([0,1,2, 16#80]))
  100. ([0,1,2, 16#80]))
  101. (fun emqx_reason_codes:compat/2),
  102. (((codes_test(unsuback))
  103. ([0, 1, 2]))
  104. ([undefined, undefined, undefined]))
  105. (fun emqx_reason_codes:compat/2).
  106. codes_test(AsistVar) ->
  107. fun(CODES) ->
  108. fun(NAMES) ->
  109. fun(Procedure) ->
  110. foreach(fun({Code, Result}) ->
  111. ?assertEqual(Result, case erlang:fun_info(Procedure, name) of
  112. {name, text} -> Procedure(Code);
  113. {name, name} -> Procedure(Code, AsistVar);
  114. {name, compat} -> Procedure(AsistVar, Code)
  115. end)
  116. end, zip(CODES, NAMES))
  117. end
  118. end
  119. end.