test(gateway): refine all test cases

This commit is contained in:
JianBo He 2023-04-01 10:30:37 +08:00
parent a70545b64a
commit b24ff9bc6e
23 changed files with 117 additions and 75 deletions

View File

@ -34,9 +34,12 @@
-import(hoconsc, [mk/2, enum/1]). -import(hoconsc, [mk/2, enum/1]).
-import(emqx_dashboard_swagger, [error_codes/2]). -import(emqx_dashboard_swagger, [error_codes/2]).
-elvis([{elvis_style, atom_naming_convention, disable}]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% API %% API
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
namespace() -> "gateway_coap". namespace() -> "gateway_coap".
api_spec() -> api_spec() ->

View File

@ -55,6 +55,8 @@
-define(OPTION_PROXY_SCHEME, 39). -define(OPTION_PROXY_SCHEME, 39).
-define(OPTION_SIZE1, 60). -define(OPTION_SIZE1, 60).
-elvis([{elvis_style, no_if_expression, disable}]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% API %% API
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------

View File

@ -80,6 +80,8 @@
-import(emqx_coap_medium, [empty/0, iter/4, reset/1, proto_out/2]). -import(emqx_coap_medium, [empty/0, iter/4, reset/1, proto_out/2]).
-elvis([{elvis_style, no_if_expression, disable}]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% API %% API
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -401,9 +403,9 @@ alloc_message_id(MsgId, TM) ->
next_message_id(MsgId) -> next_message_id(MsgId) ->
Next = MsgId + 1, Next = MsgId + 1,
if case Next >= ?MAX_MESSAGE_ID of
Next >= ?MAX_MESSAGE_ID ->
1;
true -> true ->
1;
false ->
Next Next
end. end.

View File

@ -60,6 +60,12 @@
reply/2 reply/2
]). ]).
-elvis([{elvis_style, atom_naming_convention, disable}]).
-elvis([{elvis_style, no_if_expression, disable}]).
%%--------------------------------------------------------------------
%% APIs
-spec new() -> transport(). -spec new() -> transport().
new() -> new() ->
new(undefined). new(undefined).

View File

@ -601,7 +601,7 @@ find_attrs(App, Def) ->
module_attributes(Module) -> module_attributes(Module) ->
try try
Module:module_info(attributes) apply(Module, module_info, [attributes])
catch catch
error:undef -> [] error:undef -> []
end. end.

View File

