diff --git a/apps/emqx/test/emqx_misc_SUITE.erl b/apps/emqx/test/emqx_misc_SUITE.erl index c3580545a..2ab5ee3e0 100644 --- a/apps/emqx/test/emqx_misc_SUITE.erl +++ b/apps/emqx/test/emqx_misc_SUITE.erl @@ -147,3 +147,7 @@ t_now_to_secs(_) -> t_now_to_ms(_) -> ?assert(is_integer(emqx_misc:now_to_ms(os:timestamp()))). +t_gen_id(_) -> + ?assertEqual(10, length(emqx_misc:gen_id(10))), + ?assertEqual(20, length(emqx_misc:gen_id(20))). + diff --git a/apps/emqx_authz/src/emqx_authz_api_sources.erl b/apps/emqx_authz/src/emqx_authz_api_sources.erl index a94a0e6c4..1586b1c88 100644 --- a/apps/emqx_authz/src/emqx_authz_api_sources.erl +++ b/apps/emqx_authz/src/emqx_authz_api_sources.erl @@ -450,21 +450,21 @@ write_cert(#{<<"ssl">> := #{<<"enable">> := true} = SSL} = Source) -> CertPath = filename:join([emqx:get_config([node, data_dir]), "certs"]), CaCert = case maps:is_key(<<"cacertfile">>, SSL) of true -> - {ok, CaCertFile} = write_file(filename:join([CertPath, "cacert-" ++ emqx_plugin_libs_id:gen() ++".pem"]), + {ok, CaCertFile} = write_file(filename:join([CertPath, "cacert-" ++ emqx_misc:gen_id() ++".pem"]), maps:get(<<"cacertfile">>, SSL)), CaCertFile; false -> "" end, Cert = case maps:is_key(<<"certfile">>, SSL) of true -> - {ok, CertFile} = write_file(filename:join([CertPath, "cert-" ++ emqx_plugin_libs_id:gen() ++".pem"]), + {ok, CertFile} = write_file(filename:join([CertPath, "cert-" ++ emqx_misc:gen_id() ++".pem"]), maps:get(<<"certfile">>, SSL)), CertFile; false -> "" end, Key = case maps:is_key(<<"keyfile">>, SSL) of true -> - {ok, KeyFile} = write_file(filename:join([CertPath, "key-" ++ emqx_plugin_libs_id:gen() ++".pem"]), + {ok, KeyFile} = write_file(filename:join([CertPath, "key-" ++ emqx_misc:gen_id() ++".pem"]), maps:get(<<"keyfile">>, SSL)), KeyFile; false -> "" diff --git a/apps/emqx_connector/src/emqx_connector_mqtt.erl b/apps/emqx_connector/src/emqx_connector_mqtt.erl index 914d26d94..125b5cbe4 100644 --- a/apps/emqx_connector/src/emqx_connector_mqtt.erl +++ b/apps/emqx_connector/src/emqx_connector_mqtt.erl @@ -214,7 +214,7 @@ bridge_name(Prefix, Id) -> list_to_atom(str(Prefix) ++ ":" ++ str(Id)). clientid(Id) -> - list_to_binary(str(Id) ++ ":" ++ emqx_plugin_libs_id:gen(16)). + list_to_binary(str(Id) ++ ":" ++ emqx_misc:gen_id(16)). str(A) when is_atom(A) -> atom_to_list(A); diff --git a/apps/emqx_plugin_libs/src/emqx_plugin_libs_id.erl b/apps/emqx_plugin_libs/src/emqx_plugin_libs_id.erl deleted file mode 100644 index ef5fb08c3..000000000 --- a/apps/emqx_plugin_libs/src/emqx_plugin_libs_id.erl +++ /dev/null @@ -1,57 +0,0 @@ -%%-------------------------------------------------------------------- -%% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. All Rights Reserved. -%% -%% Licensed under the Apache License, Version 2.0 (the "License"); -%% you may not use this file except in compliance with the License. -%% You may obtain a copy of the License at -%% -%% http://www.apache.org/licenses/LICENSE-2.0 -%% -%% Unless required by applicable law or agreed to in writing, software -%% distributed under the License is distributed on an "AS IS" BASIS, -%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -%% See the License for the specific language governing permissions and -%% limitations under the License. -%%-------------------------------------------------------------------- - --module(emqx_plugin_libs_id). - --export([gen/0, gen/1]). - --define(SHORT, 8). - -%%-------------------------------------------------------------------- -%% APIs -%%-------------------------------------------------------------------- --spec(gen() -> list()). -gen() -> - gen(?SHORT). - --spec(gen(integer()) -> list()). -gen(Len) -> - BitLen = Len * 4, - <> = crypto:strong_rand_bytes(Len div 2), - int_to_hex(R, Len). - -%%------------------------------------------------------------------------------ -%% Internal Functions -%%------------------------------------------------------------------------------ - -int_to_hex(I, N) when is_integer(I), I >= 0 -> - int_to_hex([], I, 1, N). - -int_to_hex(L, I, Count, N) - when I < 16 -> - pad([int_to_hex(I) | L], N - Count); -int_to_hex(L, I, Count, N) -> - int_to_hex([int_to_hex(I rem 16) | L], I div 16, Count + 1, N). - -int_to_hex(I) when 0 =< I, I =< 9 -> - I + $0; -int_to_hex(I) when 10 =< I, I =< 15 -> - (I - 10) + $a. - -pad(L, 0) -> - L; -pad(L, Count) -> - pad([$0 | L], Count - 1). diff --git a/apps/emqx_plugin_libs/test/emqx_plugin_libs_id_SUITE.erl b/apps/emqx_plugin_libs/test/emqx_plugin_libs_id_SUITE.erl deleted file mode 100644 index d13144ed3..000000000 --- a/apps/emqx_plugin_libs/test/emqx_plugin_libs_id_SUITE.erl +++ /dev/null @@ -1,28 +0,0 @@ -%%-------------------------------------------------------------------- -%% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. All Rights Reserved. -%% -%% Licensed under the Apache License, Version 2.0 (the "License"); -%% you may not use this file except in compliance with the License. -%% You may obtain a copy of the License at -%% -%% http://www.apache.org/licenses/LICENSE-2.0 -%% -%% Unless required by applicable law or agreed to in writing, software -%% distributed under the License is distributed on an "AS IS" BASIS, -%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -%% See the License for the specific language governing permissions and -%% limitations under the License. -%%-------------------------------------------------------------------- - --module(emqx_plugin_libs_id_SUITE). - --compile(export_all). --compile(nowarn_export_all). - --include_lib("eunit/include/eunit.hrl"). - -all() -> emqx_ct:all(?MODULE). - -t_gen(_) -> - ?assertEqual(10, length(emqx_plugin_libs_id:gen(10))), - ?assertEqual(20, length(emqx_plugin_libs_id:gen(20))). diff --git a/apps/emqx_rule_engine/src/emqx_rule_engine.erl b/apps/emqx_rule_engine/src/emqx_rule_engine.erl index 24cfecb03..bf0eb06e8 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_engine.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_engine.erl @@ -507,7 +507,7 @@ rule_id() -> gen_id("rule:", fun emqx_rule_registry:get_rule/1). gen_id(Prefix, TestFun) -> - Id = iolist_to_binary([Prefix, emqx_plugin_libs_id:gen()]), + Id = iolist_to_binary([Prefix, emqx_misc:gen_id()]), case TestFun(Id) of not_found -> Id; _Res -> gen_id(Prefix, TestFun) diff --git a/apps/emqx_rule_engine/src/emqx_rule_sqltester.erl b/apps/emqx_rule_engine/src/emqx_rule_sqltester.erl index 3076f414c..2f1edbeb2 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_sqltester.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_sqltester.erl @@ -48,8 +48,8 @@ test(#{<<"rawsql">> := Sql, <<"ctx">> := Context}) -> end. test_rule(Sql, Select, Context, EventTopics) -> - RuleId = iolist_to_binary(["test_rule", emqx_plugin_libs_id:gen()]), - ActInstId = iolist_to_binary(["test_action", emqx_plugin_libs_id:gen()]), + RuleId = iolist_to_binary(["test_rule", emqx_misc:gen_id()]), + ActInstId = iolist_to_binary(["test_action", emqx_misc:gen_id()]), ok = emqx_rule_metrics:create_rule_metrics(RuleId), ok = emqx_rule_metrics:create_metrics(ActInstId), Rule = #rule{