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(emqx_dashboard_swagger, [error_codes/2]).
-elvis([{elvis_style, atom_naming_convention, disable}]).
%%--------------------------------------------------------------------
%% API
%%--------------------------------------------------------------------
namespace() -> "gateway_coap".
api_spec() ->

View File

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

View File

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

View File

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

View File

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

View File

@ -33,6 +33,7 @@ all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Conf) ->
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:start_apps([emqx_authn, emqx_gateway]),
Conf.
@ -67,11 +68,11 @@ end_per_testcase(_TestCase, _Config) ->
t_registered_gateway(_) ->
[
{coap, #{cbkmod := emqx_coap_impl}},
{exproto, #{cbkmod := emqx_exproto_impl}},
{lwm2m, #{cbkmod := emqx_lwm2m_impl}},
{mqttsn, #{cbkmod := emqx_sn_impl}},
{stomp, #{cbkmod := emqx_stomp_impl}}
{coap, #{cbkmod := emqx_coap}},
{exproto, #{cbkmod := emqx_exproto}},
{lwm2m, #{cbkmod := emqx_lwm2m}},
{mqttsn, #{cbkmod := emqx_mqttsn}},
{stomp, #{cbkmod := emqx_stomp}}
] = emqx_gateway:registered_gateway().
t_load_unload_list_lookup(_) ->
@ -187,7 +188,14 @@ read_lwm2m_conf(DataDir) ->
Conf.
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),
ok = emqx_common_test_helpers:load_config(emqx_gateway_schema, Lwm2mConf),
emqx_config:put([gateway, lwm2m, xml_dir], XmlDir),

View File

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

View File

@ -66,6 +66,7 @@ end_per_group(AuthName, Conf) ->
init_per_suite(Config) ->
emqx_config:erase(gateway),
emqx_gateway_test_utils:load_all_gateway_apps(),
init_gateway_conf(),
meck:new(emqx_authz_file, [non_strict, passthrough, no_history, no_link]),
meck:expect(emqx_authz_file, create, fun(S) -> S end),
@ -225,7 +226,7 @@ t_case_sn_subscribe(_) ->
)
end,
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)
end),
Sub(<<"/badsubscribe">>, fun(Data) ->

View File

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

View File

@ -37,6 +37,7 @@ all() ->
emqx_common_test_helpers:all(?MODULE).
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:start_apps([emqx_conf, emqx_authn, emqx_gateway]),
Conf.

View File

@ -101,6 +101,12 @@ assert_fields_exist(Ks, Map) ->
end,
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

View File

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

View File

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

View File

@ -292,9 +292,9 @@ make_response(Code, Ref = #{}) ->
BaseRsp = make_base_response(Ref),
make_data_response(BaseRsp, Code).
make_response(Code, Ref = #{}, _Format, Result) ->
make_response(Code, Ref = #{}, Format, Result) ->
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:
%%

View File

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

View File

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

View File

@ -37,13 +37,18 @@
-define(TLV_LEGNTH_16_BIT, 2).
-define(TLV_LEGNTH_24_BIT, 3).
%----------------------------------------------------------------------------------------------------------------------------------------
% [#{tlv_object_instance := Id11, value := Value11}, #{tlv_object_instance := Id12, value := Value12}, ...]
-elvis([{elvis_style, no_if_expression, disable}]).
%%--------------------------------------------------------------------
% [#{tlv_object_instance := Id11, value := Value11},
% #{tlv_object_instance := Id12, value := Value12}, ...]
% 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
% 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
%
% correspond to three levels:
@ -51,8 +56,9 @@
% 2) Resource 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_loop(Data, []).

View File

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

View File

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

View File

@ -28,29 +28,6 @@
-include_lib("eunit/include/eunit.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),
?assertNotEqual(maps:get(Key, Map, undefined), undefined)
).
@ -83,7 +60,8 @@ all() ->
init_per_suite(Config) ->
application:load(emqx_gateway),
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]),
Config.
@ -94,7 +72,8 @@ end_per_suite(Config) ->
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, ClientUdpSock} = gen_udp:open(0, [binary, {active, false}]),

View File

@ -58,10 +58,10 @@ serialize_opts() ->
%% Parse MQTT-SN Message
%%--------------------------------------------------------------------
parse(<<16#01:?byte, Len:?short, Type:?byte, Var/binary>>, _State) ->
{ok, parse(Type, Len - 4, Var), <<>>, _State};
parse(<<Len:?byte, Type:?byte, Var/binary>>, _State) ->
{ok, parse(Type, Len - 2, Var), <<>>, _State}.
parse(<<16#01:?byte, Len:?short, Type:?byte, Var/binary>>, State) ->
{ok, parse(Type, Len - 4, Var), <<>>, State};
parse(<<Len:?byte, Type:?byte, Var/binary>>, State) ->
{ok, parse(Type, Len - 2, Var), <<>>, State}.
parse(Type, Len, Var) when Len =:= size(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) ->
VarBin = serialize(Type, Var, Opts),
VarLen = size(VarBin),
if
VarLen < 254 -> <<(VarLen + 2), Type, VarBin/binary>>;
true -> <<16#01, (VarLen + 4):?short, Type, VarBin/binary>>
case VarLen < 254 of
true ->
<<(VarLen + 2), Type, VarBin/binary>>;
false ->
<<16#01, (VarLen + 4):?short, Type, VarBin/binary>>
end.
serialize(?SN_ADVERTISE, {GwId, Duration}, _Opts) ->

View File

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

View File

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