@ -33,6 +33,7 @@ all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Conf) -> init_per_suite(Conf) ->
emqx_config:erase(gateway), emqx_config:erase(gateway),
emqx_gateway_test_utils:load_all_gateway_apps(),
emqx_common_test_helpers:load_config(emqx_gateway_schema, ?CONF_DEFAULT), emqx_common_test_helpers:load_config(emqx_gateway_schema, ?CONF_DEFAULT),
emqx_common_test_helpers:start_apps([emqx_authn, emqx_gateway]), emqx_common_test_helpers:start_apps([emqx_authn, emqx_gateway]),
Conf. Conf.
@ -67,11 +68,11 @@ end_per_testcase(_TestCase, _Config) ->
t_registered_gateway(_) -> t_registered_gateway(_) ->
[ [
{coap, #{cbkmod := emqx_coap_impl}}, {coap, #{cbkmod := emqx_coap}},
{exproto, #{cbkmod := emqx_exproto_impl}}, {exproto, #{cbkmod := emqx_exproto}},
{lwm2m, #{cbkmod := emqx_lwm2m_impl}}, {lwm2m, #{cbkmod := emqx_lwm2m}},
{mqttsn, #{cbkmod := emqx_sn_impl}}, {mqttsn, #{cbkmod := emqx_mqttsn}},
{stomp, #{cbkmod := emqx_stomp_impl}} {stomp, #{cbkmod := emqx_stomp}}
] = emqx_gateway:registered_gateway(). ] = emqx_gateway:registered_gateway().
t_load_unload_list_lookup(_) -> t_load_unload_list_lookup(_) ->
@ -187,7 +188,14 @@ read_lwm2m_conf(DataDir) ->
Conf. Conf.
setup_fake_usage_data(Lwm2mDataDir) -> setup_fake_usage_data(Lwm2mDataDir) ->
XmlDir = emqx_common_test_helpers:deps_path(emqx_gateway, "src/lwm2m/lwm2m_xml"), XmlDir = filename:join(
[
emqx_common_test_helpers:proj_root(),
"apps",
"emqx_lwm2m",
"lwm2m_xml"
]
),
Lwm2mConf = read_lwm2m_conf(Lwm2mDataDir), Lwm2mConf = read_lwm2m_conf(Lwm2mDataDir),
ok = emqx_common_test_helpers:load_config(emqx_gateway_schema, Lwm2mConf), ok = emqx_common_test_helpers:load_config(emqx_gateway_schema, Lwm2mConf),
emqx_config:put([gateway, lwm2m, xml_dir], XmlDir), emqx_config:put([gateway, lwm2m, xml_dir], XmlDir),

View File

@ -214,9 +214,17 @@ t_gateway_coap(_) ->
t_gateway_lwm2m(_) -> t_gateway_lwm2m(_) ->
{200, Gw} = request(get, "/gateways/lwm2m"), {200, Gw} = request(get, "/gateways/lwm2m"),
assert_gw_unloaded(Gw), assert_gw_unloaded(Gw),
XmlDir = filename:join(
[
emqx_common_test_helpers:proj_root(),
"apps",
"emqx_lwm2m",
"lwm2m_xml"
]
),
GwConf = #{ GwConf = #{
name => <<"lwm2m">>, name => <<"lwm2m">>,
xml_dir => <<"../../lib/emqx_gateway/src/lwm2m/lwm2m_xml">>, xml_dir => list_to_binary(XmlDir),
lifetime_min => <<"1s">>, lifetime_min => <<"1s">>,
lifetime_max => <<"1000s">>, lifetime_max => <<"1000s">>,
qmode_time_window => <<"30s">>, qmode_time_window => <<"30s">>,

View File

@ -66,6 +66,7 @@ end_per_group(AuthName, Conf) ->
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_config:erase(gateway), emqx_config:erase(gateway),
emqx_gateway_test_utils:load_all_gateway_apps(),
init_gateway_conf(), init_gateway_conf(),
meck:new(emqx_authz_file, [non_strict, passthrough, no_history, no_link]), meck:new(emqx_authz_file, [non_strict, passthrough, no_history, no_link]),
meck:expect(emqx_authz_file, create, fun(S) -> S end), meck:expect(emqx_authz_file, create, fun(S) -> S end),
@ -225,7 +226,7 @@ t_case_sn_subscribe(_) ->
) )
end, end,
Sub(<<"/subscribe">>, fun(Data) -> Sub(<<"/subscribe">>, fun(Data) ->
{ok, Msg, _, _} = emqx_sn_frame:parse(Data, undefined), {ok, Msg, _, _} = emqx_mqttsn_frame:parse(Data, undefined),
?assertMatch({mqtt_sn_message, _, {_, 3, 0, Payload}}, Msg) ?assertMatch({mqtt_sn_message, _, {_, 3, 0, Payload}}, Msg)
end), end),
Sub(<<"/badsubscribe">>, fun(Data) -> Sub(<<"/badsubscribe">>, fun(Data) ->

View File

@ -62,6 +62,7 @@ all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Conf) -> init_per_suite(Conf) ->
emqx_config:erase(gateway), emqx_config:erase(gateway),
emqx_gateway_test_utils:load_all_gateway_apps(),
emqx_common_test_helpers:load_config(emqx_gateway_schema, ?CONF_DEFAULT), emqx_common_test_helpers:load_config(emqx_gateway_schema, ?CONF_DEFAULT),
emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_authn, emqx_gateway]), emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_authn, emqx_gateway]),
Conf. Conf.
@ -116,11 +117,11 @@ t_gateway_registry_usage(_) ->
t_gateway_registry_list(_) -> t_gateway_registry_list(_) ->
emqx_gateway_cli:'gateway-registry'(["list"]), emqx_gateway_cli:'gateway-registry'(["list"]),
?assertEqual( ?assertEqual(
"Registered Name: coap, Callback Module: emqx_coap_impl\n" "Registered Name: coap, Callback Module: emqx_coap\n"
"Registered Name: exproto, Callback Module: emqx_exproto_impl\n" "Registered Name: exproto, Callback Module: emqx_exproto\n"
"Registered Name: lwm2m, Callback Module: emqx_lwm2m_impl\n" "Registered Name: lwm2m, Callback Module: emqx_lwm2m\n"
"Registered Name: mqttsn, Callback Module: emqx_sn_impl\n" "Registered Name: mqttsn, Callback Module: emqx_mqttsn\n"
"Registered Name: stomp, Callback Module: emqx_stomp_impl\n", "Registered Name: stomp, Callback Module: emqx_stomp\n",
acc_print() acc_print()
). ).

