diff --git a/src/emqx_channel.erl b/src/emqx_channel.erl index 74636c9b5..a60f148f1 100644 --- a/src/emqx_channel.erl +++ b/src/emqx_channel.erl @@ -1012,6 +1012,9 @@ maybe_username_as_clientid(_ConnPkt, ClientInfo = #{zone := Zone, username := Us false -> ok end. +maybe_assign_clientid(_ConnPkt, ClientInfo = #{clientid := ClientId}) + when ClientId /= undefined -> + {ok, ClientInfo}; maybe_assign_clientid(#mqtt_packet_connect{clientid = <<>>}, ClientInfo) -> %% Generate a rand clientId {ok, ClientInfo#{clientid => emqx_guid:to_base62(emqx_guid:gen())}}; diff --git a/test/emqx_client_SUITE.erl b/test/emqx_client_SUITE.erl index fc440aeb1..c7b44cbe9 100644 --- a/test/emqx_client_SUITE.erl +++ b/test/emqx_client_SUITE.erl @@ -45,7 +45,8 @@ all() -> [{group, mqttv3}, {group, mqttv4}, - {group, mqttv5} + {group, mqttv5}, + {group, others} ]. groups() -> @@ -66,17 +67,26 @@ groups() -> ]}, {mqttv5, [non_parallel_tests], [t_basic_with_props_v5 + ]}, + {others, [non_parallel_tests], + [t_username_as_clientid, + t_certcn_as_clientid ]} ]. init_per_suite(Config) -> emqx_ct_helpers:boot_modules(all), - emqx_ct_helpers:start_apps([]), + emqx_ct_helpers:start_apps([], fun set_special_confs/1), Config. end_per_suite(_Config) -> emqx_ct_helpers:stop_apps([]). +set_special_confs(emqx) -> + emqx_ct_helpers:change_emqx_opts(ssl_twoway, [{peer_cert_as_username, cn}]); +set_special_confs(_) -> + ok. + %%-------------------------------------------------------------------- %% Test cases for MQTT v3 %%-------------------------------------------------------------------- @@ -110,10 +120,7 @@ t_cm_registry(_) -> {_, Pid, _, _} = lists:keyfind(registry, 1, Info), ignored = gen_server:call(Pid, <<"Unexpected call">>), gen_server:cast(Pid, <<"Unexpected cast">>), - Pid ! <<"Unexpected info">>, - ok = application:stop(mnesia), - emqx_ct_helpers:stop_apps([]), - emqx_ct_helpers:start_apps([]). + Pid ! <<"Unexpected info">>. t_will_message(_Config) -> {ok, C1} = emqtt:start_link([{clean_start, true}, @@ -263,6 +270,23 @@ t_basic(_Opts) -> ?assertEqual(3, length(recv_msgs(3))), ok = emqtt:disconnect(C). +t_username_as_clientid(_) -> + emqx_zone:set_env(external, use_username_as_clientid, true), + Username = <<"usera">>, + {ok, C} = emqtt:start_link([{username, Username}]), + {ok, _} = emqtt:connect(C), + #{clientinfo := #{clientid := Username}} = emqx_cm:get_chan_info(Username), + emqtt:disconnect(C). + +t_certcn_as_clientid(_) -> + CN = <<"0004.novalocal">>, + emqx_zone:set_env(external, use_username_as_clientid, true), + SslConf = emqx_ct_helpers:client_ssl_twoway(), + {ok, C} = emqtt:start_link([{port, 8883}, {ssl, true}, {ssl_opts, SslConf}]), + {ok, _} = emqtt:connect(C), + #{clientinfo := #{clientid := CN}} = emqx_cm:get_chan_info(CN), + emqtt:disconnect(C). + %%-------------------------------------------------------------------- %% Helper functions %%--------------------------------------------------------------------