emqx_utils_tests.erl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2023-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_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. is_redacted_test_() ->
  19. [
  20. ?_assertNot(emqx_utils:is_redacted(password, <<"secretpass">>)),
  21. ?_assertNot(emqx_utils:is_redacted(password, <<>>)),
  22. ?_assertNot(emqx_utils:is_redacted(password, undefined)),
  23. ?_assert(emqx_utils:is_redacted(password, <<"******">>)),
  24. ?_assertNot(emqx_utils:is_redacted(password, fun() -> <<"secretpass">> end)),
  25. ?_assertNot(emqx_utils:is_redacted(password, emqx_secret:wrap(<<"secretpass">>))),
  26. ?_assert(emqx_utils:is_redacted(password, fun() -> <<"******">> end)),
  27. ?_assert(emqx_utils:is_redacted(password, emqx_secret:wrap(<<"******">>)))
  28. ].
  29. foldl_while_test_() ->
  30. [
  31. ?_assertEqual(
  32. [3, 2, 1],
  33. emqx_utils:foldl_while(fun(X, Acc) -> {cont, [X | Acc]} end, [], [1, 2, 3])
  34. ),
  35. ?_assertEqual(
  36. [1],
  37. emqx_utils:foldl_while(
  38. fun
  39. (X, Acc) when X == 2 ->
  40. {halt, Acc};
  41. (X, Acc) ->
  42. {cont, [X | Acc]}
  43. end,
  44. [],
  45. [1, 2, 3]
  46. )
  47. ),
  48. ?_assertEqual(
  49. finished,
  50. emqx_utils:foldl_while(
  51. fun
  52. (X, _Acc) when X == 3 ->
  53. {halt, finished};
  54. (X, Acc) ->
  55. {cont, [X | Acc]}
  56. end,
  57. [],
  58. [1, 2, 3]
  59. )
  60. )
  61. ].