emqx_gateway_test_utils.erl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-2024 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(emqx_gateway_test_utils).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. assert_confs(Expected0, Effected) ->
  21. Expected = maybe_unconvert_listeners(Expected0),
  22. case do_assert_confs(root, Expected, Effected) of
  23. false ->
  24. io:format(
  25. standard_error,
  26. "Expected config: ~p,\n"
  27. "Effected config: ~p",
  28. [Expected, Effected]
  29. ),
  30. exit(conf_not_match);
  31. true ->
  32. ok
  33. end.
  34. do_assert_confs(_Key, Expected, Effected) when
  35. is_map(Expected),
  36. is_map(Effected)
  37. ->
  38. Ks1 = maps:keys(Expected),
  39. lists:all(
  40. fun(K) ->
  41. do_assert_confs(
  42. K,
  43. maps:get(K, Expected),
  44. maps:get(K, Effected, undefined)
  45. )
  46. end,
  47. Ks1
  48. );
  49. do_assert_confs(Key, Expected, Effected) when
  50. Key == cacertfile;
  51. Key == <<"cacertfile">>;
  52. Key == certfile;
  53. Key == <<"certfile">>;
  54. Key == keyfile;
  55. Key == <<"keyfile">>
  56. ->
  57. case Expected == Effected of
  58. true ->
  59. true;
  60. false ->
  61. case file:read_file(Effected) of
  62. {ok, Content} -> Expected == Content;
  63. _ -> false
  64. end
  65. end;
  66. do_assert_confs(Key, [Expected | More1], [Effected | More2]) ->
  67. do_assert_confs(Key, Expected, Effected) andalso
  68. do_assert_confs(Key, More1, More2);
  69. do_assert_confs(_Key, [], []) ->
  70. true;
  71. do_assert_confs(Key, Expected, Effected) ->
  72. Res = Expected =:= Effected,
  73. Res == false andalso
  74. ct:pal(
  75. "Errors: ~p value not match, "
  76. "expected: ~p, got: ~p~n",
  77. [Key, Expected, Effected]
  78. ),
  79. Res.
  80. maybe_unconvert_listeners(Conf) when is_map(Conf) ->
  81. case maps:take(<<"listeners">>, Conf) of
  82. {Ls, Conf1} when is_list(Ls) ->
  83. Conf1#{
  84. <<"listeners">> =>
  85. emqx_gateway_conf:unconvert_listeners(Ls)
  86. };
  87. _ ->
  88. Conf
  89. end;
  90. maybe_unconvert_listeners(Conf) ->
  91. Conf.
  92. assert_fields_exist(Ks, Map) ->
  93. lists:foreach(
  94. fun(K) ->
  95. _ = maps:get(K, Map)
  96. end,
  97. Ks
  98. ).
  99. load_all_gateway_apps() ->
  100. emqx_cth_suite:load_apps(all_gateway_apps()).
  101. all_gateway_apps() ->
  102. [
  103. emqx_gateway_stomp,
  104. emqx_gateway_mqttsn,
  105. emqx_gateway_coap,
  106. emqx_gateway_lwm2m,
  107. emqx_gateway_exproto
  108. ].
  109. %%--------------------------------------------------------------------
  110. %% http
  111. -define(http_api_host, "http://127.0.0.1:18083/api/v5").
  112. request(delete = Mth, Path) ->
  113. do_request(Mth, req(Path, []));
  114. request(get = Mth, Path) ->
  115. do_request(Mth, req(Path, [])).
  116. request(get = Mth, Path, Qs) ->
  117. do_request(Mth, req(Path, Qs));
  118. request(put = Mth, Path, Body) ->
  119. do_request(Mth, req(Path, [], Body));
  120. request(post = Mth, Path, Body) ->
  121. do_request(Mth, req(Path, [], Body)).
  122. %%--------------------------------------------------------------------
  123. %% default pems
  124. ssl_server_opts() ->
  125. #{
  126. cacertfile => file_content(cert_path("cacert.pem")),
  127. certfile => file_content(cert_path("cert.pem")),
  128. keyfile => file_content(cert_path("key.pem"))
  129. }.
  130. ssl_client_opts() ->
  131. #{
  132. cacertfile => file_content(cert_path("cacert.pem")),
  133. certfile => file_content(cert_path("client-cert.pem")),
  134. keyfile => file_content(cert_path("client-key.pem"))
  135. }.
  136. cert_path(Name) ->
  137. filename:join(["../../lib/emqx/etc/certs/", Name]).
  138. file_content(Filename) ->
  139. case file:read_file(Filename) of
  140. {ok, Bin} -> Bin;
  141. Err -> error(Err)
  142. end.
  143. do_request(Mth, Req) ->
  144. case httpc:request(Mth, Req, [], [{body_format, binary}]) of
  145. {ok, {{_Vsn, Code, _Text}, _, Resp}} ->
  146. NResp =
  147. case Resp of
  148. <<>> ->
  149. #{};
  150. _ ->
  151. emqx_utils_maps:unsafe_atom_key_map(
  152. emqx_utils_json:decode(Resp, [return_maps])
  153. )
  154. end,
  155. {Code, NResp};
  156. {error, Reason} ->
  157. error({failed_to_request, Reason})
  158. end.
  159. req(Path, Qs) ->
  160. {url(Path, Qs), auth([])}.
  161. req(Path, Qs, Body) ->
  162. {url(Path, Qs), auth([]), "application/json", emqx_utils_json:encode(Body)}.
  163. url(Path, []) ->
  164. lists:concat([?http_api_host, Path]);
  165. url(Path, Qs) ->
  166. lists:concat([?http_api_host, Path, "?", binary_to_list(cow_qs:qs(Qs))]).
  167. auth(Headers) ->
  168. [emqx_mgmt_api_test_util:auth_header_() | Headers].
  169. sn_client_connect(ClientId) ->
  170. {ok, Socket} = gen_udp:open(0, [binary]),
  171. _ = emqx_sn_protocol_SUITE:send_connect_msg(Socket, ClientId),
  172. ?assertEqual(
  173. <<3, 16#05, 0>>,
  174. emqx_sn_protocol_SUITE:receive_response(Socket)
  175. ),
  176. Socket.
  177. sn_client_disconnect(Socket) ->
  178. _ = emqx_sn_protocol_SUITE:send_disconnect_msg(Socket, undefined),
  179. gen_udp:close(Socket),
  180. ok.