View File

@ -37,6 +37,7 @@ all() ->
emqx_common_test_helpers:all(?MODULE). emqx_common_test_helpers:all(?MODULE).
init_per_suite(Conf) -> init_per_suite(Conf) ->
emqx_gateway_test_utils:load_all_gateway_apps(),
emqx_common_test_helpers:load_config(emqx_gateway_schema, <<"gateway {}">>), emqx_common_test_helpers:load_config(emqx_gateway_schema, <<"gateway {}">>),
emqx_common_test_helpers:start_apps([emqx_conf, emqx_authn, emqx_gateway]), emqx_common_test_helpers:start_apps([emqx_conf, emqx_authn, emqx_gateway]),
Conf. Conf.

View File

@ -101,6 +101,12 @@ assert_fields_exist(Ks, Map) ->
end, end,
Ks Ks
). ).
load_all_gateway_apps() ->
application:load(emqx_stomp),
application:load(emqx_mqttsn),
application:load(emqx_coap),
application:load(emqx_lwm2m),
application:load(emqx_exproto).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% http %% http

View File

@ -32,6 +32,8 @@
-import(hoconsc, [mk/2, ref/1, ref/2]). -import(hoconsc, [mk/2, ref/1, ref/2]).
-import(emqx_dashboard_swagger, [error_codes/2]). -import(emqx_dashboard_swagger, [error_codes/2]).
-elvis([{elvis_style, atom_naming_convention, disable}]).
namespace() -> "lwm2m". namespace() -> "lwm2m".
api_spec() -> api_spec() ->

View File

