emqx_utils_redact_tests.erl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_utils_redact_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. is_redacted_test_() ->
  19. [
  20. ?_assertNot(is_redacted(password, <<"secretpass">>)),
  21. ?_assertNot(is_redacted(password, <<>>)),
  22. ?_assertNot(is_redacted(password, undefined)),
  23. ?_assert(is_redacted(password, <<"******">>)),
  24. ?_assertNot(is_redacted(password, fun() -> <<"secretpass">> end)),
  25. ?_assertNot(is_redacted(password, emqx_secret:wrap(<<"secretpass">>))),
  26. ?_assert(is_redacted(password, fun() -> <<"******">> end)),
  27. ?_assert(is_redacted(password, emqx_secret:wrap(<<"******">>)))
  28. ].
  29. no_redact_template_var_test() ->
  30. ?assertEqual(
  31. #{
  32. password => <<"${var}">>,
  33. account_key => <<"${path.to.var}">>,
  34. <<"secret">> => <<"******">>,
  35. private_key => <<"******">>
  36. },
  37. redact(#{
  38. password => <<"${var}">>,
  39. <<"secret">> => <<"abc">>,
  40. account_key => <<"${path.to.var}">>,
  41. private_key => <<"${var}more">>
  42. })
  43. ).
  44. redact(X) -> emqx_utils:redact(X).
  45. is_redacted(Key, Value) ->
  46. emqx_utils:is_redacted(Key, Value).