test: add test cases for listeners access_rules validation and split

This commit is contained in:
Kjell Winblad 2024-05-10 14:21:43 +02:00
parent 0aeb2cd77f
commit 2bed5894e3
2 changed files with 65 additions and 2 deletions

View File

@ -1808,8 +1808,9 @@ access_rules_converter(AccessRules) ->
DeepRules = DeepRules =
lists:foldr( lists:foldr(
fun(Rule, Acc) -> fun(Rule, Acc) ->
Rules = re:split(Rule, <<"\\s*,\\s*">>, [{return, binary}]), Rules0 = re:split(Rule, <<"\\s*,\\s*">>, [{return, binary}]),
[Rules | Acc] Rules1 = [string:trim(R) || R <- Rules0],
[Rules1 | Acc]
end, end,
[], [],
AccessRules AccessRules

View File

@ -115,6 +115,68 @@ t_update_conf(_Conf) ->
?assert(is_running('wss:default')), ?assert(is_running('wss:default')),
ok. ok.
t_update_conf_validate_access_rules(_Conf) ->
Raw = emqx:get_raw_config(?LISTENERS),
RawCorrectConf1 = emqx_utils_maps:deep_put(
[<<"tcp">>, <<"default">>, <<"access_rules">>], Raw, ["allow all"]
),
?assertMatch({ok, _}, emqx:update_config(?LISTENERS, RawCorrectConf1)),
RawCorrectConf2 = emqx_utils_maps:deep_put(
[<<"tcp">>, <<"default">>, <<"access_rules">>], Raw, ["deny all"]
),
?assertMatch({ok, _}, emqx:update_config(?LISTENERS, RawCorrectConf2)),
RawCorrectConf3 = emqx_utils_maps:deep_put(
[<<"tcp">>, <<"default">>, <<"access_rules">>], Raw, ["allow 10.0.1.0/24"]
),
?assertMatch({ok, _}, emqx:update_config(?LISTENERS, RawCorrectConf3)),
RawIncorrectConf1 = emqx_utils_maps:deep_put(
[<<"tcp">>, <<"default">>, <<"access_rules">>], Raw, ["xxx all"]
),
?assertMatch(
{error, #{
reason := <<"invalid_rule(s): xxx all">>,
value := ["xxx all"],
path := "listeners.tcp.default.access_rules",
kind := validation_error,
matched_type := "emqx:mqtt_tcp_listener"
}},
emqx:update_config(?LISTENERS, RawIncorrectConf1)
),
RawIncorrectConf2 = emqx_utils_maps:deep_put(
[<<"tcp">>, <<"default">>, <<"access_rules">>], Raw, ["allow xxx"]
),
?assertMatch(
{error, #{
reason := <<"invalid_rule(s): allow xxx">>,
value := ["allow xxx"],
path := "listeners.tcp.default.access_rules",
kind := validation_error,
matched_type := "emqx:mqtt_tcp_listener"
}},
emqx:update_config(?LISTENERS, RawIncorrectConf2)
),
ok.
t_update_conf_access_rules_split(_Conf) ->
Raw = emqx:get_raw_config(?LISTENERS),
Raw1 = emqx_utils_maps:deep_put(
[<<"tcp">>, <<"default">>, <<"access_rules">>],
Raw,
[" allow all , deny all , allow 10.0.1.0/24 "]
),
?assertMatch({ok, _}, emqx:update_config(?LISTENERS, Raw1)),
?assertMatch(
#{
tcp := #{
default := #{
access_rules := ["allow all", "deny all", "allow 10.0.1.0/24"]
}
}
},
emqx:get_config(?LISTENERS)
),
ok.
t_update_tcp_keepalive_conf(_Conf) -> t_update_tcp_keepalive_conf(_Conf) ->
Keepalive = <<"240,30,5">>, Keepalive = <<"240,30,5">>,
KeepaliveStr = binary_to_list(Keepalive), KeepaliveStr = binary_to_list(Keepalive),