@ -464,14 +464,14 @@ check_lwm2m_version(
_ -> _ ->
false false
end, end,
if case IsValid of
IsValid -> true ->
NConnInfo = ConnInfo#{ NConnInfo = ConnInfo#{
connected_at => erlang:system_time(millisecond), connected_at => erlang:system_time(millisecond),
proto_ver => Ver proto_ver => Ver
}, },
{ok, Channel#channel{conninfo = NConnInfo}}; {ok, Channel#channel{conninfo = NConnInfo}};
true -> _ ->
?SLOG(error, #{ ?SLOG(error, #{
msg => "reject_REGISTRE_request", msg => "reject_REGISTRE_request",
reason => {unsupported_version, Ver} reason => {unsupported_version, Ver}

View File

@ -292,9 +292,9 @@ make_response(Code, Ref = #{}) ->
BaseRsp = make_base_response(Ref), BaseRsp = make_base_response(Ref),
make_data_response(BaseRsp, Code). make_data_response(BaseRsp, Code).
make_response(Code, Ref = #{}, _Format, Result) -> make_response(Code, Ref = #{}, Format, Result) ->
BaseRsp = make_base_response(Ref), BaseRsp = make_base_response(Ref),
make_data_response(BaseRsp, Code, _Format, Result). make_data_response(BaseRsp, Code, Format, Result).
%% The base response format is what included in the request: %% The base response format is what included in the request:
%% %%

View File

@ -412,9 +412,11 @@ byte_size_of_signed(UInt) ->
byte_size_of_signed(UInt, N) -> byte_size_of_signed(UInt, N) ->
BitSize = (8 * N - 1), BitSize = (8 * N - 1),
Max = (1 bsl BitSize), Max = (1 bsl BitSize),
if case UInt =< Max of
UInt =< Max -> N; true ->
UInt > Max -> byte_size_of_signed(UInt, N + 1) N;
false ->
byte_size_of_signed(UInt, N + 1)
end. end.
binary_to_number(NumStr) -> binary_to_number(NumStr) ->

View File

@ -379,8 +379,8 @@ is_alternate_path(LinkAttrs) ->
true; true;
[AttrKey, _] when AttrKey =/= <<>> -> [AttrKey, _] when AttrKey =/= <<>> ->
false; false;
_BadAttr -> BadAttr ->
throw({bad_attr, _BadAttr}) throw({bad_attr, BadAttr})
end end
end, end,
LinkAttrs LinkAttrs
@ -679,10 +679,10 @@ send_to_coap(#session{queue = Queue} = Session) ->
case queue:out(Queue) of case queue:out(Queue) of
{{value, {Timestamp, Ctx, Req}}, Q2} -> {{value, {Timestamp, Ctx, Req}}, Q2} ->
Now = ?NOW, Now = ?NOW,
if case Timestamp =:= 0 orelse Timestamp > Now of
Timestamp =:= 0 orelse Timestamp > Now ->
send_to_coap(Ctx, Req, Session#session{queue = Q2});
true -> true ->
send_to_coap(Ctx, Req, Session#session{queue = Q2});
false ->
send_to_coap(Session#session{queue = Q2}) send_to_coap(Session#session{queue = Q2})
end; end;
{empty, _} -> {empty, _} ->

View File

@ -37,13 +37,18 @@
-define(TLV_LEGNTH_16_BIT, 2). -define(TLV_LEGNTH_16_BIT, 2).
-define(TLV_LEGNTH_24_BIT, 3). -define(TLV_LEGNTH_24_BIT, 3).
%---------------------------------------------------------------------------------------------------------------------------------------- -elvis([{elvis_style, no_if_expression, disable}]).
% [#{tlv_object_instance := Id11, value := Value11}, #{tlv_object_instance := Id12, value := Value12}, ...]
%%--------------------------------------------------------------------
% [#{tlv_object_instance := Id11, value := Value11},
% #{tlv_object_instance := Id12, value := Value12}, ...]
% where Value11 and Value12 is a list: % where Value11 and Value12 is a list:
% [#{tlv_resource_with_value => Id21, value => Value21}, #{tlv_multiple_resource => Id22, value = Value22}, ...] % [#{tlv_resource_with_value => Id21, value => Value21},
% #{tlv_multiple_resource => Id22, value = Value22}, ...]
% where Value21 is a binary % where Value21 is a binary
% Value22 is a list: % Value22 is a list:
% [#{tlv_resource_instance => Id31, value => Value31}, #{tlv_resource_instance => Id32, value => Value32}, ...] % [#{tlv_resource_instance => Id31, value => Value31},
% #{tlv_resource_instance => Id32, value => Value32}, ...]
% where Value31 and Value32 is a binary % where Value31 and Value32 is a binary
% %
% correspond to three levels: % correspond to three levels:
@ -51,8 +56,9 @@
% 2) Resource Level % 2) Resource Level
% 3) Resource Instance Level % 3) Resource Instance Level
% %
% NOTE: TLV does not has object level, only has object instance level. It implies TLV can not represent multiple objects % NOTE: TLV does not has object level, only has object instance level.
%---------------------------------------------------------------------------------------------------------------------------------------- % It implies TLV can not represent multiple objects
%%--------------------------------------------------------------------
parse(Data) -> parse(Data) ->
parse_loop(Data, []). parse_loop(Data, []).

View File

@ -45,6 +45,8 @@
-record(state, {}). -record(state, {}).
-elvis([{elvis_style, atom_naming_convention, disable}]).
%% ------------------------------------------------------------------ %% ------------------------------------------------------------------
%% API Function Definitions %% API Function Definitions
%% ------------------------------------------------------------------ %% ------------------------------------------------------------------
@ -124,10 +126,10 @@ code_change(_OldVsn, State, _Extra) ->
load(BaseDir) -> load(BaseDir) ->
Wild = filename:join(BaseDir, "*.xml"), Wild = filename:join(BaseDir, "*.xml"),
Wild2 = Wild2 =
if case is_binary(Wild) of
is_binary(Wild) ->
erlang:binary_to_list(Wild);
true -> true ->
erlang:binary_to_list(Wild);
false ->
Wild Wild
end, end,
case filelib:wildcard(Wild2) of case filelib:wildcard(Wild2) of

View File

@ -177,11 +177,19 @@ default_config() ->
default_config(#{}). default_config(#{}).
default_config(Overrides) -> default_config(Overrides) ->
XmlDir = filename:join(
[
emqx_common_test_helpers:proj_root(),
"apps",
"emqx_lwm2m",
"lwm2m_xml"
]
),
iolist_to_binary( iolist_to_binary(
io_lib:format( io_lib:format(
"\n" "\n"
"gateway.lwm2m {\n" "gateway.lwm2m {\n"
" xml_dir = \"../../lib/emqx_gateway/src/lwm2m/lwm2m_xml\"\n" " xml_dir = \"~s\"\n"
" lifetime_min = 1s\n" " lifetime_min = 1s\n"
" lifetime_max = 86400s\n" " lifetime_max = 86400s\n"
" qmode_time_window = 22\n" " qmode_time_window = 22\n"
@ -200,6 +208,7 @@ default_config(Overrides) ->
" }\n" " }\n"
"}\n", "}\n",
[ [
XmlDir,
maps:get(auto_observe, Overrides, false), maps:get(auto_observe, Overrides, false),
maps:get(bind, Overrides, ?PORT) maps:get(bind, Overrides, ?PORT)
] ]

View File

@ -28,29 +28,6 @@
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl"). -include_lib("common_test/include/ct.hrl").
-define(CONF_DEFAULT, <<
"\n"
"gateway.lwm2m {\n"
" xml_dir = \"../../lib/emqx_gateway/src/lwm2m/lwm2m_xml\"\n"
" lifetime_min = 100s\n"
" lifetime_max = 86400s\n"
" qmode_time_window = 200\n"
" auto_observe = false\n"
" mountpoint = \"lwm2m/${username}\"\n"
" update_msg_publish_condition = contains_object_list\n"
" translators {\n"
" command = {topic = \"/dn/#\", qos = 0}\n"
" response = {topic = \"/up/resp\", qos = 0}\n"
" notify = {topic = \"/up/notify\", qos = 0}\n"
" register = {topic = \"/up/resp\", qos = 0}\n"
" update = {topic = \"/up/resp\", qos = 0}\n"
" }\n"
" listeners.udp.default {\n"
" bind = 5783\n"
" }\n"
"}\n"
>>).
-define(assertExists(Map, Key), -define(assertExists(Map, Key),
?assertNotEqual(maps:get(Key, Map, undefined), undefined) ?assertNotEqual(maps:get(Key, Map, undefined), undefined)
). ).
@ -83,7 +60,8 @@ all() ->
init_per_suite(Config) -> init_per_suite(Config) ->
application:load(emqx_gateway), application:load(emqx_gateway),
application:load(emqx_lwm2m), application:load(emqx_lwm2m),
ok = emqx_common_test_helpers:load_config(emqx_gateway_schema, ?CONF_DEFAULT), DefaultConfig = emqx_lwm2m_SUITE:default_config(),
ok = emqx_common_test_helpers:load_config(emqx_gateway_schema, DefaultConfig),
emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_authn]), emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_authn]),
Config. Config.
@ -94,7 +72,8 @@ end_per_suite(Config) ->
Config. Config.
init_per_testcase(_AllTestCase, Config) -> init_per_testcase(_AllTestCase, Config) ->
ok = emqx_common_test_helpers:load_config(emqx_gateway_schema, ?CONF_DEFAULT), DefaultConfig = emqx_lwm2m_SUITE:default_config(),
ok = emqx_common_test_helpers:load_config(emqx_gateway_schema, DefaultConfig),
{ok, _} = application:ensure_all_started(emqx_gateway), {ok, _} = application:ensure_all_started(emqx_gateway),
{ok, ClientUdpSock} = gen_udp:open(0, [binary, {active, false}]), {ok, ClientUdpSock} = gen_udp:open(0, [binary, {active, false}]),

View File

@ -58,10 +58,10 @@ serialize_opts() ->
%% Parse MQTT-SN Message %% Parse MQTT-SN Message
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
parse(<<16#01:?byte, Len:?short, Type:?byte, Var/binary>>, _State) -> parse(<<16#01:?byte, Len:?short, Type:?byte, Var/binary>>, State) ->
{ok, parse(Type, Len - 4, Var), <<>>, _State}; {ok, parse(Type, Len - 4, Var), <<>>, State};
parse(<<Len:?byte, Type:?byte, Var/binary>>, _State) -> parse(<<Len:?byte, Type:?byte, Var/binary>>, State) ->
{ok, parse(Type, Len - 2, Var), <<>>, _State}. {ok, parse(Type, Len - 2, Var), <<>>, State}.
parse(Type, Len, Var) when Len =:= size(Var) -> parse(Type, Len, Var) when Len =:= size(Var) ->
#mqtt_sn_message{type = Type, variable = parse_var(Type, Var)}; #mqtt_sn_message{type = Type, variable = parse_var(Type, Var)};
@ -160,9 +160,11 @@ parse_topic(2#11, Topic) -> Topic.
serialize_pkt(#mqtt_sn_message{type = Type, variable = Var}, Opts) -> serialize_pkt(#mqtt_sn_message{type = Type, variable = Var}, Opts) ->
VarBin = serialize(Type, Var, Opts), VarBin = serialize(Type, Var, Opts),
VarLen = size(VarBin), VarLen = size(VarBin),
if case VarLen < 254 of
VarLen < 254 -> <<(VarLen + 2), Type, VarBin/binary>>; true ->
true -> <<16#01, (VarLen + 4):?short, Type, VarBin/binary>> <<(VarLen + 2), Type, VarBin/binary>>;
false ->
<<16#01, (VarLen + 4):?short, Type, VarBin/binary>>
end. end.
serialize(?SN_ADVERTISE, {GwId, Duration}, _Opts) -> serialize(?SN_ADVERTISE, {GwId, Duration}, _Opts) ->

View File

@ -151,9 +151,9 @@ init([InstaId, PredefTopics]) ->
key = {predef, TopicName}, key = {predef, TopicName},
value = TopicId value = TopicId
}), }),
if case TopicId > AccId of
TopicId > AccId -> TopicId; true -> TopicId;
true -> AccId false -> AccId
end end
end, end,
0, 0,

View File

@ -36,6 +36,8 @@
outgoing => #heartbeater{} outgoing => #heartbeater{}
}. }.
-elvis([{elvis_style, no_if_expression, disable}]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% APIs %% APIs
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------